Statistics for analysis · lesson 2 of 13
Parts in this lesson
Afrom scipy import statsSciPy's statistics module
Bd = stats.norm(loc=160, scale=7)a distribution, as an object
Cd.cdf(x), d.sf(x), d.ppf(q), d.pmf(k), d.rvs(size)probabilities below and above, percentiles, exact counts, random draws
Dstats.probplot(x, dist="norm", plot=ax)a Q–Q plot

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:

Distribution gallerysix shapes, and where they come from
mean 0.00-10-50510density
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:

ProcessDistributionExample
every value in a range equally likelyuniforma random minute in an hour
a sum of many small, independent effectsnormalmeasurement error; the mean of a sample
yeses in n tries with the same chance pbinomialGCash payers among 20 customers
events at random, at a steady ratePoissoncustomers in an hour
the wait between those eventsexponentialminutes until the next customer
a product of many small factorslog-normalincomes, 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.

distributions.ipynb

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.

distributions.ipynb

The first cell is a made-up group of heights, normal with mean 160 cm and standard deviation 7 cm:

MethodAnswersHere
cdf(x)the chance of a value at or below x7.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 percentilethe middle 95% are 146.3 to 173.7 cm
rvs(size)random drawsfive made-up people
pmf(k)for counts: the chance of exactly kused 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 n=20 and p=0.31:

P(X=k)=(nk)pk(1−p)n−k

(nk), said "n choose k", counts the ways of picking which k of the n 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.

Galton board12 rows of pegs, left or right at each
0123456789101112

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 n=12, p=0.5: mean 6, standard deviation 12×0.5×0.5=3≈1.73. 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.

distributions.ipynb
  • 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.

receipts.ipynb

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

A call centre counts the calls that arrive each minute. Which distribution is the natural first guess?
Predict, then check: stats.norm(0, 1).ppf(0.975) is about…
A Q–Q plot against the normal is straight in the middle but curves up sharply at the right end. What does that say?

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.

All sources for Simple Data Analysis