Exploring Probability Distributions in Python with SciPy

 

Exploring Probability Distributions in Python with SciPy

Whether you are modeling test scores, financial risk, or daily website traffic, understanding probability distributions is a foundational skill in data science and statistics. Python’s scientific stack makes working with these distributions intuitive and visual.

Specifically, the scipy.stats library provides a comprehensive suite of pre-built continuous and discrete distributions—including Normal, Binomial, Poisson, and Uniform distributions.

Here is a hands-on walkthrough of how to define, visualize, and calculate key probabilities using the Normal Distribution in Python.

1. Setting Up the Distribution

A normal distribution (or bell curve) is defined by two parameters:

  • Mean ($\mu$ or loc): The central tendency or peak of the curve.

  • Standard Deviation ($\sigma$ or scale): The spread or dispersion of the data.

Using scipy.stats.norm, you can instantiate a frozen distribution with your chosen parameters:

Python
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

# Define a normal distribution with mean = 75 and standard deviation = 8
distribution = norm(loc=75, scale=8)

2. Visualizing the Probability Density Function (PDF)

The Probability Density Function (PDF) represents the relative likelihood of a continuous random variable taking on a given value.

To plot it:

  1. Generate an array of evenly spaced numbers covering roughly $\pm 3.5$ standard deviations from the mean (e.g., between 45 and 105).

  2. Calculate the corresponding density values with .pdf(x).

  3. Use Matplotlib to plot the curve and shade regions of interest using plt.fill_between().

Python
# Generate x-values spanning the distribution
x = np.linspace(45, 105, 300)
y = distribution.pdf(x)

# Plot the distribution
plt.figure(figsize=(9, 4.5))
plt.plot(x, y, linewidth=3)

# Highlight values at or below the mean (x <= 75)
plt.fill_between(x, y, where=(x <= 75), alpha=0.25)

plt.title("Normal Distribution: ฮผ = 75, ฯƒ = 8", fontsize=15, weight="bold")
plt.xlabel("Value")
plt.ylabel("Density")
plt.tight_layout()
plt.show()
The resulting plot gives you a clear visual of the classic symmetrical bell curve centered at 75, with the lower half neatly shaded to represent 50% of the total area under the curve.

3. Calculating Probabilities: CDF and PPF

Beyond visualization, scipy.stats gives you straightforward methods to solve practical statistical questions.

Cumulative Distribution Function (CDF)

The CDF answers: What is the probability that the random variable $X$ is less than or equal to a specific value $x$?

  • $P(X \le 70)$:

    Python
    prob_less_70 = round(distribution.cdf(70), 4)
    print("P(X ≤ 70):", prob_less_70)
    # Output: 0.2660
    
    This indicates roughly a 26.6% chance of observing a score of 70 or lower.

  • $P(X > 70)$:
    Because the total probability under the curve equals 1, the probability of exceeding 70 is simply the complement:

    Python
    prob_greater_70 = round(1 - distribution.cdf(70), 4)
    print("P(X > 70):", prob_greater_70)
    # Output: 0.7340
    

Percent Point Function (PPF)

The PPF (the inverse of the CDF or quantile function) answers the reverse question: What value corresponds to a specific cumulative probability or percentile?

  • Finding the 90th percentile:

    Python
    percentile_90 = round(distribution.ppf(0.90), 2)
    print("90th percentile:", percentile_90)
    # Output: 85.25
    
    A value of approximately 85.25 cuts off the top 10% of the distribution from the bottom 90%.

Summary Cheat Sheet

SciPy MethodPurposeTypical Question Answered
norm(loc, scale)Initializes the distribution"Set up a normal curve with mean 75 and standard deviation 8."
.pdf(x)Probability Density Function"What is the relative likelihood height at value $x$?"
.cdf(x)Cumulative Distribution Function"What is the probability that $X \le x$?"
.ppf(q)Percent Point Function (Inverse CDF)"What value marks the $q$-th percentile?"
By swapping out norm for binom, poisson, or uniform, you can apply these same core methods across virtually any common statistical distribution in Python.

Comments

Popular posts from this blog

I am from mechanical background.. how I can start with python? Tell me career opportunities also .

Root Finding in Python: Solving and Visualizing Equations with SciPy

Here's a step-by-step guide to help you install Visual Studio Code (VSCode) for Python on Windows