9  Gamma - Poisson Conjugate

Open In Colab

The Gamma-Poisson conjugate is another helpful shortcut we can use to calculate the posterior directly without having to integrate the denominator in Bayes theorem.

9.1 Poisson Distribution

  • The Poisson distribution models the probability of a specific number of events occurring within a fixed interval of time or space.

  • It is suitable for scenarios with rare, independent events occurring at a constant rate, such as plane crashes, traffic accidents, or insurance claims.

  • It is defined by a single parameter: the mean (λ) which is also equal to its variance.

The poisson distribution is given by the equation

\[P(X;λ) = \frac{λ^xe^{-λ}}{x!}\]

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

# Define lambda
mu = 3

# Range of possible outcomes
x = np.arange(0, 20)

# Calculate Poisson probabilities using poisson.pmf
probabilities = poisson.pmf(x, mu)

# Create a bar plot
plt.figure(figsize=(10, 6))
plt.bar(x, probabilities)

# Add title and labels
plt.title(f"Poisson Distribution with λ = {mu}")
plt.xlabel("Number of Events (x)")
plt.ylabel("Probability P(X=x)")

# Display the plot
plt.show()

9.1.1 Finding the average rate of plane crashes

The Poisson distribution is very interesting because it tells us that even rare events can occur with a surprisingly high frequency. For example, if the average rate of plane accidents were 3, it is not impossible to see 5, 6, or 7 accidents happen in any given year. While rare, it is not impossible as shown by the probability on the y-axis above.

Let’s see this with an example!
If average rate = 3, what is the probability of observing 5 crashes in a year?

\[P(X;λ) = \frac{λ^xe^{-λ}}{x!}\]

\[P(5;3) = \frac{3^5e^{-3}}{5!} = 0.1\]

Therefore, there’s a 10% chance of seeing 5 plane crashes in a year when the average rate = 3

from scipy.stats import poisson

# Define lambda and x
lam = 3
x = 5

# Calculate probability using poisson.pmf
probability = poisson.pmf(x, lam)

print(f"The probability of X={x} for a Poisson distribution with lambda={lam} is: {probability:.2f}")
The probability of X=5 for a Poisson distribution with lambda=3 is: 0.10

9.2 Gamma Distribution

A Gamma distribution is a continuous probability distribution defined for non-negative values (from 0 to infinity) on the x-axis, with its probability density function represented on the y-axis.

It is defined by two positive parameters, α (shape) and β (rate), and is commonly used as a prior distribution in Bayesian statistics, particularly for parameters that must be positive, such as rates in a Poisson distribution. \[g(x;α,β) = \frac{\beta^\alpha x^{\alpha-1}e^{-\beta x}}{\Gamma(\alpha)}\]

  • The function g takes inputs x, α and β.
  • The random variable X must assume values greater than or equal to 0, while α and β must be positive. The function’s output represents the probability density of x given α and β.

9.3 How do we use Gamma distributions as priors?

Let’s assume, the average rate of plane crashes from a Poisson distribution has a mean of 3.

We can then use the following equation to find the α and β parameters for the Gamma priors:

\[\mu =\frac{\alpha}{\beta}\]

Since we know mean = 3, we can choose α = 3 and β = 1 (choosing α and β is subjective as long as the mean equation is correct). We get a gamma distribution like so

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

# Define shape (alpha) and scale (beta) parameters for the Gamma distribution
alpha = 3  # Shape parameter
beta = 1   # Scale parameter (often denoted as theta or 1/beta in other contexts)

# Generate x values (representing the rate lambda, which must be non-negative)
x = np.linspace(0, 20, 100)

# Calculate the probability density function (PDF) of the Gamma distribution
pdf_values = gamma.pdf(x, a=alpha, scale=beta)

# Create the plot
plt.figure(figsize=(10, 6))
plt.plot(x, pdf_values, label=f'Gamma Distribution (α={alpha}, β={beta})')

# Add title and labels
plt.title("Gamma Distribution")
plt.xlabel("Rate (λ)")
plt.ylabel("Probability Density")
plt.legend()
plt.grid(True)

# Display the plot
plt.show()

We now observe 5 plane crashes in the real world. Using the earlier poisson distribution where λ = 3 , the likelihood of seeing 5 events is given by \[P(X;λ) = \frac{λ^xe^{-λ}}{x!}= \frac{3^5e^{-3}}{5!} = 0.1\] However, we want to find all possible values and determine the probability of observing 5 events in a year. For a continuous variable, Bayes’ theorem then becomes: \[P(λ|data) = \frac{P(data|λ) * P(λ)}{∫P(data|λ)*P(λ)dλ}\] where, P(λ) = prior
P(data|λ) = likelihood
The denominator here includes an integral which can be very difficult to solve.

