σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
statsmodels skill›Module 1 · Regression and its checks›Topic 1.2·~50 minIntermediate

The formula API and the OLS summary

Skill positionModule 1 of 3 · Topic 2 of 8
1.2 The formula API and the OLS summary
—Lab score— / 100Total XP—Streak—

One line to fit, one table to read

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ˉ)2S_{xx}=\sum(x_i-\bar x)^2Sxx​=∑(xi​−xˉ)2 and Sxy=∑(xi−xˉ)(yi−yˉ)S_{xy}=\sum(x_i-\bar x)(y_i-\bar y)Sxy​=∑(xi​−xˉ)(yi​−yˉ​), the slope is β^=Sxy/Sxx\hat\beta=S_{xy}/S_{xx}β^​=Sxy​/Sxx​ and the intercept is α^=yˉ−β^xˉ\hat\alpha=\bar y-\hat\beta\bar xα^=yˉ​−β^​xˉ. The residuals are ei=yi−α^−β^xie_i=y_i-\hat\alpha-\hat\beta x_iei​=yi​−α^−β^​xi​, and their sum of squares is SSE\mathrm{SSE}SSE. The noise level is estimated by s2=SSE/(n−k)s^2=\mathrm{SSE}/(n-k)s2=SSE/(n−k) where kkk counts the fitted coefficients, here 2. The standard error of the slope is s2/Sxx\sqrt{s^2/S_{xx}}s2/Sxx​​, the t-statistic is β^\hat\betaβ^​ divided by that, and R2=1−SSE/SSTR^2=1-\mathrm{SSE}/\mathrm{SST}R2=1−SSE/SST where SST=∑(yi−yˉ)2\mathrm{SST}=\sum(y_i-\bar y)^2SST=∑(yi​−yˉ​)2 is the total variation around the mean.

β^=SxySxx,α^=yˉ−β^xˉ,s2=SSEn−k,t=β^s2/Sxx,R2=1−SSESST\hat\beta=\frac{S_{xy}}{S_{xx}},\qquad \hat\alpha=\bar y-\hat\beta\bar x,\qquad s^{2}=\frac{\mathrm{SSE}}{n-k},\qquad t=\frac{\hat\beta}{\sqrt{s^{2}/S_{xx}}},\qquad R^{2}=1-\frac{\mathrm{SSE}}{\mathrm{SST}}β^​=Sxx​Sxy​​,α^=yˉ​−β^​xˉ,s2=n−kSSE​,t=s2/Sxx​​β^​​,R2=1−SSTSSE​(1)
Five months by handMarket excess returns x=−2,−1,0,1,2x=-2,-1,0,1,2x=−2,−1,0,1,2 (in percent) and stock excess returns y=−3,0,1,2,5y=-3,0,1,2,5y=−3,0,1,2,5. Then xˉ=0\bar x=0xˉ=0, yˉ=1\bar y=1yˉ​=1, Sxx=10S_{xx}=10Sxx​=10 and Sxy=8+1+0+1+8=18S_{xy}=8+1+0+1+8=18Sxy​=8+1+0+1+8=18, so β^=1.8\hat\beta=1.8β^​=1.8 and α^=1\hat\alpha=1α^=1. The fitted values are −2.6,−0.8,1,2.8,4.6-2.6,-0.8,1,2.8,4.6−2.6,−0.8,1,2.8,4.6, the residuals are −0.4,0.8,0,−0.8,0.4-0.4,0.8,0,-0.8,0.4−0.4,0.8,0,−0.8,0.4 and SSE=1.6\mathrm{SSE}=1.6SSE=1.6, while SST=16+1+0+1+16=34\mathrm{SST}=16+1+0+1+16=34SST=16+1+0+1+16=34. So R2=1−1.6/34=0.953R^2=1-1.6/34=0.953R2=1−1.6/34=0.953. With n−k=3n-k=3n−k=3 the noise estimate is s2=1.6/3=0.5333s^2=1.6/3=0.5333s2=1.6/3=0.5333, the standard error is 0.5333/10=0.2309\sqrt{0.5333/10}=0.23090.5333/10​=0.2309 and t=1.8/0.2309=7.79t=1.8/0.2309=7.79t=1.8/0.2309=7.79. On real data, Apple's monthly excess return against the market's, 319319319 months from February 2000 to August 2026, gives β^=1.31\hat\beta=1.31β^​=1.31 with standard error 0.1120.1120.112 (t=11.7t=11.7t=11.7) and an intercept of 1.5%1.5\%1.5% a month (t=2.9t=2.9t=2.9), but R2R^2R2 is only 0.300.300.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 R2R^2R2 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
Tasks
1Numeric answer15 ptsNot answered

Market excess returns are −2,−1,0,1,2-2,-1,0,1,2−2,−1,0,1,2 and stock excess returns are −3,0,1,2,5-3,0,1,2,5−3,0,1,2,5. With Sxx=10S_{xx}=10Sxx​=10, what is β^=Sxy/Sxx\hat\beta=S_{xy}/S_{xx}β^​=Sxy​/Sxx​? First find Sxy=∑(xi−xˉ)(yi−yˉ)S_{xy}=\sum(x_i-\bar x)(y_i-\bar y)Sxy​=∑(xi​−xˉ)(yi​−yˉ​). Give one decimal.

b=b =b=accepted: ± 0.02
2Numeric answer10 ptsNot answered

With β^=1.8\hat\beta=1.8β^​=1.8, xˉ=0\bar x=0xˉ=0 and yˉ=1\bar y=1yˉ​=1, what is the intercept α^=yˉ−β^xˉ\hat\alpha=\bar y-\hat\beta\bar xα^=yˉ​−β^​xˉ?

a=a =a=accepted: ± 0.01
3Numeric answer15 ptsNot answered

The residuals of the fit are −0.4,0.8,0,−0.8,0.4-0.4,0.8,0,-0.8,0.4−0.4,0.8,0,−0.8,0.4, so SSE=1.6\mathrm{SSE}=1.6SSE=1.6, and the total variation around the mean is SST=34\mathrm{SST}=34SST=34. What is R2=1−SSE/SSTR^2=1-\mathrm{SSE}/\mathrm{SST}R2=1−SSE/SST? Give three decimals.

r=r =r=accepted: ± 0.002
4Numeric answer20 ptsNot answered

The noise estimate is s2=SSE/(n−2)=0.5333s^2=\mathrm{SSE}/(n-2)=0.5333s2=SSE/(n−2)=0.5333 and Sxx=10S_{xx}=10Sxx​=10. What is t=β^/s2/Sxxt=\hat\beta/\sqrt{s^2/S_{xx}}t=β^​/s2/Sxx​​ for β^=1.8\hat\beta=1.8β^​=1.8? Use 0.05333=0.2309\sqrt{0.05333}=0.23090.05333​=0.2309 and give two decimals.

t=t =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.

  1. 1Read the summary table
  2. 2Fit the formula with ols and fit
  3. 3Subtract the risk-free rate to get excess returns
  4. 4Look at the residuals before trusting it
  5. 5Line up stock and factor returns by month
7Explain in words20 ptsNot answered

A stock's beta has t=11.7t=11.7t=11.7 yet the regression has R2=0.30R^2=0.30R2=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

Log in to run your code.

Every lecture is free to read. Create an account or log in to run code, run tests and submit this lab.

  • In-browser IDE with a bash terminal — nothing to install
  • Instant grading, XP and your place on the skill
Create an accountLog in
OR
Continue with GitHub