Section 2 · Lesson 3
The normal distribution and z-scores
Use the bell curve, the 68-95-99.7 rule, z-scores and scipy.stats.norm to judge how unusual a value is.
Plot a histogram of time on page and you’ll see a hill: most visits cluster around a minute, with fewer and fewer as you move toward very short or very long visits, roughly the same on both sides. That shape is the normal distribution, the bell curve.
The bell curve
A normal distribution is completely described by two numbers: its mean (where the center is) and its standard deviation (how wide it is). It is symmetric, so mean = median. Measurements that are the sum of many small, independent influences tend to look normal, and as you’ll see in the next section, so do averages of almost anything.
The 68-95-99.7 rule: about 68% of values fall within 1 SD of the mean, 95% within 2 SDs, and 99.7% within 3 SDs. So a value 3 SDs out is rare (about 3 in 1,000).
For a continuous variable, probability is the area under the curve. The probability of exactly 63.500000… seconds is zero; questions are always about ranges (“more than 100 s”).
z-scores
A z-score says how many standard deviations a value is from the mean:
z = (x - mean) / sd
z = 0 is exactly average, z = 2 is two SDs above, z = -1.5 is one and a half below. Because z-scores have no units, you can compare things on different scales: a campaign’s CTR and its CPC, or an Army PT score and a civilian 5K time. Converting every value to z gives the standard normal: mean 0, SD 1.
In Python with SciPy
scipy.stats.norm works like binom, but loc is the mean and scale is the SD (they
default to 0 and 1):
from scipy import stats
stats.norm.cdf(1.0) # P(Z ≤ 1) ≈ 0.841
stats.norm.sf(2.0) # P(Z > 2) ≈ 0.023
stats.norm.ppf(0.975) # value with 97.5% below ≈ 1.96
stats.norm.cdf(80, loc=63.5, scale=21.4) # P(time ≤ 80 s) under a normal model
ppf (percent point function) is the inverse of cdf: give it a probability, get back a
value. Remember 1.96: it cuts off 2.5% in each tail, and it shows up again in confidence
intervals.
norm.pdf(x) returns the height of the curve, not a probability. Use cdf/sf for
probabilities.
Is my data normal enough?
Time on page in our A/B data has mean ≈ 63.5 s, SD ≈ 21.4 s and skewness near 0, and in the tasks you’ll find the 68-95 rule holds closely. Order amounts are a different story: skewness around 5. A normal model with mean $76 and SD $91 would predict that about 20% of orders have a negative amount. Before using normal formulas on raw data, check the skew and compare a few model percentiles with the real ones.
In the real world
- Anomaly alerts: many monitoring tools flag a day when a KPI’s z-score passes ±3 versus the trailing average. Seasonality and heavy tails make naive z-alerts noisy, so baseline them on comparable days (same weekday) where you can.
- Benchmarks across channels: z-scores let you say “this video campaign’s CTR is 1.8 SDs above other video campaigns” without comparing raw CTRs across very different channels.
- The normal distribution powers the confidence intervals and z-tests in the rest of the course, even when the raw data are skewed, thanks to the central limit theorem.
Practice
Write your answer, press Run to see results and Check answer to grade it. Shortcuts: Ctrl/⌘ + Enter run, Shift + Enter check.
How unusual is a 2-minute visit?
DoneLoad ab_test.csv and work with time_on_page_sec.
mean_tp: the meanstd_tp: the sample standard deviation (pandas.std()default)z_120: the z-score of a visit lasting 120 seconds, rounded to 2 decimalsn_beyond_3: the number of users whose time on page has a z-score **more than 3 SDs
from the mean** in either direction (|z| > 3), as an integer
Need a hint?
- z = (x - mean) / sd. You can apply it to the whole column at once:
z = (t - mean_tp) / std_tp. z.abs() > 3catches both very short and very long visits;.sum()counts them.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
import pandas as pd
ab = pd.read_csv("ab_test.csv")
t = ab["time_on_page_sec"]
mean_tp = t.mean()
std_tp = None
z_120 = None
n_beyond_3 = None
print(mean_tp, std_tp, z_120, n_beyond_3)
Files in the working directory
ab_test.csv (4,000 rows)daily_stats.csv (1,561 rows)orders.csv (900 rows)campaigns.csv (40 rows)Does the 68-95 rule hold?
DoneUsing time_on_page_sec z-scores (mean and sample SD), create, each rounded to 4 decimals:
within_1sd: the share of users with |z| ≤ 1within_2sd: the share of users with |z| ≤ 2normal_within_1sd: the share a perfect normal distribution puts within 1 SD,
norm.cdf(1) - norm.cdf(-1) from scipy.stats
Compare the observed shares to the 68-95-99.7 rule.
Need a hint?
(z.abs() <= 1).mean()is the share of users within one SD of the mean.stats.norm.cdf(x)with no loc/scale uses the standard normal (mean 0, SD 1).
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
import pandas as pd
from scipy import stats
ab = pd.read_csv("ab_test.csv")
t = ab["time_on_page_sec"]
z = (t - t.mean()) / t.std()
within_1sd = None
within_2sd = None
normal_within_1sd = 0.68
print(within_1sd, within_2sd, normal_within_1sd)
Files in the working directory
ab_test.csv (4,000 rows)daily_stats.csv (1,561 rows)orders.csv (900 rows)campaigns.csv (40 rows)Use a normal model
DoneModel time on page as a normal distribution with mean 63.5 and standard deviation 21.4 seconds. With scipy.stats.norm (loc = mean, scale = SD), create:
p_over_100: P(time > 100 seconds) under the model, rounded to 4 decimalscutoff_top_10: the time that separates the top 10% of visits from the rest
(the 90th percentile of the model), rounded to 2 decimals
Then compare with the data: the real share over 100 s and the real 90th percentile.
Need a hint?
stats.norm.sf(x, loc=mu, scale=sigma)= P(X > x).stats.norm.ppf(q, loc=mu, scale=sigma)turns a probability into a value: it is the inverse ofcdf.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
from scipy import stats
mu, sigma = 63.5, 21.4
p_over_100 = stats.norm.cdf(100, loc=mu, scale=sigma) # is this "over" 100?
cutoff_top_10 = None
print(p_over_100, cutoff_top_10)
Files in the working directory
ab_test.csv (4,000 rows)daily_stats.csv (1,561 rows)orders.csv (900 rows)campaigns.csv (40 rows)