Introduction to Quant Finance · Lesson 22
Regression
Fit a straight line to data by making its squared misses as small as possible. Its slope is beta, its R² says how much of the spread it removes, and on a short sample or with many inputs it can fit pure noise.
Know the basics? Read the lesson notesFive days, one line
A day's return is what an asset gained or lost that day, in percent. Over five made-up days, a market returned −1%, 0%, +1%, +2% and +3%, and one stock returned −1%, −1%, +1%, +3% and +3%. The scatter plot below draws each day as a dot: across for the market, up for the stock.
A straight line through the dots is a guess at the stock's return from the market's. A day's miss is the stock's return minus the line's value: how far the dot sits above the line (below gives a negative miss). Each square has a miss as its side, so its area is that miss squared. Drag the line's two ends and make the total area as small as you can.
Five made-up days: the market and one stock
Drag the two round ends up or down (or select one and use the arrow keys). Colour: the squared misses.
Close. No straight line gets below 1.6: the dashed line, which rises 1.2% for each 1% of the market and sits at −0.2% where the market is 0%. The line that makes the sum of squared misses as small as possible is the least-squares line, and fitting it is called linear regression.
Why square the misses?
A flat line at the stock's average, +1%, misses the five days by −2, −2, 0, +2 and +2. The least-squares line misses by +0.4, −0.8, 0, +0.8 and −0.4. Add up each set of misses as they are, without squaring. Which total is smaller?
Misses above the line and below it cancel, so a plain sum can't tell a good line from a bad one. Squaring makes every miss count, and a miss twice as big counts four times as much, as in a standard deviation (how far numbers typically land from their average, built from squared distances). Squared, the flat line's misses add up to 4 + 4 + 0 + 4 + 4 = 16, the least-squares line's to 0.16 + 0.64 + 0 + 0.64 + 0.16 = 1.6.
Slope and intercept from scratch
You don't have to drag: the least-squares line has a formula. Its slope is how far the line rises for each 1 that x moves; its intercept is its value where x is 0. The slope is the covariance of x and y (their distances from their averages multiplied pair by pair, added up and divided by n − 1) divided by the variance of x (its squared distances from its average, added up and divided by n − 1). The n − 1 is a small-sample correction, and here it cancels.
- x, y
- the paired numbers, n pairs (here the market's and the stock's returns)
- x̄, ȳ
- their averages
- cov(x, y)
- the sum of (x − x̄)(y − ȳ), divided by n − 1
- var(x)
- the sum of (x − x̄)², divided by n − 1: the square of x's standard deviation
- b, a
- the slope and the intercept of the least-squares line
Averages: market (−1 + 0 + 1 + 2 + 3) ÷ 5 = 1, stock (−1 − 1 + 1 + 3 + 3) ÷ 5 = 1. Distances: market −2, −1, 0, +1, +2; stock −2, −2, 0, +2, +2. Products: 4, 2, 0, 2, 4, adding up to 12, so cov = 12 ÷ 4 = 3. Squared market distances: 4, 1, 0, 1, 4, adding up to 10, so var = 10 ÷ 4 = 2.5.
b = 3 ÷ 2.5 = 1.2, and a = 1 − 1.2 × 1 = −0.2: the line you found by dragging. Both divide by n − 1 = 4, so it cancels: b = 12 ÷ 10.
- you want the straight line that misses y least, counting each miss squared;
- every day counts the same.
The link is curved (the Correlation and diversification lesson showed a perfect U-shaped link with a correlation near 0). One extreme day pulls the line a lot, because its miss is squared.
New numbers. x is 1, 2 and 3; y is 2, 3 and 7. What is the slope?
Beta: the slope on the market
When x is a market's return and y a stock's, the slope has its own name, beta: how far the stock tends to move for each 1% the market moves. Below are 250 simulated days of one market and two stocks, each with its least-squares line. Their correlation with the market (from −1 to +1: how closely the dots follow a straight line) is 0.48 for stock A and 0.89 for stock B. Which stock has the larger beta?
One market, two stocks, 250 days (simulated)
One dot a day: across for the market, up for the stock. The two charts share one scale. Colour: the least-squares line.
Stock A · correlation 0.48
Stock B · correlation 0.89
Correlation says how closely the dots follow the line; beta says how steep it is. The two are linked. The correlation ρ ("rho") is cov ÷ (sx × sy), where sx and sy are the market's and the stock's standard deviations, and var(x) is sx², so beta = cov ÷ var = ρ × sy ÷ sx. Stock A was built with a beta of 1.5 and stock B with 0.5.
R²: the share of the spread the line removes
Before any line, the stock's spread is the sum of its squared distances from its own average: the flat line's 16. After the least-squares line, 1.6 is left. The share the line removed is R² ("R squared"). Textbooks say the line "explains" that share; it means only that the misses shrink on these days, not that x causes y.
- ŷ
- "y-hat", the line's value, a + b × x; so y − ŷ are its misses
- y − ȳ
- the distances from y's average: the flat line's misses
- R²
- for a least-squares line with an intercept, on the data it was fitted to: from 0 (no better than the average) to 1 (every dot on the line); with one input it equals the correlation squared
1 − 1.6 ÷ 16 = 1 − 0.1 = 0.9: on these five days the line removes 90% of the stock's spread. The correlation is 12 ÷ √(10 × 16) ≈ 0.95, and squared that is 144 ÷ 160 = 0.9.
- the line is fitted by least squares with an intercept;
- you read R² for the data the line was fitted to.
R² says nothing about cause, and on new data it is usually lower. An input added never lowers it on the data used, even an input of pure noise (two cards on).
New numbers. Over 100 days a stock's squared distances from its average add up to 40. Fitted on the market's returns, its squared misses add up to 30. What is R²?
A line through noise
Every line has a slope, even with nothing there. Take a made-up signal X and a stock's return Y with nothing between them: each day of each is its own bell-curve draw (most days near the average, fewer far out) with a standard deviation of 1%, so the true slope is 0. Call a slope "big" at 0.3 or more either way: the stock moving 0.3% for each 1% of the signal. Guess first: of 1,000 samples of 20 days, how many show a big slope?
1,000 samples of each length, true slope 0 (simulated)
Each column counts the samples whose measured slope landed in a range 0.05 wide. Colour: 0.3 or more either way.
You guessed about 10. It was 203.
You guessed about 200, the closest of the three. It was 203.
You guessed about 500. It was 203.
In this simulation, 203 of the 1,000 samples of 20 days showed a slope of 0.3 or more either way, with nothing there: about one in five. Of 1,000 samples of 500 days, 0 did. On a short sample the slope is mostly luck.
More inputs, a better fit, on the days used
A regression can take several inputs at once, each with its own slope; least squares picks them all together. Here a stock's return over 20 days is pure noise, and so are 19 made-up inputs: none says anything about the stock. The line is fitted on those 20 days, then tried on 20 new days. Guess first: as inputs are added, what does R² on the 20 days used do?
One sample: 20 days used, then 20 new days (simulated)
Dots: the stock's return each day. Colour: the fitted line's value. Shaded: the 20 days used for fitting.
You guessed it rises, and it does.
You guessed it stays near 0. It rises.
You guessed it falls. It rises.
An added input can only shrink the squared misses on the days used, because least squares could always give it a slope of 0. In this simulation R² on the days used never fell with an input added, in 1,000 of 1,000 samples, and reached 1.00 at 19 inputs: 20 numbers fitted to 20 days pass through every dot. On the 20 new days, the 10-input line missed by more than a flat line at the 20 days' average (no inputs at all) in 952 of 1,000. Fitting noise in the data used is called overfitting; the Overfitting lesson in Backtesting Essentials shows it in a backtest.
A hidden driver
Two more simulated series, X and Y, over 250 days. Each day a hidden series Z moves both: X is Z plus a move of its own, and Y is Z plus another, all three separate bell-curve draws of one size. X has no part in Y. Yet fit Y on X and the slope is 0.49 on these days, and 0.50 on average over 1,000 such samples.
X and Y, 250 days (simulated)
Give the regression Z as a second input, so X's slope is measured with Z's part counted separately. What will X's slope be?
On these days it was −0.03. Across 1,000 samples it averaged 0.00 and was 0.3 or more either way in 0, against 1,000 for the slope of Y on X alone. The figure's second view shows X and Y with Z's part removed from each.
A slope, like a correlation, can't tell X moving Y from a third series moving both, a confounder: correlation is not causation. Adding Z fixed it here only because Z was known and measured; nothing in X and Y alone shows that Z exists.
The slope is an estimate
Stock A, on the beta card, was built with a true beta of 1.5, and its 250 days measured 1.49. Now build 1,000 more samples of 250 days the same way and measure beta in each. Where do the middle 950 land?
Beta measured on 1,000 samples of 250 days, true beta 1.5 (simulated)
Each column counts the samples whose measured beta landed in a range 0.05 wide. Shaded: the middle 950, from 1.17 to 1.81.
A measured slope carries its own uncertainty. Its usual measure is the slope's standard error: the typical gap between a measured slope and the true one, about 0.16 here. And a beta measured on past days describes those days; these simulated stocks keep one beta throughout, so they can't show it changing.
For the precise
The measured beta landed within two standard errors of the true 1.5 in 953 of 1,000 samples, near the 95 in 100 the formula aims at when the days are independent (one day's result doesn't change the next's) and beta stays the same. The standard error of the slope is √[(sum of (y − ŷ)²) ÷ (n − 2) ÷ (sum of (x − x̄)²)], for independent days whose misses have one size throughout. For stock A's 250 days it is 0.17. Across the 1,000 samples the measured betas averaged 1.5 and ran from 0.95 to 2.09.What this means for your backtest
A backtest runs a trading rule over past prices to see what it would have done. Turn over three habits.
- Read a slope with its sample size.
On a short sample, a signal with nothing in it often shows a slope that looks big.
- Judge a fit on days it didn't see.
R² on the data used never falls with an input added, noise or not. Only new days show whether a fit holds.
- Ask what else moves both.
A slope can come from a hidden third series that moves both.
"It moves 0.6 times the market"
From these numbers, what is the stock's beta?
Hint: beta = ρ × sy ÷ sx, with the stock as y and the market as x.
"Our model explains 60%"
What does that R² tell you?
Lesson done
Linear regression fits the straight line whose squared misses add up to as little as possible. Its slope is the covariance of x and y divided by the variance of x; for a stock on a market it is called beta. R² is the share of the spread the line removes, on the data it was fitted to; it says nothing about cause. In this lesson's simulation a line through 20 days of noise showed a slope of 0.3 or more either way in 203 of 1,000 samples, and inputs of pure noise never lowered R² on the data used. A measured slope describes past days and carries its own uncertainty, its standard error.
Key ideas
- Least squares
- fitting a line by making the sum of its squared misses as small as possible
- Linear regression
- fitting a least-squares line: one input, or several, each with its own slope
- Slope / intercept
- how far the line rises for each 1 that x moves / the line's value where x is 0
- Beta
- the slope of a stock's returns on a market's: how far the stock tended to move for each 1% the market moved, over the days measured
- R²
- the share of y's spread (squared distances from its average) the line removes, on the data it was fitted to; not a measure of cause
- Overfitting
- a fit that matches noise in the data used, so it looks better there than on new data
- Confounder
- a third series that moves two others, giving one a slope on the other though neither moves the other
- Standard error of the slope
- the typical distance of a measured slope from the true one, estimated from one sample when the days are independent
Formulas
b = cov(x, y) ÷ var(x) = ρ × sy ÷ sx · a = ȳ − b × x̄
R² = 1 − sum of (y − ŷ)² ÷ sum of (y − ȳ)²
Both describe the data the line was fitted to; on a short sample, or with many inputs, they can come from noise.