σμ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 2 · Curves and integrals›Topic 2.4·~50 minIntermediate

Integrals and ODEs with quad and solve_ivp

Skill positionModule 2 of 3 · Topic 4 of 5
2.4 Integrals and ODEs with quad and solve_ivp
—Lab score— / 100Total XP—Streak—

Expectations are integrals, dynamics are ODEs

Much of quantitative finance is one of two jobs. The first is an expectation, which is an integral: an option price is the discounted average of its payoff, e^(-rT)∫(S_T-K)⁺ φ(z) dz, and an expected shortfall is the average loss beyond a quantile. scipy.integrate.quad(f, a, b) returns the value and an error estimate, accepts -np.inf and np.inf as limits, and is accurate to many digits when the integrand is smooth. Where the integrand has a kink, as a call payoff does at the strike, start the integral at the kink instead of letting quad straddle it. Even when a closed form exists, computing the integral too is the best test that you understand the formula, and the integral still works for the payoffs that have no formula.

The second job is a dynamic: a quantity evolves according to a rule for its rate of change, an ordinary differential equation y'=f(t,y). scipy.integrate.solve_ivp(f, [t0, t1], y0, rtol=..., atol=...) follows it forward and returns the path; the last column of sol.y is the value at t₁. The Vasicek short-rate model gives a bond price P=e^(A(τ)-B(τ)r₀) whose two functions solve B'=1-κB and A'=-κθB+1/2σ²B² from zero, and the solver reproduces the textbook formula to 10⁻¹³ once you tighten the tolerances. The t distribution with 2.6 degrees of freedom has a 1% expected shortfall of 8.54 standard units, against 2.67 for the normal: the integral shows what the fat tail costs. From the fed funds rate of 3.88% the model's yield curve slopes gently from 3.96% at one year to 4.36% at thirty.

  • An expectation is an integral: quad handles infinite limits; start at the kink of a payoff.
  • A dynamic is an ODE: solve_ivp follows it from its starting values; tighten rtol and atol to match a formula.
  • Check numerics against a closed form: when both exist, compute both and look at the gap.

Lab · what's graded

  • A call price as an integral20 pts
  • An expected shortfall by integration15 pts
  • The closed form10 pts
  • A bond price from two ODEs25 pts
  • The Vasicek formula10 pts
  • The model yield curve10 pts
  • The study10 pts
Explorer · lab-sp-18
▾ lab-sp-18
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-sp-18$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

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