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

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

Regression in matrix form

The model in matrix form

Lesson 5.10.1 explained yy with one regressor xx. Most questions involve several: x1,x2,…x_1, x_2, \ldots, each measured on every observation. Write one equation per observation and stack them, as lesson 3.5.3 stacked the equations of a line.

Rule: Linear regression

y=Xβ+εyi=β0+β1xi1+⋯+βpxip+εi\begin{gathered} \mathbf{y} = X\boldsymbol{\beta} + \boldsymbol{\varepsilon} \\[4pt] y_i = \beta_0 + \beta_1x_{i1} + \cdots \\ + \beta_px_{ip} + \varepsilon_i \end{gathered}

y\mathbf{y} holds the nn responses and ε\boldsymbol{\varepsilon} the nn errors, each with mean 0 and variance σ2\sigma^2. The design matrix XX is n×kn \times k: a column of ones, then one column for each of the pp regressors, so k=p+1k = p + 1. β=(β0,…,βp)\boldsymbol{\beta} = (\beta_0, \ldots, \beta_p) holds the kk coefficients, in the order of the columns.

Simple regression is the case p=1p = 1: XX has the columns 1\mathbf{1} and x\mathbf{x}, as in lesson 3.5.3. Here is an example with two regressors. The second, x2x_2, is a dummy variable: 1 for the four points of a second group and 0 for the first.

ii12345678
xi1x_{i1}12343456
xi2x_{i2}00001111
yiy_i3.565.571.51.52.54.5

Row ii of XX is (1,xi1,xi2)(1, x_{i1}, x_{i2}): the first row is (1,1,0)(1, 1, 0) and the last (1,6,1)(1, 6, 1). So XX is 8×38 \times 3 and β=(β0,β1,β2)\boldsymbol{\beta} = (\beta_0, \beta_1, \beta_2).

The normal equations

Least squares chooses β^\hat{\boldsymbol{\beta}} to make ∥y−Xb∥2\lVert \mathbf{y} - X\mathbf{b} \rVert^2 as small as possible over all b\mathbf{b}, the sum of squared residuals. That is lesson 3.5.3's problem with A=XA = X, so its answer carries over.

Rule: Least-squares estimate

X⊤X β^=X⊤yβ^=(X⊤X)−1X⊤y\begin{gathered} X^\top X\,\hat{\boldsymbol{\beta}} = X^\top\mathbf{y} \\[4pt] \hat{\boldsymbol{\beta}} = (X^\top X)^{-1}X^\top\mathbf{y} \end{gathered}

The inverse exists when the columns of XX are independent (lesson 3.5.1). The residuals e=y−Xβ^\mathbf{e} = \mathbf{y} - X\hat{\boldsymbol{\beta}} are orthogonal to every column: X⊤e=0X^\top\mathbf{e} = \mathbf{0}. Compute β^\hat{\boldsymbol{\beta}} with np.linalg.lstsq, not by inverting X⊤XX^\top X (lesson 3.5.3).

Each entry of X⊤XX^\top X is a dot product of two columns: the count of points, the regressors' sums, and their sums of squares and products. X⊤yX^\top\mathbf{y} holds the sum of the yiy_i and the sums of each regressor times yy. For the eight points:

X⊤X=(828428116184184)X⊤y=(3211010)\begin{gathered} X^\top X = \begin{pmatrix} 8 & 28 & 4 \\ 28 & 116 & 18 \\ 4 & 18 & 4 \end{pmatrix} \\[6pt] X^\top\mathbf{y} = \begin{pmatrix} 32 \\ 110 \\ 10 \end{pmatrix} \end{gathered}

The 8 is the number of points, 28 the sum of xi1x_{i1} and 4 the number in the second group. The 18 is the sum of xi1x_{i1} over that group, and the last 4 is the sum of xi22x_{i2}^2, again the group's size.

Solving gives β^=(3,1,−5)\hat{\boldsymbol{\beta}} = (3, 1, -5). The fitted values are 4,5,6,74, 5, 6, 7 for the first group and 1,2,3,41, 2, 3, 4 for the second. The residuals are −0.5,1,−0.5,0,0.5,−0.5,−0.5,0.5-0.5, 1, -0.5, 0, 0.5, -0.5, -0.5, 0.5, and their sum of squares is 2.5.

