SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 5.10.1 Simple linear regressionConcept · 35 min
      2. 5.10.2 Regression in matrix formConcept · 30 min
      3. 5.10.3 Assumptions, t tests and F testsConcept · 35 min
      4. 5.10.4 Practice set: linear regressionPractice set · 25 min
      5. 5.10.5 Case study: estimating beta and hedge ratiosCase study · 35 min

Level 5, Probability and statistics 5.10 Linear regression Lesson 3 of 5

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

Assumptions, t tests and F tests

The assumptions

Lesson 5.10.2 fitted y=Xβ+ε\mathbf{y} = X\boldsymbol{\beta} + \boldsymbol{\varepsilon} by least squares, which needs no assumptions at all. To say how far β^\hat{\boldsymbol{\beta}} may be from β\boldsymbol{\beta}, you need a model of the errors. These are the usual assumptions, with the xx values treated as fixed numbers, as lesson 5.10.1's simulation kept the same 20 values of xx.

  1. Linear mean. E[y]=Xβ\mathbb{E}[\mathbf{y}] = X\boldsymbol{\beta}: the columns of XX capture how the mean of yy depends on the regressors.
  2. Independent columns. XX has rank kk, so (X⊤X)−1(X^\top X)^{-1} exists (lesson 5.10.2).
  3. Errors unrelated to the regressors. Each εi\varepsilon_i has mean 0, whatever the regressors are. This fails when a regressor that matters is left out and moves with one that is in: lesson 5.10.2's omitted-variable bias.
  4. Constant variance. Var⁡(εi)=σ2\operatorname{Var}(\varepsilon_i) = \sigma^2, the same for every observation.
  5. Uncorrelated errors. Cov⁡(εi,εj)=0\operatorname{Cov}(\varepsilon_i, \varepsilon_j) = 0 for i≠ji \neq j: one error says nothing about another.
  6. Normal errors. Each εi\varepsilon_i is normal. This one is needed only for the exact t and F distributions below.

Rule: What the assumptions buy

E[β^]=βCov⁡(β^)=σ2(X⊤X)−1\begin{gathered} \mathbb{E}[\hat{\boldsymbol{\beta}}] = \boldsymbol{\beta} \\[4pt] \operatorname{Cov}(\hat{\boldsymbol{\beta}}) = \sigma^2(X^\top X)^{-1} \end{gathered}

Assumptions 1 to 3 make β^\hat{\boldsymbol{\beta}} unbiased (lesson 5.8.1): centred on the truth. Adding 4 and 5 gives its covariance matrix, whose diagonal holds the variances of the coefficients. Adding 6 makes β^\hat{\boldsymbol{\beta}} jointly normal.

The covariance of the estimates

Three steps give the rule, each using a lesson you have met. Write A=(X⊤X)−1X⊤A = (X^\top X)^{-1}X^\top, a fixed k×nk \times n matrix, so that β^=Ay\hat{\boldsymbol{\beta}} = A\mathbf{y}.

  1. 1

    β^=β+Aε\hat{\boldsymbol{\beta}} = \boldsymbol{\beta} + A\boldsymbol{\varepsilon}

  2. 2

    E[β^]=β+A E[ε]=β\mathbb{E}[\hat{\boldsymbol{\beta}}] = \boldsymbol{\beta} + A\,\mathbb{E}[\boldsymbol{\varepsilon}] = \boldsymbol{\beta}

  3. 3

    Cov⁡(β^)=σ2AA⊤=σ2(X⊤X)−1\operatorname{Cov}(\hat{\boldsymbol{\beta}}) = \sigma^2AA^\top = \sigma^2(X^\top X)^{-1}

For lesson 3.5.3's 20 values of xx, with σ=1\sigma = 1:

σ2(X⊤X)−1=(0.2158−0.0158−0.01580.0015)\sigma^2(X^\top X)^{-1} = \begin{pmatrix} 0.2158 & -0.0158 \\ -0.0158 & 0.0015 \end{pmatrix}

