# [SciPy-user] Gamma distribution questions

Robert Kern rkern at ucsd.edu
Thu Sep 8 17:07:57 CDT 2005

```Jinsheng you wrote:
> I have some projects related to the gamma distribution.   would you

source distribution. The following probably won't make sense otherwise.

> 1) Assuming that I have a gamma distribution dataset, how can I estimate
> parameters such as alpha, beta from the dataset.

I assume for the rest of this post that alpha is the shape parameter and
beta is the scale parameter.

Currently, the fit() method of distributions is broken. There's a
problem with the way it passes arguments to the nnlf() method; that
problem seems to apply to all distributions. There is also a problem
with distributions like Gamma which are intrinsically positive; all of
the distribution objects take a loc parameter. For intrinsically
positive variates like Gamma, this really should be fixed to 0 all of
the time.

So you are going to have to do a little bit of this manually.

from scipy import *
def f(params, data):
return -sum(log(stats.gamma.pdf(data, params[0], scale=params[1]))

And then you can use one of the minimizers in scipy.optimize to minimize
f(). That's called the "maximum likelihood method," which may or may not
be appropriate for what you want to do.

> 2) assuming that I know the alpha and beta of the distribution, how can
> I get the value for a given probability.

Probability of what?

> 3) assuming that I know the alpha and beta of the distribution, how can
> I get the probability for a given x.

By definition, the probability for any given point value is 0. You can
get the value of the probability *density function* by using the pdf()
method of scipy.stats.gamma . If you want the probability of getting a
value <= x, then the cdf() method will give you that. If you want the
probability of getting a value >= x, then 1-cdf().

--
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

```