σμ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 3 · Matrices, signals and structure›Topic 3.2·~55 minIntermediate

Correlated Monte Carlo with Cholesky and VaR

Skill positionModule 3 of 3 · Topic 2 of 10
3.2 Correlated Monte Carlo with Cholesky and VaR
—Lab score— / 100Total XP—Streak—

From independent noise to correlated assets

A random number generator gives independent draws. Assets are correlated, so a simulation of a portfolio needs draws with the right covariance matrix Σ. The Cholesky factorisation finds a lower-triangular matrix L with LLᵀ=Σ; then if z is a vector of independent standard normals, Lz has covariance exactly L I Lᵀ=Σ. In NumPy the draws arrive as rows, so you compute z @ L.T for an (n, d) array z. scipy.linalg.cholesky(cov, lower=True) does the factorisation and raises an error if the matrix is not positive definite, which is useful: it is the alarm that your covariance matrix has a problem. Seed the generator with np.random.default_rng(seed) and the experiment is repeatable.

With the simulator you can compute risk numbers. The simulated portfolio return of each draw is draws @ w, and the one-day value at risk at level α is minus the α-quantile of those returns (np.quantile). For normal draws it must agree with the variance–covariance formula -(wᵀμ+z_α√(wᵀΣw)), and for twelve assets it does: 2.07% by simulation and 2.08% by the formula, against 2.33% by history, because real returns have fatter tails than any normal model. Estimated covariance matrices can also fail: if you set the correlation of two assets to an impossible 1.2, the smallest eigenvalue turns negative (about -6×10⁻⁵) and Cholesky refuses the matrix. The standard repair uses scipy.linalg.eigh: lift every eigenvalue below a small floor up to the floor, rebuild the matrix and symmetrise it. The result is positive definite and differs from the original by only about 3×10⁻⁵.

  • Cholesky turns noise into correlation: z @ L.T has covariance cov when cov = L @ L.T.
  • Simulation and formula should agree for normal draws: the gap to history is the cost of fat tails.
  • A failed factorisation is information: repair a matrix that is not positive definite by lifting its small or negative eigenvalues.

Lab · what's graded

  • The Cholesky factor10 pts
  • Correlated draws20 pts
  • Monte Carlo value at risk25 pts
  • The parametric value at risk15 pts
  • Repairing a covariance matrix15 pts
  • The twelve-asset study15 pts
Explorer · lab-sp-19
▾ lab-sp-19
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-sp-19$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

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