The square roots of the diagonal are 0.4645 for the intercept and 0.0388 for the slope: lesson 5.10.1's σ665\frac{\sigma}{\sqrt{665}}. Lesson 5.10.1's 10,000 simulated samples give the sample covariance matrix (0.215−0.0157−0.01570.0015)\begin{pmatrix} 0.215 & -0.0157 \\ -0.0157 & 0.0015 \end{pmatrix}, close to it.

The off-diagonal entry is negative: the correlation of β^0\hat{\beta}_0 and β^1\hat{\beta}_1 is −0.8765-0.8765. Every fitted line passes through the mean point (xˉ,yˉ)(\bar{x}, \bar{y}), here with xˉ=10.5\bar{x} = 10.5. A line that comes out steeper must cross x=0x = 0 lower, so a high slope comes with a low intercept.

Try it

The first lines compute (X⊤X)−1(X^\top X)^{-1} for the 20 values of xx, which is Cov⁡(β^)\operatorname{Cov}(\hat{\boldsymbol{\beta}}) when σ=1\sigma = 1, and the correlation it implies.

Then lesson 5.10.1's simulation, with its seed: the sample covariance matrix and correlation of the 10,000 pairs (β^0,β^1)(\hat{\beta}_0, \hat{\beta}_1).

⌘/Ctrl + Enter runs the cell

Standard errors and t statistics

The rule needs σ2\sigma^2, which is unknown. Estimate it from the residuals, as lesson 5.10.1 did with n−2n - 2; with kk coefficients, divide by n−kn - k. The square root of each diagonal entry then becomes a standard error.

Maximum likelihood divides RSS by nn instead (lesson 5.9.2), which makes the estimate too small on average. Dividing by n−kn - k removes that bias, as n−1n - 1 does for a sample variance (lesson 5.8.1).

Rule: Standard errors and the t statistic

s2=RSS⁡n−kse⁡(β^j)=s[(X⊤X)−1]jjt=β^j−bse⁡(β^j)\begin{gathered} s^2 = \frac{\operatorname{RSS}}{n - k} \\[4pt] \operatorname{se}(\hat{\beta}_j) = s\sqrt{\big[(X^\top X)^{-1}\big]_{jj}} \\[4pt] t = \frac{\hat{\beta}_j - b}{\operatorname{se}(\hat{\beta}_j)} \end{gathered}

[(X⊤X)−1]jj[(X^\top X)^{-1}]_{jj} is the diagonal entry for coefficient jj, and n−kn - k is the number of degrees of freedom. Under all six assumptions, if the true βj\beta_j is the number bb, then tt has the t distribution with n−kn - k degrees of freedom (lesson 5.4.4).

That gives the two tools of module 5.8, which read as follows for a coefficient. Lesson 5.8.2 built a 95% interval as the estimate plus or minus a t quantile times its standard error. Lesson 5.8.3 tested a value by its t statistic and p-value.

β^j±t0.975, n−kse⁡(β^j)p=2 P(Tn−k≥∣t∣)\begin{gathered} \hat{\beta}_j \pm t_{0.975,\,n-k}\operatorname{se}(\hat{\beta}_j) \\[4pt] p = 2\,P(T_{n-k} \ge \lvert t \rvert) \end{gathered}

Here t0.975, n−kt_{0.975,\,n-k} is the 0.975-quantile of the t distribution, t.ppf(0.975, n - k), and Tn−kT_{n-k} a variable with that distribution.

How far the fit may be from the truth

Lesson 3.5.3's 20 points gave β^=(2.4162,0.4571)\hat{\boldsymbol{\beta}} = (2.4162, 0.4571). With n−k=18n - k = 18 degrees of freedom, s=1.1571s = 1.1571, so the slope's standard error is 1.1571665=0.0449\frac{1.1571}{\sqrt{665}} = 0.0449 and the intercept's is 0.5375. The quantile is t0.975, 18=2.1009t_{0.975,\,18} = 2.1009.

