Linear regression — fitting a line through returns
▸ 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 for , linear regression fits the best straight line:
Where:
- is the intercept
- is the slope
- is the residual at observation
"Best" usually means ordinary least squares (OLS): minimize the sum of squared residuals .
The OLS estimates have closed forms:
So beta is "how much moves with , in -units per -unit, normalized by the variance of ."
In finance: regressing on market returns
The single most-used regression in finance:
- = return on asset at time
- = market return (usually SPY or a broader index)
- = asset's idiosyncratic mean return (what you'd earn if market were flat)
- = asset's sensitivity to market
For AAPL on SPY since 2010, . For utilities, (less market-sensitive). For high-beta tech and biotech, can be 1.5–2.5. For long/short hedged portfolios, 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 and slope are related but different:
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 and , :
You can have high correlation but low beta (correlated but moves less than ) or low correlation but high beta (rarely co-moves but when it does, moves a lot).
— explained variance
measures the fraction of 's variance explained by the regression:
For a one-factor model, (the squared correlation).
- : regression explains nothing.
- : regression is a perfect fit (all residuals zero — usually a sign of overfitting or an identity).
- : regression explains half the variance.
For AAPL on SPY: . 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 :
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:
- Linearity — the true relationship is linear.
- No autocorrelation — residuals are independent over time.
- Homoscedasticity — residuals have constant variance.
- Normality of residuals — only needed for hypothesis tests, not the point estimate.
- 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:
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 cardWhat is the slope of an OLS regression in formula form?
⧉ Review cardWhat is beta in finance?
⧉ Review cardWhat's the relationship between correlation and regression slope?
⧉ Review cardWhat does R² = 0.5 mean?
⧉ Review cardWhy are OLS standard errors usually wrong for financial data?
Predict before the next lesson
Tomorrow: CAPM (D2-01), where we interpret the alpha and beta of the market regression economically. Predict:
- If a stock has positive alpha vs. the market, what does this imply about its risk-adjusted return?
- Why might a stock with β = 1.5 reasonably be expected to have higher returns than the market?
◈ 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
- bookWasserman (2004), All of Statistics — §13
- bookHamilton (1994), Time Series Analysis — §8
- bookTsay (2010), Analysis of Financial Time Series, 3e — §2.3
- bookGelman & Hill (2007), Data Analysis Using Regression — §3, 4