Least-squares slope
The straight line that makes the squared vertical errors as small as possible. Its slope is the covariance of x and y divided by the variance of x.
01The formula
- the fitted slope: how much y moves, on average, per unit move in x
- the fitted intercept: the line's value when x is zero
- the i-th pair of observations, for example the market's and a stock's return on day i
- the averages of x and of y
- the sample covariance: how x and y move together
- the sample variance of x: how much x moves on its own
02See it move
03Where it is used
- Quant researcherRegresses next-day returns on a signal; the slope says how many percent of return each unit of signal is worth.
- Risk managerEstimates a stock's beta to the market as the slope of its returns on index returns.
- Execution traderFits price impact against order size to budget the cost of the next trade.
04Why traders use it
Nearly every number a quant quotes, from a beta to a hedge ratio to a signal's strength, is a slope fitted this way. Knowing it is just Cov/Var lets you compute it in your head and see when it will be unstable.
05Worked example
On 60 simulated days where the true slope is 0.80, Cov(x, y) = 1.577 and Var(x) = 1.596, so the fitted slope is 0.988 and the intercept 0.180. The gap from 0.80 is pure sampling noise.
06How it is built
- Write the line as and the squared error as .
- Set the derivative with respect to to zero: the line must pass through , so .
- Substitute and set the derivative with respect to to zero: .
- Divide both sides by : .
07The common mistake
Swapping x and y. Regressing y on x does not give the reciprocal of regressing x on y; both slopes shrink towards zero when the fit is noisy, so a hedge ratio depends on which way round you fit it.
08In Python and LaTeX
import numpy as np rng = np.random.default_rng(7) n, b, sigma = 60, 0.8, 0.8 x = 1.2 * rng.standard_normal(n) y = 0.2 + b * x + sigma * rng.standard_normal(n) beta = np.cov(x, y)[0, 1] / np.var(x, ddof=1) print(np.isclose(beta, np.polyfit(x, y, 1)[0])) # True: Cov/Var is the least-squares slope
\hat\beta = \frac{\operatorname{Cov}(x, y)}{\operatorname{Var}(x)} = \frac{\sum_i (x_i - \bar x)(y_i - \bar y)}{\sum_i (x_i - \bar x)^2}, \qquad \hat\alpha = \bar y - \hat\beta\,\bar x09Where it fits
Rather not code the maths yourself? Describe your trading idea and let AI build the bot.
Build a bot with AgenKitThe worked example and the model run the same code, so every number matches. Models simplify markets on purpose. This is education, not financial advice.