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

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

Simple linear regression

The model

A regression describes how the mean of one variable changes with another. The variable to explain, yy, is the response; the one it is explained by, xx, is the regressor. Simple linear regression assumes that the mean of yy is a straight line in xx, and that each observation scatters around that line.

Rule: Simple linear regression

yi=β0+β1xi+εiE[εi]=0,Var⁡(εi)=σ2\begin{gathered} y_i = \beta_0 + \beta_1 x_i + \varepsilon_i \\[4pt] \mathbb{E}[\varepsilon_i] = 0, \quad \operatorname{Var}(\varepsilon_i) = \sigma^2 \end{gathered}

There are nn observations (xi,yi)(x_i, y_i), i=1,…,ni = 1, \ldots, n. β0\beta_0 is the intercept and β1\beta_1 the slope, in units of yy per unit of xx.

The error εi\varepsilon_i (epsilon) is random, with mean 0 and the same standard deviation σ\sigma at every xx. So E[yi]=β0+β1xi\mathbb{E}[y_i] = \beta_0 + \beta_1x_i: the line is the mean of yy at each xx.

β0\beta_0, β1\beta_1 and σ\sigma are unknown numbers. Data give estimates of them, written with hats, β^0\hat{\beta}_0 and β^1\hat{\beta}_1, as for the estimator θ^\hat{\theta} of lesson 5.8.1. Lesson 5.10.3 lists the assumptions in full and says what happens when they fail.

Lesson 3.5.3 made 20 points from the line y=2+0.5xy = 2 + 0.5x, adding errors of standard deviation 1. That data follows this model with β0=2\beta_0 = 2, β1=0.5\beta_1 = 0.5 and σ=1\sigma = 1. These are known only because the data was invented with them.

Case study 5.5.5 met the model too. For two jointly normal variables, E[Y∣X=x]\mathbb{E}[Y \mid X = x] is a straight line with slope ρσYσX\rho\frac{\sigma_Y}{\sigma_X}: 0.6944 for its returns of B and C. The spread around it is the same at every xx, so the pair follows this model exactly: its regression line is the line of conditional means.

The least-squares estimates

Least squares (lesson 3.5.3) picks the line b0+b1xb_0 + b_1x that makes the sum of squared vertical misses as small as possible:

RSS⁡(b0,b1)=∑i=1n(yi−b0−b1xi)2\operatorname{RSS}(b_0, b_1) = \sum_{i=1}^n \big(y_i - b_0 - b_1x_i\big)^2

Lesson 3.5.3 found the best line from the normal equations. Lesson 4.4.1 set the gradient of RSS to zero, which gives the same two equations. Their solution reads best in the sample statistics of lesson 5.5.2.

Rule: Least-squares estimates

β^1=sxysx2=∑i(xi−xˉ)(yi−yˉ)∑i(xi−xˉ)2β^0=yˉ−β^1xˉ\begin{gathered} \hat{\beta}_1 = \frac{s_{xy}}{s_x^2} \\[6pt] = \frac{\sum_i (x_i - \bar{x})(y_i - \bar{y})}{\sum_i (x_i - \bar{x})^2} \\[6pt] \hat{\beta}_0 = \bar{y} - \hat{\beta}_1\bar{x} \end{gathered}

sxys_{xy} is the sample covariance and sx2s_x^2 the sample variance of the xix_i; their divisors n−1n - 1 cancel. The line passes through the mean point (xˉ,yˉ)(\bar{x}, \bar{y}). Since sxy=r sxsys_{xy} = r\,s_xs_y, the slope is also β^1=rsysx\hat{\beta}_1 = r\frac{s_y}{s_x}, with the sign of the correlation rr.

A worked example

Lesson 1.2.1 gave np.polyfit four points that are not quite on a line, and it returned y=1.94x+1.09y = 1.94x + 1.09. Here is how. The means are xˉ=1.5\bar{x} = 1.5 and yˉ=4\bar{y} = 4.

xix_i0123sum
yiy_i1.12.95.26.8
xi−xˉx_i - \bar{x}−1.5-1.5−0.5-0.50.51.50
yi−yˉy_i - \bar{y}−2.9-2.9−1.1-1.11.22.80
product4.350.550.64.29.7
(xi−xˉ)2(x_i - \bar{x})^22.250.250.252.255