estimatestandard error95% intervalt for the truthp
intercept2.41620.53751.2869 to 3.54540.77420.4488
slope0.45710.04490.3628 to 0.5513−0.9571-0.95710.3512

Both intervals cover the true values, 2 and 0.5. The t statistics test those true values: a p-value of 0.3512 says a miss as large as 0.5−0.45710.5 - 0.4571 is common. This is how far the fit may be from the truth, from one sample alone.

The test that matters most in practice is whether the slope is 0 at all. Here t=0.45710.0449=10.1862t = \frac{0.4571}{0.0449} = 10.1862, and the p-value is about 7×10−97 \times 10^{-9}: yy does depend on xx.

Try it

The cell fits lesson 3.5.3's points, computes ss, the standard errors and the t quantile with scipy.stats.t.

Then the intervals, the t statistics and p-values for the true values (2,0.5)(2, 0.5), and the t statistics for 0 with the slope's p-value and t2t^2.

⌘/Ctrl + Enter runs the cell

Why the t quantile, not 1.96

Lesson 5.10.1's 10,000 samples can check the interval. With each sample's own ss, the interval β^1±2.1009se⁡\hat{\beta}_1 \pm 2.1009\operatorname{se} covers 0.5 in 95.01% of them. With 1.96 in place of 2.1009 it covers only 93.54%.

The standard error uses ss, which varies from sample to sample, and that widens the spread of tt: lesson 5.4.4's fatter tails. Across the samples, s2s^2 averages 0.9982, close to σ2=1\sigma^2 = 1, which is why n−kn - k is the divisor. With many degrees of freedom the t quantile comes close to 1.96.

Try it

The cell repeats lesson 5.10.1's 10,000 samples, computes each one's residuals, ss and standard error, and counts how often each interval covers 0.5.

The last line is the average of s2s^2 over the samples.

⌘/Ctrl + Enter runs the cell

Two regressors

Lesson 5.10.2's eight points have n−k=5n - k = 5 and RSS⁡=2.5\operatorname{RSS} = 2.5, so s2=0.5s^2 = 0.5. The diagonal of (X⊤X)−1(X^\top X)^{-1} is 78,110,910\frac{7}{8}, \frac{1}{10}, \frac{9}{10}, so se⁡(β^1)=0.5×0.1=0.2236\operatorname{se}(\hat{\beta}_1) = \sqrt{0.5 \times 0.1} = 0.2236.

estimatestandard errort for 0p
intercept30.66144.53560.0062
x1x_110.22364.47210.0066
group x2x_2−5-50.6708−7.4536-7.45360.0007

With 5 degrees of freedom the quantile is 2.5706, so the 95% interval for β1\beta_1 runs from 0.4252 to 1.5748.

The F test

A t test asks about one coefficient. The F test asks whether any regressor matters: its null hypothesis is that every slope is 0, β1=⋯=βp=0\beta_1 = \cdots = \beta_p = 0, so that only the intercept is left.

Under that null the best fit is the flat line at yˉ\bar{y}, which leaves TSS. The full model leaves RSS. The F statistic compares the drop, per slope, with what is left, per degree of freedom.

Rule: The F test that all slopes are 0

F=(TSS⁡−RSS⁡)/(k−1)RSS⁡/(n−k)=R2/(k−1)(1−R2)/(n−k)\begin{gathered} F = \frac{(\operatorname{TSS} - \operatorname{RSS})/(k - 1)}{\operatorname{RSS}/(n - k)} \\[6pt] = \frac{R^2/(k - 1)}{(1 - R^2)/(n - k)} \end{gathered}