With p=1p = 1, the two normal equations are those of lesson 3.5.3's line, and their solution is lesson 5.10.1's β^1=sxysx2\hat{\beta}_1 = \frac{s_{xy}}{s_x^2} and β^0=yˉ−β^1xˉ\hat{\beta}_0 = \bar{y} - \hat{\beta}_1\bar{x}.

Try it

The cell builds XX from a column of ones and the two regressors, prints X⊤XX^\top X and X⊤yX^\top\mathbf{y}, and solves the normal equations.

Then lstsq gives the same β^\hat{\boldsymbol{\beta}} and the rank, 3. The last line prints the residuals, X⊤eX^\top\mathbf{e} (rounded, as it is rounding noise) and the sum of squares.

⌘/Ctrl + Enter runs the cell

What a coefficient means

With several regressors, each coefficient is the change in the mean of yy per unit of its own regressor, with the others held fixed.

Here β^1=1\hat{\beta}_1 = 1: within either group, one more unit of x1x_1 goes with one more unit of yy. And β^2=−5\hat{\beta}_2 = -5: at the same x1x_1, the second group sits 5 lower. The fit is two parallel lines, y=3+x1y = 3 + x_1 for the first group and y=−2+x1y = -2 + x_1 for the second.

Inside each group yy rises one for one with x1x_1. The second group has larger x1x_1 and lower yy, so the dashed line, fitted to x1x_1 alone, mixes the two effects and slopes down.

Leaving a regressor out

Fit yy on x1x_1 alone and the slope is −19≈−0.1111-\frac{1}{9} \approx -0.1111, with R2=0.0073R^2 = 0.0073. On its own, x1x_1 seems to do nothing, or even to lower yy. With x2x_2 in the model, R2R^2 is 0.918.

The reason is that x1x_1 and x2x_2 move together: their correlation is 0.6667, as the second group's x1x_1 values average 4.5 against 2.5. The slope of x2x_2 on x1x_1 is δ^=29≈0.2222\hat{\delta} = \frac{2}{9} \approx 0.2222 (δ\delta is delta). Leaving x2x_2 out credits x1x_1 with part of x2x_2's effect.

Rule: Leaving a regressor out

slope of y on x1 alone=β^1+β^2 δ^\begin{gathered} \text{slope of } y \text{ on } x_1 \text{ alone} \\ = \hat{\beta}_1 + \hat{\beta}_2\,\hat{\delta} \end{gathered}

β^1\hat{\beta}_1 and β^2\hat{\beta}_2 come from the regression on both, and δ^\hat{\delta} is the slope of x2x_2 on x1x_1. It holds exactly for least-squares fits. The slope alone equals β^1\hat{\beta}_1 only when δ^=0\hat{\delta} = 0 (the regressors are uncorrelated) or β^2=0\hat{\beta}_2 = 0.

Here 1+(−5)×29=−191 + (-5) \times \frac{2}{9} = -\frac{1}{9}. The gap is called omitted-variable bias. It is why a coefficient means nothing until you know which other regressors are in the model.

Try it

The first lines fit yy on x1x_1 alone and print its intercept and slope, then R2R^2 alone and with both regressors, and TSS.

Then the slope of x2x_2 on x1x_1, the correlation of the two regressors, the mean of x1x_1 in each group, and the rule's check.

⌘/Ctrl + Enter runs the cell

Linear in the coefficients

"Linear" regression means that y\mathbf{y} is linear in β\boldsymbol{\beta}, not that it is a straight line in xx. A column may hold x2x^2, ln⁡x\ln x or a dummy, and least squares works the same.

A curve. The points (−2,5)(-2, 5), (−1,2)(-1, 2), (0,1)(0, 1), (1,1)(1, 1) and (2,4)(2, 4) bend upwards. A straight line fits them badly: R2=0.0682R^2 = 0.0682. Add the column x2x^2 and the fit is y^=0.7429−0.3x+0.9286x2\hat{y} = 0.7429 - 0.3x + 0.9286x^2, with R2=0.9827R^2 = 0.9827.

np.polyfit(x, y, 2) gives the same coefficients, highest power first.

