statsmodels is the library for answering the question *how sure are we?* about a regression. Where scikit-learn hands back predictions, statsmodels hands back a full report: the coefficients, their standard errors, t-statistics, p-values, confidence intervals and several measures of fit. You describe the model with a formula string. In smf.ols('ex ~ mkt_rf + hml', data).fit() the name left of the tilde is the response, the names on the right are the regressors, and an intercept is added for you (write - 1 to remove it). The array interface sm.OLS(y, X) does not add the intercept, so you call sm.add_constant(X) yourself, and forgetting this is the most common slip.
For one regressor the arithmetic is short enough to do by hand. With Sxx=∑(xi−xˉ)2 and Sxy=∑(xi−xˉ)(yi−yˉ), the slope is β^=Sxy/Sxx and the intercept is α^=yˉ−β^xˉ. The residuals are ei=yi−α^−β^xi, and their sum of squares is SSE. The noise level is estimated by s2=SSE/(n−k) where k counts the fitted coefficients, here 2. The standard error of the slope is s2/Sxx, the t-statistic is β^ divided by that, and R2=1−SSE/SST where SST=∑(yi−yˉ)2 is the total variation around the mean.
Five months by handMarket excess returns x=−2,−1,0,1,2 (in percent) and stock excess returns y=−3,0,1,2,5. Then xˉ=0, yˉ=1, Sxx=10 and Sxy=8+1+0+1+8=18, so β^=1.8 and α^=1. The fitted values are −2.6,−0.8,1,2.8,4.6, the residuals are −0.4,0.8,0,−0.8,0.4 and SSE=1.6, while SST=16+1+0+1+16=34. So R2=1−1.6/34=0.953. With n−k=3 the noise estimate is s2=1.6/3=0.5333, the standard error is 0.5333/10=0.2309 and t=1.8/0.2309=7.79. On real data, Apple's monthly excess return against the market's, 319 months from February 2000 to August 2026, gives β^=1.31 with standard error 0.112 (t=11.7) and an intercept of 1.5% a month (t=2.9), but R2 is only 0.30.
Formula first: y ~ a + b fits an intercept and two slopes, - 1 drops the intercept, and sm.OLS needs add_constant.
Read the table in order: coefficient, standard error, t-statistic, p-value, interval, then R2 and the sample size.
Lab · what's graded
The slope by hand15 pts
The intercept10 pts
The R-squared15 pts
The t-statistic20 pts
Read the summary table10 pts
A regression workflow10 pts
Precise but small20 pts
0/ 7 answered
1Numeric answer15 ptsNot answered
Market excess returns are −2,−1,0,1,2 and stock excess returns are −3,0,1,2,5. With Sxx=10, what is β^=Sxy/Sxx? First find Sxy=∑(xi−xˉ)(yi−yˉ). Give one decimal.
b=accepted: ± 0.02
2Numeric answer10 ptsNot answered
With β^=1.8, xˉ=0 and yˉ=1, what is the intercept α^=yˉ−β^xˉ?
a=accepted: ± 0.01
3Numeric answer15 ptsNot answered
The residuals of the fit are −0.4,0.8,0,−0.8,0.4, so SSE=1.6, and the total variation around the mean is SST=34. What is R2=1−SSE/SST? Give three decimals.
r=accepted: ± 0.002
4Numeric answer20 ptsNot answered
The noise estimate is s2=SSE/(n−2)=0.5333 and Sxx=10. What is t=β^/s2/Sxx for β^=1.8? Use 0.05333=0.2309 and give two decimals.
t=accepted: ± 0.05
5Match10 ptsNot answered
Match each item of the summary with what it tells you.
Pick the partner of each item on the left.
coef
std err
t
R-squared
6Order the steps10 ptsNot answered
Put the steps of fitting a factor regression for one stock in order.
Drag to reorder, or focus an item, press Space to lift it, the arrow keys to move it and Space to drop it.
1Read the summary table
2Fit the formula with ols and fit
3Subtract the risk-free rate to get excess returns
4Look at the residuals before trusting it
5Line up stock and factor returns by month
7Explain in words20 ptsNot answered
A stock's beta has t=11.7 yet the regression has R2=0.30. In one or two sentences, explain why these two facts do not contradict each other.
Math modeKeyboard or LaTeX inputTask 1 of 7 · Ctrl ⏎ to check