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 2 of 5

ConceptLesson 5.8.2 in Level 5, Probability and statistics. About 35 minutes.

Sampling distributions and confidence intervals

The sampling distribution of the mean

Lesson 5.8.1 found that the sample mean Xˉ\bar{X} of nn independent draws has mean μ\mu and variance σ2n\frac{\sigma^2}{n}. Its distribution over all possible samples is its sampling distribution. Its standard deviation has its own name.

Rule: Standard error of the mean

SE⁡(Xˉ)=σnSE⁡^=sn\begin{gathered} \operatorname{SE}(\bar{X}) = \frac{\sigma}{\sqrt{n}} \\[4pt] \widehat{\operatorname{SE}} = \frac{s}{\sqrt{n}} \end{gathered}

The standard error of an estimator is its standard deviation: the typical size of its error. It is in the units of the data. When σ\sigma is unknown, the sample standard deviation ss of lesson 5.8.1 stands in for it.

The shape is known too. For normal draws, Xˉ∼N(μ,σ2n)\bar{X} \sim \mathcal{N}(\mu, \frac{\sigma^2}{n}) exactly (lesson 5.6.3). For any other distribution with a finite variance it is close to that for large nn, by the central limit theorem.

An example. Exponential draws with mean 1 have σ=1\sigma = 1 (lesson 5.4.2), so the mean of 30 has standard error 130≈0.1826\frac{1}{\sqrt{30}} \approx 0.1826. The total of 30 exponentials has a gamma distribution (lesson 5.6.3 met the total of two), so the exact answers are known:

exactnormal approximationsimulated
P(Xˉ≤1)P(\bar{X} \le 1)0.52430.50.5253
P(∣Xˉ−1∣≤1.96 SE⁡)P(\lvert \bar{X} - 1 \rvert \le 1.96\,\operatorname{SE})0.95190.950.9509

The skew of the exponential leaves a trace: slightly more than half the means fall below μ\mu. But the band of ±1.96\pm 1.96 standard errors holds almost exactly 95% of them.

Two case studies used this rule already. In case study 5.3.5, 1,000 daily returns with volatility 1% have a mean with standard error 1%1000≈0.0316%\frac{1\%}{\sqrt{1000}} \approx 0.0316\%. Case study 4.6.5 quoted σiT\frac{\sigma_i}{\sqrt{T}} for an annual mean from TT years. Each year is one draw with standard deviation σi\sigma_i, so n=Tn = T; for B that is 0.1810≈0.0569\frac{0.18}{\sqrt{10}} \approx 0.0569.

Try it

gamma(30, scale=1/30) is the exact distribution of the mean of 30 exponentials with mean 1. The first line prints the standard error and the two exact probabilities.

Then 100,000 seeded samples of 30: the mean and standard deviation of their means, and the two shares. The last line is the two case studies' standard errors.

⌘/Ctrl + Enter runs the cell

A confidence interval when σ\sigma is known

Standardise the mean: Z=Xˉ−μσ/nZ = \frac{\bar{X} - \mu}{\sigma/\sqrt{n}} is standard normal, exactly or approximately. So P(−1.96≤Z≤1.96)=0.95P(-1.96 \le Z \le 1.96) = 0.95 (lesson 5.4.3). Rearranged, the same event says that μ\mu lies within 1.96 σn1.96\,\frac{\sigma}{\sqrt{n}} of Xˉ\bar{X}.

Rule: Confidence interval for μ, σ known

xˉ±z σnz=1.645, 1.96, 2.576for 90%, 95%, 99%\begin{gathered} \bar{x} \pm z\,\frac{\sigma}{\sqrt{n}} \\[4pt] z = 1.645,\ 1.96,\ 2.576 \\ \text{for } 90\%,\ 95\%,\ 99\% \end{gathered}

For a confidence level 1−α1 - \alpha, zz is norm.ppf(1 - α/2), leaving α2\frac{\alpha}{2} in each tail. The interval runs from xˉ−zσn\bar{x} - z\frac{\sigma}{\sqrt{n}} to xˉ+zσn\bar{x} + z\frac{\sigma}{\sqrt{n}}; half its width is the margin of error.