Under the null and the six assumptions, FF has the F distribution with k−1k - 1 and n−kn - k degrees of freedom. That is a ratio of two independent χ2\chi^2 variables (lesson 5.4.4), each divided by its degrees of freedom. Large FF is evidence against the null; the p-value is f.sf(F, k - 1, n - k).

For the eight points, TSS⁡=30.5\operatorname{TSS} = 30.5 and RSS⁡=2.5\operatorname{RSS} = 2.5:

F=28/22.5/5=28F = \frac{28/2}{2.5/5} = 28

With 2 and 5 degrees of freedom, the p-value is 0.0019; the 5% critical value is 5.7861. With a single slope, F=t2F = t^2: lesson 3.5.3's points give F=103.758=10.18622F = 103.758 = 10.1862^2, with the same p-value as the t test.

When t and F disagree

Take 30 invented points with two regressors that are almost the same: x2x_2 is x1x_1 plus a little noise, and their correlation is 0.9993. The true model is y=1+0.5x1+0.5x2+εy = 1 + 0.5x_1 + 0.5x_2 + \varepsilon.

The fit gives β^1=3.3173\hat{\beta}_1 = 3.3173 and β^2=−2.425\hat{\beta}_2 = -2.425, far from the truth, with standard errors 3.5078 and 3.4812. Neither t test finds anything: their p-values are 0.353 and 0.492. Yet F=21.1101F = 21.1101, with p-value 3×10−63 \times 10^{-6}: together the two regressors matter a great deal.

The data cannot tell x1x_1 from x2x_2, so it cannot split their effect, but it pins down the total. The sum β^1+β^2=0.8924\hat{\beta}_1 + \hat{\beta}_2 = 0.8924 has standard error 0.1376, close to the true 1.

This is collinearity, and large standard errors beside a significant F are its mark. Keep one of the two regressors, or test their combined effect.

Try it

The first block repeats the eight points: the diagonal of (X⊤X)−1(X^\top X)^{-1}, standard errors, t statistics and p-values, and the interval for β1\beta_1. Then FF, its p-value and the 5% critical value from scipy.stats.f.

The second block makes the 30 nearly collinear points with a fixed seed. It prints the estimates, their standard errors and p-values, FF and its p-value, and the sum with its standard error a⊤Va\sqrt{\mathbf{a}^\top V\mathbf{a}} for a=(0,1,1)\mathbf{a} = (0, 1, 1) (lesson 5.5.3).

⌘/Ctrl + Enter runs the cell

When the assumptions fail

Least squares still fits a line when the assumptions fail. What goes wrong is the statistics: the standard errors, and so every interval and p-value. Plot the residuals to see which assumption is in doubt.

Residuals from three invented samples: in the first the assumptions hold, in the second the spread grows with xx. In the third each error is close to its neighbour, so the residuals run in long stretches above or below 0.

Changing variance. Suppose the errors' standard deviation grows in proportion to xx, scaled so that their average variance is still 1. Lesson 5.10.1's experiment then gives slopes whose true spread is 0.0431, but the usual standard error averages 0.0379. The nominal 95% intervals cover 0.5 only 92.26% of the time.

Correlated errors. Data in time order often has errors close to their neighbours: make each error correlated 0.5 with the one before (a lag-one correlation). The slopes' true spread becomes 0.0606, while the usual standard error averages 0.0344. The intervals cover only 75.28% of the time, and 50.93% at a lag-one correlation of 0.8.

With the errors as assumed, the same experiment gives a true spread of 0.0391, an average standard error of 0.0381 and coverage of 94.92%. In both failures β^\hat{\boldsymbol{\beta}} is still unbiased, but σ2(X⊤X)−1\sigma^2(X^\top X)^{-1} is the wrong covariance matrix. Here it was too small, so t statistics came out too large and slopes looked significant too often.

Rule: What each failure does