Instead of solving this, we can use the Gamma-Poisson analytical shortcut to find the posterior. As stated earlier, the Gamma distribution is a conjugate prior to a Poisson distribution. When a Gamma prior is used with a Poisson likelihood, we get a Gamma posterior like so:
\[α_{posterior} = α_{prior}+∑x_i\] \[β_{posterior} = β_{prior}+n \]

If we saw 5 events in the year then x=5 and n=1 \[α_{posterior} = 3+5 = 8\] \[β_{posterior} = 1+1 = 2\]

We can use these values to plot the gamma distribution of the posterior which includes all possible values of λ

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

# Define shape (alpha) and rate (beta) parameters
gamma_rate_params = [(3, 1), (8, 2)]

# Convert rate (beta) to scale (theta or scale) for scipy.stats.gamma
gamma_scale_params = [(alpha, 1/beta) for alpha, beta in gamma_rate_params]

# Generate x values (representing the rate lambda, which must be non-negative)
x = np.linspace(0, 20, 100)

# Create the plot
plt.figure(figsize=(10, 6))


# Plot each Gamma distribution using the scale parameter
for i, (alpha, scale_val) in enumerate(gamma_scale_params):
    pdf_values = gamma.pdf(x, a=alpha, scale=scale_val)
    label = f"Prior: (α={alpha}, β={1/scale_val:.0f})" if i == 0 else f"Posterior: (α={alpha}, β={1/scale_val:.0f})"
    plt.plot(x, pdf_values, label=label)

# Add title and labels
plt.title("Prior and Posterior Gamma Distributions")
plt.xlabel("Rate (λ)")
plt.ylabel("Probability Density")
plt.legend()
plt.grid(True)

# Display the plot
plt.show()

You can see above, based on our observation we updated our prior to form a new posterior. We used the gamma-poisson shortcut to easily find the posterior.
As we get more data, this posterior becomes the new prior and we repeat the previous steps.
For eg: lets say we get the following data in the following 3 years:

year no. of accidents
1 3
2 4
3 7

The posterior can then be updated to reflect these observations like so \[α_{posterior} = α_{prior}+∑x_i\] \[β_{posterior} = β_{prior}+n \]

Therefore,
\[α_{posterior} = 8+(3+4+7) = 22\] \[β_{posterior} = 2+3 = 5\]

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

# Define shape (alpha) and rate (beta) parameters
gamma_rate_params = [(8, 2),(22,5)]

# Convert rate (beta) to scale (theta or scale) for scipy.stats.gamma
gamma_scale_params = [(alpha, 1/beta) for alpha, beta in gamma_rate_params]

# Generate x values (representing the rate lambda, which must be non-negative)
x = np.linspace(0, 20, 100)

# Create the plot
plt.figure(figsize=(10, 6))


# Plot each Gamma distribution using the scale parameter
for i, (alpha, scale_val) in enumerate(gamma_scale_params):
    pdf_values = gamma.pdf(x, a=alpha, scale=scale_val)
    label = f"Prior: (α={alpha}, β={1/scale_val:.0f})" if i == 0 else f"Posterior: (α={alpha}, β={1/scale_val:.0f})"
    plt.plot(x, pdf_values, label=label)

# Add title and labels
plt.title("Prior and Posterior Gamma Distributions")
plt.xlabel("Rate (λ)")
plt.ylabel("Probability Density")
plt.legend()
plt.grid(True)

# Display the plot
plt.show()

9.4 Summary

  • Gamma - Poisson conjugate priors are another set of conjugates that allow us to quickly find the posterior without having to deal with the integral in Bayes theorem.
  • When dealing with a Poisson likelihood, using a Gamma distribution as a prior provides a convenient “shortcut” because the resulting posterior distribution is also a Gamma distribution.
  • This simplification of the posterior avoids calculating complex integrals in the denominator.
  • By starting with a Gamma prior that reflects our initial beliefs about the rate parameter (e.g., the average rate of plane crashes), and then updating it with observed data from a Poisson process (e.g., the number of observed plane crashes), we directly obtain a Gamma posterior that captures all possible values of the rate parameter and their probabilities.