Mervyn Lee
Mervyn Lee

Reputation: 2187

Getting draws from a fitted distribution

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

Upvotes: 1

Views: 783

Answers (1)

Ignacio Vergara Kausel
Ignacio Vergara Kausel

Reputation: 6026

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

Upvotes: 1

Related Questions