Roll a die 30 times; its σ=35/12≈1.7078\sigma = \sqrt{35/12} \approx 1.7078 is known. One seeded sample has xˉ≈3.7333\bar{x} \approx 3.7333. The margin at 95% is 1.96×1.707830≈0.61111.96 \times \frac{1.7078}{\sqrt{30}} \approx 0.6111, so the interval 3.1222 to 4.3445 contains the true mean 3.5.

  • More confidence costs width. At 90% the margin is 0.5129; at 99% it is 0.8032.
  • More data buys width slowly. The margin shrinks like 1n\frac{1}{\sqrt{n}}: four times the rolls, 120, halve it to 0.3056.

Try it

The loop prints, for each level, zz, the margin and the interval for the seeded sample of 30 rolls.

The last line prints σ\sigma, xˉ\bar{x} and the 95% margin for 120 rolls.

⌘/Ctrl + Enter runs the cell

What "95%" means

Before the rolls, the interval is random: its centre Xˉ\bar{X} changes from sample to sample. The 95% is the chance that this procedure produces an interval that covers μ\mu. It is called the interval's coverage.

After the rolls, the interval from 3.1222 to 4.3445 is fixed, and so is μ\mu. Either it contains μ\mu or it does not; nothing random is left. "95% confidence" describes the method, not this one interval.

In one seeded run of 10,000 samples of 30 rolls, 95.58% of the intervals cover 3.5. The exact share, from the distribution of the total of 30 dice, is 0.9525. It is a little above 95% because that total moves in whole steps.

The explorer starts with the die. Set n=30n = 30: the share of means within 1.96 σ/n1.96\,\sigma/\sqrt{n} of μ\mu is the share of intervals xˉ±1.96 σ/n\bar{x} \pm 1.96\,\sigma/\sqrt{n} that cover μ\mu. Then try the coin with small nn: its mean takes only a few values, and the share jumps around 95%.

Explore

Each bar counts how many of 2,000 samples had their mean in that range. Raise the sample size n: the histogram narrows like 1/√n and takes the shape of the normal curve, even from a skewed start.

24600.20.40.6
True mean and standard deviation
μ = 3.5, σ = 1.7078
The 2000 sample means
average 3.5223, standard deviation 0.7615 against σ/√n = 0.7638
Within 1.96 σ/√n of μ
94% of the means (the normal curve says 95%)

Try it

The first lines draw 10,000 seeded samples of 30 rolls and print the share of intervals that cover 3.5.

The loop builds the exact distribution of the total of 30 dice by convolution, one die at a time (lesson 5.6.3). The last line adds up the totals whose interval covers 3.5.

⌘/Ctrl + Enter runs the cell

When σ\sigma is unknown: the t interval

In practice σ\sigma is unknown, and the sample standard deviation ss takes its place. But ss varies from sample to sample, and now and then it is much too small. Then the standardised mean is too large, so it has fatter tails than a normal.

Rule: The standardised mean with s

T=Xˉ−μs/n∼tn−1T = \frac{\bar{X} - \mu}{s/\sqrt{n}} \sim t_{n - 1}

For normal draws, TT has the Student t distribution with n−1n - 1 degrees of freedom (lesson 5.4.4), the same n−1n - 1 that ss divides by. For other distributions it is approximately tn−1t_{n-1} for large nn.

This is lesson 5.4.4's ratio ZV/ν\frac{Z}{\sqrt{V/\nu}}, with ν=n−1\nu = n - 1. Divide top and bottom by σ/n\sigma/\sqrt{n}:

T=ZV/(n−1)Z=Xˉ−μσ/nV=(n−1)s2σ2\begin{gathered} T = \frac{Z}{\sqrt{V/(n - 1)}} \\[4pt] Z = \frac{\bar{X} - \mu}{\sigma/\sqrt{n}} \\[4pt] V = \frac{(n - 1)s^2}{\sigma^2} \end{gathered}

For normal draws VV is χn−12\chi^2_{n-1} and independent of ZZ (stated here); the next section uses VV.

Rule: t interval for μ

xˉ±tn−1 sntn−1=t.ppf(0.975, n - 1)\begin{gathered} \bar{x} \pm t_{n-1}\,\frac{s}{\sqrt{n}} \\[4pt] t_{n-1} = \texttt{t.ppf(0.975, n - 1)} \end{gathered}

