5.4.14 · D1Scientific Computing (Python)

Foundations — scipy.stats — distributions, hypothesis tests

3,627 words16 min readBack to topic

This page assumes nothing. We build every letter, every squiggle, every function name used in the parent note from the ground up, in an order where each idea needs only the ones before it.


0. What is "randomness" even drawing?

Imagine you measure something that wobbles — the height of a person, the roll of a die, the noise in a sensor. You can't predict one measurement, but if you take thousands and stack them into a bar chart (a histogram), a shape appears. That shape is stable even though each measurement is not.

Figure — scipy.stats — distributions, hypothesis tests

Walk-through of the figure above: the pale blue bars are a histogram of 4000 simulated measurements — the horizontal axis is the measured value, the vertical axis is how often each range occurred. Notice the bars are jagged, yet the orange curve laid over them traces a clean, symmetric hump. That orange curve is the stable shape the raw randomness is hiding: individual bars flicker if you re-sample, but the hump barely moves.

A specific value we actually observed is written with a small letter . So is the die; is the "4" you rolled.


1. Probability — the symbol

So reads out loud as "the probability that my next measurement is 130 or below." The little means "less than or equal to."


2. From bars to a smooth curve — the PDF, symbol

If we take infinitely many measurements and make the bars infinitely thin, the jagged histogram smooths into a continuous curve. (From here on we assume is continuous — the smooth-curve picture.)

Walk-through of the figure above: the left panel shows the smooth orange PDF; the vertical red stick at measures the curve's height there — that height is . The right panel is the same curve, but now the blue region from far-left up to is shaded — that area is . Same curve, two different questions: left asks "how tall here?", right asks "how much area so far?".

The symbol just means "a tiny width." Height times tiny width = tiny area = tiny probability. This "area = probability" idea is the engine of everything next.

See Normal Distribution for the specific bell-shaped the parent uses most.


3. Adding up all the area so far — the CDF, symbol

Note : the slab area is "area up to " minus "area up to ."

The link between and : PDF is the steepness of the CDF. Where the bell is tall, area piles up fast, so rises steeply. That "rate of climb" is what the derivative symbol means — see §7.


4. The leftover area — the Survival Function, symbol or .sf

Because all area sums to (normalization): (area left of ) + (area right of ) , so . That's the whole derivation.


5. Running the CDF backwards — the PPF (quantile), symbol

Sometimes you don't have a value and want its area; you have an area and want the value.

The little means "undo": turns a value into an area; turns an area back into a value. For a continuous distribution whose climbs smoothly, they cancel cleanly: .


6. The Greek letters — , , and

Walk-through of the figure above: two bells share the same center. The tall narrow blue curve has a small (values cluster tightly); the short wide orange curve has a large (values scatter). The dashed grey line marks (the balance point), identical for both, and the small horizontal blue arrow shows one step out from center — the ruler that provides.

The hat symbol as in means "an estimate from data" (a guess), while plain is the true (usually unknown) value. stats.norm.fit returns hatted estimates — see Maximum Likelihood Estimation.


7. Why a derivative appears — the symbol

So PDF and CDF are two views of one object: PDF = slope of CDF, CDF = running total of PDF. (This derivative bridge is another reason PDF/CDF live in the continuous world — a staircase CDF has no slope at its jumps.)


8. Sampling and the mystery — symbols , , , SE

This shrinkage is the reason big samples give sharp conclusions, and it powers the Central Limit Theorem the whole toolbox leans on.


9. Standardizing — the z-score, symbol


10. The t-statistic, symbol


11. The question every test asks — the p-value and

Read more on interpreting this correctly in p-values and Significance. The mismatch-measuring cousin of the normal, the Chi-square Distribution, powers the die-fairness test.

Walk-through of the figure above: the orange curve is the distribution of the test statistic assuming is true. The vertical red line marks the value you actually observed. The red-shaded tail to its right is the p-value — the chance pure luck under would push the statistic this far out or further. A thin red tail means "luck can barely reach here," which is why a small p-value makes you doubt .


12. How the foundations feed the topic

Random variable X

Probability P as area

PDF f of x = curve height

Normalization total area = 1

CDF F of x = area collected

Survival S = right tail

PPF = inverse CDF

Derivative links PDF and CDF

mean mu and spread sigma

z-score z = x minus mu over sigma

sample mean x-bar and SE

t-statistic

p-value = tail area

threshold alpha

Hypothesis tests


Equipment checklist

Test yourself — cover the right side. If any answer is fuzzy, reread that section before the deep dives.

What does the capital mean vs small ?
is the not-yet-decided random measurement; is one specific observed value.
Continuous vs discrete ?
Continuous lands on any real value (smooth curve, areas); discrete takes separate values (spikes, sums).
in plain English?
The probability my next measurement is 130 or less — the area under the curve left of 130.
Is (the PDF) a probability?
No — it's a density (height); probability comes from area height width .
The two rules a legal PDF must obey?
everywhere, and (normalization).
Probability of a region ?
— the area of that slab.
What does (CDF) accumulate, and its range?
Total area under the PDF up to ; it climbs monotonically from to .
In , what is ?
A dummy (placeholder) variable that sweeps up to the fixed endpoint ; its name carries no meaning.
How do you get the RIGHT-tail area?
Survival , i.e. .sf(x).
What does the PPF do, and when can it misbehave?
Undoes the CDF (); for discrete it may be non-unique (flat step) or undefined (jump).
What are and picturewise, and by formula?
= balance point ; = spread .
Write the sample standard deviation .
— data-based estimate of , divided by .
Why divide the spread by ?
The sample mean's variance is , so its standard error is ; averaging shrinks noise.
Write the z-score formula.
— how many standard deviations sits from the center.
Write the one-sample t-statistic.
— the z-idea for a sample mean using estimated SE.
Define the p-value and .
p ; is the pre-set threshold (e.g. ) below which you reject .