1

I refered to Fitting empirical distribution to theoretical ones with Scipy (Python)? and generated the best fit distribution to my sample data. I wish to generate random numbers according to the best fit distribution. See image below.

enter image description here

However, in https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.random.f.html#numpy.random.f, there is only 3 parameters, dfnum, dfden, size=None, where should I insert loc and scale. By the way, the dnd and dfd in best fit distribution are float and in numpy.random, it wants integer.

If I use only dnd and dfd in the code df_members['bd'] = df_members.bd.apply(lambda x: np.rint((np.random.f(dfnum=1441, dfden=19))) if x==-999 else x ) ,such values will be generated, which is false.

enter image description here

Ignacio Vergara Kausel
  • 5,521
  • 4
  • 31
  • 41
Mervyn Lee
  • 1,957
  • 4
  • 28
  • 54

1 Answers1

1

You can generate use from the scipy.stats module the f distribution and ask random values from it given the parameters you already found using the f.rvs method which accepts the four parameters plus the size (number of draws you want).

from scipy.stats import f
import matplotlib.pyplot as plt

values = f.rvs(1441.41, 19.1, -0.24, 26.5, 100000)

values is a 100000 length array with draws from the given distribution. You can see it as follows

plt.hist(values, bins=25)
plt.show()

enter image description here

Ignacio Vergara Kausel
  • 5,521
  • 4
  • 31
  • 41
  • Do you mind show me the code which the random number can be generated from `scipy.stats` ? – Mervyn Lee Oct 26 '17 at 11:42
  • I am trying to assign those random values into dataframe. `df_members['bd'] = df_members.bd.apply(lambda x: int(st.johnsonsu(a=-3.75,b=1.78, loc=11.36, scale=3.79)) if x==-999 else x )`, I am trying to round it into integer, any idea? – Mervyn Lee Oct 26 '17 at 11:56
  • 1
    That's another question, better open a new one and not mix two different problems. – Ignacio Vergara Kausel Oct 26 '17 at 11:59
  • But what you should do is (at least what I'd try) is to get the length and positions of -999 cases in your `bd` column and then create an array of random values casting the type to in with the `astype` method and then go through the positions (indexes) and setting the new value. – Ignacio Vergara Kausel Oct 26 '17 at 12:12
  • @MervynLee basically the step you're asking extra is already answered in one of your previous questionhttps://stackoverflow.com/questions/46938085/generate-different-random-numbers-to-multiple-rows-in-a-column in a better fashion than what I could have done – Ignacio Vergara Kausel Oct 26 '17 at 12:22
  • yep I solved it few minutes after I ask you. Thanks for the help anyway! – Mervyn Lee Oct 26 '17 at 13:59