1

If I would like to generate 10 random samples from a gamma distribution with (with the following form):

enter image description here

with alpha = 2 and beta = 3, how would I do it?

The documentation: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.gamma.html is a bit unclear to me.

My guess is that it would be like:

a = 2 
b = 3 
scipy.stats.gamma.rvs(a, loc = 0, scale = 1/b, size = 10)

Can anyone verify wether this is correct or provide the correct solution?

Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214

1 Answers1

2

Yes, that is correct. In the formula that you show, β is often called the rate parameter. The gamma distribution in SciPy uses a scale parameter, which corresponds to 1/β. You can see the formulas for these two common parameterizations side-by-side in the wikipedia article on the gamma distribution.

If all you need is the generation of random samples (and not all the other methods provided by scipy.stats.gamma), you can use the gamma method of the NumPy class numpy.random.Generator. It uses the same parameter conventions as the SciPy gamma distribution, except that it does not have the loc parameter:

In [26]: import numpy as np

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

In [28]: a = 2

In [29]: b = 3

In [30]: rng.gamma(a, scale=1/b, size=10)
Out[30]: 
array([0.637065  , 0.18436688, 1.36876183, 0.74692619, 0.12608862,
       0.38395668, 0.81947237, 0.63437319, 0.47902819, 0.39094079])
Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214