Linear regression: the first model, a straight line
On this page
Linear regression is usually the first model anyone learns, and sections 3.1 to 3.3 of An Introduction to Statistical Learning (ISLP) cover it well. It’s worth learning properly, because it’s simple, fast, easy to explain to a business user, and the starting point for many other models.
This post continues the regression vs classification example: predicting how volatile the market will be next month.
The idea
You have some months. For each one you know how volatile it was (the standard deviation of its daily returns, annualised), and how volatile the next month turned out to be. Plot them, and they roughly form a line: a wild month tends to be followed by a wild month. Linear regression finds the one straight line that fits the points best, so you can read off a forecast for any level of current volatility.
A straight line has two numbers. In the notation of An Introduction to Statistical Learning (ISLP), equations 3.1 and 3.2, with hats marking numbers estimated from data:
β̂ and ŷ, what the model estimatesy, the actual value
| Symbol | Meaning | In our example |
|---|---|---|
| the input (feature) | this month’s volatility, % a year | |
| the prediction (“y-hat”) | next month’s volatility, % a year | |
| (“beta-one-hat”) | the slope: how much goes up when goes up by 1 | points of next-month volatility per point of this month’s |
| the intercept: the value of when | the line’s starting level |
What “best” means: least squares
For each month, the line makes a prediction . The real value is . The difference is the residual, the miss.
Linear regression picks the line that makes the residual sum of squares (RSS) as small as possible (ISLP equation 3.3):
RSS, the error left over
Why square them? Three reasons. A miss above the line and a miss below don’t cancel out. Big misses count much more than small ones. And squaring gives a neat formula for the answer, with no searching needed. This method is called ordinary least squares (OLS).
For one input, the best slope and intercept are (ISLP equation 3.4):
where and (read “x-bar”, “y-bar”) are the averages.
Plain meaning of the slope formula: the top measures how and move together. When a month is wilder than usual, is the next one also wilder than usual? The bottom measures how much moves on its own. Their ratio is “how much moves per unit of ”. (The top part, divided by , is the covariance, which gets its own post in this series.)
Worked example: five months by hand
Five SPY months at two-month gaps through the 2020 recovery, volatility rounded to whole percentage points so you can follow the arithmetic:
| Month starting | This month | Next month | product | |||
|---|---|---|---|---|---|---|
| Jun 2020 | 20 | 30 | 2 | 13.4 | 26.8 | 4 |
| Aug 2020 | 13 | 8 | −5 | −8.6 | 43.0 | 25 |
| Oct 2020 | 24 | 21 | 6 | 4.4 | 26.4 | 36 |
| Dec 2020 | 16 | 8 | −2 | −8.6 | 17.2 | 4 |
| Feb 2021 | 17 | 16 | −1 | −0.6 | 0.6 | 1 |
| Average / Sum | 114.0 | 70 |
Reading it: on these five months, each extra point of volatility this month came with about extra points next month. A month at 20%: expected next.
Be careful with the intercept. It says that a month with zero volatility is followed by one at −12.7%, which makes no sense (volatility can’t be negative). We never saw a month anywhere near zero. A line is only trustworthy inside the range of data it learned from. Using it far outside that range is called extrapolation, and it is the usual way a linear model goes wrong in practice.
The dotted lines on the left are the residuals. The fitted line is the one where the squares of those dotted lines add up to the smallest possible total, here .
How good is the fit? R²
(“R squared”, ISLP equation 3.17) answers: what share of the ups and downs in does the line explain?
RSS and TSS, the error left over and the total spread
- TSS (total sum of squares) is how much varies around its own average: the error you’d make if you ignored and always guessed the average.
- RSS (residual sum of squares) is the error left over after using the line.
For the five months, with RSS :
The line explains of the month-to-month variation in volatility. Real market data is noisy; nothing here will score in the nineties. means no better than guessing the average. means perfect.
More than one input
A month is more than its volatility. Markets that have just fallen tend to stay nervous, so add last month’s return as a second input:
Now the “line” is a flat surface in three dimensions, but the idea is the same: find the numbers that make the squared misses smallest.
Trained on the 3,685 ticker-months of 2019 to 2021 (all five tickers), then tested on 2022 to 2023:
| Item | One input | Two inputs |
|---|---|---|
| Intercept | 9.74 | 11.03 |
| Per point of this month’s volatility | 0.604 | 0.577 |
| Per point of last month’s return | −0.315 | |
| on the training months | 0.367 | 0.386 |
| on the test months |
Reading : a month that fell 10% instead of staying flat means more points of next-month volatility, with this month’s volatility held the same. That last part matters. In multiple regression, each number is the effect of one input while the others stay fixed. The sign is right (falls make markets nervous), and the size is sensible.
Now look at the last row. The second input improved the fit on the training months, as adding an input always does, and made the fit on the test months slightly worse: fell from 0.458 to 0.414. The return carries a little information, but the model also learned some of 2019 to 2021’s particular quirks through it, and those didn’t repeat. More inputs are not automatically better; only the test score can tell you. That tension gets a whole post, bias and variance.
(Why is the test higher than the training one for the one-input model? 2020’s crash made the training years unusually hard to fit; 2022 and 2023 were tamer. depends on the data as much as on the model.)
The matrix form
With many inputs, the formula is written with matrices. Put the inputs in a matrix (one row per day, plus a column of 1s for the intercept) and the answers in a vector . The best coefficients are:
This comes from the normal equations (Murphy, Probabilistic Machine Learning, chapter 11). ISLP doesn’t print the matrix form, because it needs matrix algebra. In practice, libraries solve it with more numerically stable methods than inverting the matrix, but the answer is the same. On the five months it gives exactly .
The code
import numpy as np
from sklearn.linear_model import LinearRegression
x = np.array([20, 13, 24, 16, 17]) # this month's volatility, % a year
y = np.array([30, 8, 21, 8, 16]) # next month's
# By hand
b1 = ((x - x.mean()) * (y - y.mean())).sum() / ((x - x.mean()) ** 2).sum()
b0 = y.mean() - b1 * x.mean()
print(b0, b1) # -12.7 1.63
# With scikit-learn: same answer
model = LinearRegression().fit(x.reshape(-1, 1), y)
print(model.intercept_, model.coef_) # -12.7 [1.63]
print(model.predict([[20]])) # [19.9]
When linear regression goes wrong
- The relationship isn’t a straight line. If volatility only matters above some level, a line will miss it. (Decision trees, the next post, handle this.)
- Extrapolation. Never trust the line far outside the data, like the “−12.7% next month after a month with no volatility”.
- Outliers. Because misses are squared, one extreme month (March 2020) can pull the line a long way. The five-month example avoided it; the three-year fit had to live with it.
- Inputs that move together. If two inputs are strongly linked (5-day and 20-day volatility, say), the model can’t tell their effects apart, and the individual numbers become unstable, even when the predictions are fine.
- Correlation is not causation. The slope says this month’s and next month’s volatility move together. It doesn’t say one causes the other; both may be reacting to the same news.
Summary
| Idea | Formula | Remember it as |
|---|---|---|
| The line | Start level plus slope times input | |
| Best fit | minimise | Least squares |
| Slope | How they move together ÷ how moves alone | |
| Intercept | The line passes through the averages | |
| Fit quality | Share of the variation explained | |
| Many inputs | The normal equation |
Next
Decision trees: a model that asks a series of yes/no questions instead of drawing a line, and can follow patterns a straight line can’t.
References
- James, Witten, Hastie, Tibshirani and Taylor, An Introduction to Statistical Learning, with Applications in Python (Springer, 2023), sections 3.1 to 3.3: the model (equations 3.1, 3.2), RSS (3.3), least-squares estimates (3.4), (3.17)
- Kevin P. Murphy, Probabilistic Machine Learning: An Introduction (MIT Press, 2022), chapter 11: the normal equations
- scikit-learn: ordinary least squares