Cheat sheet
Every distribution and rule of the course: its formula, when to use it, and the Python (scipy.stats and numpy) and Excel that compute it on the course's own examples. Every value is the lab's engine's, and the tests run every line against it.
The Python lines assume these imports:
import math
import numpy as np
from scipy import stats
from scipy.stats import randint, uniform, binom, poisson, norm, expon, t, chi2, gammaRules of probability
The complement rule
Chapter 1 · Probability and eventsWhen to use it: When the opposite is simpler, above all for "at least one": work out "none" and subtract it from 1.
| What it computes | Python | Excel | Value |
|---|---|---|---|
| At least one six in four rolls of a die | 1 - (5/6)**4 |
=1-(5/6)^4 |
0.5177 |
The addition rule
Chapter 1 · Probability and eventsWhen to use it: For "A or B". Take the overlap away once; for mutually exclusive events it is 0.
| What it computes | Python | Excel | Value |
|---|---|---|---|
| Fever or fatigue, for the clinic's 150 patients | 0.54 + 0.36 - 0.18 |
=0.54+0.36-0.18 |
0.72 |
Conditional probability and the multiplication rule
Chapter 2 · Conditional probability and BayesWhen to use it: When one event is known to have happened, or for the chance that both happen. Multiply the plain probabilities only for independent events.
| What it computes | Python | Excel | Value |
|---|---|---|---|
| Fever given fatigue: 27 of the 54 patients with fatigue | 27 / 54 |
=27/54 |
0.5 |
| Two heads in two independent flips | 0.5 * 0.5 |
=0.5*0.5 |
0.25 |
Total probability and Bayes' rule
Chapter 2 · Conditional probability and BayesWhen to use it: To turn the chance of a positive result given the condition into the chance of the condition given a positive result. Never leave out the base rate.
| What it computes | Python | Excel | Value |
|---|---|---|---|
| A positive screening test: prevalence 0.01, sensitivity 0.95, specificity 0.9 | 0.01*0.95 + 0.99*0.1 |
=0.01*0.95+0.99*0.1 |
0.1085 |
| The condition, given a positive test | 0.01*0.95 / (0.01*0.95 + 0.99*0.1) |
=0.01*0.95/(0.01*0.95+0.99*0.1) |
0.0876 |
Random variables
Expected value, variance and linearity
Chapter 3 · Random variablesWhen to use it: The mean is the long-run average and the standard deviation the typical distance from it. Scaling and shifting a variable scales and shifts its mean; a shift leaves the spread alone.
The shop's units sold, k, and their probabilities, p, are in A2:A7 and B2:B7.
k = np.array([0, 1, 2, 3, 4, 5])
p = np.array([0.05, 0.15, 0.3, 0.25, 0.15, 0.1])| What it computes | Python | Excel | Value |
|---|---|---|---|
| The mean of the shop's daily sales | np.dot(k, p) |
=SUMPRODUCT(A2:A7, B2:B7) |
2.6 |
| Their standard deviation | np.sqrt(np.dot((k - 2.6)**2, p)) |
=SQRT(SUMPRODUCT((A2:A7-2.6)^2, B2:B7)) |
1.3191 |
| The mean daily profit, 20 a unit less 15 | 20 * 2.6 - 15 |
=20*2.6-15 |
37 |
Distributions
The discrete uniform distribution
Chapter 4 · The uniform distributionWhen to use it: Whole numbers in a known range, each equally likely: a fair die, a bookseller's 90 to 100 books a day. Not when real values cluster in the middle.
| What it computes | Python | Excel | Value |
|---|---|---|---|
| At least 96 books | randint.sf(95, 90, 101) |
=(100-96+1)/(100-90+1) |
0.4545 |
| Fewer than 93 books | randint.cdf(92, 90, 101) |
=(92-90+1)/(100-90+1) |
0.2727 |
| The 90th percentile | randint.ppf(0.9, 90, 101) |
=90-1+CEILING(0.9*11, 1) |
99 |
The continuous uniform distribution
Chapter 4 · The uniform distributionWhen to use it: A measurement equally likely anywhere in a range, when the range is all that is known. Every simulation starts from U(0, 1).
| What it computes | Python | Excel | Value |
|---|---|---|---|
| A bug fix takes more than 4 hours | uniform.sf(4, loc=1, scale=4) |
=(5-4)/(5-1) |
0.25 |
| Between 2 and 3.5 hours | uniform.cdf(3.5, loc=1, scale=4) - uniform.cdf(2, loc=1, scale=4) |
=(3.5-2)/(5-1) |
0.375 |
| The 90th percentile | uniform.ppf(0.9, loc=1, scale=4) |
=1+0.9*(5-1) |
4.6 |
The binomial distribution
Chapter 5 · The binomial distributionWhen to use it: The number of successes in a fixed number of independent trials, each with the same probability p. Not when sampling without replacement from a small group, or when there is no fixed n.
| What it computes | Python | Excel | Value |
|---|---|---|---|
| Exactly 3 unsatisfactory doses of 10, p = 1/3 | binom.pmf(3, 10, 1/3) |
=BINOM.DIST(3, 10, 1/3, FALSE) |
0.2601 |
| At most 3 | binom.cdf(3, 10, 1/3) |
=BINOM.DIST(3, 10, 1/3, TRUE) |
0.5593 |
| At least 11 of 20 | binom.sf(10, 20, 1/3) |
=1-BINOM.DIST(10, 20, 1/3, TRUE) |
0.0376 |
| The 90th percentile for a batch of 10 | binom.ppf(0.9, 10, 1/3) |
=BINOM.INV(10, 1/3, 0.9) |
5 |
| The standard deviation for a batch of 10 | binom.std(10, 1/3) |
=SQRT(10*(1/3)*(2/3)) |
1.4907 |
The Poisson distribution
Chapter 6 · The Poisson distributionWhen to use it: Counts of events in an interval, arriving independently at a steady rate, with no fixed number of trials; rare events among many chances. Not when events cluster or the rate changes.
| What it computes | Python | Excel | Value |
|---|---|---|---|
| No calls in an hour, at 4 an hour | poisson.pmf(0, 4) |
=POISSON.DIST(0, 4, FALSE) |
0.0183 |
| At most 2 calls | poisson.cdf(2, 4) |
=POISSON.DIST(2, 4, TRUE) |
0.2381 |
| 7 or more calls | poisson.sf(6, 4) |
=1-POISSON.DIST(6, 4, TRUE) |
0.1107 |
The normal distribution
Chapter 7 · The normal distributionWhen to use it: Measurements that cluster symmetrically around a mean. Not for skewed data, values close to a bound, or questions about the tails of returns.
| What it computes | Python | Excel | Value |
|---|---|---|---|
| A dose works in under 11.5 hours, N(13.39, 4.76) | norm.cdf(11.5, 13.39, 4.76) |
=NORM.DIST(11.5, 13.39, 4.76, TRUE) |
0.3457 |
| More than 10 hours | norm.sf(10, 13.39, 4.76) |
=1-NORM.DIST(10, 13.39, 4.76, TRUE) |
0.7618 |
| Between 10 and 15 hours | norm.cdf(15, 13.39, 4.76) - norm.cdf(10, 13.39, 4.76) |
=NORM.DIST(15, 13.39, 4.76, TRUE)-NORM.DIST(10, 13.39, 4.76, TRUE) |
0.3942 |
| The 90th percentile | norm.ppf(0.9, 13.39, 4.76) |
=NORM.INV(0.9, 13.39, 4.76) |
19.49 |
| The density at the mean: a density, not a probability | norm.pdf(13.39, 13.39, 4.76) |
=NORM.DIST(13.39, 13.39, 4.76, FALSE) |
0.0838 |
The standard normal and z-scores
Chapter 8 · The standard normal and z-scoresWhen to use it: To compare values measured on different scales, and to find a normal percentile as μ + zσ. Use the distribution's own μ and σ, not a handful of values'.
| What it computes | Python | Excel | Value |
|---|---|---|---|
| A 51-minute delivery, mean 35, standard deviation 8 | (51 - 35) / 8 |
=STANDARDIZE(51, 35, 8) |
2 |
| P(Z > 2.4) | norm.sf(2.4) |
=1-NORM.S.DIST(2.4, TRUE) |
0.0082 |
| Within 2 standard deviations | norm.cdf(2) - norm.cdf(-2) |
=NORM.S.DIST(2, TRUE)-NORM.S.DIST(-2, TRUE) |
0.9545 |
| z for the 97.5th percentile | norm.ppf(0.975) |
=NORM.S.INV(0.975) |
1.96 |
The normal approximation to the binomial
Chapter 8 · The standard normal and z-scoresWhen to use it: To see why large counts look like a bell. Add the continuity correction, the half; with software at hand, compute the binomial exactly.
| What it computes | Python | Excel | Value |
|---|---|---|---|
| At most 45 opens of 200, p = 0.25, corrected | norm.cdf(45.5, 50, np.sqrt(200*0.25*0.75)) |
=NORM.DIST(45.5, 50, SQRT(200*0.25*0.75), TRUE) |
0.2312 |
| The same, exactly | binom.cdf(45, 200, 0.25) |
=BINOM.DIST(45, 200, 0.25, TRUE) |
0.2332 |
The exponential distribution
Chapter 9 · The exponential distributionWhen to use it: The wait between events arriving independently at a steady rate λ, and random failures without wear. scipy takes the mean, scale = 1/λ; Excel takes the rate.
| What it computes | Python | Excel | Value |
|---|---|---|---|
| The next customer within 2 minutes, at 0.2 a minute | expon.cdf(2, scale=5) |
=EXPON.DIST(2, 0.2, TRUE) |
0.3297 |
| A wait of more than 10 minutes | expon.sf(10, scale=5) |
=1-EXPON.DIST(10, 0.2, TRUE) |
0.1353 |
| The median wait | expon.median(scale=5) |
=LN(2)/0.2 |
3.4657 |
| The 90th percentile | expon.ppf(0.9, scale=5) |
=-LN(1-0.9)/0.2 |
11.5129 |
Student's t distribution
Chapter 11 · Student's t and confidence intervalsWhen to use it: A mean standardised with the sample's s in place of σ, from a roughly normal population; the critical values of t intervals. Close to the normal once the degrees of freedom are large.
| What it computes | Python | Excel | Value |
|---|---|---|---|
| P(T > 2) with 9 degrees of freedom | t.sf(2, 9) |
=T.DIST.RT(2, 9) |
0.0383 |
| More than 2 from 0, either side | 2 * t.sf(2, 9) |
=T.DIST.2T(2, 9) |
0.0766 |
| t* for 95 % with 9 degrees of freedom | t.ppf(0.975, 9) |
=T.INV.2T(0.05, 9) |
2.2622 |
The chi-square distribution
The chi-square distributionWhen to use it: The sum of k squared standard normals: the distribution behind the sample variance and Student's t, and the tests of a variance in the Hypothesis Testing Lab.
| What it computes | Python | Excel | Value |
|---|---|---|---|
| Z² at most 3.841, one degree of freedom | chi2.cdf(3.841, 1) |
=CHISQ.DIST(3.841, 1, TRUE) |
0.95 |
| The 95th percentile with 9 degrees of freedom | chi2.ppf(0.95, 9) |
=CHISQ.INV(0.95, 9) |
16.919 |
Sampling and estimation
The standard error and the central limit theorem
Chapter 10 · Sampling distributions and the CLTWhen to use it: The mean of n independent values: its spread is σ/√n for every n, and its shape is close to normal once n is large. n ≥ 30 is a rule of thumb; skewed populations and questions about tails need more.
| What it computes | Python | Excel | Value |
|---|---|---|---|
| The standard error of the mean of 16 bug fixes, U(1, 5) | (4 / np.sqrt(12)) / np.sqrt(16) |
=(4/SQRT(12))/SQRT(16) |
0.2887 |
| The mean of 30 waits with mean 3 above 4.5, by the CLT | norm.sf(4.5, 3, 3/np.sqrt(30)) |
=1-NORM.DIST(4.5, 3, 3/SQRT(30), TRUE) |
0.0031 |
| The same, exactly, from the gamma | gamma.sf(4.5, a=30, scale=3/30) |
=1-GAMMA.DIST(4.5, 30, 3/30, TRUE) |
0.0073 |
The sample mean, s and Bessel's correction
Chapter 11 · Student's t and confidence intervalsWhen to use it: To estimate μ and σ from a sample. Divide by n − 1 for s; numpy's std() divides by n unless ddof=1 is given.
The 10 wildfire damages are in B2:B11.
x = np.array([15, 80, 31, 104, 131, 46, 15, 62, 16, 36])| What it computes | Python | Excel | Value |
|---|---|---|---|
| The mean damage of 10 wildfires, in thousands of dollars | x.mean() |
=AVERAGE(B2:B11) |
53.6 |
| s, with n − 1 | x.std(ddof=1) |
=STDEV.S(B2:B11) |
40.24 |
| Dividing by n instead: biased low | x.std(ddof=0) |
=STDEV.P(B2:B11) |
38.17 |
| The standard error of the mean | stats.sem(x) |
=STDEV.S(B2:B11)/SQRT(COUNT(B2:B11)) |
12.72 |
A z interval for a mean
Chapter 11 · Student's t and confidence intervalsWhen to use it: When σ is known, as for a long-running machine, and the population is roughly normal or n is large. Excel's CONFIDENCE.NORM gives the margin of error, not the interval.
| What it computes | Python | Excel | Value |
|---|---|---|---|
| A 95 % interval for 50 cups with mean 113.18 mg, σ = 7 | norm.interval(0.95, loc=113.18, scale=7/np.sqrt(50)) |
=113.18-CONFIDENCE.NORM(0.05, 7, 50)=113.18+CONFIDENCE.NORM(0.05, 7, 50) |
(111.24, 115.12) |
| Its margin of error | norm.ppf(0.975) * 7 / np.sqrt(50) |
=CONFIDENCE.NORM(0.05, 7, 50) |
1.94 |
| The n for a margin of 1 mg | np.ceil((norm.ppf(0.975) * 7 / 1)**2) |
=CEILING((NORM.S.INV(0.975)*7/1)^2, 1) |
189 |
A t interval for a mean
Chapter 11 · Student's t and confidence intervalsWhen to use it: When σ is estimated by the sample's s, from a roughly normal population when n is small. The 95 % describes the procedure: 95 % of intervals built this way contain μ.
The 10 wildfire damages are in B2:B11.
x = np.array([15, 80, 31, 104, 131, 46, 15, 62, 16, 36])| What it computes | Python | Excel | Value |
|---|---|---|---|
| A 95 % interval for the mean wildfire damage | t.interval(0.95, len(x) - 1, loc=x.mean(), scale=stats.sem(x)) |
=AVERAGE(B2:B11)-CONFIDENCE.T(0.05, STDEV.S(B2:B11), 10)=AVERAGE(B2:B11)+CONFIDENCE.T(0.05, STDEV.S(B2:B11), 10) |
(24.82, 82.38) |
| Its margin of error | t.ppf(0.975, 9) * stats.sem(x) |
=CONFIDENCE.T(0.05, STDEV.S(B2:B11), 10) |
28.78 |