SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 5.8.1 Estimators: bias, variance and consistencyConcept · 35 min
      2. 5.8.2 Sampling distributions and confidence intervalsConcept · 35 min
      3. 5.8.3 Hypothesis testsConcept · 30 min
      4. 5.8.4 Practice set: statistical inferencePractice set · 25 min
      5. 5.8.5 Case study: is this Sharpe ratio significant?Case study · 35 min

Level 5, Probability and statistics 5.8 Statistical inference Lesson 3 of 5

ConceptLesson 5.8.3 in Level 5, Probability and statistics. About 30 minutes.

Hypothesis tests

Null and alternative hypotheses

A coin is tossed 100 times and shows 60 heads. Is it fair? A hypothesis test answers such a question by asking how surprising the data would be if a stated claim were true.

  • The null hypothesis H0H_0 is the claim under test, usually "nothing special": here p=0.5p = 0.5, with pp the chance of heads.
  • The alternative hypothesis H1H_1 is what the test looks for instead: here p≠0.5p \ne 0.5. It is two-sided, because a bias either way counts.
  • The test statistic is a number computed from the data whose distribution is known when H0H_0 is true. Here it is the number of heads XX, which is Bin⁡(100,0.5)\operatorname{Bin}(100, 0.5) under H0H_0 (lesson 5.2.2).

Under H0H_0, XX has mean 50 and standard deviation 100×0.5×0.5=5\sqrt{100 \times 0.5 \times 0.5} = 5 (lesson 5.3.2). Counts far from 50, in either direction, speak against H0H_0.

The p-value

Rule: p-value

p=P(a statistic at least as extremeas the one observed∣H0)\begin{gathered} p = P(\text{a statistic at least as extreme} \\ \text{as the one observed} \mid H_0) \end{gathered}

Small pp: data like these would be rare if H0H_0 were true, so they are evidence against it. The p-value is a probability computed assuming H0H_0; it is not the probability that H0H_0 is true.

Sixty heads is 10 away from 50, so "at least as extreme" means ∣X−50∣≥10\lvert X - 50 \rvert \ge 10. Each tail has probability 0.0284, so p=0.0569p = 0.0569.

If the coin is fair, the highlighted counts are at least as far from 50 as the 60 heads seen. Their total probability, 0.0569, is the p-value.

To decide, fix a significance level α\alpha before looking at the data, often 0.05 or 0.01.

Rule: Significance level

reject H0 when p≤α\text{reject } H_0 \text{ when } p \le \alpha

The result is then statistically significant at level α\alpha. Otherwise the data do not reject H0H_0, which is not the same as showing it is true.

  • 60 heads: p=0.0569p = 0.0569, above 0.05, so the data do not reject fairness at the 5% level.
  • 61 heads: p=0.0352p = 0.0352, so they do. One toss changes the verdict, which is why a report should give the p-value, not only "reject" or "keep".
  • Not the chance that H0H_0 is true. The p-value is P(data this extreme∣H0)P(\text{data this extreme} \mid H_0). The reverse, P(H0∣data)P(H_0 \mid \text{data}), needs Bayes' rule and a prior (lesson 5.1.4).

A normal approximation, z=60−505=2z = \frac{60 - 50}{5} = 2, gives p=2N(−2)≈0.0455p = 2N(-2) \approx 0.0455 instead of 0.0569. Near the cut-off it flips the verdict, so use the exact distribution when you have it.

Try it

binom.sf(59, 100, 0.5) is P(X≥60)P(X \ge 60) and binom.cdf(40, 100, 0.5) is P(X≤40)P(X \le 40). The loop prints one tail and the two-sided p-value for 60 and 61 heads, then SciPy's binomtest as a check.

The last lines give the normal approximation, then 100,000 seeded runs of 100 fair tosses. In 5.59% of them the count lands 10 or more from 50.

⌘/Ctrl + Enter runs the cell

Two kinds of error

A test can go wrong in two ways.

H0H_0 trueH0H_0 false
reject H0H_0error of the first kind (Type I): a false alarmcorrect
keep H0H_0correcterror of the second kind (Type II): a miss

Rule: Error rates and power

P(Type I)=αP(Type II)=βpower=1−β\begin{gathered} P(\text{Type I}) = \alpha \\[4pt] P(\text{Type II}) = \beta \\[4pt] \text{power} = 1 - \beta \end{gathered}

α\alpha is chosen; β\beta (beta) depends on how far the truth is from H0H_0, on nn and on α\alpha. The power is the chance of rejecting H0H_0 when it is false.

