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

Newey–West standard errors by hand

Skill positionModule 1 of 3 · Topic 7 of 10
1.7 Newey–West standard errors by hand
—Lab score— / 100Total XP—Streak—

When the errors remember

Ordinary standard errors assume that each error is independent of the others. Overlapping returns break that: the return of January to March and the return of February to April share two months, so their errors move together. The coefficients are still fine, but their classical standard errors are too small and the t-statistics too big. The Newey–West (HAC) covariance repairs this. It starts from the usual 'sandwich' (XᵀX)⁻¹ S (XᵀX)⁻¹ and builds the middle with the products xₜeₜ of each row and its residual: S=Σₜeₜ²xₜxₜᵀ+ Σₖ₌₁^LwₖΣₜeₜeₜ₋ₖ (xₜxₜ₋ₖᵀ+ xₜ₋ₖxₜᵀ), where the Bartlett weights wₖ=1-k/(L+1) shrink the older autocovariances towards zero and keep the matrix valid.

In statsmodels you get the same matrix with sm.OLS(y, X).fit(cov_type='HAC', cov_kwds={'maxlags': L}), and res.tvalues then uses the robust standard errors. Writing it by hand once is worth the effort: you see that with zero lags it collapses to White's heteroskedasticity-robust covariance, that positive autocorrelation makes the robust variance larger than the classical one, and that the choice of lags is a judgement (a common rule is about the length of the overlap). For the three-month returns of SPY, 256 overlapping observations from May 2005 to August 2026 average 2.78% and the classical t is 6.10. With Newey–West it falls to 4.18 with 2 lags and to 3.65 with 6 lags: still significant, but the evidence is weaker than the first number suggested.

  • Overlap means memory: overlapping returns have autocorrelated errors, so the classical t-statistic overstates the evidence.
  • The sandwich: Newey–West is (X'X)^-1 S (X'X)^-1 with S built from x_t e_t and Bartlett-weighted autocovariances.
  • Check the library against your formula: when a by-hand matrix equals the library's, you understand what the option does.

Lab · what's graded

  • Overlapping returns10 pts
  • The classical t-statistic10 pts
  • Bartlett weights10 pts
  • Newey–West by hand25 pts
  • HAC t-statistics with statsmodels15 pts
  • The SPY study30 pts
Explorer · lab-sx-13
▾ lab-sx-13
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-sx-13$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-sx-13 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