Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating random samples from fit PDF in SciPy (Python)

Given a fit distribution to a dataset using scipy.stats with something similar to:

data = fetch_data(file)
x = np.linspace(0, 100, 1000)

param = scipy.stats.norm.fit(data)
fit_pdf = scipy.stats.norm.pdf(x, param[0], param[1])

What is the best way to generate N=1000 random samples from this fit data? Is it possible to generate random samples given any array of values in a PDF?

like image 437
Thingable Avatar asked Jun 09 '26 05:06

Thingable


1 Answers

The best way to generate the random samples is:

data = fetch_data(file)
x = np.linspace(0, 100, 1000)

param = scipy.stats.norm.fit(data)
random_samples = scipy.stats.norm.rvs(param[0], param[1], size=1000)

To generate random samples using a given pdf as an array you can use the following:

fit_pdf = scipy.stats.norm.pdf(x, param[0], param[1])

samples = np.random.choice(x, size=1000, p=fit_pdf/np.sum(fit_pdf)) 
like image 70
Diego Palacios Avatar answered Jun 11 '26 19:06

Diego Palacios



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!