QTQuant Terminal
A2-06A2·intermediate·~20 min

Linear regression — fitting a line through returns

statisticsregressionolsbetafactor-models

▸ Pretest — guess, even if you don't know

You regress AAPL daily returns on SPY daily returns and find slope = 1.2, intercept = 0.0001. What does the slope mean?

The setup

Given pairs of observations (xt,yt)(x_t, y_t) for t=1,,Nt = 1, \ldots, N, linear regression fits the best straight line:

yt=α+βxt+εty_t = \alpha + \beta x_t + \varepsilon_t

Where:

"Best" usually means ordinary least squares (OLS): minimize the sum of squared residuals εt2\sum \varepsilon_t^2.

The OLS estimates have closed forms:

β^=Cov(x,y)Var(x),α^=yˉβ^xˉ\hat\beta = \frac{\text{Cov}(x, y)}{\text{Var}(x)}, \qquad \hat\alpha = \bar{y} - \hat\beta \bar{x}

So beta is "how much yy moves with xx, in yy-units per xx-unit, normalized by the variance of xx."

In finance: regressing on market returns

The single most-used regression in finance:

ri,t=αi+βirm,t+εi,tr_{i,t} = \alpha_i + \beta_i \cdot r_{m,t} + \varepsilon_{i,t}

For AAPL on SPY since 2010, β1.1\beta \approx 1.1. For utilities, β0.5\beta \approx 0.5 (less market-sensitive). For high-beta tech and biotech, β\beta can be 1.5–2.5. For long/short hedged portfolios, β0\beta \approx 0 by construction.

This is the Capital Asset Pricing Model regression — we'll cover its economic interpretation in the next lesson (D2-01).

Correlation vs. slope

A frequent confusion: correlation ρ\rho and slope β\beta are related but different:

ρxy=Cov(x,y)σxσy,β=Cov(x,y)σx2=ρxyσyσx\rho_{xy} = \frac{\text{Cov}(x, y)}{\sigma_x \sigma_y}, \qquad \beta = \frac{\text{Cov}(x, y)}{\sigma_x^2} = \rho_{xy} \cdot \frac{\sigma_y}{\sigma_x}

Correlation is unitless and bounded in [−1, 1]. Slope has units of "y per x" and can be any real number.

If returns of AAPL and SPY have ρ=0.7\rho = 0.7 and σAAPL=1.5%\sigma_\text{AAPL} = 1.5\%, σSPY=1.0%\sigma_\text{SPY} = 1.0\%:

β=0.71.51.0=1.05\beta = 0.7 \cdot \frac{1.5}{1.0} = 1.05

You can have high correlation but low beta (correlated but yy moves less than xx) or low correlation but high beta (rarely co-moves but when it does, yy moves a lot).

R2R^2 — explained variance

R2R^2 measures the fraction of yy's variance explained by the regression:

R2=1Var(ε)Var(y)R^2 = 1 - \frac{\text{Var}(\varepsilon)}{\text{Var}(y)}

For a one-factor model, R2=ρ2R^2 = \rho^2 (the squared correlation).

For AAPL on SPY: R20.5R^2 \approx 0.5. About half the variance of AAPL is explained by the market; the rest is idiosyncratic (Apple-specific news).

Multiple regression — more factors

If you have multiple predictors x1,x2,,xkx_1, x_2, \ldots, x_k:

yt=α+β1x1,t+β2x2,t++βkxk,t+εty_t = \alpha + \beta_1 x_{1,t} + \beta_2 x_{2,t} + \cdots + \beta_k x_{k,t} + \varepsilon_t

OLS still has a closed form (the normal equations), but the math is easier in matrix notation. We'll meet this in Track A3 (linear algebra) and use it in Track D2 (factor models — Fama-French 3 and 5 factors, etc.).

A common mistake: regress AAPL on (SPY, momentum, value) and get coefficient estimates that "look reasonable." But if the factors are correlated with each other (multicollinearity), the individual coefficient estimates become unstable. We'll cover diagnostics in A2-07.

Code snippet (for laptop)

import statsmodels.api as sm
import yfinance as yf
import numpy as np

data = yf.download(["AAPL", "SPY"], start="2010-01-01")["Adj Close"]
returns = np.log(data / data.shift(1)).dropna()

# Need to add a constant for the intercept
X = sm.add_constant(returns["SPY"])
y = returns["AAPL"]

model = sm.OLS(y, X).fit()
print(model.summary())
# Look for: const (alpha), SPY (beta), R-squared, t-statistics, p-values

statsmodels gives you a full diagnostic printout — standard errors, t-stats, p-values, R². For Phase 1 we'll use statsmodels for almost all regressions; for production models, numpy.linalg.lstsq or scikit-learn are also options.

Assumptions OLS makes (and where they break)

OLS is "BLUE" (Best Linear Unbiased Estimator) under these assumptions:

  1. Linearity — the true relationship is linear.
  2. No autocorrelation — residuals are independent over time.
  3. Homoscedasticity — residuals have constant variance.
  4. Normality of residuals — only needed for hypothesis tests, not the point estimate.
  5. No multicollinearity among predictors.

Financial data breaks 2, 3, and often 5. The point estimates are still consistent — the standard errors are wrong, which means your t-statistics and p-values are wrong.

Fix: use Newey-West standard errors (corrects for autocorrelation), White / robust standard errors (corrects for heteroscedasticity), or both (HAC). statsmodels provides these via model.fit(cov_type='HAC', cov_kwds={'maxlags': 5}).

We'll cover diagnostics and corrections in A2-07.

Try it

Compute beta from the closed form — no statsmodels needed, just the covariance-over-variance formula:

▮ EXERCISE · a2-06-ex1

Implement beta(asset_returns, market_returns) = Cov(asset, market) / Var(market). Use np.cov(asset_returns, market_returns)[0, 1] for the covariance and np.var(market_returns, ddof=1) for the variance - np.cov defaults to ddof=1, so the denominators must match.

⧉ Review card
What is the slope of an OLS regression in formula form?
β̂ = Cov(x,y) / Var(x). The intercept is α̂ = ȳ − β̂x̄.
⧉ Review card
What is beta in finance?
The slope of a regression of an asset's returns on the market's returns. Measures sensitivity to market moves. β > 1 = more sensitive than market; β < 1 = less; β = 0 = market-neutral.
⧉ Review card
What's the relationship between correlation and regression slope?
β = ρ × (σ_y / σ_x). Correlation is unitless and bounded [-1, 1]; slope has units and is unbounded. They give different information.
⧉ Review card
What does R² = 0.5 mean?
The regression explains 50% of the variance of y. The remaining 50% is residual variance (noise or factors not in the model). For one predictor, R² = ρ².
⧉ Review card
Why are OLS standard errors usually wrong for financial data?
Financial returns violate homoscedasticity (vol clustering) and sometimes independence (autocorrelation). Point estimates are still consistent but standard errors are wrong — use HAC (Newey-West) or robust (White) corrections.

Predict before the next lesson

Tomorrow: CAPM (D2-01), where we interpret the alpha and beta of the market regression economically. Predict:

◈ Calibration check

Could you set up and interpret a single-factor regression on real return data?

1 = guessing · 5 = could teach it

⏻ End of lesson

Mark it read to book its 5 review cards into your deck.

Sources & further reading