A coefficient that moves
A regression gives one number per coefficient, as if the relation never changed. A stock's market beta does change: with its business, its leverage and the market's mood. A rolling regression refits the same model on a moving window of the last w observations and gives a time series of coefficients. In statsmodels this is RollingOLS(y, X, window=w).fit(); its params attribute is a DataFrame with one row per date and one column per coefficient, and the first w-1 rows are NaN because the window is not yet full. The window length is a trade-off: a short window reacts fast but is noisy, a long one is smooth but slow. For a single regressor and a constant, the slope has a closed form that needs no regression at all: the rolling covariance of y and x divided by the rolling variance of x, computed with .rolling(w).cov(...) and .rolling(w).var().
Apple has 319 monthly returns between February 2000 and August 2026. Its full-sample market beta is 1.31. The 60-month rolling beta tells another story: it was 2.51 in September 2008, fell to 0.75 in April 2015, and stands at 1.04 at the end of the sample, with a standard deviation of 0.31 along the way. No single number describes that. The two routes, RollingOLS and the rolling covariance over the rolling variance, agree to 10⁻¹⁴, which is a good check that you understand what the library does. A moving coefficient is also a warning: a risk model that uses last decade's beta may be hedging a company that no longer exists.
- Rolling means refitting: same model, moving window, one set of coefficients per date.
- Check the library with plain pandas: for one regressor the slope is rolling covariance over rolling variance.
- A single beta hides the story: the range of the rolling beta shows how much the full-sample number misleads.
Lab · what's graded
- The rolling beta25 pts
- The same beta by hand20 pts
- The gap between two routes10 pts
- Lowest and highest beta15 pts
- The Apple study30 pts