So β^1=9.75=1.94\hat{\beta}_1 = \frac{9.7}{5} = 1.94 and β^0=4−1.94×1.5=1.09\hat{\beta}_0 = 4 - 1.94 \times 1.5 = 1.09. In sample statistics, sxy=9.73=3.2333s_{xy} = \frac{9.7}{3} = 3.2333 and sx2=53=1.6667s_x^2 = \frac{5}{3} = 1.6667, whose ratio is again 1.94.

The noisy points of lesson 3.5.3 give sxy=15.997s_{xy} = 15.997 and sx2=35s_x^2 = 35, so β^1=0.4571\hat{\beta}_1 = 0.4571. With xˉ=10.5\bar{x} = 10.5 and yˉ=7.2152\bar{y} = 7.2152, the intercept is β^0=2.4162\hat{\beta}_0 = 2.4162: the same line that lstsq found there.

Every sign, and where it breaks

  • sxy>0s_{xy} > 0: the line rises; sxy<0s_{xy} < 0: it falls.
  • sxy=0s_{xy} = 0: the line is flat at yˉ\bar{y}. Knowing xx does not change the best straight-line guess of yy.
  • Every xix_i the same: sx2=0s_x^2 = 0 and there is no slope. A best line would be vertical, as lesson 3.5.3 found.
  • Two points: the line goes through both, and every miss is 0.

Try it

fit(x, y) is the rule: the slope from the sample covariance and variance, then the intercept through the mean point.

The cell fits lesson 1.2.1's four points and checks them against np.polyfit, which lists the slope first. Then it rebuilds lesson 3.5.3's noisy points from their seed and fits them.

⌘/Ctrl + Enter runs the cell

Fitted values and residuals

The fitted value at xix_i is the line's height there, y^i=β^0+β^1xi\hat{y}_i = \hat{\beta}_0 + \hat{\beta}_1x_i. The residual is what the line misses, ei=yi−y^ie_i = y_i - \hat{y}_i, a vertical gap as in lesson 3.5.3. For the four points:

xix_i0123
yiy_i1.12.95.26.8
y^i=1.09+1.94xi\hat{y}_i = 1.09 + 1.94x_i1.093.034.976.91
eie_i0.01−0.13-0.130.23−0.11-0.11

The residuals add up to 0, and so do the products xieix_ie_i. That is lesson 3.5.3's right angle: the residual vector is orthogonal to the column of ones and to the column of xx values. The sum of squared residuals is RSS⁡=0.082\operatorname{RSS} = 0.082.

Errors are not residuals

The error εi=yi−β0−β1xi\varepsilon_i = y_i - \beta_0 - \beta_1x_i is the gap to the true line, which no one sees. The residual eie_i is the gap to the fitted line, which you can compute. They differ because the fitted line differs from the true one.

The same 20 points measured from two lines. The errors are gaps to the true line, known here only because the data was invented; the residuals are gaps to the fitted line. Least squares makes the squared residuals add up to less than the squared errors.

For lesson 3.5.3's points, the squared errors add up to 25.3502 and the squared residuals to 24.0997. The residuals are smaller because least squares chose the line that minimises their sum, against every other line, the true one included. So the residuals understate the errors a little, and an estimate of σ\sigma divides by n−2n - 2 to make up for it.

Rule: Residual standard deviation

s=∑iei2n−2s = \sqrt{\frac{\sum_i e_i^2}{n - 2}}

ss estimates σ\sigma, the typical size of an error, in the units of yy. The divisor is n−2n - 2 because the line used two estimates, β^0\hat{\beta}_0 and β^1\hat{\beta}_1. The sample variance of lesson 5.5.2 divided by n−1n - 1 for its one estimate, the mean; lesson 5.8.1 explains why such corrections work.

For the four points, s=0.082/2=0.2025s = \sqrt{0.082 / 2} = 0.2025. For lesson 3.5.3's 20 points, s=1.1571s = 1.1571, against the true σ=1\sigma = 1. With two points, n−2=0n - 2 = 0 and there is nothing left to estimate σ\sigma from.

Try it

The first lines print the fitted values and residuals of the four points, then the two sums that are 0, RSS and ss. The sums are rounded, since a computer leaves noise of about 10−1510^{-15}.

Then lesson 3.5.3's points: the sums of squared errors and of squared residuals, and ss.

⌘/Ctrl + Enter runs the cell

R²: how much the line explains

