σμ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 3 · Cross-sections and tails›Topic 3.2·~55 minAdvanced

Quantile regression for value at risk

Skill positionModule 3 of 3 · Topic 2 of 4
3.2 Quantile regression for value at risk
—Lab score— / 100Total XP—Streak—

Modelling the tail, not the mean

Least squares fits the mean: it minimises Σeₜ². The q-quantile of y given x is the value below which a share q of the outcomes fall, and quantile regression fits it by minimising the pinball loss Σρ_q(eₜ) with ρ_q(e)=max(q e, (q-1) e) and e=y-pred. An under-forecast costs q per unit, an over-forecast 1-q: the asymmetry pulls the line to the q-quantile. At q=0.5 the loss is half the absolute error and the fit is the median. QuantReg(y, sm.add_constant(X)).fit(q=0.05) fits the 5% quantile; res.params holds the intercept and slopes. Different quantiles can have different slopes, which is the point: the effect of x may be on the spread of the outcome, not on its centre.

Use 21-day annualised volatility to predict the next day's SPY return over 5,383 days. The slope is -0.096 at the 5% quantile, +0.087 at the 95% quantile and 0.001 at the median, while least squares finds 0.0008: the mean does not move with volatility, the tails do, in both directions. The 5% fit turns into a value at risk table, -(a+b·vol): 1.2% at a volatility of 10%, 2.2% at 20%, 6.3% at 60%. Judge it as you would a forecast. The pinball loss on the last 30% of days is 0.001295 against 0.001342 for the unconditional 5% quantile, a modest gain. The coverage, the share of days with a return below the predicted quantile, should be near 5% everywhere. The unconditional value at risk is breached on 2.2% of calm days and on 7.8% of stormy ones; the conditional one gives 6.7%, 4.3%, 2.8%: better balanced, not perfect, which is what out-of-sample honesty looks like.

  • Quantile regression minimises the pinball loss max(qe,(q-1)e) and fits any quantile, not only the mean.
  • Slopes can differ by quantile: volatility moves the tails of the next return and leaves its mean alone.
  • Check coverage overall and by regime: a 5% value at risk should be breached about 5% of the time everywhere.

Lab · what's graded

  • The quantile fit10 pts
  • Slopes by quantile10 pts
  • The fitted quantile5 pts
  • The pinball loss15 pts
  • The coverage5 pts
  • Coverage by volatility tercile15 pts
  • Values at risk10 pts
  • Out of sample15 pts
  • The quantile study15 pts
Explorer · lab-sx-20
▾ lab-sx-20
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-sx-20$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

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