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

Root finding by hand and with brentq

Skill positionModule 1 of 3 · Topic 6 of 8
1.6 Root finding by hand and with brentq
—Lab score— / 100Total XP—Streak—

Search for the number that zeroes the error

A bond price is easy to compute from a yield; the yield from a price is not. You solve f(y)=P(y)-market price=0 by searching. Newton's method follows the tangent: from a guess y it jumps to y-f(y)/f'(y), and near the answer the number of correct digits roughly doubles each step, so a handful of iterations is enough, but it needs the derivative and can wander off from a bad start. Bisection needs only a bracket [lo,hi] where f changes sign: it halves the bracket again and again, so it cannot fail, but it gains only one binary digit per step, 40 steps to shrink a bracket of width 65 below 10⁻¹⁰. Brent's method, scipy.optimize.brentq, keeps the safety of a bracket and adds interpolation steps, usually needing about as many function calls as a few bisections. With full_output=True it also returns a record whose function_calls field says how many times it evaluated your function.

The same tool inverts the Black–Scholes formula. The price of a call grows with volatility, so for each quoted price you ask brentq for the σ in [10⁻⁴,5] that reproduces it. That only works when the price is strictly between the intrinsic value max(S-Ke^(-rT),0) and the stock price S; outside, no volatility exists and the right answer is NaN, not an exception, so one bad quote does not stop a whole smile. On 9 October 2026 the 10-year Treasury yield is 5.24%, so a 5% annual 10-year bond costs 97.121. Newton recovers the yield in 4 iterations, Brent in 10 function calls and bisection in 40. The implied volatilities of 10%, 20%, 40% and 80% come back to better than 10⁻⁶. Knowing what each method costs and when it can fail is what lets you pick the right one for a whole curve of bonds or an option chain.

  • Newton is fast but needs a derivative and a decent start: it took 4 steps here.
  • Bisection cannot fail but is slow: 40 steps for a bracket of width 65.
  • Use brentq and handle the impossible cases: a bracket plus interpolation, and NaN when no solution exists.

Lab · what's graded

  • A bond price10 pts
  • Newton by hand20 pts
  • Bisection by hand15 pts
  • Brent with scipy15 pts
  • Three solvers side by side10 pts
  • Implied volatilities15 pts
  • The real-data study15 pts
Explorer · lab-sp-15
▾ lab-sp-15
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-sp-15$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

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