The total sum of squares TSS⁡=∑i(yi−yˉ)2\operatorname{TSS} = \sum_i (y_i - \bar{y})^2 measures how much yy varies around its mean. RSS is what is left after the line. The share the line accounts for is R2R^2.

Rule: R squared

R2=1−RSS⁡TSS⁡R2=r2 for one regressor\begin{gathered} R^2 = 1 - \frac{\operatorname{RSS}}{\operatorname{TSS}} \\[4pt] R^2 = r^2 \text{ for one regressor} \end{gathered}

R2R^2 lies between 0 and 1 and has no units. It is 1 when every point is on the line and 0 when the fitted line is flat. With one regressor it is the square of the sample correlation rr of lesson 5.5.2.

For the four points, TSS⁡=18.9\operatorname{TSS} = 18.9 and RSS⁡=0.082\operatorname{RSS} = 0.082, so R2=0.9957R^2 = 0.9957; the correlation is r=0.9978r = 0.9978, whose square is the same. For lesson 3.5.3's 20 points, R2=0.8522R^2 = 0.8522. Case study 3.5.5 read the same number as the share of a stock's variance that a hedge removes.

R2R^2 measures how tightly the points hug the line, not how steep the line is. Two thousand points with a true slope of 0.1 and errors of size 1 give R2=0.0094R^2 = 0.0094. Yet the estimated slope is 0.0992, close to the truth: a small R2R^2 does not mean the slope is 0.

Try it

The first lines compute R2R^2 both ways for the four points. The next block gives r2r^2 for lesson 3.5.3's 20 points.

The last lines draw 2,000 points with a true slope of 0.1 and a fixed seed. In this run the slope comes out at 0.0992 and R2R^2 at 0.0094.

⌘/Ctrl + Enter runs the cell

How far the estimates can stray

Lesson 3.5.3's fit, y=2.4162+0.4571xy = 2.4162 + 0.4571x, missed the true line y=2+0.5xy = 2 + 0.5x. How far off a fit like this can be is a question of statistics. The estimates are random variables (lesson 5.8.1): new errors would give new estimates.

Run the experiment again: keep the same 20 values of xx, draw new errors, and fit again, 10,000 times. In one seeded run the slopes average 0.5001 with standard deviation 0.0386; the intercepts average 1.9998 with standard deviation 0.4637. The middle 95% of the slopes runs from 0.4243 to 0.5758.

So the estimates are centred on the truth. The 0.4571 is an ordinary draw: 27.26% of the samples miss 0.5 by at least as much. The spread of the slope has a formula.

Rule: Spread of the estimated slope

sd⁡(β^1)=σ∑i(xi−xˉ)2\operatorname{sd}(\hat{\beta}_1) = \frac{\sigma}{\sqrt{\sum_i (x_i - \bar{x})^2}}

Under the model, β^1\hat{\beta}_1 has mean β1\beta_1 and this standard deviation. It shrinks with less noise (smaller σ\sigma), more points, or xx values spread more widely. Lesson 5.10.3 replaces the unknown σ\sigma by ss, which gives a standard error from one sample, and tests the slope.

Here ∑i(xi−xˉ)2=665\sum_i (x_i - \bar{x})^2 = 665 and σ=1\sigma = 1, so the formula gives 1665=0.0388\frac{1}{\sqrt{665}} = 0.0388, as the simulation found. Taking each xx four times halves it to 0.0194; so does spreading the xx values twice as wide. Lesson 5.8.2's σn\frac{\sigma}{\sqrt{n}} for a mean behaved the same way.

Try it

The cell redraws the errors 10,000 times with a fixed seed, all at once. Each row of Y is one sample, and each slope is this lesson's rule applied to one row.

The second line is the share of samples whose slope misses 0.5 by at least 0.5−0.45710.5 - 0.4571. The last line is the formula, for the 20 values of xx and for each taken four times.

⌘/Ctrl + Enter runs the cell

The explorer draws jointly normal pairs with correlation 0.6 and σY=1\sigma_Y = 1, so the dashed line of conditional means has slope 0.6. The solid line is the least-squares fit to the dots, with their residuals.

Cut the draws to 20 and press "Draw again" a few times: the fitted slope wanders, as β^1\hat{\beta}_1 did above. At 500 draws it stays close to the dashed line; then move ρ\rho and watch r2r^2.

Explore

