0

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?

Vishal Anand
  • 178
  • 2
  • 10

2 Answers2

1

You can code the PDF and CDF according to the definition of the gamma distribution:

enter image description here

enter image description here

enter image description here

enter image description here

Source: https://en.wikipedia.org/wiki/Gamma_distribution

You can choose the alpha and beta parameters. Alternatively, you can use the scipy.stats.gamma package.

digital_hog
  • 202
  • 1
  • 11
0

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) 
Vishal Anand
  • 178
  • 2
  • 10