Take draws from N(μ,1)\mathcal{N}(\mu, 1) and test H0:μ=0H_0: \mu = 0 against μ≠0\mu \ne 0 with n=25n = 25. The statistic z=xˉ1/25=5xˉz = \frac{\bar{x}}{1/\sqrt{25}} = 5\bar{x} is standard normal under H0H_0, so rejecting when ∣z∣>1.96\lvert z \rvert > 1.96 gives α=0.05\alpha = 0.05.

If in fact μ=0.5\mu = 0.5, then z∼N(2.5,1)z \sim \mathcal{N}(2.5, 1). It lands beyond ±1.96\pm 1.96 with probability 0.7054, the power, so β=0.2946\beta = 0.2946.

The same rule judged under two truths. If μ=0\mu = 0, rejecting is a false alarm, with probability α\alpha. If μ=0.5\mu = 0.5, keeping H0H_0 is a miss, with probability β\beta.

Power grows with the sample and with the size of the effect:

n=10n = 10n=25n = 25n=50n = 50n=100n = 100
power when μ=0.5\mu = 0.50.35260.70540.94240.9988

At n=25n = 25 the power is 0.1701 when μ=0.2\mu = 0.2 and 0.9988 when μ=1\mu = 1. Cutting α\alpha to 0.01 (cut-off 2.576) lowers it from 0.7054 to 0.4697: fewer false alarms cost more misses.

How many draws? To detect a difference δ\delta (delta) with 80% power at α=0.05\alpha = 0.05, the test needs about n=((1.96+0.8416) σδ)2n = \left(\frac{(1.96 + 0.8416)\,\sigma}{\delta}\right)^2 draws, with 0.8416=z0.80.8416 = z_{0.8}. For δ=0.5\delta = 0.5 and σ=1\sigma = 1 that is 31.4, so 32 draws. Case study 5.8.5 asks the same question about years of returns.

Try it

power(mu, n) adds the two tails of N(μn,1)\mathcal{N}(\mu\sqrt{n}, 1) beyond the cut-offs. The lines print the table, the effect sizes, the stricter α\alpha and the sample size.

The loop simulates 100,000 seeded samples of 25 under each truth: the test rejects 5.05% of the time when μ=0\mu = 0 and 70.34% when μ=0.5\mu = 0.5.

⌘/Ctrl + Enter runs the cell

The t test for a mean

To test a claimed mean μ0\mu_0 with σ\sigma unknown, standardise with ss, as lesson 5.8.2 did for the t interval.

Rule: One-sample t test

t=xˉ−μ0s/np=2 P(Tn−1≥∣t∣)\begin{gathered} t = \frac{\bar{x} - \mu_0}{s/\sqrt{n}} \\[4pt] p = 2\,P(T_{n-1} \ge \lvert t \rvert) \end{gathered}

H0:μ=μ0H_0: \mu = \mu_0 against μ≠μ0\mu \ne \mu_0. Under H0H_0 the statistic has the t distribution with n−1n - 1 degrees of freedom, exactly for normal draws and approximately for large samples otherwise. SciPy: ttest_1samp(x, mu0).

Five measurements. Lesson 5.8.2's values 10.2, 9.6, 10.9, 10.4 and 9.9 have xˉ=10.2\bar{x} = 10.2 and standard error 0.2214.

  • Claimed value 10: t=0.20.2214≈0.9035t = \frac{0.2}{0.2214} \approx 0.9035 and p=0.4173p = 0.4173. No evidence against 10.
  • Claimed value 9.5: t≈3.1623t \approx 3.1623 and p=0.0341p = 0.0341, so 9.5 is rejected at the 5% level.

Lesson 5.8.2's 95% interval, 9.5854 to 10.8146, contains 10 and leaves out 9.5. That is no coincidence.

Rule: Tests and intervals

reject μ0 at level α  ⟺  μ0 is outside the (1−α) interval\begin{gathered} \text{reject } \mu_0 \text{ at level } \alpha \\ \iff \mu_0 \text{ is outside the } (1 - \alpha) \text{ interval} \end{gathered}

For the two-sided t test and the t interval from the same data. The interval is the set of claimed values the test does not reject.

Try it

The loop computes tt and the p-value by the rule for both claimed values, then checks them with ttest_1samp.

The last line is the one-sided test of the next section.

⌘/Ctrl + Enter runs the cell

A loaded die. Make a six half again as likely as each other face. Its probability is then 0.2308, and the die's mean is 3.6923, not 3.5. So H0:μ=3.5H_0: \mu = 3.5 is false.

Sixty seeded rolls give xˉ≈3.4667\bar{x} \approx 3.4667, t≈−0.1372t \approx -0.1372 and p=0.8913p = 0.8913: no evidence at all. That is an error of the second kind.

That is the usual outcome, not bad luck. With 60 rolls the t test detects this die only 13.6% of the time, in a seeded simulation. For 80% power it needs about 666 rolls, by the sample-size formula with σ≈1.7709\sigma \approx 1.7709.

