σμ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.4·~55 minIntermediate

A sparse tridiagonal solve for an option price

Skill positionModule 3 of 3 · Topic 4 of 10
3.4 A sparse tridiagonal solve for an option price
—Lab score— / 100Total XP—Streak—

Store only what is there

Pricing an option on a grid replaces a differential equation by a chain of linear systems. Each grid node is linked to its two neighbours only, so the matrix of the system is tridiagonal: three diagonals of numbers and zeros everywhere else. A dense n×n array stores n² numbers, almost all of them zero; a sparse format stores only the 3n-2 that matter. scipy.sparse.diags([lower, diag, upper], [-1, 0, 1], format='csc') builds such a matrix from its diagonals (the sub-diagonal has n-1 entries and so does the super-diagonal, which is why the lower diagonal is cut to start at the second row and the upper one to end before the last column). The CSC format is the one the sparse solvers want.

The implicit scheme steps the option value back in time by solving A V^new=V^old+boundary at every step. The matrix A does not change from one step to the next, so you factorise it once with scipy.sparse.linalg.splu(A) and each step is only lu.solve(rhs), a very cheap operation for a tridiagonal matrix. The boundary condition at S=0 (a put is worth Ke^(-rτ) there) enters the first equation, which is why the first right-hand side is corrected by lower[0] * K * exp(-r * m * dt). With a 1-year rate of 4.47% and σ=25% the closed-form at-the-money put is worth 7.6987; the scheme gives 7.6958 on a 200×200 grid and 7.6965 on a 400×400 grid, about 0.003 away. A 2000-node grid stores 5,995 numbers in sparse form instead of almost 4 million.

  • Store the diagonals, not the zeros: sparse.diags keeps 3n - 2 numbers instead of n squared.
  • Factorise once, solve many times: splu pays the cost up front, then every time step is one cheap solve.
  • Boundaries live in the right-hand side: the known value at the edge of the grid is moved to the other side of the equation.

Lab · what's graded

  • The three diagonals20 pts
  • The sparse matrix15 pts
  • Pricing with one factorisation30 pts
  • Sparse against dense10 pts
  • The pricing error5 pts
  • The study on a real rate20 pts
Explorer · lab-sp-20
▾ lab-sp-20
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-sp-20$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

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