σμ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.5·~50 minIntermediate

Residual diagnostics in code

Skill positionModule 1 of 3 · Topic 5 of 10
1.5 Residual diagnostics in code
—Lab score— / 100Total XP—Streak—

Do not trust a table you have not tested

A regression summary always looks authoritative, and statsmodels hands you the tools to check it. The variance inflation factor, variance_inflation_factor(matrix, i), measures how well one regressor is explained by the others: it equals 1/(1-Rⱼ²), values above 5 to 10 mean the coefficients are unstable, and the matrix must include the constant column (so the first regressor sits at position 1). durbin_watson(resid) is near 2 when errors are uncorrelated and near 0 when they trail each other. het_breuschpagan(resid, exog) returns four numbers whose second is the p-value of the test that the error variance is constant. jarque_bera(resid) returns the statistic, the p-value, the skewness and the kurtosis, and acorr_ljungbox(resid, lags=[12]) returns a DataFrame with the column lb_pvalue. For all of them a small p-value is a warning.

Two more checks complete the set. Cook's distance, res.get_influence().cooks_distance[0], scores each observation by how much the fit would move if it were removed, so the months with the largest values are the ones that drive the result. And the spurious regression is the classic trap: regress the log price of Home Depot on the log price of Lowe's and you get R²=0.93 with a Durbin–Watson statistic of 0.002, because two trending series always look related. A high R² with a Durbin–Watson near zero is the sign that you regressed one random walk on another. For Apple's three-factor regression (258 months) the factor VIFs are 1.13, 1.13 and 1.02, Durbin–Watson is 1.94, the Breusch–Pagan p-value is 0.21 and the Jarque–Bera p-value 0.015; adding SPY beside the market factor gives VIFs of 186 and 177; the most influential months are September 2008, January 2022 and October 2008.

  • One number per worry: VIF for copies, Durbin–Watson and Ljung–Box for memory, Breusch–Pagan for spread, Jarque–Bera for shape, Cook's distance for single months.
  • Mind the constant: VIF needs the design matrix with its constant column, and the regressors then start at position 1.
  • A high R-squared proves nothing alone: two trending prices regress beautifully and the Durbin–Watson statistic gives it away.

Lab · what's graded

  • OLS with the array API10 pts
  • Variance inflation factors20 pts
  • Four residual tests20 pts
  • Influential months15 pts
  • A spurious regression15 pts
  • The whole diagnosis20 pts
Explorer · lab-sx-12
▾ lab-sx-12
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-sx-12$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-sx-12 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes

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