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

Minimising and root finding

Skill positionModule 1 of 3 · Topic 4 of 6
1.4 Minimising and root finding
—Lab score— / 100Total XP—Streak—

Two jobs: find the lowest point, find where a curve crosses zero

scipy.optimize solves two kinds of problems. The first is minimising a function f(x)f(x)f(x), that is, finding the input with the smallest output. You call minimize(f, x0) where x0 is your starting guess. The method BFGS follows the slope of fff downhill. Nelder-Mead needs no slope and just compares values. SLSQP also accepts bounds and constraints, such as weights that must add up to 1. The answer is only a local minimum, the bottom of the valley where you started, and a different x0 can give a different answer. Always check res.success.

The second problem is root finding: solving g(x)=0g(x)=0g(x)=0. brentq(g, a, b) needs a bracket, two points where ggg has opposite signs, and then it is sure to find a root by repeatedly halving the interval. After nnn halvings the interval is (b−a)/2n(b-a)/2^n(b−a)/2n wide. newton(g, x0, fprime) uses the slope of ggg and is much faster, but it needs a good start and can jump far away when the slope is nearly flat. In finance the yield to maturity of a bond is a root: it is the rate yyy that makes the bond's price formula equal to its market price. A Newton step is ynew=y−g(y)/g′(y)y_{\text{new}}=y-g(y)/g'(y)ynew​=y−g(y)/g′(y).

ynew=y−g(y)g′(y),g(y)=51+y+105(1+y)2−98,width after n halvings=b−a2ny_{\text{new}}=y-\frac{g(y)}{g'(y)},\qquad g(y)=\frac{5}{1+y}+\frac{105}{(1+y)^{2}}-98,\qquad \text{width after }n\text{ halvings}=\frac{b-a}{2^{n}}ynew​=y−g′(y)g(y)​,g(y)=1+y5​+(1+y)2105​−98,width after n halvings=2nb−a​(1)
The yield of a two-year bondA bond pays 555 after one year and 105105105 after two years, and it costs 989898. Start at y=6%y=6\%y=6%. The price formula gives 98.1798.1798.17, so g=0.17g=0.17g=0.17. The slope is g′=−5/1.062−210/1.063=−180.8g'=-5/1.06^{2}-210/1.06^{3}=-180.8g′=−5/1.062−210/1.063=−180.8. One Newton step gives y=0.06−0.17/(−180.8)=6.092%y=0.06-0.17/(-180.8)=6.092\%y=0.06−0.17/(−180.8)=6.092%, and brentq on the bracket [0,0.5][0,0.5][0,0.5] confirms the root 6.0923%6.0923\%6.0923%. For bisection on the bracket [0,1][0,1][0,1] to be within 0.0010.0010.001 you need 2n≥10002^{n}\ge10002n≥1000, so n=10n=10n=10 halvings. For a minimum, two assets with volatilities 202020 and 101010 and correlation 0.20.20.2 have a covariance of 404040, so the minimum-variance weight in the first is (100−40)/(400+100−80)=0.143(100-40)/(400+100-80)=0.143(100−40)/(400+100−80)=0.143. On real data, SPY and TLT over 5,4045{,}4045,404 days from February 2005 have volatilities of 19.0%19.0\%19.0% and 14.6%14.6\%14.6% a year and a correlation of −0.30-0.30−0.30. The minimum-variance weight in SPY is 40%40\%40%, giving a volatility of 9.7%9.7\%9.7%. For a function with two valleys, such as x4−3x2+xx^{4}-3x^{2}+xx4−3x2+x, minimize from x0=2x_0=2x0​=2 stops at 1.131.131.13 and from x0=−2x_0=-2x0​=−2 at −1.30-1.30−1.30, which is the lower one.
  • Minimise or solve: minimize finds a lowest point, brentq and newton find where a function is zero.
  • A bracket is a guarantee: brentq always converges when the signs differ, while newton is faster but needs a good start.
  • A minimum can be local: try several starting points and read res.success before trusting res.x.

Lab · what's graded

  • One Newton step15 pts
  • Bisection halvings10 pts
  • A minimum-variance weight15 pts
  • The right tool for each job15 pts
  • An implied-volatility workflow10 pts
  • A possibly local minimum10 pts
  • Why a bracket, and why Newton can fail25 pts
0/ 7 answered
Tasks
1Numeric answer15 ptsNot answered

For the bond above, at y=0.06y=0.06y=0.06 we have g=0.1666g=0.1666g=0.1666 and g′=−180.77g'=-180.77g′=−180.77. What is the new guess y−g/g′y-g/g'y−g/g′, in percent? Give three decimals.

y=y =y=accepted: ± 0.003
2Numeric answer10 ptsNot answered

You bracket a root in [0,1][0,1][0,1]. How many halvings nnn are needed so that the interval is at most 0.0010.0010.001 wide, that is 2n≥10002^{n}\ge10002n≥1000?

n=n =n=accepted: ± 0.01
3Numeric answer15 ptsNot answered

Two assets have volatilities 202020 and 101010 and covariance 404040. The weight in the first that minimises variance is w=(σb2−c)/(σa2+σb2−2c)w=(\sigma_b^{2}-c)/(\sigma_a^{2}+\sigma_b^{2}-2c)w=(σb2​−c)/(σa2​+σb2​−2c). What is www? Give three decimals.

w=w =w=accepted: ± 0.002
4Match15 ptsNot answered

Match each tool with the situation where it fits best.

Pick the partner of each item on the left.

brentq
newton
minimize (SLSQP)
minimize (Nelder-Mead)
5Order the steps10 ptsNot answered

Put the steps of backing out an implied volatility from an option price 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. 1Reprice the option with the answer to check it
  2. 2Call brentq on the bracket
  3. 3Pick a low and a high volatility whose errors differ in sign
  4. 4Write the pricing error as a function of volatility
6Choice10 ptsNot answered

minimize started at x0=2x_0=2x0​=2 on a curve with two valleys and returned x=1.13x=1.13x=1.13 with success=True. You suspect a deeper valley exists. What is the best next step?

7Explain in words25 ptsNot answered

In one or two sentences, explain why brentq asks for two points with opposite signs, and why Newton's method can fail even though it is faster.

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