How to Use the Beta Distribution in Python

How to Use the Beta Distribution in Python

The Beta distribution is a continuous probability distribution that is useful for modeling events that have probabilistic behavior bound between 0 and 1, such as proportions and probabilities.

It is used in Bayesian statistics to usually represent uncertainty about a probability parameter. For example, we might use the Beta distribution to model the probability of success in a sequence of trials—like A/B testing—or to express prior beliefs in Bayesian inference.

In this tutorial, we’ll learn more about the Beta distribution unique, the mathematical foundations behind it, and how to work with it in Python using the SciPy stats module. Let’s get started.

▶️ Link to the Google Colab notebook

Understanding the Beta Distribution

The Beta distribution is defined over the interval [0,1] and is parameterized by two shape parameters, α and β, which dictate the distribution’s shape. The density function (or PDF) of the Beta distribution is:

beta-dist

where B( α,β) is the Beta function, acting as a normalization constant to ensure the PDF integrates to 1. The shape parameters have the following effects:

  • When α > β, the distribution skews towards 1.
  • When α < β, the distribution skews towards 0.
  • When α = β = 1, the distribution is uniform between 0 and 1.
  • When both α and β are greater than 1, the distribution takes on a unimodal shape (bell-shaped) over [0, 1].

These parameters make the Beta distribution very flexible for modeling a range of behaviors.

Working with the Beta Distribution in Python

We’ll use Python’s scipy.stats module, which provides a simple and efficient way to work with the Beta distribution. Let’s go over some practical examples.

Importing Required Libraries

First, let’s import the necessary libraries:

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

Visualizing the Beta Distribution

To get a better intuition of how the shape parameters influence the Beta distribution, let’s plot it for various values of α and β.

# Define alpha and beta value pairs
alpha_beta_pairs = [(2, 5), (5, 2), (2, 2), (0.5, 0.5), (5, 5)]

# Set up the x-axis values
x = np.linspace(0, 1, 100)

# Plot each distribution
plt.figure(figsize=(10, 6))
for alpha, beta_val in alpha_beta_pairs:
    y = beta.pdf(x, alpha, beta_val)
    plt.plot(x, y, label=f'α={alpha}, β={beta_val}')

plt.title('Beta Distribution with Different α and β values')
plt.xlabel('x')
plt.ylabel('Probability Density Function')
plt.legend()
plt.show()

This plot shows the Beta distribution for different α and β values.

beta-dist-plot

Generating Beta-Distributed Random Numbers

Generating random samples from a Beta distribution is quite simple with beta.rvs from SciPy’s stats module. Here’s an example where we generate a sample and compute its mean and variance:

# Set shape parameters
alpha, beta_val = 2, 5

# Generate samples from the Beta distribution
samples = beta.rvs(alpha, beta_val, size=1000)

# Plot histogram of samples
plt.hist(samples, bins=30, density=True, alpha=0.6, color='blue')
plt.title(f'Histogram of Beta-distributed samples (α={alpha}, β={beta_val})')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.show()

# Sample mean and variance
sample_mean = np.mean(samples)
sample_variance = np.var(samples)
print(f'Sample Mean: {sample_mean:.3f}, Sample Variance: {sample_variance:.3f}')

beta-hist

The sample mean and variance are:

Sample Mean: 0.288, Sample Variance: 0.02

Example: Bayesian Inference

The Beta distribution is a popular choice for modeling prior distributions in Bayesian inference, especially when working with probabilities like a conversion rate or success probability. Here’s an example of how we might update beliefs about a probability using observed data.

# Prior beliefs: alpha and beta for the prior distribution
prior_alpha, prior_beta = 2, 2

# Observed successes and failures
successes = 30
trials = 50
failures = trials - successes

# Posterior parameters
posterior_alpha = prior_alpha + successes
posterior_beta = prior_beta + failures

# Visualize prior and posterior distributions
x = np.linspace(0, 1, 100)
prior_pdf = beta.pdf(x, prior_alpha, prior_beta)
posterior_pdf = beta.pdf(x, posterior_alpha, posterior_beta)

plt.plot(x, prior_pdf, label=f'Prior (α={prior_alpha}, β={prior_beta})', color='blue')
plt.plot(x, posterior_pdf, label=f'Posterior (α={posterior_alpha}, β={posterior_beta})', color='red')
plt.title('Prior and Posterior Distributions in Bayesian Inference')
plt.xlabel('Probability of Success')
plt.ylabel('Density')
plt.legend()
plt.show()

Which outputs:

beta-example

This example shows how observing more data updates our distribution from the prior to the posterior—refining our belief about the success probability.

Conclusion

In this tutorial, we’ve covered the basics of the Beta distribution: the math and working with it in Python. We explored various shapes of the distribution, generated samples, and saw a practical example of Bayesian inference.

As mentioned, the Beta distribution is useful for modeling probabilities, especially when prior knowledge can be updated with new data.

Cover image: Created by Author | Segmind SSD-1B Model

Leave a Reply

Your email address will not be published. Required fields are marked *