Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot gamma distribution with alpha and beta parameters in python

Tags:

I want to plot a gamma distribution with alpha = 29 (the scale) and beta = 3 (the size). In other words, I want to plot the pdf for Gamma(29,3). How do I do this if according to the documentation, the python gamma function only has parameters a and x and the size parameter doesn't exist?

I thought loc was beta, but I think it's actually offset, so the code below is wrong...

import numpy as np
import scipy.stats as stats 
from matplotlib import pyplot as plt

x = np.linspace (0, 100, 200) 
y1 = stats.gamma.pdf(x, a=29, loc=3) #a is alpha, loc is beta???
plt.plot(x, y1, "y-", label=(r'$\alpha=29, \beta=3$')) 


plt.ylim([0,0.08])
plt.xlim([0,150])
plt.show()
like image 231
14wml Avatar asked Feb 10 '17 03:02

14wml


People also ask

How do you find the alpha and beta parameter of gamma distribution?

To estimate the parameters of the gamma distribution that best fits this sampled data, the following parameter estimation formulae can be used: alpha := Mean(X, I)^2/Variance(X, I) beta := Variance(X, I)/Mean(X, I)

What are the parameters of gamma distribution?

Gamma distributions have two free parameters, named as alpha (α) and beta (β), where; α = Shape parameter. β = Rate parameter (the reciprocal of the scale parameter)

How do you find a and b/in gamma distribution?

α=E2[X]Var(x), β=E[X]Var(x).


2 Answers

According to the documentation, you want to use the scale parameter (theta), but since you are defining beta, which is the inverse of theta, then you pass scale with the value of 1/beta, which in your example would be 1/3 or 0.33333.

Therefore, try:

y1 = stats.gamma.pdf(x, a=29, scale=0.33333)
like image 50
Scratch'N'Purr Avatar answered Sep 28 '22 04:09

Scratch'N'Purr


As @Hielke replied, as far as explained in scipy.stats 1.4.1 documentation it seems that the scalar parameter is equal to beta. Indeed, the function originally developped is :

gamma.pdf(x, a) = x^(a-1) * exp(-x) / gamma(a)

If one replaces x by a combination of the two optional parameters loc and scale as :

x = (y - loc) / scale

One should have :

gamma.pdf(x, a) = (y - loc)^(a-1) * exp( -(y - loc)/scale ) / (scale^(a-1) * gamma(a))

If you take loc = 0 then you recognized the expression of the Gamma distribution as usually defined. You multiply by the inverse of scale and you can conclude that scale = beta in this function and loc is an offset.

Actually I have tried to detail the documentation explaination :

Specifically, gamma.pdf(x, a, loc, scale) is identically equivalent to gamma.pdf(y, a) / scale with y = (x - loc) / scale.

like image 26
eidal Avatar answered Sep 28 '22 04:09

eidal