changing variance orcorrelated errors:β^ unbiased,standard errors wrongerrors related to a regressor:β^ biased\begin{gathered} \text{changing variance or} \\ \text{correlated errors:} \\ \hat{\boldsymbol{\beta}} \text{ unbiased,} \\ \text{standard errors wrong} \\[6pt] \text{errors related to a regressor:} \\ \hat{\boldsymbol{\beta}} \text{ biased} \end{gathered}

Changing variance is called heteroscedasticity, and correlated errors autocorrelation. Robust standard errors (White's for changing variance, Newey and West's for correlation too) estimate the spread without those assumptions. No standard error repairs bias from a left-out regressor.

Non-normal errors matter less. With many observations the central limit theorem (lesson 5.7.3) makes β^\hat{\boldsymbol{\beta}} close to normal anyway, so t and F are close to right. Fat tails and outliers are the exception: a single wild point with high leverage (lesson 5.10.2) can move the fit.

Try it

The cell reruns lesson 5.10.1's experiment three ways, 10,000 samples each with a fixed seed. coverage prints the slopes' true spread, the average usual standard error, and how often the 95% interval covers 0.5.

The errors are first as assumed, then with a standard deviation in proportion to xx, then with each error correlated with the one before.

⌘/Ctrl + Enter runs the cell

Case study 5.10.5 applies these tests to a stock's beta and alpha.

Common mistakes

  1. Divide RSS by n−kn - k, the number of observations minus the number of coefficients, intercept included.
  2. Use the t quantile with n−kn - k degrees of freedom, not 1.96, for a small sample: with 18 degrees of freedom, 1.96 covers 93.54%, not 95%.
  3. A large p-value does not prove the null. Lesson 3.5.3's test of a slope of 0.5 gave p=0.3512p = 0.3512. Every value inside the interval, 0.3628 to 0.5513, would also pass.
  4. Insignificant t statistics do not mean no regressor matters. Collinear regressors can each look useless while the F test is overwhelming.
  5. Standard errors rest on assumptions 4 and 5. With changing variance or correlated errors they can be far too small; look at the residuals.
  6. No standard error fixes a left-out regressor. That makes β^\hat{\boldsymbol{\beta}} itself biased (lesson 5.10.2).

Assumptions, t tests and F 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

  • Assumptions: linear mean, independent columns, errors of mean 0 unrelated to the regressors, constant variance, uncorrelated errors; normal errors for exact t and F.
  • Then β^=β+(X⊤X)−1X⊤ε\hat{\boldsymbol{\beta}} = \boldsymbol{\beta} + (X^\top X)^{-1}X^\top\boldsymbol{\varepsilon} is unbiased with Cov⁡(β^)=σ2(X⊤X)−1\operatorname{Cov}(\hat{\boldsymbol{\beta}}) = \sigma^2(X^\top X)^{-1}.
  • s2=RSS⁡n−ks^2 = \frac{\operatorname{RSS}}{n - k} and se⁡(β^j)=s[(X⊤X)−1]jj\operatorname{se}(\hat{\beta}_j) = s\sqrt{[(X^\top X)^{-1}]_{jj}}. The interval β^j±t0.975, n−kse⁡\hat{\beta}_j \pm t_{0.975,\,n-k}\operatorname{se} and the test t=β^j−bse⁡t = \frac{\hat{\beta}_j - b}{\operatorname{se}} use n−kn - k degrees of freedom.
  • Lesson 3.5.3's slope, 0.4571, has standard error 0.0449 and interval 0.3628 to 0.5513, which covers the true 0.5.
  • The F test that all slopes are 0: F=R2/(k−1)(1−R2)/(n−k)F = \frac{R^2/(k - 1)}{(1 - R^2)/(n - k)}; with one slope F=t2F = t^2. Collinear regressors can fail each t test and pass F.
  • Changing variance and correlated errors leave β^\hat{\boldsymbol{\beta}} unbiased but make the standard errors wrong, often too small; a left-out regressor biases β^\hat{\boldsymbol{\beta}}.

Free further reading

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