Groups. A dummy shifts the line for its group, as x2x_2 did. With gg groups and a column of ones, use g−1g - 1 dummies. A dummy for every group adds up to the column of ones, so the columns are dependent (lesson 3.5.1).

For the eight points, the columns 1\mathbf{1}, x2x_2 and 1−x21 - x_2 have rank 2. lstsq returns one of infinitely many β^\hat{\boldsymbol{\beta}}, as lesson 3.5.3 found for dependent columns. Its fitted values, the two group means 5.5 and 2.5, are still unique.

Try it

The first block fits the five points with and without the column x2x^2 and compares with polyfit.

The second puts a dummy for each group beside the column of ones. lstsq reports rank 2 of 3 columns and returns its shortest answer.

⌘/Ctrl + Enter runs the cell

The hat matrix

The fitted values are y^=Xβ^=X(X⊤X)−1X⊤y\hat{\mathbf{y}} = X\hat{\boldsymbol{\beta}} = X(X^\top X)^{-1}X^\top\mathbf{y}. The matrix in front of y\mathbf{y} is the projection matrix of lesson 3.5.2 onto the column space of XX. Regression calls it the hat matrix HH, because it puts the hat on y\mathbf{y}.

Rule: The hat matrix

H=X(X⊤X)−1X⊤y^=Hy,e=(I−H)yH2=H,H⊤=H,trace⁡H=k\begin{gathered} H = X(X^\top X)^{-1}X^\top \\[4pt] \hat{\mathbf{y}} = H\mathbf{y}, \quad \mathbf{e} = (I - H)\mathbf{y} \\[4pt] H^2 = H, \quad H^\top = H, \quad \operatorname{trace} H = k \end{gathered}

HH is n×nn \times n. The fitted values are the projection of y\mathbf{y} onto the column space, and the residuals are what is left, orthogonal to it. The trace, the sum of the diagonal entries, equals the number of columns kk.

For lesson 3.5.3's five points, (0,1)(0, 1) to (4,6)(4, 6), with X=[1 x]X = [\mathbf{1}\ \mathbf{x}]:

H=110(6420−2432102222201234−20246)H = \frac{1}{10}\begin{pmatrix} 6 & 4 & 2 & 0 & -2 \\ 4 & 3 & 2 & 1 & 0 \\ 2 & 2 & 2 & 2 & 2 \\ 0 & 1 & 2 & 3 & 4 \\ -2 & 0 & 2 & 4 & 6 \end{pmatrix}

Hy=(1,2.2,3.4,4.6,5.8)H\mathbf{y} = (1, 2.2, 3.4, 4.6, 5.8), lesson 3.5.3's fitted values. Each fitted value is a weighted sum of all the yjy_j, with row ii of HH as the weights. The middle row gives every point the weight 0.2: the fitted value at xˉ=2\bar{x} = 2 is the mean yˉ\bar{y}.

Leverage

The diagonal entry hiih_{ii} is the leverage of point ii: the weight of yiy_i in its own fitted value. Each lies between 0 and 1, and they add up to kk, so they average kn\frac{k}{n}.

For the five points the leverages are 0.6,0.3,0.2,0.3,0.60.6, 0.3, 0.2, 0.3, 0.6, adding up to 2. In simple regression hii=1n+(xi−xˉ)2∑j(xj−xˉ)2h_{ii} = \frac{1}{n} + \frac{(x_i - \bar{x})^2}{\sum_j (x_j - \bar{x})^2}: points far from xˉ\bar{x} pull hardest.

Move the last point from x=4x = 4 to x=12x = 12 and its leverage becomes 0.9571. The line then has to pass close to it, whatever its yy. So check the leverages when one xx sits far from the rest: a large error there moves the whole fit.

Try it

The cell builds HH for the five points, printed as 10H10H, and checks H2=HH^2 = H, H⊤=HH^\top = H and the trace.

Then HyH\mathbf{y}, (I−H)y(I - H)\mathbf{y}, the leverages against the formula, and the leverages with the last point moved to 12.

⌘/Ctrl + Enter runs the cell

R² with several regressors

