Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating random samples from geometric distribution in python

I want to generate random sample from geometric distribution using python.

I was using the numpy.random.geometric(p=0.3, size=100) function. But I later realized that this function generates sample from geometric distribution with pmf: p*q^(x-1), x=1,2,3....

But I want random samples from geometric distribution with pmf: p*q^(x), x=0,1,2,...

Kindly help.

like image 200
BTM Avatar asked Dec 19 '25 07:12

BTM


1 Answers

But I want random samples from geometric distribution with pmf: p*q^(x), x=0,1,2,...

You can use NumPy's implementation of geometric, and then subtract one from the samples:

In [37]: rng = np.random.default_rng()

In [38]: nsamples = 1000

In [39]: r = rng.geometric(0.3, size=nsamples) - 1

r is a sample from the distribution that you want.

like image 163
Warren Weckesser Avatar answered Dec 20 '25 22:12

Warren Weckesser



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!