σμ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
  • 日本語
  • 中文
Particle filtering and non-stationary time series skill›Module 3 · A market that keeps changing›Topic 3.3·~1 hr 40 minAdvanced

Capstone: tracking the beta and volatility of a stock

Skill positionModule 3 of 3 · Topic 3 of 3
3.3 Capstone: tracking the beta and volatility of a stock
—Lab score— / 100Total XP—Streak—

Following a stock's beta as it moves

In this capstone you build three trackers for the beta of Apple against SPY and compare them. The model is a random-walk beta: each day the stock return is the beta times the market return plus noise, and the beta itself drifts a little. The first tracker is a rolling regression over the last 60 days, which equals a hard window. The second is a Kalman filter, the exact answer when everything is bell-shaped. The third is a bootstrap particle filter that you write with its own weight normalisation, effective sample size and systematic resampling. An exponentially weighted estimator follows the volatility of what the market does not explain.

βₜ=βₜ₋₁+wₜ, yₜ=βₜ mₜ+eₜ, K=(P⁻mₜ)/(P⁻mₜ²+r)
What the real data saysOn the 5,404 days of Apple and SPY since 2005, the full-sample beta is 1.07. On the last day, the 60-day regression says 0.20, the Kalman filter 0.40 and the particle filter with 500 particles 0.43. Predicting each day's return from yesterday's beta, the mean squared errors are 2.379×10⁻⁴ for one fixed beta, 2.378×10⁻⁴ for the rolling beta, 2.354×10⁻⁴ for Kalman and 2.362×10⁻⁴ for the particles. Following the beta helps by about 1%, not more, because daily returns are mostly noise. The particle filter resamples on 107 days, and its effective sample size drops to about 7 of 500. The exponentially weighted volatility of the residuals is about 32% a year at the end of the data.
  • Same model, three solvers: a hard window, the exact Gaussian filter and a cloud of weighted particles.
  • Reproducible randomness: the random numbers come in as arguments, so the same inputs give the same answer.
  • Honest scoring: each method predicts a day's return with yesterday's beta only.
Simplifications to rememberThe beta is a random walk with a fixed step q=10⁻³ that was chosen by hand, and the noise is treated as normal with a constant variance, which ignores volatility clustering. The score uses the squared error of a single stock over one sample. In this linear Gaussian model Kalman is exact, so the particle filter can only match it, and its advantage appears when the noise is fat-tailed or the variance moves.

Lab · what's graded

  • Safe normalisation5 pts
  • Effective sample size5 pts
  • Systematic resampling10 pts
  • A rolling beta10 pts
  • Exponentially weighted volatility10 pts
  • The Kalman filter15 pts
  • The bootstrap particle filter25 pts
  • The beta study20 pts
Explorer · lab-pf-10
▾ lab-pf-10
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-pf-10$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-pf-10 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