The 95% interval when σ\sigma is estimated. For 90% or 99%, use the 0.95 or 0.995 quantile.

nn251030100normal
tn−1t_{n-1}, 0.975-quantile12.7062.7762.2622.0451.9841.960

For n=5n = 5 the multiplier is 2.776 instead of 1.96, as lesson 5.4.4 found: small samples need wider intervals. From about 30 draws on, the difference is small.

Five measurements. A quantity is measured five times: 10.2, 9.6, 10.9, 10.4 and 9.9. Then xˉ=10.2\bar{x} = 10.2, s2=0.245s^2 = 0.245, s≈0.495s \approx 0.495, and the standard error is 0.4955≈0.2214\frac{0.495}{\sqrt{5}} \approx 0.2214. The margin is 2.776×0.2214≈0.61462.776 \times 0.2214 \approx 0.6146, so the 95% interval runs from 9.5854 to 10.8146.

With 1.96 in place of 2.776, the margin would be 0.4339: too narrow. The coverage of such an interval is P(∣T∣≤1.96)P(\lvert T \rvert \le 1.96) for T∼tn−1T \sim t_{n-1}:

nn51030
coverage of xˉ±1.96 sn\bar{x} \pm 1.96\,\frac{s}{\sqrt{n}}0.87840.91840.9403
coverage of the t interval0.950.950.95

The same 100 samples of five, with two multipliers. Highlighted intervals miss the true value 10: 5 with the t quantile, 10 with 1.96. Each interval's length depends on its own ss.

Try it

The first lines compute the five-measurement interval by hand. t.interval is SciPy's shortcut for the same interval. Then the t quantiles of the table.

⌘/Ctrl + Enter runs the cell

Data that are not normal

The t interval assumes normal draws. For other data it relies on the central limit theorem, so it works for large samples and can fall short for small, skewed ones. For exponential draws its coverage is 90.09%, 92.72% and 94.18% at nn = 10, 30 and 100, in seeded runs.

Try it

The first loop is the exact coverage of ±1.96 s/n\pm 1.96\,s/\sqrt{n} from the t distribution. Next, 100,000 seeded normal samples of five: about 88% of the 1.96 intervals cover 10, against 95% for the t intervals.

The last loop repeats the t interval on 50,000 seeded exponential samples of each size.

⌘/Ctrl + Enter runs the cell

An interval for a variance

The VV above gives an interval for σ2\sigma^2 itself. For normal draws, V=(n−1)s2σ2V = \frac{(n - 1)s^2}{\sigma^2} lies between the 0.025- and 0.975-quantiles of χn−12\chi^2_{n-1} with probability 0.95. Solving both ends for σ2\sigma^2 gives the interval.

Rule: Interval for σ², normal data

(n−1)s2χ0.9752≤σ2≤(n−1)s2χ0.0252\begin{gathered} \frac{(n - 1)s^2}{\chi^2_{0.975}} \le \sigma^2 \le \frac{(n - 1)s^2}{\chi^2_{0.025}} \end{gathered}

χp2\chi^2_p is chi2.ppf(p, n - 1). Dividing by the larger quantile gives the lower end. Take square roots of both ends for an interval for σ\sigma.

For the five measurements, (n−1)s2=0.98(n - 1)s^2 = 0.98 and the quantiles of χ42\chi^2_4 are 0.4844 and 11.1433. So σ2\sigma^2 lies between 0.0879 and 2.023, and σ\sigma between 0.2966 and 1.4223.

The estimate s2=0.245s^2 = 0.245 is far from the middle: the interval stretches to the right, because χ2\chi^2 is skewed (lesson 5.4.4). Five values say little about a spread.

This interval needs normal data much more than the t interval does. A fat tail (lesson 5.3.3) makes s2s^2 swing more than χ2\chi^2 allows.

Take samples of 30, in one seeded run. The coverage is 94.99% for normal draws, but 82.54% for a Student t with 5 degrees of freedom.

Try it

The first lines compute the interval for the five measurements, for σ2\sigma^2 and for σ\sigma.

The loop checks coverage with 50,000 seeded samples of 30. First come normal draws with σ2=1\sigma^2 = 1, then t draws with 5 degrees of freedom, whose variance is 53\frac{5}{3} (lesson 5.4.4).

