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

Distributions and fitting with scipy.stats

Skill positionModule 1 of 3 · Topic 1 of 6
1.1 Distributions and fitting with scipy.stats
—Lab score— / 100Total XP—Streak—

One object for every distribution

scipy.stats gives every probability distribution as one object, for example stats.norm, stats.t and stats.lognorm. They all share the same four methods. pdf(x) is the density, which says how likely values near xxx are. cdf(x) is the probability of being at or below xxx. ppf(p) is the inverse of the cdf: it returns the value xxx that has probability ppp below it, which is called a quantile. rvs(size) draws random numbers. Every distribution has a location (loc) and a scale (scale), so a value is loc + scale * standard_value. The call dist.fit(data) runs maximum likelihood, which means it picks the parameters that make your data most probable. For a normal distribution the fitted mean is the sample mean and the fitted standard deviation divides by nnn, not by n−1n-1n−1.

Daily returns have fat tails: big days happen far more often than a normal curve says. A Student t distribution with ν\nuν degrees of freedom has fatter tails, and a smaller ν\nuν means fatter tails. Its variance is ν/(ν−2)\nu/(\nu-2)ν/(ν−2) (it needs ν>2\nu>2ν>2), so to give it a chosen standard deviation σ\sigmaσ you use the scale s=σ(ν−2)/νs=\sigma\sqrt{(\nu-2)/\nu}s=σ(ν−2)/ν​. The one-day 99% value at risk (VaR) is the loss that is exceeded on only 1% of days. You get it from the 1% quantile of the returns.

VaR99=−(μ+σz0.01) with z0.01=−2.3263,VaR99t=−(μ+s t0.01,ν),s=σν−2ν\mathrm{VaR}_{99}=-(\mu+\sigma z_{0.01})\ \text{with}\ z_{0.01}=-2.3263,\qquad \mathrm{VaR}^{t}_{99}=-(\mu+s\,t_{0.01,\nu}),\quad s=\sigma\sqrt{\frac{\nu-2}{\nu}}VaR99​=−(μ+σz0.01​) with z0.01​=−2.3263,VaR99t​=−(μ+st0.01,ν​),s=σνν−2​​(1)
A 99% value at risk two waysTake a mean daily return of μ=0.05%\mu=0.05\%μ=0.05% and a standard deviation of σ=1.2%\sigma=1.2\%σ=1.2%. With a normal curve, z0.01=−2.3263z_{0.01}=-2.3263z0.01​=−2.3263, so VaR=2.3263⋅1.2−0.05=2.74%\mathrm{VaR}=2.3263\cdot1.2-0.05=2.74\%VaR=2.3263⋅1.2−0.05=2.74%. Now use a t distribution with ν=4\nu=4ν=4 and the same σ\sigmaσ. The scale is s=1.22/4=0.85s=1.2\sqrt{2/4}=0.85s=1.22/4​=0.85 and the 1% quantile of the standard t is −3.7469-3.7469−3.7469, so VaR=3.7469⋅0.85−0.05=3.13%\mathrm{VaR}=3.7469\cdot0.85-0.05=3.13\%VaR=3.7469⋅0.85−0.05=3.13%. The t curve gives a larger loss because it puts more weight far out. On real data, SPY has 5,4035{,}4035,403 daily returns with mean 0.039%0.039\%0.039% and standard deviation 1.196%1.196\%1.196%. The normal fit puts the 1% quantile at −2.74%-2.74\%−2.74%, but the real 1% quantile is −3.52%-3.52\%−3.52%. Its excess kurtosis is 14.614.614.6, and the best t fit has ν=2.45\nu=2.45ν=2.45. A fall of more than 4%4\%4% happened on 424242 days, where the normal curve expects about 222.
  • Same four methods: pdf for density, cdf for probability below, ppf for the quantile and rvs for random draws.
  • Fit, then check: fit gives the maximum likelihood parameters (a normal sigma divides by nnn), and you still compare the tail counts with the data.
  • Fat tails matter for risk: a t distribution with a small ν\nuν gives a bigger VaR than a normal with the same standard deviation.

Lab · what's graded

  • A normal VaR15 pts
  • The maximum likelihood sigma15 pts
  • The scale of a t distribution10 pts
  • The t distribution's VaR15 pts
  • The four shared methods10 pts
  • A tail-risk workflow10 pts
  • Why the normal understates the loss25 pts
0/ 7 answered
Tasks
1Numeric answer15 ptsNot answered

Daily returns have mean μ=0.05%\mu=0.05\%μ=0.05% and standard deviation σ=1.2%\sigma=1.2\%σ=1.2%. With a normal curve and z0.01=−2.3263z_{0.01}=-2.3263z0.01​=−2.3263, what is the 99% one-day VaR =−(μ+σz0.01)=-(\mu+\sigma z_{0.01})=−(μ+σz0.01​) in percent? Give two decimals.

var=var =var=accepted: ± 0.02
2Numeric answer15 ptsNot answered

Five daily returns (in percent) are −1, 3, 0, −2, 5-1,\ 3,\ 0,\ -2,\ 5−1, 3, 0, −2, 5. Their mean is 111. The fitted standard deviation of stats.norm.fit divides the sum of squared deviations by nnn. What is it? Give two decimals.

s=s =s=accepted: ± 0.02
3Numeric answer10 ptsNot answered

You want a t distribution with ν=4\nu=4ν=4 degrees of freedom and a standard deviation of σ=1.2\sigma=1.2σ=1.2. Use s=σ(ν−2)/νs=\sigma\sqrt{(\nu-2)/\nu}s=σ(ν−2)/ν​. What is the scale sss? Give two decimals.

s=s =s=accepted: ± 0.01
4Numeric answer15 ptsNot answered

With μ=0.05%\mu=0.05\%μ=0.05%, the scale s=0.85s=0.85s=0.85 and the 1% quantile of the standard t with ν=4\nu=4ν=4 equal to −3.7469-3.7469−3.7469, what is the 99% VaR =−(μ+s⋅(−3.7469))=-(\mu+s\cdot(-3.7469))=−(μ+s⋅(−3.7469)) in percent? Give two decimals.

var=var =var=accepted: ± 0.03
5Match10 ptsNot answered

Match each method of a scipy.stats distribution with what it returns.

Pick the partner of each item on the left.

pdf
cdf
ppf
rvs
6Order the steps10 ptsNot answered

Put the steps of estimating a daily VaR from prices in order.

Drag to reorder, or focus an item, press Space to lift it, the arrow keys to move it and Space to drop it.

  1. 1Read the 1% quantile with ppf
  2. 2Compare the number of extreme days with the data
  3. 3Turn prices into daily returns
  4. 4Fit a candidate distribution with fit
7Explain in words25 ptsNot answered

A normal fit puts the 1% quantile of SPY at −2.74%-2.74\%−2.74%, but the real one is −3.52%-3.52\%−3.52%. In one or two sentences, explain why the normal fit understates the 1% loss.

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