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

Integration with quad and ODE solvers

Skill positionModule 2 of 3 · Topic 2 of 3
2.2 Integration with quad and ODE solvers
—Lab score— / 100Total XP—Streak—

Adding up a curve: areas and moving systems

An integral is the area under a curve. integrate.quad(f, a, b) computes ∫abf(x) dx\int_a^b f(x)\,dx∫ab​f(x)dx for a function you write, and returns the value together with an error estimate. It is adaptive: it samples more where the function changes fast. If you only have values at points, not a function, you add up thin slices instead. The trapezoid rule, np.trapezoid, joins neighbouring points by straight lines and adds the areas of the trapezoids. Simpson's rule, integrate.simpson, uses small parabolas and is much more accurate for smooth curves. A function with a kink, such as a call payoff max⁡(S−K,0)\max(S-K,0)max(S−K,0), breaks the smoothness that quad expects, so you tell it where the kink is with points=[K].

The second tool is for systems that move. An ordinary differential equation (ODE) says how fast something changes, for example a short interest rate pulled toward a long-run level θ\thetaθ at speed κ\kappaκ: drdt=κ(θ−r)\dfrac{dr}{dt}=\kappa(\theta-r)dtdr​=κ(θ−r). integrate.solve_ivp(f, [0, T], [r0]) follows it forward in time. The simplest hand method is the Euler step, rnew=r+h f(r)r_{\text{new}}=r+h\,f(r)rnew​=r+hf(r), which assumes the speed stays constant over a step of length hhh. In finance, integrals turn a forward-rate curve into a discount factor: D(T)=exp⁡ ⁣(−∫0Tf(t) dt)D(T)=\exp\!\big(-\int_0^T f(t)\,dt\big)D(T)=exp(−∫0T​f(t)dt), and they turn a payoff and a probability density into an expected value.

∫abf dx≈h(f02+f1+⋯+fn−1+fn2),rnew=r+h κ(θ−r),D(T)=e−∫0Tf(t) dt\int_a^b f\,dx\approx h\Big(\tfrac{f_0}{2}+f_1+\dots+f_{n-1}+\tfrac{f_n}{2}\Big),\qquad r_{\text{new}}=r+h\,\kappa(\theta-r),\qquad D(T)=e^{-\int_0^T f(t)\,dt}∫ab​fdx≈h(2f0​​+f1​+⋯+fn−1​+2fn​​),rnew​=r+hκ(θ−r),D(T)=e−∫0T​f(t)dt(1)
An area, a rate and a discount factorTake ∫01x2dx\int_0^1x^{2}dx∫01​x2dx with 444 slices (h=0.25h=0.25h=0.25). The heights are 000, 0.06250.06250.0625, 0.250.250.25, 0.56250.56250.5625 and 111, so the trapezoid rule gives 0.25⋅(0.5⋅0+0.0625+0.25+0.5625+0.5⋅1)=0.343750.25\cdot(0.5\cdot0+0.0625+0.25+0.5625+0.5\cdot1)=0.343750.25⋅(0.5⋅0+0.0625+0.25+0.5625+0.5⋅1)=0.34375, a little above the exact 1/31/31/3. Simpson's rule with just 333 points gives exactly 0.33330.33330.3333. Now let a rate start at r0=2%r_0=2\%r0​=2% with κ=0.5\kappa=0.5κ=0.5 and θ=4%\theta=4\%θ=4%. One Euler step of length h=1h=1h=1 gives r=0.02+0.5⋅0.02=3.0%r=0.02+0.5\cdot0.02=3.0\%r=0.02+0.5⋅0.02=3.0%, while the exact solution θ+(r0−θ)e−κ\theta+(r_0-\theta)e^{-\kappa}θ+(r0​−θ)e−κ is 2.787%2.787\%2.787% and solve_ivp agrees. A forward-rate curve f(t)=0.03+0.01tf(t)=0.03+0.01tf(t)=0.03+0.01t has area 0.080.080.08 from 000 to 222 years, so D(2)=e−0.08=0.9231D(2)=e^{-0.08}=0.9231D(2)=e−0.08=0.9231. For a call with S=K=100S=K=100S=K=100, volatility 20%20\%20% and one year, quad of the payoff times the normal density, with points at the kink, gives 7.96567.96567.9656, the Black–Scholes price. On real data, the Treasury 10-year yield of 5.24%5.24\%5.24% on 9 October 2026, read as a continuously compounded rate, gives D(10)=e−0.524=0.592D(10)=e^{-0.524}=0.592D(10)=e−0.524=0.592.
  • Function or points: use quad when you can write the function, and trapezoid or Simpson when you only have values.
  • Tell quad about kinks: a payoff such as max⁡(S−K,0)\max(S-K,0)max(S−K,0) needs points=[K] for a reliable answer.
  • Integrals price things: area under a forward curve gives a discount factor, and payoff times density gives an expected value.

Lab · what's graded

  • A trapezoid area10 pts
  • An Euler step10 pts
  • The exact solution15 pts
  • A discount factor15 pts
  • The right integrator15 pts
  • Pricing a call by integration10 pts
  • Why tell quad about the kink25 pts
0/ 7 answered
Tasks
1Numeric answer10 ptsNot answered

Heights 0, 0.0625, 0.25, 0.5625, 10,\ 0.0625,\ 0.25,\ 0.5625,\ 10, 0.0625, 0.25, 0.5625, 1 are spaced h=0.25h=0.25h=0.25 apart. The trapezoid rule is h (f0/2+f1+f2+f3+f4/2)h\,(f_0/2+f_1+f_2+f_3+f_4/2)h(f0​/2+f1​+f2​+f3​+f4​/2). What is the area? Give four decimals.

a=a =a=accepted: ± 0.001
2Numeric answer10 ptsNot answered

A rate has r0=2%r_0=2\%r0​=2%, κ=0.5\kappa=0.5κ=0.5 and θ=4%\theta=4\%θ=4%. One Euler step of length h=1h=1h=1 is r=r0+h κ(θ−r0)r=r_0+h\,\kappa(\theta-r_0)r=r0​+hκ(θ−r0​). What is rrr in percent? Give one decimal.

r=r =r=accepted: ± 0.05
3Numeric answer15 ptsNot answered

The exact solution is r(t)=θ+(r0−θ)e−κtr(t)=\theta+(r_0-\theta)e^{-\kappa t}r(t)=θ+(r0​−θ)e−κt. With e−0.5=0.6065e^{-0.5}=0.6065e−0.5=0.6065, what is r(1)r(1)r(1) in percent? Give three decimals.

r=r =r=accepted: ± 0.005
4Numeric answer15 ptsNot answered

A forward-rate curve is f(t)=0.03+0.01 tf(t)=0.03+0.01\,tf(t)=0.03+0.01t. Its area from 000 to 222 years is 0.080.080.08. What is the discount factor D(2)=e−0.08D(2)=e^{-0.08}D(2)=e−0.08? Give four decimals.

d=d =d=accepted: ± 0.0005
5Match15 ptsNot answered

Match each tool with the job it fits best.

Pick the partner of each item on the left.

quad
np.trapezoid
simpson
solve_ivp
6Order the steps10 ptsNot answered

Put the steps of pricing a call as an integral 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. 1Compare with the closed-form price
  2. 2Call quad with points at the strike
  3. 3Choose finite limits that cover almost all the probability
  4. 4Write payoff times density as one function
7Explain in words25 ptsNot answered

The call payoff max⁡(S−K,0)\max(S-K,0)max(S−K,0) bends at the strike. In one or two sentences, explain why quad should be given points=[K] there.

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