σμ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 2 · Models for time series›Topic 2.4·~1 hrAdvanced

State space models and the Kalman filter

Skill positionModule 2 of 3 · Topic 4 of 5
2.4 State space models and the Kalman filter
—Lab score— / 100Total XP—Streak—

Tracking something you cannot see

A state-space model separates what is real from what is measured. A hidden state μt\mu_tμt​, such as the true volatility level, moves on its own, and what you observe is that state plus noise. In the local-level model yt=μt+εty_t=\mu_t+\varepsilon_tyt​=μt​+εt​ with εt∼N(0,r)\varepsilon_t\sim N(0,r)εt​∼N(0,r) and μt=μt−1+ηt\mu_t=\mu_{t-1}+\eta_tμt​=μt−1​+ηt​ with ηt∼N(0,q)\eta_t\sim N(0,q)ηt​∼N(0,q). The measurement variance rrr says how noisy a reading is and the level variance qqq says how fast the truth drifts. In statsmodels the class UnobservedComponents(y, 'llevel') fits this model, and it estimates qqq and rrr by maximum likelihood, so you do not have to guess them.

The Kalman filter turns each new reading into an updated belief, in two steps. Predict: the mean stays and the uncertainty grows, P−=P+qP^{-}=P+qP−=P+q. Update: the gain K=P−/(P−+r)K=P^{-}/(P^{-}+r)K=P−/(P−+r) says how much of the surprise y−ay-ay−a to believe, the new mean is a+K(y−a)a+K(y-a)a+K(y−a) and the new uncertainty is (1−K)P−(1-K)P^{-}(1−K)P−. When a reading is missing you do only the predict step. The filter uses data up to today, while the smoother looks back from the whole sample to give the best estimate of every past state.

P−=P+q,K=P−P−+r,a′=a+K(y−a),P′=(1−K)P−P^{-}=P+q,\qquad K=\frac{P^{-}}{P^{-}+r},\qquad a'=a+K(y-a),\qquad P'=(1-K)P^{-}P−=P+q,K=P−+rP−​,a′=a+K(y−a),P′=(1−K)P−(1)
Three months by handStart with belief a=10a=10a=10, P=4P=4P=4 and take q=1q=1q=1, r=5r=5r=5. Month one: P−=5P^{-}=5P−=5, K=5/10=0.5K=5/10=0.5K=5/10=0.5, and a reading 121212 gives a=10+0.5⋅2=11a=10+0.5\cdot2=11a=10+0.5⋅2=11 and P=0.5⋅5=2.5P=0.5\cdot5=2.5P=0.5⋅5=2.5. Month two: P−=3.5P^{-}=3.5P−=3.5, K=3.5/8.5=0.4118K=3.5/8.5=0.4118K=3.5/8.5=0.4118, a reading 999 gives surprise −2-2−2, so a=11−0.4118⋅2=10.18a=11-0.4118\cdot2=10.18a=11−0.4118⋅2=10.18 and P=0.5882⋅3.5=2.06P=0.5882\cdot3.5=2.06P=0.5882⋅3.5=2.06. Month three has no reading: only the predict step runs, so aaa stays 10.1810.1810.18 and PPP grows to 2.06+1=3.062.06+1=3.062.06+1=3.06. On real data, take the log of SPY's annualised realised volatility for each of 259259259 months since February 2005. Maximum likelihood gives r^=0.0889\hat r=0.0889r^=0.0889 and q^=0.0449\hat q=0.0449q^​=0.0449, so q/r=0.50q/r=0.50q/r=0.50, and the steady-state gain is K=0.50K=0.50K=0.50: each new month moves the volatility estimate halfway to the latest reading. This is exactly an ARIMA(0,1,1) with moving-average coefficient −0.498=−(1−K)-0.498=-(1-K)−0.498=−(1−K) and the same log-likelihood −151.09-151.09−151.09. At the end of the sample the filtered level is 12.4%12.4\%12.4% annualised against a last reading of 11.7%11.7\%11.7%, with a standard error of 0.210.210.21 in logs, so a 95% band of about 8%8\%8% to 19%19\%19%.
Filtered or smoothedUse the filtered estimate for anything you could have known at the time, such as a trading rule or a backtest. The smoothed estimate uses future data and is only for describing the past.

Lab · what's graded

  • The predicted variance10 pts
  • The Kalman gain15 pts
  • The updated mean15 pts
  • The updated variance10 pts
  • The next month's gain15 pts
  • A missing reading10 pts
  • The signal-to-noise ratio25 pts
0/ 7 answered
Tasks
1Numeric answer10 ptsNot answered

The belief has variance P=4P=4P=4 and the level variance is q=1q=1q=1. What is the predicted variance P−=P+qP^{-}=P+qP−=P+q?

pp=pp =pp=accepted: ± 0.01
2Numeric answer15 ptsNot answered

With P−=5P^{-}=5P−=5 and measurement variance r=5r=5r=5, what is the gain K=P−/(P−+r)K=P^{-}/(P^{-}+r)K=P−/(P−+r)? Give one decimal.

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

The belief mean is a=10a=10a=10, the gain is K=0.5K=0.5K=0.5 and the reading is y=12y=12y=12. What is the new mean a+K(y−a)a+K(y-a)a+K(y−a)?

a=a =a=accepted: ± 0.01
4Numeric answer10 ptsNot answered

With P−=5P^{-}=5P−=5 and K=0.5K=0.5K=0.5, what is the new variance (1−K)P−(1-K)P^{-}(1−K)P−? Give one decimal.

p=p =p=accepted: ± 0.02
5Numeric answer15 ptsNot answered

The variance is now P=2.5P=2.5P=2.5 and q=1q=1q=1, r=5r=5r=5. What is the next gain K=(P+q)/(P+q+r)K=(P+q)/(P+q+r)K=(P+q)/(P+q+r)? Give three decimals.

k=k =k=accepted: ± 0.003
6Choice10 ptsNot answered

In a month with no reading, what does the Kalman filter do?

7Explain in words25 ptsNot answered

For SPY volatility the fitted ratio is q/r=0.50q/r=0.50q/r=0.50 and the gain is K=0.50K=0.50K=0.50. In one or two sentences, explain what a much smaller q/rq/rq/r would do to the filtered estimate.

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