Each dot is one draw of the pair (X, Y). Move ρ and watch the cloud stretch along a line. The dashed line is E[Y | X = x], the average of Y for each x; turn on the fitted line to see least squares estimate it from the dots.

−202−202
Correlation
true ρ = 0.6; sample r = 0.62
Slope of E[Y | X]
ρσ_Y/σ_X = 0.6 × 1 / 1 = 0.6
Fitted line
y = 0.011 + 0.61x, sum of squared residuals 136.18
Explained
r² = 0.385: the share of the variance of Y that the line accounts for

Regressing x on y is a different line

The regression of yy on xx predicts yy from xx. To predict xx from yy, swap their roles: the slope becomes sxysy2=rsxsy\frac{s_{xy}}{s_y^2} = r\frac{s_x}{s_y}, not 1β^1\frac{1}{\hat{\beta}_1}.

The product of the two slopes is r2r^2. For the four points, xx on yy has slope 0.5132, while 11.94=0.5155\frac{1}{1.94} = 0.5155; their product with 1.94 is 0.9957, which is R2R^2. Both lines pass through (xˉ,yˉ)(\bar{x}, \bar{y}), and they coincide only when r=±1r = \pm 1.

The weaker the link, the further apart the two lines. In case study 5.5.5, C on B has slope 0.6944 and B on C has 0.36, not 10.6944=1.44\frac{1}{0.6944} = 1.44: their product is ρ2=0.25\rho^2 = 0.25.

Try it

The cell fits both regressions to the four points and compares the second slope with 1β^1\frac{1}{\hat{\beta}_1}. The last line is case study 5.5.5's pair, from ρ=0.5\rho = 0.5 and the volatilities 0.18 and 0.25.

⌘/Ctrl + Enter runs the cell

Case study 5.10.5 estimates a stock's beta as the slope of such a line.

Common mistakes

  1. Residuals are not errors. Residuals are gaps to the fitted line and add up to 0 exactly. Errors are gaps to the true line, which is unknown.
  2. Divide RSS by n−2n - 2 for ss, not by nn or n−1n - 1: the line used two estimates.
  3. Regress the variable you want to predict on the one you know. The slope of xx on yy is not 1β^1\frac{1}{\hat{\beta}_1} unless r=±1r = \pm 1.
  4. A small R2R^2 is not a zero slope, and a large R2R^2 does not prove that a straight line is the right shape. R2R^2 measures how tightly the points hug the line.
  5. The slope has units. With xx in percent rather than decimals, the slope is 100 times smaller; R2R^2 does not change.
  6. An estimate is not the truth. One sample gave 0.4571 for a true 0.5. The slope's standard deviation, 0.0388, says how far such misses typically go.
  7. Do not extrapolate far. The line describes the mean of yy over the range of xx in the data. Outside it, nothing in the data checks the straight line.

Simple linear regression

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

  • Simple linear regression: yi=β0+β1xi+εiy_i = \beta_0 + \beta_1x_i + \varepsilon_i, with errors of mean 0 and standard deviation σ\sigma. The line is the mean of yy at each xx; for a jointly normal pair, the line of conditional means.
  • Least squares: β^1=sxysx2=rsysx\hat{\beta}_1 = \frac{s_{xy}}{s_x^2} = r\frac{s_y}{s_x} and β^0=yˉ−β^1xˉ\hat{\beta}_0 = \bar{y} - \hat{\beta}_1\bar{x}. Lesson 1.2.1's four points give y=1.09+1.94xy = 1.09 + 1.94x.
  • Residuals ei=yi−y^ie_i = y_i - \hat{y}_i add up to 0 and are orthogonal to the xix_i. They estimate the unseen errors; s=RSS⁡/(n−2)s = \sqrt{\operatorname{RSS}/(n - 2)} estimates σ\sigma.
  • R2=1−RSS⁡TSS⁡=r2R^2 = 1 - \frac{\operatorname{RSS}}{\operatorname{TSS}} = r^2: how tightly the points hug the line, not how steep it is.
  • The estimates vary from sample to sample around the truth: sd⁡(β^1)=σ∑(xi−xˉ)2\operatorname{sd}(\hat{\beta}_1) = \frac{\sigma}{\sqrt{\sum(x_i - \bar{x})^2}}, 0.0388 for lesson 3.5.3's points.
  • The regression of xx on yy is a different line; the two slopes multiply to r2r^2.

Free further reading

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