Search code examples
pythonnumpyscipystatisticsgamma-distribution

How to get the equations for Probability distribution function and Cumulative density function of a gamma distribution in scipy (Python)


Just as we can write the CDF and PDF of a random variable X, following a normal distribution, with its parameters - std and mean using scipy in the following manner:

from numpy import sqrt, pi, exp
mean, std = 295, 250

# defining Cumulative density function
def cdf(x):
    cdf_eqn = lambda t: (1 / (std * sqrt(2 * pi))) * exp(-(((t - mean) ** 2) / (2 * std ** 2)))
    cdf = quad(cdf_eqn, -np.inf, x)[0]
    return cdf

# defining Probability distribution function
def pdf(x):
    return (1 / (std * sqrt(2 * pi))) * exp(-(((x - mean) ** 2) / (2 * std ** 2)))

How can I define the CDF and PDF of a gamma distribution in the same way above?


Solution

  • Found out how to do it:

    from scipy.stats import gamma
    
    mean = 259
    std = 250
    
    x = 100
    
    alpha = ( mean / std)**2
    beta = std**2 / mean
    
    
    def pdf(x,aplha,beta):
      return gamma.pdf(x, a = alpha, scale = beta)
    
    def cdf(x,alpha,beta):
      return gamma.cdf(x, a = alpha, scale = beta)