statistics

Beta Distribution Explained with Python Examples

In this post, you will learn about Beta probability distribution with the help of Python examples. As a data scientist, it is very important to understand beta distribution as it is used very commonly as prior in Bayesian modeling. In this post, the following topics get covered:

  • Beta distribution intuition and examples
  • Introduction to beta distribution
  • Beta distribution python examples

Beta Distribution Intuition & Examples

Beta distribution is widely used to model the prior beliefs or probability distribution in real world applications. Here is a great article on understanding beta distribution with an example of baseball game. You may want to pay attention to the fact that even if the baseball player got strikeout in first couple of matches, one still may chose to believe based on his prior belief (prior distribution) that he would end up achieving his batting average. However, as the match proceed, the prior may change based on his performance in subsequent matches. This would mean altering the parameters value of [latex]\alpha[/latex] and [latex]\beta[/latex].

If there exists a prior distribution about any event having outcome within an interval (a < X < b or 0 < X < 1), based on the upcoming event outcomes, the prior may change. The diagram below represents the hypothetical scenario representing the change in prior probability distribution which happens due to change in the value of shape parameters value of [latex]\alpha[/latex] and [latex]\beta[/latex].

Fig 1. Change in Prior Distributions (Beta Distribution)

Once the shape parameters, [latex]\alpha[/latex] and [latex]\beta[/latex] get determined, one could use the probability density function to determine the probability of event having with value of random variable falling within a given interval. Let’s understand this with an example. Let’s say you create a beta distribution to model the percentage of votes a particular politician would get in an upcoming interval. Once the shape parameters’ values of [latex]\alpha[/latex] and [latex]\beta[/latex] are known, one could find out the value that the politician will get votes falling between percentage ranges.

Introduction to Beta Distribution

Beta distribution is continuous probability distribution representing probabilities of the random variable which can have only finite set of values. This is unlike other probability distributions where the random variable’s value can take infinity as values, at least in one direction.

Beta distribution have two shape parameters namely [latex]\alpha[/latex] and [latex]\beta[/latex]. The random variable in beta distribution can have values between finite set of values such as a and b or 0 and 1. When the random variable has value between a and b and parameters [latex]\alpha[/latex] and [latex]\beta[/latex], the beta distribution is termed as general beta distribution. Given the fact that there are four parameters to be determined, it is also termed as four parameters beta distribution. Here is the probability distribution function for 4-parameters beta distribution. Note the parameters a, b, q as [latex]\alpha[/latex] and r as [latex]\beta[/latex]. B(q, r) is beta function. Note that the shape parameters are always positive.

Fig 2. Four-parameters beta distribution (General beta distribution)

When the random variable can have values between 0 and 1 and parameters [latex]\alpha[/latex] and [latex]\beta[/latex], the beta distribution is termed as standard beta distribution. Given the fact that there are two parameters to be determined, it is also termed as two parameters beta distribution. A four-parameters or general beta distribution can be transformed into two-parameters or standard beta distribution. Recall normal distribution and standard normal distribution (mean as 0 and standard deviation as 1). Here is the probability distribution function for standard beta distribution or 2-parameters beta distribution. Pay attention to a and b taking value as 0 and 1 respectively. The shape parameters are q and r ([latex]\alpha[/latex] and [latex]\beta[/latex])

Fig 3. Standard Beta Distribution with a = 0, b = 1

Here is the probability distribution diagram for standard beta distribution (0 < X < 1) representing different shapes. Note that for different values of the parameters [latex]\alpha[/latex] and [latex]\beta[/latex], the shape of the beta distribution will change.

Fig 4. Beta Distribution and different shapes based on alpha and beta values

The very fact that the beta distribution can have different shapes based on different values of parameters make this distribution very useful. Also, for random variable having values between 0 and 1, beta distribution can be used to model probabilities whose values lie between 0 and 1. Thus, for modeling probabilities, both the X axis and Y axis represent probabilities. For that matter, standard beta distribution can also be termed as probability distribution of probabilities.

Given the fact that standard beta distribution is used to model probability distribution of probabilities, it is most commonly used as prior in Bayesian modeling.

The mean of beta distribution is [latex]\frac{\aplha}{\alpha + \beta}[/latex].

As beta distribution is used as prior distribution, beta distribution can act as conjugate prior to the likelihood probability distribution function. Thus, if the likelihood probability function is binomial distribution, in that case, beta distribution will be called as conjugate prior of binomial distribution.

Beta Distribution Python Examples

Here is the Python code which can be used to create beta distribution.

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import beta
#
# Set the shape paremeters
#
a, b = 80, 10
#
# Generate the value between 
#
x = np.linspace(beta.ppf(0.01, a, b),beta.ppf(0.99, a, b), 100)
#
# Plot the beta distribution
#
plt.figure(figsize=(7,7))
plt.xlim(0.7, 1)
plt.plot(x, beta.pdf(x, a, b), 'r-')
plt.title('Beta Distribution', fontsize='15')
plt.xlabel('Values of Random Variable X (0, 1)', fontsize='15')
plt.ylabel('Probability', fontsize='15')
plt.show()

Here is how the plot would look like for above code:

Fig 5. Beta Distribution plot

References

Conclusions

Here is the summary of what you learned in this post in relation to beta distribution:

  • Beta distribution can be used to model probability distribution of probabilities
  • Beta distribution is more often used in the Bayesian modeling
  • When four parameters such as inner and outer bound of interval and [latex]\alpha[/latex] and [latex]\beta[/latex] are unknown, the beta distribution is known as general beta distribution or four-parameters beta distribution.
  • When two parameters such as [latex]\alpha[/latex] and [latex]\beta[/latex] are unknown and interval varies between 0 and 1, the beta distribution is known as standard beta distribution or two-parameters beta distribution.
Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

View Comments

  • Just began learning python: trying to understand how to solve/code: finding the MLE of a histogram using the scipy.stats.beta class. The true parameters are a=4, b=2 using the raw data?

Recent Posts

Agentic Reasoning Design Patterns in AI: Examples

In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…

1 month ago

LLMs for Adaptive Learning & Personalized Education

Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…

2 months ago

Sparse Mixture of Experts (MoE) Models: Examples

With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…

2 months ago

Anxiety Disorder Detection & Machine Learning Techniques

Anxiety is a common mental health condition that affects millions of people around the world.…

2 months ago

Confounder Features & Machine Learning Models: Examples

In machine learning, confounder features or variables can significantly affect the accuracy and validity of…

2 months ago

Credit Card Fraud Detection & Machine Learning

Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…

2 months ago