Visual walkthrough — scipy.stats — distributions, hypothesis tests
Step 1 — Start with a bag of numbers and one claim
WHAT. We have five measured values — say the weight of cereal boxes labelled "5.0 kg": Each is one number the machine spat out, an independent draw from a Normal population (the two assumptions above). Someone claims the true average weight is exactly . We call that claim the null hypothesis and write it , with .
WHY. Before we can measure "how surprising" our data is, we need something to be surprised relative to. is that reference line. Without a claim, there is nothing to test.
PICTURE. The dots are our five numbers scattered on a number line. The plum line is the claim . The question the whole page answers: are these dots too far from the plum line to be luck?
Step 2 — Collapse the bag to one summary: the sample mean
WHAT. Add all the numbers and divide by how many:
Reading the symbols where they sit: (say "x-bar") is our best single guess for the centre; the out front shares the total equally among the values; the sum inside piles them up.
WHY. We cannot compare five dots to one claim directly. So we squeeze the whole bag into a single balance point — the mean. Now it's one number () versus one number ().
PICTURE. The orange triangle is the balance point where the dots would sit level on a seesaw. That balance point is . The gap between it and the plum line is the raw "effect" we're worried about.
Step 3 — Measure the spread, because a gap only matters relative to noise
WHAT. Compute the sample standard deviation :
Term by term: is how far one dot sits from the balance point; squaring makes every distance positive and punishes big misses harder; adds those squared misses up; averages them (we divide by , not , because one degree of "wiggle" was already spent computing ); the outer brings the units back from "squared kg" to "kg".
WHY. A gap of is huge if the dots are tightly bunched, but trivial if they're scattered wildly. We literally cannot judge the gap until we know the typical spread. is that yardstick. It estimates the true, unknown from Step 1.
PICTURE. The teal band shows : the typical wobble of a single measurement. Notice how the same gap looks alarming against a narrow band and harmless against a wide one.
Step 4 — Shrink the noise of the mean itself: the standard error
WHAT. The mean is far steadier than any single dot. Its own spread is the standard error:
Here is the spread of one measurement (Step 3), and in the denominator shrinks it because we averaged of them.
WHY. We're comparing to , so the ruler must describe how much jitters, not how much a single jitters. Averaging cancels ups against downs: because the draws are independent, the variance of a mean of values is , so its standard deviation is . Estimating by gives . (If the draws were not independent, the cross-terms would not vanish and this clean shrinkage would break.) This is exactly the shrinkage the Central Limit Theorem promises.
PICTURE. Left: the wide teal band, the spread of one measurement. Right: the much narrower orange band, the spread of the mean — same data, shrunk by . More data ⇒ tighter guess.
Step 5 — Standardize: turn the gap into "how many rulers away"
WHAT. Divide the gap by the standard error:
Top (): the raw gap between our balance point and the claim. Bottom (): the size of one "ruler" of mean-jitter. The whole fraction answers: how many standard errors does the gap span?
WHY. A gap in kilograms can't be judged on its own — is kg a lot? By dividing by SE we get a unit-free number. This is the identical z-trick from the parent note (center, then scale), applied to the mean. The result means the same thing for cereal boxes, exam scores, or reaction times.
PICTURE. The gap is now drawn as a count of SE-rulers laid end to end. Here the gap is a small fraction of one ruler — a tiny .
Step 6 — Put on its distribution and read the tail
WHAT. Under the claim , the value we just built is one draw from . The two-sided p-value is the total area in both tails beyond :
(absolute value) folds a negative to positive, because "the mean is too low" is just as surprising as "too high." The is the upper-tail area (survival = ); the factor counts the mirror-image lower tail.
WHY. The p-value asks: if the claim were true, how often would pure luck hand us a this far from zero or farther? That "at least this extreme" is exactly the shaded tail area. Small area ⇒ our data lives out in the boonies ⇒ the claim looks wrong. This is p-values and Significance made literal.
PICTURE. The Student-t bell centred at . Our sits just off centre; its mirror on the left. Both burnt-orange shaded slivers together are . Two-sided means both slivers count.
Step 7 — Edge & degenerate cases (never leave the reader stranded)
WHAT & WHY, case by case. The formula has a denominator and an area — both can misbehave.
PICTURE. Four mini-panels: (a) centred, all area, ; (b) arrow flying to the edge, ; (c) equal-and-opposite shading the same total area; (d) a needle-thin bell from huge where a tiny gap already reaches the tail.
The one-picture summary
Everything on this page is one pipeline: bag of independent Normal numbers → one gap → one ruler → one standardized → one tail area → the p-value.
Recall Feynman retelling — say it to a 12-year-old
A cereal box says "5 kilos." You weigh five boxes — each weighed on its own, so one reading never leans on another (that's the independence assumption), and the factory's weights wobble in a nice symmetric bell (the normal assumption). First you find their average (the balance point of the dots) — that's your one honest guess for the truth. It's a little off from 5. But "a little off" means nothing until you know how jumpy the scale is, so you measure the spread of the five weights. Then here's the clever bit: the average of five weights wobbles much less than any single weight, so you shrink the spread by dividing by the square root of five — that's your real ruler, the standard error. Now you ask: how many rulers is my average away from 5? That count is . Finally you look at a bell curve of "how often plain luck gives a this big," and you shade the far tails. That shaded sliver is the p-value. Big sliver: "eh, luck, boxes are fine." Tiny sliver: "no way that's luck — the factory is cheating." That is the entire test, and stats.ttest_1samp just does these five squeezes for you.