HOWTO · SciPy
How to Calculate the Probability of Normal Distribution using SciPy
This article provides a comprehensive guide on calculating the probability of normal distribution using SciPy in Python. Learn about the Probability Density Function (PDF), Cumulative Distribution Function (CDF), and how to find percentiles and Z-scores. With practical examples and explanations, this post will enhance your understanding of normal distribution and its applications in data analysis.
On this page
SciPy’s scipy.stats.norm object models the normal (Gaussian) distribution, so you can calculate densities, cumulative probabilities, and percentiles with a few calls instead of working through the formulas by hand. This guide shows how to set the distribution’s mean and standard deviation and how to pick between norm.pdf(), norm.cdf(), and norm.ppf() for the question you are trying to answer.
Understand the normal distribution and its parameters
A normal distribution is fully described by two numbers: the mean μ, which centers the bell curve, and the standard deviation σ, which controls its width. In SciPy these appear as the loc and scale arguments — loc is the mean and scale is the standard deviation, not the variance. The standard normal distribution is the special case loc=0, scale=1.
Because the normal distribution is continuous, the functions below answer different kinds of questions. norm.pdf() returns the height of the density curve at a point: useful for comparing relative likelihood, but it is a density, not the probability of that exact value (for a continuous variable, the probability of hitting any single exact value is 0). Real probabilities live over intervals, and norm.cdf() is what gives you those areas.
Calculate probability density with norm.pdf()
Pass a value, or an array of values, to norm.pdf() together with loc and scale to get the density at those points:
from scipy.stats import norm
density_at_mean = norm.pdf(0, loc=0, scale=1)
density_at_1_5 = norm.pdf(1.5, loc=0, scale=1)
print(f"PDF at the mean: {density_at_mean:.4f}")
print(f"PDF at x = 1.5: {density_at_1_5:.4f}")
PDF at the mean: 0.3989
PDF at x = 1.5: 0.1295
The density peaks at the mean and falls off symmetrically, so values near 0 are far more likely per unit of x than values near 1.5. Remember that 0.3989 is a density — with a small scale, densities can even rise above 1, which is impossible for a probability.
The same call accepts a whole grid of points, which is how you obtain the curve’s data. Evaluate the density at each x from -5 to 5 and you can chart the pairs with a plotting library such as Matplotlib (plt.plot(xs, densities)). The script below computes the grid and locates the curve’s peak:
from scipy.stats import norm
xs = [i / 20 for i in range(-100, 101)]
densities = norm.pdf(xs, loc=0, scale=1)
peak_index = int(densities.argmax())
print(f"Computed {len(xs)} density values from {xs[0]:.1f} to {xs[-1]:.1f}")
print(f"Peak density {densities[peak_index]:.4f} at x = {xs[peak_index]:.1f}")
Computed 201 density values from -5.0 to 5.0
Peak density 0.3989 at x = 0.0
The curve peaks at the mean of 0 and narrows or widens as scale shrinks or grows, which is the visual signature of the standard deviation.
Calculate cumulative probabilities with norm.cdf()
To turn density into an actual probability, ask for the area under the curve. norm.cdf() returns the probability that a value drawn from the distribution is less than or equal to a threshold — the accumulated area to the left of that point.
Run this example:
from scipy.stats import norm
mean = 0
std_dev = 1
value = 1.5
cdf_value = norm.cdf(value, mean, std_dev)
print(f"The CDF at x = {value} is {cdf_value:.4f}")
The CDF at x = 1.5 is 0.9332
About 93.32% of draws from a standard normal fall at or below 1.5. Because the CDF accumulates area from the far left, the probability of landing inside an interval is the difference between two CDF values:
from scipy.stats import norm
p_between = norm.cdf(1, loc=0, scale=1) - norm.cdf(-1, loc=0, scale=1)
print(f"P(-1 < X < 1) = {p_between:.4f}")
P(-1 < X < 1) = 0.6827
That 0.6827 is the familiar rule of thumb that about 68% of a normal distribution sits within one standard deviation of the mean.
Find percentiles and z-scores with norm.ppf()
norm.ppf() inverts the CDF: give it a probability as a fraction between 0 and 1, and it returns the cutoff value at that percentile. On the standard normal, that cutoff is exactly the z-score — the number of standard deviations above the mean:
from scipy.stats import norm
percentile = 0.95
z_score = norm.ppf(percentile, loc=0, scale=1)
print(f"The z-score at the 95th percentile is {z_score:.4f}")
The z-score at the 95th percentile is 1.6449
So 95% of a standard normal falls below z = 1.6449. For a distribution with its own mean and standard deviation, norm.ppf() applies the transformation x = loc + z·scale for you and returns the cutoff in the distribution’s units:
from scipy.stats import norm
cutoff = norm.ppf(0.90, loc=100, scale=15)
print(f"The 90th percentile is {cutoff:.4f}")
The 90th percentile is 119.2233
For example, if test scores follow a normal distribution with a mean of 100 and a standard deviation of 15, about 90% of scores are at or below 119.22.
Interpret results and common pitfalls
scaleis the standard deviation, not the variance. Passing σ² instead of σ silently widens the distribution and corrupts every result.scalemust also be positive; a zero or negative value yieldsnanresults.- Density is not probability.
norm.pdf()can return values above 1 whenscaleis small, and it never gives the probability of an exact point. Usenorm.cdf()differences for probabilities over intervals. norm.ppf()expects a fraction, not a percent. Pass0.95for the 95th percentile, not95; inputs outside 0–1 return infinite or undefined quantiles.- Endpoints do not matter for intervals. Because the distribution is continuous, P(X ≤ t) equals P(X < t), so inclusive or strict bounds give the same interval probability.
- Far tails need the survival function. For extreme thresholds,
1 - norm.cdf(x)loses floating-point precision;norm.sf(x)computes the right-tail probability directly and stays accurate.
With these conventions in place, scipy.stats.norm covers the everyday questions — how likely a region is, where a percentile falls, and how one distribution’s parameters reshape the same bell curve.