⌘/Ctrl + Enter runs the cell

The bootstrap

The intervals so far needed a formula for the sampling distribution. When there is none, the bootstrap simulates it from the data. It treats the sample as if it were the whole population, and draws new samples from it.

Rule: Bootstrap percentile interval

θ^1∗,…,θ^B∗\hat{\theta}^*_1, \ldots, \hat{\theta}^*_B

Draw nn values from the sample with replacement, so some repeat and some are left out, and compute the estimate θ^∗\hat{\theta}^*. Repeat BB times, say 10,000. The 0.025- and 0.975-quantiles of the BB estimates form a 95% interval.

Take 40 seeded exponential draws with mean 1. Their mean is 1.0984 and s=1.3211s = 1.3211. The median, the middle value, is 0.7425; the true median of this distribution is ln⁡2≈0.6931\ln 2 \approx 0.6931.

statisticestimatebootstrap 95% intervalformula interval
mean1.09840.7287 to 1.5262t: 0.6759 to 1.5209
median0.74250.2667 to 1.0386none in this module

For the mean, the standard deviation of the 10,000 bootstrap means is 0.2055, close to sn≈0.2089\frac{s}{\sqrt{n}} \approx 0.2089. The bootstrap interval is not centred on 1.0984: it leans right, as the skewed data do. For the median, the bootstrap gives an interval where this module has no formula.

The bootstrap has limits.

  • It needs independent draws. For a time series, whose values depend on their neighbours, resample blocks of neighbouring values instead.
  • It cannot see what the sample lacks. A small sample without its distribution's rare extremes gives bootstrap samples without them too.
  • It is a simulation. Use many resamples and a fixed seed, so the interval does not move from run to run.

Case study 5.8.5 bootstraps a Sharpe ratio.

Try it

idx holds 10,000 rows of 40 positions drawn with replacement, and data[idx] the resampled values. Each row's mean and median is one bootstrap estimate.

The lines print the sample's statistics, the bootstrap interval and standard deviation of the mean, the t interval, and the bootstrap interval of the median.

⌘/Ctrl + Enter runs the cell

Lesson 5.8.3 turns the same standardised mean into a test of a claimed value of μ\mu.

Common mistakes

  1. "There is a 95% chance that μ\mu lies between 9.5854 and 10.8146." Not so: once computed, the interval contains μ\mu or does not. The 95% belongs to the method.
  2. Using 1.96 with ss in a small sample. With five values it covers only 87.84% of the time; use tn−1t_{n-1}.
  3. Confusing standard deviation and standard error. ss measures the spread of single draws; sn\frac{s}{\sqrt{n}} measures the spread of their mean.
  4. Expecting twice the data to halve the interval. It takes four times the data.
  5. Reading the variance interval as symmetric. It stretches to the right, and it is reliable only for normal data.
  6. Expecting the bootstrap to add information. It reuses the sample, so it cannot fix a small or unrepresentative one.

Sampling distributions and confidence intervals

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

  • The sample mean has standard error σn\frac{\sigma}{\sqrt{n}}, estimated by sn\frac{s}{\sqrt{n}}. It is normal for normal draws and close to normal for large nn otherwise.
  • With σ\sigma known, xˉ±zσn\bar{x} \pm z\frac{\sigma}{\sqrt{n}} with z=1.645z = 1.645, 1.96, 2.576 for 90%, 95%, 99%. "95%" is the coverage of the method over samples, not a probability about one computed interval.
  • With σ\sigma estimated, T=Xˉ−μs/n∼tn−1T = \frac{\bar{X} - \mu}{s/\sqrt{n}} \sim t_{n-1} for normal draws, so use xˉ±tn−1sn\bar{x} \pm t_{n-1}\frac{s}{\sqrt{n}}: 2.776 for n=5n = 5, near 1.96 from n=30n = 30.
  • For normal draws (n−1)s2σ2∼χn−12\frac{(n - 1)s^2}{\sigma^2} \sim \chi^2_{n-1}, which gives a skewed interval for σ2\sigma^2; fat tails break it.
  • The bootstrap resamples the data with replacement and reads an interval from the quantiles of the recomputed estimates; it needs independent draws.

Free further reading

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