Thingable
Thingable

Reputation: 55

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?

Upvotes: 3

Views: 4765

Answers (1)

Diego Palacios
Diego Palacios

Reputation: 1144

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)) 

Upvotes: 5

Related Questions