The model
A regression describes how the mean of one variable changes with another. The variable to explain, , is the response; the one it is explained by, , is the regressor. Simple linear regression assumes that the mean of is a straight line in , and that each observation scatters around that line.
Rule: Simple linear regression
There are observations , . is the intercept and the slope, in units of per unit of .
The error (epsilon) is random, with mean 0 and the same standard deviation at every . So : the line is the mean of at each .
, and are unknown numbers. Data give estimates of them, written with hats, and , as for the estimator 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 , adding errors of standard deviation 1. That data follows this model with , and . 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, is a straight line with slope : 0.6944 for its returns of B and C. The spread around it is the same at every , 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 that makes the sum of squared vertical misses as small as possible:
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
is the sample covariance and the sample variance of the ; their divisors cancel. The line passes through the mean point . Since , the slope is also , with the sign of the correlation .
A worked example
Lesson 1.2.1 gave np.polyfit four points that are not quite on a line, and it returned . Here is how. The means are and .
| 0 | 1 | 2 | 3 | sum | |
|---|---|---|---|---|---|
| 1.1 | 2.9 | 5.2 | 6.8 | ||
| 0.5 | 1.5 | 0 | |||
| 1.2 | 2.8 | 0 | |||
| product | 4.35 | 0.55 | 0.6 | 4.2 | 9.7 |
| 2.25 | 0.25 | 0.25 | 2.25 | 5 |
So and . In sample statistics, and , whose ratio is again 1.94.
The noisy points of lesson 3.5.3 give and , so . With and , the intercept is : the same line that lstsq found there.
Every sign, and where it breaks
- : the line rises; : it falls.
- : the line is flat at . Knowing does not change the best straight-line guess of .
- Every the same: 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.
Fitted values and residuals
The fitted value at is the line's height there, . The residual is what the line misses, , a vertical gap as in lesson 3.5.3. For the four points:
| 0 | 1 | 2 | 3 | |
|---|---|---|---|---|
| 1.1 | 2.9 | 5.2 | 6.8 | |
| 1.09 | 3.03 | 4.97 | 6.91 | |
| 0.01 | 0.23 |
The residuals add up to 0, and so do the products . That is lesson 3.5.3's right angle: the residual vector is orthogonal to the column of ones and to the column of values. The sum of squared residuals is .
Errors are not residuals
The error is the gap to the true line, which no one sees. The residual 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 divides by to make up for it.
Rule: Residual standard deviation
estimates , the typical size of an error, in the units of . The divisor is because the line used two estimates, and . The sample variance of lesson 5.5.2 divided by for its one estimate, the mean; lesson 5.8.1 explains why such corrections work.
For the four points, . For lesson 3.5.3's 20 points, , against the true . With two points, and there is nothing left to estimate 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 . The sums are rounded, since a computer leaves noise of about .
Then lesson 3.5.3's points: the sums of squared errors and of squared residuals, and .
R²: how much the line explains
The total sum of squares measures how much varies around its mean. RSS is what is left after the line. The share the line accounts for is .
Rule: R squared
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 of lesson 5.5.2.
For the four points, and , so ; the correlation is , whose square is the same. For lesson 3.5.3's 20 points, . Case study 3.5.5 read the same number as the share of a stock's variance that a hedge removes.
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 . Yet the estimated slope is 0.0992, close to the truth: a small does not mean the slope is 0.
Try it
The first lines compute both ways for the four points. The next block gives 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 at 0.0094.
How far the estimates can stray
Lesson 3.5.3's fit, , missed the true line . 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 , 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
Under the model, has mean and this standard deviation. It shrinks with less noise (smaller ), more points, or values spread more widely. Lesson 5.10.3 replaces the unknown by , which gives a standard error from one sample, and tests the slope.
Here and , so the formula gives , as the simulation found. Taking each four times halves it to 0.0194; so does spreading the values twice as wide. Lesson 5.8.2's 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 . The last line is the formula, for the 20 values of and for each taken four times.
The explorer draws jointly normal pairs with correlation 0.6 and , 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 did above. At 500 draws it stays close to the dashed line; then move and watch .
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.
- 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 on predicts from . To predict from , swap their roles: the slope becomes , not .
The product of the two slopes is . For the four points, on has slope 0.5132, while ; their product with 1.94 is 0.9957, which is . Both lines pass through , and they coincide only when .
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 : their product is .
Try it
The cell fits both regressions to the four points and compares the second slope with . The last line is case study 5.5.5's pair, from and the volatilities 0.18 and 0.25.
Case study 5.10.5 estimates a stock's beta as the slope of such a line.
Common mistakes
- 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.
- Divide RSS by for , not by or : the line used two estimates.
- Regress the variable you want to predict on the one you know. The slope of on is not unless .
- A small is not a zero slope, and a large does not prove that a straight line is the right shape. measures how tightly the points hug the line.
- The slope has units. With in percent rather than decimals, the slope is 100 times smaller; does not change.
- 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.
- Do not extrapolate far. The line describes the mean of over the range of in the data. Outside it, nothing in the data checks the straight line.
Simple linear regression
0 of 12 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 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,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for 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: , with errors of mean 0 and standard deviation . The line is the mean of at each ; for a jointly normal pair, the line of conditional means.
- Least squares: and . Lesson 1.2.1's four points give .
- Residuals add up to 0 and are orthogonal to the . They estimate the unseen errors; estimates .
- : how tightly the points hug the line, not how steep it is.
- The estimates vary from sample to sample around the truth: , 0.0388 for lesson 3.5.3's points.
- The regression of on is a different line; the two slopes multiply to .