R2=1−RSS⁡TSS⁡R^2 = 1 - \frac{\operatorname{RSS}}{\operatorname{TSS}} as in lesson 5.10.1, with XX including the column of ones. For the eight points, RSS⁡=2.5\operatorname{RSS} = 2.5 and TSS⁡=30.5\operatorname{TSS} = 30.5, so R2=0.918R^2 = 0.918.

Adding a column can never raise RSS. The column space only grows, so the nearest point in it can only come closer to y\mathbf{y} (lesson 3.5.2). So R2R^2 never falls, even when the new column is pure noise.

Lesson 3.5.3's 20 points have R2=0.8522R^2 = 0.8522. Add a column of 20 random numbers, unrelated to anything, and R2R^2 rises to 0.853. The adjusted R2R^2 charges for each column, and here it falls, from 0.844 to 0.8358.

Rule: Adjusted R squared

Rˉ2=1−RSS⁡/(n−k)TSS⁡/(n−1)\bar{R}^2 = 1 - \frac{\operatorname{RSS}/(n - k)}{\operatorname{TSS}/(n - 1)}

Each sum is divided by its degrees of freedom: n−kn - k for the residuals and n−1n - 1 for yy around its mean. Lesson 5.10.1's s2s^2 used n−2n - 2, the case k=2k = 2. A column that adds little lowers Rˉ2\bar{R}^2; lesson 5.10.3 tests whether regressors help.

Try it

The cell rebuilds lesson 3.5.3's points, then adds a column of noise drawn with a fixed seed. For each fit it prints R2R^2 and Rˉ2\bar{R}^2.

The noise column's coefficient is small, and in this run R2R^2 still goes up.

⌘/Ctrl + Enter runs the cell

Lesson 5.10.3 adds the statistics of β^\hat{\boldsymbol{\beta}}: its standard errors, and tests of each coefficient and of all of them together. Case study 5.10.5 fits a stock's returns on two factors at once.

Common mistakes

  1. Keep the column of ones. Without it the fit is forced through the origin, and R2R^2 as defined here no longer makes sense.
  2. A coefficient depends on the other columns. x1x_1's slope is 1 with x2x_2 in the model and −0.1111-0.1111 without it.
  3. Use one dummy fewer than the number of groups. A dummy for every group, plus the ones, makes the columns dependent.
  4. The order of β^\hat{\boldsymbol{\beta}} follows the columns of XX. np.polyfit lists the highest power first.
  5. Do not invert X⊤XX^\top X. Use lstsq and read its rank (lesson 3.5.3).
  6. A higher R2R^2 is not proof that a new regressor matters. Even noise raises it; compare Rˉ2\bar{R}^2 or test it (lesson 5.10.3).
  7. Look for high leverage. A point far from the other xx values can move the whole line.

Regression in matrix form

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

  • y=Xβ+ε\mathbf{y} = X\boldsymbol{\beta} + \boldsymbol{\varepsilon}: XX is n×kn \times k, a column of ones and one column per regressor, and β\boldsymbol{\beta} follows the order of the columns.
  • Least squares: X⊤X β^=X⊤yX^\top X\,\hat{\boldsymbol{\beta}} = X^\top\mathbf{y}, so β^=(X⊤X)−1X⊤y\hat{\boldsymbol{\beta}} = (X^\top X)^{-1}X^\top\mathbf{y} when the columns are independent; compute it with lstsq. The residuals are orthogonal to every column.
  • Each coefficient is the effect of its regressor with the others held fixed. Leaving x2x_2 out changes x1x_1's slope to β^1+β^2δ^\hat{\beta}_1 + \hat{\beta}_2\hat{\delta}.
  • Columns may be x2x^2 or dummies: the model is linear in β\boldsymbol{\beta}. Use g−1g - 1 dummies for gg groups.
  • y^=Hy\hat{\mathbf{y}} = H\mathbf{y} with H=X(X⊤X)−1X⊤H = X(X^\top X)^{-1}X^\top, the projection onto the column space: H2=HH^2 = H, trace⁡H=k\operatorname{trace} H = k. Its diagonal holds the leverages.
  • R2R^2 never falls when a column is added; Rˉ2=1−RSS⁡/(n−k)TSS⁡/(n−1)\bar{R}^2 = 1 - \frac{\operatorname{RSS}/(n - k)}{\operatorname{TSS}/(n - 1)} charges for each one.

Free further reading

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