Maoritzio
Maoritzio

Reputation: 1232

Generating random numbers given required distribution and empirical sampling

I have two sets of samplings, one distributes exponentially and the second- Bernoli (I used scipy.stats.expon and scipy.stats.bernoulli to fit my data).

Based on these sampling, I want to create two random generators that will enable me to sample numbers from the two distributions.

What alternatives are there for doing so?

How can I find the correct parameters for creating the random generators?

Upvotes: 2

Views: 226

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114811

Use the rvs method to generate a sample using the estimated parameters. For example, suppose x holds my initial data.

In [56]: x
Out[56]: 
array([ 0.366,  0.235,  0.286,  0.84 ,  0.073,  0.108,  0.156,  0.029,
        0.11 ,  0.122,  0.227,  0.148,  0.095,  0.233,  0.317,  0.027])

Use scipy.stats.expon to fit the expononential distribution to this data. I assume we are interested in the usual case where the location parameter is 0, so I use floc=0 in the fit call.

In [57]: from scipy.stats import expon

In [58]: loc, scale = expon.fit(x, floc=0)

In [59]: scale
Out[59]: 0.21076203455218898

Now use those parameters to generate a random sample.

In [60]: sample = expon.rvs(loc=0, scale=scale, size=8)

In [61]: sample
Out[61]: 
array([ 0.21576877,  0.23415911,  0.6547364 ,  0.44424148,  0.07870868,
        0.10415167,  0.12905163,  0.23428833])

Upvotes: 3

Related Questions