A test with little power says little when it keeps H0H_0.

Try it

The first lines build the loaded die's probabilities, mean and standard deviation. Then one sample of 60 rolls and its t test.

The last lines are the power, as the share of 20,000 seeded samples of 60 with a p-value below 0.05. Then comes the formula's sample size for 80%.

⌘/Ctrl + Enter runs the cell

One-sided tests

Sometimes only one direction matters: a new process should raise the mean, not lower it. Then H1:μ>μ0H_1: \mu > \mu_0, and only large values of tt count against H0H_0.

Rule: One-sided p-value

H1:μ>μ0p=P(Tn−1≥t)\begin{gathered} H_1: \mu > \mu_0 \\[4pt] p = P(T_{n-1} \ge t) \end{gathered}

For H1:μ<μ0H_1: \mu < \mu_0 use P(Tn−1≤t)P(T_{n-1} \le t). In SciPy, alternative="greater" or "less".

For the claimed value 9.5, the one-sided p-value is 0.0171, half of the two-sided 0.0341, because tt is positive. Had tt been negative, the one-sided p-value would be above 0.5.

Choose the side before seeing the data. Picking it afterwards, to match the sign of tt, halves every p-value and doubles the false alarms.

Many tests at once

Each test at α=0.05\alpha = 0.05 raises a false alarm 5% of the time when its H0H_0 is true. Run many and some will.

With 20 independent tests of true nulls, the chance of at least one rejection is 1−0.9520≈0.64151 - 0.95^{20} \approx 0.6415, and one false rejection is expected. In a seeded simulation of 10,000 batches of 20 t tests, 63.46% of batches had at least one. The smallest of the 20 p-values averaged 0.0478.

Rule: Bonferroni correction

test each of m hypotheses at αm\text{test each of } m \text{ hypotheses at } \frac{\alpha}{m}

Then the chance of any false alarm among the mm is at most α\alpha. For 20 tests, each is run at 0.0025, and the chance of any false rejection is 0.0488. The price is power: each test now needs stronger evidence.

Significant is not the same as large. Take 10,000 draws with standard deviation 1. A sample mean of 0.02 gives t=2t = 2 and p=0.0455p = 0.0455: significant, but tiny.

Report the estimate and its interval alongside the p-value. Case study 5.8.5 meets both problems with trading strategies.

Try it

The first line is the exact chance of a false alarm among 20 tests, the Bonferroni level, and its chance of any false alarm.

Then 10,000 seeded batches of 20 t tests, each on 30 standard normal draws, so every H0:μ=0H_0: \mu = 0 is true. The last line is the large-sample example.

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. Reading pp as the chance that H0H_0 is true. It is the chance of data this extreme if H0H_0 is true.
  2. Treating "not rejected" as "proved". The loaded die kept H0H_0 with p=0.8913p = 0.8913; the test had 13.6% power.
  3. Choosing α\alpha or the side after seeing the data. Both make false alarms more common than α\alpha says.
  4. Equating significant with important. A tiny effect is significant with enough data; give the estimate and its interval.
  5. Ignoring how many tests were run. Twenty tests of true nulls give at least one p≤0.05p \le 0.05 about 64% of the time.
  6. Trusting an approximation at the cut-off. For 60 heads the normal approximation gives 0.0455 and the exact test 0.0569.

Hypothesis tests

0 of 12 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 12.
How to type answers
  • Powers with ^: x^7, 2^(1/3), a^-2.
  • Multiply with * or by writing side by side: 3x^2, 2(x+1).
  • ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
  • Your first answer to each question is saved: it counts towards your score and brings the question back for review.
  • Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.

Summary card

  • A test states H0H_0 (the claim) and H1H_1 (two-sided or one-sided), and uses a statistic whose distribution under H0H_0 is known.
  • The p-value is P(a statistic at least as extreme∣H0)P(\text{a statistic at least as extreme} \mid H_0); reject at level α\alpha when p≤αp \le \alpha. It is not the probability that H0H_0 is true.
  • Type I error (false alarm) has probability α\alpha; Type II (a miss) has probability β\beta; power =1−β= 1 - \beta grows with nn and the effect. For 80% power, n≈((1.96+0.8416)σδ)2n \approx \left(\frac{(1.96 + 0.8416)\sigma}{\delta}\right)^2.
  • t test: t=xˉ−μ0s/nt = \frac{\bar{x} - \mu_0}{s/\sqrt{n}} against tn−1t_{n-1}. It rejects μ0\mu_0 exactly when μ0\mu_0 is outside the matching interval.
  • Many tests produce false alarms (1−0.9520≈0.641 - 0.95^{20} \approx 0.64); Bonferroni tests each at αm\frac{\alpha}{m}. Significant is not the same as large.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.