σμ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
  • 日本語
  • 中文
SciPy skill›Module 1 · Fitting and finding›Topic 1.2·~45 minIntermediate

Fitting return distributions and testing the fit

Skill positionModule 1 of 3 · Topic 2 of 8
1.2 Fitting return distributions and testing the fit
—Lab score— / 100Total XP—Streak—

One model for the daily return

Every risk number starts with a model of the daily return. The simplest model is a normal curve with the average and the standard deviation of the sample. The Student t model adds one number, the degrees of freedom ν: a small ν means heavy tails, and when ν is large the t curve turns into the normal one. scipy.stats gives every distribution the same four methods: fit finds the parameters by maximum likelihood, logpdf gives the log of the density, cdf gives the probability of falling below a value and ppf is its inverse, the quantile. Learn the four once and every distribution in SciPy works the same way, so changing the model is a one-word change.

To choose between models you need a score and a test. The log-likelihood log L=Σᵢlog f(rᵢ) adds up how likely the whole sample is under the model, and the Akaike criterion AIC=2k-2log L, with k the number of fitted parameters, charges a price for every extra parameter: lower is better. The Kolmogorov–Smirnov test measures the largest gap between the sample's cumulative curve and the model's. stats.kstest(r, dist.cdf, args=params) returns it with a p-value, and a tiny p-value says the sample does not look like a draw from that model. Because the parameters were fitted on the same sample the p-value is flattering, so use it to compare models, not as a final verdict. Finally the model becomes a risk number: the one-day value at risk is minus the quantile, -dist.ppf(alpha, *params), and the expected number of bad days is the sample size times dist.cdf(-level, *params).

What the real data saysSPY has 2,500 daily returns from September 2016 to August 2026. The Student t fit gives ν≈2.6, a very heavy tail, and an AIC 867 points lower than the normal's. The KS test rejects the normal curve (p-value about 10⁻²⁵) but not the t (0.19). The 1% value at risk is 2.57% under the normal and 3.12% under the t. Six days fell by more than 5%: a normal curve expects 0.01 such days in ten years, the t model 7.9. Over 3%, the count is 32 against 8.6 for the normal and 27 for the t.
  • Same four methods everywhere: fit, logpdf, cdf and ppf work for every scipy.stats distribution, so models are interchangeable.
  • Score, then test: AIC compares models with a price for parameters, the KS test asks whether the sample could come from a model at all.
  • Tail counts expose the model: count the days below -5% and compare them with n * cdf; a normal curve misses by a factor of hundreds.

Lab · what's graded

  • Daily returns of a column10 pts
  • Two fitted models15 pts
  • A log-likelihood10 pts
  • Comparing models with the AIC10 pts
  • A Kolmogorov–Smirnov p-value15 pts
  • A value at risk from a quantile10 pts
  • Observed and expected extreme days10 pts
  • The whole study20 pts
Explorer · lab-sp-12
▾ lab-sp-12
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-sp-12$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-sp-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