Distributions
Last updated
A histogram shows the shape of the data you have. A distribution is a shape with a name and a formula: a model of where data like yours comes from. A handful of them turn up again and again, because the same few processes keep making data: sums, counts, waits and products. In this lesson you'll meet six, use them with scipy.stats, see a normal curve build itself from coin tosses, and check whether a model fits with a Q–Q plot.
By the end of this lesson you can
- Name six common distributions and the processes that make them
- Compute probabilities and percentiles with scipy.stats
- Check whether data fits a distribution with a Q–Q plot
Shapes with names
A distribution says which values a quantity can take and how likely each is. For counts (whole numbers) it gives a probability for each value; for measurements it gives a density, a curve whose area between two values is the probability of landing between them. Either way, the total is 1.
Six distributions cover a lot of real data. Try each one, and move its sliders:
stats.norm(loc=0, scale=1.0) Normal. Sums and averages of many small, independent effects: measurement errors, heights, the mean of a big sample (lesson 8.4). Mean 0.00, standard deviation 1.00.
What matters most is where each comes from, because that's how you guess which one fits your data:
| Process | Distribution | Example |
|---|---|---|
| every value in a range equally likely | uniform | a random minute in an hour |
| a sum of many small, independent effects | normal | measurement error; the mean of a sample |
| yeses in tries with the same chance | binomial | GCash payers among 20 customers |
| events at random, at a steady rate | Poisson | customers in an hour |
| the wait between those events | exponential | minutes until the next customer |
| a product of many small factors | log-normal | incomes, prices, receipt sizes |
Run the cell: it makes each process with NumPy (5,000 times) and draws the distribution's formula, from scipy.stats, over the result.
The bars are the simulation, the pink line or dots the formula. Notice the second panel: adding up 12 uniform numbers, none of them bell-shaped, gives a bell. Step 3 shows why.
Distributions in scipy.stats
SciPy is the scientific library built on NumPy; its scipy.stats module has more than 100 distributions, and the tests used in the rest of this stage. A distribution is an object: make one with its parameters, then ask it questions. Run the cells.
The first cell is a made-up group of heights, normal with mean 160 cm and standard deviation 7 cm:
| Method | Answers | Here |
|---|---|---|
cdf(x) | the chance of a value at or below x | 7.7% are under 150 cm |
sf(x) | the chance of a value above x (1 - cdf, but more precise far out) | 1.6% are over 175 cm |
ppf(q) | the value with a share q below it: a percentile | the middle 95% are 146.3 to 173.7 cm |
rvs(size) | random draws | five made-up people |
pmf(k) | for counts: the chance of exactly k | used in the next cell |
The second cell tests a model against the receipts. 31% of customers paid by GCash, so if customers choose independently, the number of GCash payers among 20 in a row should follow a binomial distribution with and :
, said "n choose k", counts the ways of picking which of the customers are the yeses. The table and the chart compare the 60 groups of 20 with the formula. With only 60 groups the bars are bumpy, but they follow the dots: most groups have 4 to 8 GCash payers, and none has more than 12.
Sums make bells
Why is the normal curve everywhere? Drop some balls. At each of the 12 rows of pegs a ball goes left or right, like a coin toss, and its bin is the number of rights.
Drop some balls. Each one's bin is the number of times it went right: a sum of 12 coin tosses.
Each ball's bin is a sum of 12 small random steps, so it's a binomial count with , : mean 6, standard deviation . With a few balls the pile is ragged. With a thousand it fills out the pink normal curve with the same mean and standard deviation.
Francis Galton built a board like this in the 1870s to show the same idea: whenever a quantity is the sum of many small, independent pushes, the result is close to normal, whatever the pushes look like. Lesson 8.4 makes this exact, as the central limit theorem, and it's why averages of samples behave so predictably.
Does it fit? Q–Q plots
A histogram can make almost any shape look roughly bell-like. A Q–Q plot (quantile–quantile plot) is a sharper check. It sorts the data and plots each value against where it would be if the data came from the distribution: the 1st percentile against the model's 1st percentile, and so on. If the data fits, the points lie on a straight line. Run the cells.
- pesos: the points curve upward at the right end. The biggest receipts are much bigger than a normal distribution would allow: that's the right skew from 8.1. The left end bends too, because receipts can't go below zero.
- log of pesos: taking logarithms turns a product into a sum, so if the receipts were log-normal, their logs would be normal and lie on the line. They're much straighter. The ends still bend the other way, though, and the skew has gone from 1.26 to −0.57: the logs have a slight tail to the left. Receipts are close to log-normal, not exactly.
How to read the bends: a curve up at the right means a longer right tail than the model; an S shape means heavier tails on both sides; a straight line means the model fits.
Wrong way: normal receipts
The owners want to know how often a receipt is over ₱300, to decide whether to offer a discount on big purchases. Someone uses the normal distribution, because it's the one everybody knows. Run the cells.
The normal model has the right mean and standard deviation, and it's still wrong in ways that matter:
- "Mean ± 2 standard deviations", the usual "range of normal values", runs from −₱34 to ₱253. A negative receipt is impossible, and the model says 6.4% of receipts would be below zero.
- It says 0.4% of receipts are over ₱300. In the real week it was 1.6%, four times as many. The tail is exactly where a skewed distribution and a normal one disagree most, and it's the part the question is about.
The log-normal model (second cell) can't go below zero, but says 4.5% are over ₱300, now too many: as step 4 showed, the receipts aren't quite log-normal either. When the question is about the data itself, the real share, 1.6%, beats both models.
The check: before using a distribution's formula, look at a Q–Q plot of your data against it, and compare what the model says with the data for the part you care about (here, the share over ₱300). If you can count the answer in the data, count it.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- SciPy: Statistical functions (scipy.stats): the distributions, and their methods
- SciPy: scipy.stats.probplot: Q–Q plots against a distribution
- NIST/SEMATECH e-Handbook of Statistical Methods: Gallery of Distributions: the common distributions, their shapes and uses
- NIST/SEMATECH e-Handbook of Statistical Methods: Normal Probability Plot: reading a normal Q–Q plot: straight, curved, S-shaped
- Limpert, Stahel and Abbt, Log-normal Distributions across the Sciences, BioScience 51(5), 2001: why products of many factors give log-normal data