Type at least 2 letters. Enter opens the first result, Esc closes.

    Start here: notation and Python · Lesson 8 · First looks at the main tracks

    Chance and data: probability, mean, variance and the normal curve

    Beginner 60 min 5 interactive figures 4 videos Colab notebook

    Before this lesson: lesson 02 (∑\sum-notation), lesson 03 (seeded random numbers, axis) and lesson 04 (exe^x). Time: about 60 minutes, plus the notebook. The full story is in the Probability and statistics track.


    Why this matters #

    • Preprocessing. StandardScaler in scikit-learn turns each feature into z-scores: subtract the mean, divide by the standard deviation. Many models train better after this step.
    • Initialization. Neural network weights start as random numbers, often from a normal distribution N(0,σ2)\mathcal N(0, \sigma^2) with σ2\sigma^2 chosen from the layer size (Glorot and He initialization).
    • Losses. If you assume that the noise in the targets is normal, the best fit is the one with the smallest squared error. This is why the mean squared error is the default loss for regression.
    • Evaluation. A test accuracy is a frequency. On 1000 test examples, an accuracy of 0.90 has an uncertainty of about ±0.02\pm 0.02. Two models that differ by 0.005 may not differ at all.
    • Training noise. A mini-batch gradient is an average over BB examples. Its noise shrinks like 1/B1/\sqrt{B}, the same law that you see for coin flips below.

    1. Predict first #

    2. The picture #

    2.1 Probability as long-run frequency #

    Flip a fair coin many times and keep a running score: the relative frequency of heads, that is, (number of heads so far) / (number of flips so far). After 10 flips it can be 0.3 or 0.7. After 10 000 flips it is very close to 0.5. This is the frequency view of probability: P(heads)=0.5P(\text{heads}) = 0.5 means that the relative frequency gets close to 0.5 in a long series of independent flips. (The Probability and statistics track also treats probability as a degree of belief; the rules are the same.)

    The important word is slowly. The typical gap between the frequency and pp is p(1−p)/n\sqrt{p(1-p)/n} (section 3.4 proves this). It shrinks like 1/n1/\sqrt n: to make the gap 10 times smaller, you need 100 times more flips.

    Interactive figure: loading…

    Flip, then compare runs. Each line is one run of 10 000 flips (run 1 is blue). The aqua dashed lines are the band p±2p(1−p)/np \pm 2\sqrt{p(1-p)/n}; for large nn about 95 % of runs are inside it. Press New flips a few times: early on the runs disagree a lot, later they all stay near pp. The x-axis is logarithmic, so each step to the right is 10 times more flips.

    2.2 Centre and spread #

    Put the data 2,4,4,4,5,5,7,92, 4, 4, 4, 5, 5, 7, 9 on a number line. The mean xˉ\bar x is the point where the line would balance if each data point were a small equal weight. The median is the middle value after sorting. To measure the spread, take each deviation xi−xˉx_i - \bar x, square it, and average the squares: this is the variance. Its square root is the standard deviation.

    Interactive figure: loading…

    Drag the blue dots. The violet triangle is the mean (the balance point) and the yellow diamond is the median. Each orange square has side ∣xi−xˉ∣|x_i - \bar x|, so its area is the squared deviation; the variance is the average area. Press Add outlier at 20 and watch the mean move but the median hardly move. Press Shift all +1: the mean moves, the variance does not.

    2.3 Histograms #

    For many data points, a dot plot is too crowded. A histogram cuts the number line into bins of equal width ww and draws a bar for each bin. The bar height is either the count (number of points in the bin) or the density count/(nw)\text{count} / (n w). With density heights, the area of each bar is the fraction of the data in that bin, so all bar areas add up to 1. Only density histograms can be compared with a density curve, or with a histogram of a different size nn.

    Interactive figure: loading…

    Change the bin width ww. The blue bars are the histogram; the orange curve is the true density the samples came from. With w=2w = 2 the two peaks merge into one; with w=0.1w = 0.1 and n=50n = 50 the bars are mostly 0 or 1 sample and look like noise. Switch between count and density: the shape stays, the y-axis changes, and in density mode the bar areas add up to 1.

    2.4 The normal curve #

    As nn grows and ww shrinks, a density histogram gets close to a smooth curve: the density of the distribution. The most common one is the bell-shaped normal curve N(μ,σ2)\mathcal N(\mu, \sigma^2). Its centre is the mean μ\mu, and its width is the standard deviation σ\sigma. A probability is an area under the curve: P(a≤X≤b)P(a \le X \le b) is the area between aa and bb.

    Interactive figure: loading…

    Drag aa and bb, then change σ\sigma. The yellow area under the orange curve is P(a≤X≤b)P(a \le X \le b). Use the presets ±1σ\pm 1\sigma, ±2σ\pm 2\sigma, ±3σ\pm 3\sigma to see 0.6827, 0.9545 and 0.9973. When you move μ\mu or σ\sigma, the blue handles keep their z-scores and the area does not change. With σ=0.1\sigma = 0.1 the peak is near 4, above the dashed line at density 1.

    Quick check

    For a fair coin (p=0.5p = 0.5), the typical gap between the running frequency and pp is p(1−p)/n=0.5/n\sqrt{p(1-p)/n} = 0.5/\sqrt n. About how many flips do you need until the band p±2⋅0.5/np \pm 2 \cdot 0.5/\sqrt n is only ±0.01\pm 0.01 wide?

    Quick check

    The data 2,4,4,4,5,5,7,92, 4, 4, 4, 5, 5, 7, 9 have mean 5 and median 4.5. You add one value, 50. What are the new mean and median?

    3. Definitions and main results #

    3.1 Experiments, events and the rules of probability #

    An experiment has outcomes that we cannot predict, for example one roll of a die. The set of all outcomes is the sample space Ω={1,2,3,4,5,6}\Omega = \{1, 2, 3, 4, 5, 6\}. An event is a set of outcomes, for example A={2,4,6}A = \{2, 4, 6\} ("even"). A probability PP gives each event a number with three properties:

    1. 0≤P(A)≤10 \le P(A) \le 1 for every event AA;
    2. P(Ω)=1P(\Omega) = 1 (something happens);
    3. if AA and BB are disjoint (they have no outcome in common), then P(A or B)=P(A)+P(B)P(A \text{ or } B) = P(A) + P(B).

    For a fair die, every outcome has probability 1/61/6 and P(A)=∣A∣/6P(A) = |A|/6, so P(even)=3/6P(\text{even}) = 3/6.

    Two rules that follow.

    • Complement: P(not A)=1−P(A)P(\text{not } A) = 1 - P(A). Proof. AA and "not AA" are disjoint, and together they are Ω\Omega. Property 3 and property 2 give P(A)+P(not A)=P(Ω)=1P(A) + P(\text{not } A) = P(\Omega) = 1. ■\blacksquare
    • "Or" for any two events: P(A or B)=P(A)+P(B)−P(A and B)P(A \text{ or } B) = P(A) + P(B) - P(A \text{ and } B). The outcomes in both events are counted twice in P(A)+P(B)P(A) + P(B), so we subtract them once. Example with one die, A={2,4,6}A = \{2, 4, 6\} and B={5,6}B = \{5, 6\}: 36+26−16=46\frac36 + \frac26 - \frac16 = \frac46, and indeed A or B={2,4,5,6}A \text{ or } B = \{2, 4, 5, 6\}.

    Independence. Two events are independent if P(A and B)=P(A) P(B)P(A \text{ and } B) = P(A)\,P(B). In words: knowing that BB happened does not change the chance of AA. Separate flips of a coin are independent, so P(two heads)=12⋅12=14P(\text{two heads}) = \frac12 \cdot \frac12 = \frac14. The product rule is only for independent events: for one die, P(even and odd)=0P(\text{even and odd}) = 0, not 14\frac14.

    Conditional probability

    The probability of AA given that BB happened is P(A∣B)=P(A and B)/P(B)P(A \mid B) = P(A \text{ and } B) / P(B) (for P(B)>0P(B) > 0). Independence is the same as P(A∣B)=P(A)P(A \mid B) = P(A). Bayes' rule, P(A∣B)=P(B∣A)P(A)/P(B)P(A \mid B) = P(B \mid A) P(A) / P(B), follows from writing P(A and B)P(A \text{ and } B) in two ways. The Probability and statistics track builds on this.

    3.2 Random variables and distributions #

    A random variable XX is a number that depends on the outcome: the face of a die, the height of a random person, the error of a model on a random test example.

    • A discrete random variable takes separate values. Its distribution is a table of probabilities P(X=x)P(X = x) that add up to 1. For a fair die, P(X=x)=1/6P(X = x) = 1/6 for x=1,…,6x = 1, \dots, 6.
    • A continuous random variable can take any value in an interval. Its distribution is a density p(x)≥0p(x) \ge 0 with total area 1 under the curve, and
    P(a≤X≤b)=area under p between a and b=∫abp(x) dx.P(a \le X \le b) = \text{area under } p \text{ between } a \text{ and } b = \int_a^b p(x)\,dx .

    (The integral sign ∫\int means "area under the curve"; the Calculus track defines it.)

    A density is not a probability. For a small width hh, P(x≤X≤x+h)≈p(x) hP(x \le X \le x + h) \approx p(x)\,h. So p(x)p(x) is "probability per unit length", and it can be larger than 1 when the distribution is narrow. Example: the uniform distribution on [0,0.5][0, 0.5] has density p(x)=2p(x) = 2 on that interval; the area is 2×0.5=12 \times 0.5 = 1. Also, for a continuous XX, P(X=a)=0P(X = a) = 0 for every single value aa: a line has no area.

    Quick check

    A density curve p(x)p(x) has the value 2.52.5 at x=0x = 0. What can you conclude?

    3.3 Mean, median, variance and standard deviation #

    For data x1,…,xnx_1, \dots, x_n (a sample):

    Quantity Formula Unit
    mean xˉ=1n∑i=1nxi\bar x = \frac{1}{n} \sum_{i=1}^n x_i same as the data
    median middle value after sorting (the average of the two middle values if nn is even) same as the data
    variance σ2=1n∑i=1n(xi−xˉ)2\sigma^2 = \frac{1}{n} \sum_{i=1}^n (x_i - \bar x)^2 data unit squared
    standard deviation σ=σ2\sigma = \sqrt{\sigma^2} same as the data

    The standard deviation (std) is the one to report: it has the unit of the data (cm, not cm²), and it is a typical distance from the mean.

    For a random variable, the same ideas use probabilities as weights. The expectation (or expected value) is E[X]=∑xx P(X=x)\mathbb E[X] = \sum_x x\, P(X = x), and the variance is Var⁡(X)=E[(X−μ)2]\operatorname{Var}(X) = \mathbb E\big[(X - \mu)^2\big] with μ=E[X]\mu = \mathbb E[X]. For a fair die, E[X]=1+2+⋯+66=3.5\mathbb E[X] = \frac{1 + 2 + \dots + 6}{6} = 3.5 (not a possible face: an expectation is an average, not a typical outcome), and Var⁡(X)=35/12≈2.92\operatorname{Var}(X) = 35/12 \approx 2.92. The data formulas are the special case where each of the nn points has probability 1/n1/n.

    Three facts with short proofs.

    1. The deviations add up to 0: ∑i(xi−xˉ)=∑ixi−nxˉ=nxˉ−nxˉ=0\sum_i (x_i - \bar x) = \sum_i x_i - n\bar x = n\bar x - n\bar x = 0. This is why the mean is the balance point.
    2. Shortcut formula: σ2=1n∑ixi2−xˉ2\sigma^2 = \frac1n \sum_i x_i^2 - \bar x^2 (mean of the squares minus the square of the mean). Proof. Expand (xi−xˉ)2=xi2−2xixˉ+xˉ2(x_i - \bar x)^2 = x_i^2 - 2 x_i \bar x + \bar x^2 and average over ii: 1n∑ixi2−2xˉ⋅xˉ+xˉ2=1n∑ixi2−xˉ2\frac1n\sum_i x_i^2 - 2\bar x \cdot \bar x + \bar x^2 = \frac1n\sum_i x_i^2 - \bar x^2. ■\blacksquare
    3. Shift and scale: if yi=axi+by_i = a x_i + b, then yˉ=axˉ+b\bar y = a\bar x + b and σy=∣a∣ σx\sigma_y = |a|\,\sigma_x. Proof. Averaging gives yˉ=axˉ+b\bar y = a \bar x + b. So yi−yˉ=a(xi−xˉ)y_i - \bar y = a(x_i - \bar x): the shift bb cancels. Squaring gives (yi−yˉ)2=a2(xi−xˉ)2(y_i - \bar y)^2 = a^2 (x_i - \bar x)^2, so σy2=a2σx2\sigma_y^2 = a^2 \sigma_x^2 and σy=∣a∣ σx\sigma_y = |a|\,\sigma_x. ■\blacksquare

    Outliers. One extreme value can move the mean and the std a lot, because the mean uses the values and the variance uses their squares. The median uses only the order, so it is robust to outliers.

    3.4 Sums and averages of independent variables #

    Two rules for random variables X,YX, Y (proofs in the probability track):

    • E[X+Y]=E[X]+E[Y]\mathbb E[X + Y] = \mathbb E[X] + \mathbb E[Y] always, and E[aX+b]=a E[X]+b\mathbb E[aX + b] = a\,\mathbb E[X] + b;
    • Var⁡(X+Y)=Var⁡(X)+Var⁡(Y)\operatorname{Var}(X + Y) = \operatorname{Var}(X) + \operatorname{Var}(Y) if XX and YY are independent, and Var⁡(aX)=a2Var⁡(X)\operatorname{Var}(aX) = a^2 \operatorname{Var}(X).
    Why the variances add for independent variables

    Write μX,μY\mu_X, \mu_Y for the means. Then (X+Y)−(μX+μY)=(X−μX)+(Y−μY)(X + Y) - (\mu_X + \mu_Y) = (X - \mu_X) + (Y - \mu_Y). Square and take expectations:

    Var⁡(X+Y)=Var⁡(X)+Var⁡(Y)+2 E[(X−μX)(Y−μY)].\operatorname{Var}(X + Y) = \operatorname{Var}(X) + \operatorname{Var}(Y) + 2\,\mathbb E\big[(X - \mu_X)(Y - \mu_Y)\big].

    For independent variables, the expectation of a product is the product of the expectations, so the last term is 2 E[X−μX] E[Y−μY]=2⋅0⋅0=02\,\mathbb E[X - \mu_X]\,\mathbb E[Y - \mu_Y] = 2 \cdot 0 \cdot 0 = 0. ■\blacksquare

    The 1/n1/\sqrt n law. Let X1,…,XnX_1, \dots, X_n be independent, each with mean μ\mu and variance σ2\sigma^2, and let Xˉ=1n∑iXi\bar X = \frac1n \sum_i X_i. Then

    E[Xˉ]=μ,Var⁡(Xˉ)=1n2⋅nσ2=σ2n,sd⁡(Xˉ)=σn.\mathbb E[\bar X] = \mu, \qquad \operatorname{Var}(\bar X) = \frac{1}{n^2} \cdot n\sigma^2 = \frac{\sigma^2}{n}, \qquad \operatorname{sd}(\bar X) = \frac{\sigma}{\sqrt n}.

    Back to the coin. Let Xi=1X_i = 1 for heads and 00 for tails. Then E[Xi]=p\mathbb E[X_i] = p and E[Xi2]=p\mathbb E[X_i^2] = p, so Var⁡(Xi)=p−p2=p(1−p)\operatorname{Var}(X_i) = p - p^2 = p(1-p) (shortcut formula). The running frequency is Xˉ\bar X, so its typical gap to pp is p(1−p)/n\sqrt{p(1-p)/n}. This is the band in the coin figure. The law of large numbers says the same in words: as nn grows, Xˉ\bar X gets close to μ\mu with probability close to 1.

    3.5 Dividing by nn or by n−1n - 1 #

    The sample variance divides by n−1n - 1:

    s2=1n−1∑i=1n(xi−xˉ)2.s^2 = \frac{1}{n - 1} \sum_{i=1}^n (x_i - \bar x)^2 .

    Why? We usually want the variance σ2\sigma^2 of the whole population, but we measure the deviations from the sample mean xˉ\bar x, not from the true mean μ\mu. The sample mean sits in the middle of this sample, so the deviations from it are a bit too small on average. Dividing by n−1n - 1 instead of nn corrects this exactly: E[s2]=σ2\mathbb E[s^2] = \sigma^2 (we say s2s^2 is unbiased).

    Why n−1n - 1 gives an unbiased estimate

    Let x1,…,xnx_1, \dots, x_n be independent with mean μ\mu and variance σ2\sigma^2.

    1. Write xi−μ=(xi−xˉ)+(xˉ−μ)x_i - \mu = (x_i - \bar x) + (\bar x - \mu) and square: (xi−μ)2=(xi−xˉ)2+2(xi−xˉ)(xˉ−μ)+(xˉ−μ)2(x_i - \mu)^2 = (x_i - \bar x)^2 + 2(x_i - \bar x)(\bar x - \mu) + (\bar x - \mu)^2.
    2. Sum over ii. The middle term is 2(xˉ−μ)∑i(xi−xˉ)=02(\bar x - \mu)\sum_i (x_i - \bar x) = 0 (fact 1 of section 3.3). So ∑i(xi−μ)2=∑i(xi−xˉ)2+n(xˉ−μ)2\sum_i (x_i - \mu)^2 = \sum_i (x_i - \bar x)^2 + n(\bar x - \mu)^2.
    3. Take expectations. On the left, each term has expectation σ2\sigma^2, so we get nσ2n\sigma^2. On the right, E[(xˉ−μ)2]=Var⁡(xˉ)=σ2/n\mathbb E[(\bar x - \mu)^2] = \operatorname{Var}(\bar x) = \sigma^2/n (section 3.4). So nσ2=E[∑i(xi−xˉ)2]+σ2n\sigma^2 = \mathbb E\big[\sum_i (x_i - \bar x)^2\big] + \sigma^2.
    4. Hence E[∑i(xi−xˉ)2]=(n−1)σ2\mathbb E\big[\sum_i (x_i - \bar x)^2\big] = (n - 1)\sigma^2, and dividing by n−1n - 1 gives E[s2]=σ2\mathbb E[s^2] = \sigma^2. ■\blacksquare

    The square root ss is still slightly too small on average, but for large nn the difference is tiny.

    Library defaults differ. NumPy's np.var and np.std divide by nn (ddof=0, "delta degrees of freedom"); pandas' .var() and .std() divide by n−1n - 1 (ddof=1). scikit-learn's StandardScaler uses nn. For the lesson data, np.std gives 2.0 and pandas gives 2.138. For large nn the difference is small; for small nn, say which one you use.

    Quick check

    For the same 8 numbers, np.std(x) prints 2.0 and pd.Series(x).std() prints 2.138. Why?

    3.6 The normal distribution #

    The normal (or Gaussian) distribution N(μ,σ2)\mathcal N(\mu, \sigma^2) has the density

    p(x)=1σ2πexp⁡ ⁣(−(x−μ)22σ2).p(x) = \frac{1}{\sigma\sqrt{2\pi}} \exp\!\left(-\frac{(x - \mu)^2}{2\sigma^2}\right).

    Read it from the inside out: (x−μ)/σ(x - \mu)/\sigma is the distance from the centre in units of σ\sigma; it is squared, so the curve is symmetric; e−(⋅)e^{-(\cdot)} makes the curve fall fast away from the centre (lesson 04); the factor 1σ2π\frac{1}{\sigma\sqrt{2\pi}} makes the total area 1. The peak height is 1σ2π\frac{1}{\sigma\sqrt{2\pi}}: 0.3990.399 for σ=1\sigma = 1 and 3.993.99 for σ=0.1\sigma = 0.1.

    z-scores. The standard normal is Z∼N(0,1)Z \sim \mathcal N(0, 1). If X∼N(μ,σ2)X \sim \mathcal N(\mu, \sigma^2), then the z-score Z=(X−μ)/σZ = (X - \mu)/\sigma is standard normal (shift and scale, section 3.3). So every normal probability is a standard normal probability:

    P(a≤X≤b)=Φ(zb)−Φ(za),za=a−μσ,  zb=b−μσ,P(a \le X \le b) = \Phi(z_b) - \Phi(z_a), \qquad z_a = \frac{a - \mu}{\sigma},\; z_b = \frac{b - \mu}{\sigma},

    where Φ(z)=P(Z≤z)\Phi(z) = P(Z \le z) is the standard normal cumulative distribution function (CDF), in scipy.stats.norm.cdf.

    The 68–95–99.7 rule. For any normal distribution, P(∣X−μ∣≤σ)=0.6827P(|X - \mu| \le \sigma) = 0.6827, P(∣X−μ∣≤2σ)=0.9545P(|X - \mu| \le 2\sigma) = 0.9545, P(∣X−μ∣≤3σ)=0.9973P(|X - \mu| \le 3\sigma) = 0.9973. A value more than 3 standard deviations from the mean is rare (about 3 in 1000), if the data are normal.

    3.7 The central limit theorem #

    Theorem (central limit theorem, CLT). Let X1,…,XnX_1, \dots, X_n be independent and have the same distribution, with mean μ\mu and finite variance σ2>0\sigma^2 > 0. Then, for large nn, the mean Xˉ\bar X is approximately N(μ,σ2/n)\mathcal N(\mu, \sigma^2/n). Precisely: for every zz,

    P ⁣(Xˉ−μσ/n≤z)→Φ(z)as n→∞.P\!\left(\frac{\bar X - \mu}{\sigma/\sqrt n} \le z\right) \to \Phi(z) \quad \text{as } n \to \infty .

    The same holds for the sum X1+⋯+XnX_1 + \dots + X_n, which is approximately N(nμ,nσ2)\mathcal N(n\mu, n\sigma^2).

    Interactive figure: loading…

    Increase the number of dice kk. The blue bars are the exact distribution of the mean of kk dice; the orange curve is the normal curve with the same mean and standard deviation σ/k\sigma/\sqrt k. One die is flat, two dice give a triangle, and from about 5 dice the bars follow the curve closely. The loaded die is skewed: it needs more dice before the bars look normal.

    What the CLT does not say.

    • It does not say that data are normal. It is about averages and sums of many independent effects. Incomes, waiting times and word frequencies are strongly skewed.
    • It does not say how large nn must be. For a symmetric distribution, n=5n = 5 can be enough; for a skewed one you need more.
    • It needs a finite variance and independence. The mean of samples from the Cauchy distribution (which has no finite variance) never settles: it has the same distribution for every nn.

    This is why the normal curve is everywhere: a measurement error is often the sum of many small independent errors.

    Answers to section 1. (1) Not necessarily. A fair coin gives 7 or more heads in 10 flips with probability 176/1024≈0.17176/1024 \approx 0.17 (section 4c), about 1 time in 6. Ten flips are too few to tell. (2) The standard deviation is 2 (section 4a). After adding 50, the mean jumps from 5 to 10, but the median only moves from 4.5 to 5. (3) Yes. Density is probability per unit length; only areas must be at most 1. The peak of N(0,0.12)\mathcal N(0, 0.1^2) is 3.99.

    4. Worked example #

    (a) Mean, variance and standard deviation by hand. Data: 2,4,4,4,5,5,7,92, 4, 4, 4, 5, 5, 7, 9, so n=8n = 8.

    Mean: xˉ=2+4+4+4+5+5+7+98=408=5\bar x = \frac{2 + 4 + 4 + 4 + 5 + 5 + 7 + 9}{8} = \frac{40}{8} = 5.

    xix_i 2 4 4 4 5 5 7 9 sum
    xi−xˉx_i - \bar x −3-3 −1-1 −1-1 −1-1 0 0 2 4 0
    (xi−xˉ)2(x_i - \bar x)^2 9 1 1 1 0 0 4 16 32

    The deviations add up to 0 (fact 1). Variance: σ2=32/8=4\sigma^2 = 32/8 = 4, so σ=2\sigma = 2. Sample variance: s2=32/7≈4.571s^2 = 32/7 \approx 4.571, so s≈2.138s \approx 2.138. Median: the sorted data are already in order; the two middle values are 4 and 5, so the median is 4.5.

    Check with the shortcut formula: ∑ixi2=4+3⋅16+2⋅25+49+81=232\sum_i x_i^2 = 4 + 3 \cdot 16 + 2 \cdot 25 + 49 + 81 = 232, so 2328−52=29−25=4\frac{232}{8} - 5^2 = 29 - 25 = 4 ✓.

    z-scores zi=(xi−5)/2z_i = (x_i - 5)/2: −1.5,−0.5,−0.5,−0.5,0,0,1,2-1.5, -0.5, -0.5, -0.5, 0, 0, 1, 2. The value 9 is 2 standard deviations above the mean.

    Shift and scale: for yi=3xi+5y_i = 3x_i + 5, the mean is 3⋅5+5=203 \cdot 5 + 5 = 20 and the std is 3⋅2=63 \cdot 2 = 6.

    Outlier: add 50. Mean =90/9=10= 90/9 = 10, median =5= 5, and σ\sigma grows from 2 to about 14.27.

    (b) A normal probability. Adult heights in some population are about N(170,102)\mathcal N(170, 10^2) (in cm). What fraction is between 160 and 180 cm, and what fraction is above 190 cm?

    1. z-scores: z=(160−170)/10=−1z = (160 - 170)/10 = -1 and z=(180−170)/10=1z = (180 - 170)/10 = 1.
    2. P(160≤X≤180)=Φ(1)−Φ(−1)=0.8413−0.1587=0.6827P(160 \le X \le 180) = \Phi(1) - \Phi(-1) = 0.8413 - 0.1587 = 0.6827: the "68" of the rule.
    3. For 190 cm: z=(190−170)/10=2z = (190 - 170)/10 = 2. By symmetry, the two tails outside ±2\pm 2 have 1−0.9545=0.04551 - 0.9545 = 0.0455 together, so each tail has 0.02280.0228. About 2.3 % are taller than 190 cm.

    (c) Is 7 heads in 10 flips surprising? Assume the coin is fair. Each of the 210=10242^{10} = 1024 sequences of heads and tails has probability 1/10241/1024. The number of sequences with exactly kk heads is (10k)\binom{10}{k}, the number of ways to choose which kk flips are heads. So

    P(7 or more heads)=(107)+(108)+(109)+(1010)1024=120+45+10+11024=1761024≈0.172.P(\text{7 or more heads}) = \frac{\binom{10}{7} + \binom{10}{8} + \binom{10}{9} + \binom{10}{10}}{1024} = \frac{120 + 45 + 10 + 1}{1024} = \frac{176}{1024} \approx 0.172 .

    A fair coin does this about once in 6 tries. It is not good evidence of a bias.

    5. In code #

    NumPy, pandas and SciPy check the numbers of section 4. The notebook for this lesson has these cells and the exercises.

    import numpy as np
    import pandas as pd
    from scipy import stats
    
    x = np.array([2, 4, 4, 4, 5, 5, 7, 9], dtype=float)
    print(x.mean(), np.median(x))               # 5.0 4.5
    print(np.var(x), np.std(x))                 # 4.0 2.0           (divide by n, ddof=0)
    print(np.var(x, ddof=1), np.std(x, ddof=1)) # 4.571... 2.138... (divide by n - 1)
    print(pd.Series(x).std())                   # 2.138...          (pandas: ddof=1 by default)
    print((x - x.mean()) / x.std())             # z-scores: [-1.5 -0.5 -0.5 -0.5  0.   0.   1.   2. ]
    print(np.std(3 * x + 5))                    # 6.0 = 3 * 2
    
    # (b) normal probabilities with the CDF
    print(stats.norm.cdf(180, 170, 10) - stats.norm.cdf(160, 170, 10))  # 0.6826894921370859
    print(stats.norm.sf(190, 170, 10))          # 0.022750131948179198  (sf = 1 - cdf)
    
    # (c) 7 or more heads in 10 fair flips
    print(stats.binom.sf(6, 10, 0.5))           # 0.171875 = 176/1024  (P(X > 6))
    
    # the running frequency of heads, with a seed
    rng = np.random.default_rng(0)
    flips = rng.random(10_000) < 0.5            # True = heads
    freq = np.cumsum(flips) / np.arange(1, 10_001)
    print(freq[[9, 99, 999, 9999]])             # [0.3   0.44  0.473 0.499]  (n = 10, 100, 1000, 10 000)
    

    With this seed, the first 10 flips give only 3 heads. The frequency gets close to 0.5, but slowly. Another seed gives other numbers, with the same slow approach.

    6. Where it shows up #

    • Feature standardization. StandardScaler stores the mean and std of each column of the training data and maps x↦(x−xˉ)/σx \mapsto (x - \bar x)/\sigma. Use the training statistics also for the test data.
    • Batch normalization and layer normalization. Inside a network, these layers compute a mean and a variance over a batch or over the features, and standardize. To normalize, they divide by nn.
    • Weight initialization. He initialization draws weights from N(0,2/nin)\mathcal N(0, 2/n_\text{in}), so that the variance of the activations stays about the same from layer to layer (variances of independent terms add, section 3.4).
    • Gaussian noise and squared error. Linear regression assumes y=w⊤x+b+εy = w^\top x + b + \varepsilon with ε∼N(0,σ2)\varepsilon \sim \mathcal N(0, \sigma^2). Maximizing the likelihood of this model is the same as minimizing the squared error (the ML Fundamentals track shows this).
    • Uncertainty of a metric. An accuracy p^\hat p on nn test examples has a standard error of about p^(1−p^)/n\sqrt{\hat p(1 - \hat p)/n}: 0.9⋅0.1/1000≈0.0095\sqrt{0.9 \cdot 0.1/1000} \approx 0.0095 for p^=0.9\hat p = 0.9, n=1000n = 1000.
    • Generative models. Diffusion models and variational autoencoders start from standard normal noise and transform it into images, molecules or weather fields.
    • ML for science. Repeated measurements are averaged and reported as mean ± std (or ± std$/\sqrt n$ for the uncertainty of the mean). Ensembles of climate or weather simulations are summarized by their mean and spread.

    7. Common mistakes #

    • Reading a short run as the probability. 7 heads in 10 flips does not mean p=0.7p = 0.7. The typical error after nn trials is about p(1−p)/n\sqrt{p(1-p)/n}, which is 0.16 for p=0.5p = 0.5 and n=10n = 10.
    • Treating a density value as a probability. p(x)p(x) can be larger than 1. Probabilities are areas.
    • Mixing nn and n−1n - 1. NumPy and pandas use different defaults. Say which one you use, and do not compare a NumPy std with a pandas std.
    • Reporting the variance in data units. The variance of heights is in cm². Report the std, which is in cm.
    • Using the product rule for events that are not independent. P(A and B)=P(A)P(B)P(A \text{ and } B) = P(A)P(B) only for independent events.
    • Trusting the mean with outliers. A single extreme value can move the mean and the std a lot. Look at the median and at a histogram too.
    • Believing the CLT makes data normal. It is about averages of many independent terms, not about the data themselves.
    • Choosing a histogram's bin width without looking. Too wide hides structure (two peaks become one); too narrow shows noise. Try a few widths, or bins="auto" in NumPy and matplotlib.

    8. Check yourself #

    1. You roll two fair dice. What is the probability that the sum is 7?
      AnswerThere are 6⋅6=366 \cdot 6 = 36 equally likely pairs. The pairs with sum 7 are (1,6),(2,5),(3,4),(4,3),(5,2),(6,1)(1,6), (2,5), (3,4), (4,3), (5,2), (6,1): 6 pairs. So the probability is 6/36=1/66/36 = 1/6.
    2. P(A)=0.3P(A) = 0.3, P(B)=0.5P(B) = 0.5, and AA and BB are independent. Compute P(A and B)P(A \text{ and } B) and P(A or B)P(A \text{ or } B).
      AnswerP(A and B)=0.3⋅0.5=0.15P(A \text{ and } B) = 0.3 \cdot 0.5 = 0.15. P(A or B)=0.3+0.5−0.15=0.65P(A \text{ or } B) = 0.3 + 0.5 - 0.15 = 0.65.
    3. Test scores are about N(60,152)\mathcal N(60, 15^2). About what fraction of students score above 90?
      Answerz=(90−60)/15=2z = (90 - 60)/15 = 2. The two tails beyond ±2\pm 2 hold 1−0.9545=0.04551 - 0.9545 = 0.0455, so one tail holds about 0.0230.023: about 2.3 %.
    4. One measurement has standard deviation 5. You average 25 independent measurements. What is the standard deviation of the average?
      Answerσ/n=5/25=1\sigma/\sqrt n = 5/\sqrt{25} = 1. To halve it again, you need 4⋅25=1004 \cdot 25 = 100 measurements.
    5. You make a histogram of 50 values with bin width 0.05. Why does it look noisy, and what should you do?
      AnswerThere are many more bins than the data can fill: most bins hold 0 or 1 value, so the heights jump. Use wider bins (fewer bins), or collect more data.
    6. A model has accuracy 0.90 on 1000 test examples. A second model has 0.905. Is the second model better?
      AnswerNot clearly. The standard error of each accuracy is about 0.9⋅0.1/1000≈0.0095\sqrt{0.9 \cdot 0.1 / 1000} \approx 0.0095, so a difference of 0.005 is well inside the noise. (A paired comparison on the same test set is more precise, but 0.005 is still small.)
    Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
    1. In the frequency view, what does P(heads)=0.3P(\text{heads}) = 0.3 mean?

    2. How does the typical gap between a relative frequency and pp depend on the number of trials nn?

    3. Write the variance of data x1,…,xnx_1, \dots, x_n (dividing by nn).

    4. Why do we usually report the standard deviation and not the variance?

    5. Compute the mean and standard deviation (divide by nn) of 2,4,4,4,5,5,7,92, 4, 4, 4, 5, 5, 7, 9.

    6. What divisor do np.std and pandas .std() use by default?

    7. All data are multiplied by aa and then bb is added. What happens to the standard deviation?

    8. What fractions of a normal distribution lie within 1, 2 and 3 standard deviations of the mean?

    9. Why can a probability density be larger than 1?

    10. What does the central limit theorem say?

    Open the review →

    9. Interview angle #

    • "Why divide by n−1n - 1 in the sample variance?" — The deviations are measured from xˉ\bar x, which is fitted to the same sample, so they are too small on average. With n−1n - 1, E[s2]=σ2\mathbb E[s^2] = \sigma^2. Know the defaults: NumPy ddof=0, pandas ddof=1.
    • "What is the variance of the mean of nn i.i.d. variables?" — σ2/n\sigma^2/n; the std is σ/n\sigma/\sqrt n. Use it for error bars on a metric and for the noise of a mini-batch gradient.
    • "State the central limit theorem. When does it fail?" — Averages of i.i.d. variables with finite variance become normal. It fails without a finite variance (Cauchy) and can be slow for very skewed data; it says nothing about the data themselves.
    • "Mean or median?" — The median is robust to outliers (for example house prices, latencies); the mean is the right summary when you care about totals, and it is what squared-error losses estimate.
    • "Is a density value a probability?" — No; probability is the area under the density, and p(x)p(x) can be larger than 1.

    10. Practice #

    The notebook for this lesson runs in Colab or in your browser. It has five exercises, each with a check cell and a folded solution:

    1. Simulate coin flips with a seeded generator and plot the running frequency of several runs with the ±2p(1−p)/n\pm 2\sqrt{p(1-p)/n} band.
    2. Implement the mean, variance and standard deviation with a ddof argument, and compare with NumPy and pandas.
    3. Compute a density histogram with np.histogram and check that the bar areas add up to 1.
    4. Standardize a dataset to z-scores, then count the fraction of normal samples within 1 and 2 standard deviations.
    5. The CLT in action: average kk uniform numbers many times and compare the std of the averages with 1/12/k\sqrt{1/12}/\sqrt k.

    11. Watch and read #

    Why we divide by n − 1 for a sample, with a simple picture.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Dice sums turning into a bell curve, and the role of the mean and the standard deviation.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    • Daniel Kunin and others, Seeing Theory (Brown University), interactive chapters 1–3: chance events, expectation and variance, distributions, and the CLT.
    • Illowsky and Dean, Introductory Statistics 2e (OpenStax, free): chapter 2 (descriptive statistics, histograms), chapter 3 (probability rules), chapter 6 (the normal distribution) and chapter 7 (the central limit theorem).
    • Grinstead and Snell, Introduction to Probability (free PDF): chapter 1 (simulation and frequencies), chapter 6 (expected value and variance) and chapter 9 (CLT).
    • Allen Downey, Think Stats (free online): the same ideas with Python and real data.