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

Interpolation and splines for curves

Skill positionModule 2 of 3 · Topic 1 of 3
2.1 Interpolation and splines for curves
—Lab score— / 100Total XP—Streak—

Drawing a smooth line through a few points

Markets quote a yield curve at a few maturities, such as 2, 3 and 5 years, but you need the value at any maturity, such as 2.5 years. Interpolation fills the gaps between known points. The simplest way is linear: draw a straight line between two neighbours with np.interp or interpolate.interp1d. A cubic spline, interpolate.CubicSpline, joins the points with pieces of cubic curve so that the slope and the curvature also match at every point, and the result looks smooth. A spline can overshoot between points. PchipInterpolator avoids that by never going above or below its neighbours. Inside the known range any of these is fine, but outside it you are extrapolating, which is a guess, and a cubic can then run away.

Which quantity you interpolate matters. Linear interpolation of yields is easy, but finance often interpolates the logarithm of the discount factor instead. With continuous compounding the discount factor is D(t)=e−y(t) tD(t)=e^{-y(t)\,t}D(t)=e−y(t)t, so ln⁡D(t)=−y(t) t\ln D(t)=-y(t)\,tlnD(t)=−y(t)t. Interpolating this linearly keeps forward rates constant between knots. The forward rate between times t1t_1t1​ and t2t_2t2​ is the rate you can lock in today for that future period: f=y(t2)t2−y(t1)t1t2−t1f=\dfrac{y(t_2)t_2-y(t_1)t_1}{t_2-t_1}f=t2​−t1​y(t2​)t2​−y(t1​)t1​​. A smooth, sensible forward curve is the real test of an interpolation.

D(t)=e−y(t) t,ln⁡D(t) linear between knots,f(t1,t2)=y(t2)t2−y(t1)t1t2−t1D(t)=e^{-y(t)\,t},\qquad \ln D(t)\ \text{linear between knots},\qquad f(t_1,t_2)=\frac{y(t_2)t_2-y(t_1)t_1}{t_2-t_1}D(t)=e−y(t)t,lnD(t) linear between knots,f(t1​,t2​)=t2​−t1​y(t2​)t2​−y(t1​)t1​​(1)
The 2.5-year yieldThe yield is 4.0%4.0\%4.0% at 222 years and 4.5%4.5\%4.5% at 333 years. Linear interpolation gives 4.25%4.25\%4.25% at 2.52.52.5 years. Interpolating ln⁡D=−yt\ln D=-ytlnD=−yt, which is −0.080-0.080−0.080 at 222 years and −0.135-0.135−0.135 at 333 years, gives −0.1075-0.1075−0.1075 at 2.52.52.5 years, so the yield is 0.1075/2.5=4.30%0.1075/2.5=4.30\%0.1075/2.5=4.30%. The forward rate between 222 and 333 years is (0.135−0.080)/1=5.5%(0.135-0.080)/1=5.5\%(0.135−0.080)/1=5.5%. A natural cubic spline through (0,0)(0,0)(0,0), (1,1)(1,1)(1,1) and (2,0)(2,0)(2,0) is S(x)=−0.5x3+1.5xS(x)=-0.5x^{3}+1.5xS(x)=−0.5x3+1.5x on [0,1][0,1][0,1], so S(0.5)=0.6875S(0.5)=0.6875S(0.5)=0.6875, while a straight line gives 0.50.50.5. On real data, the Treasury curve of 9 October 2026 has 5.24%5.24\%5.24% at 101010 years and 5.65%5.65\%5.65% at 202020 years. At 151515 years, linear interpolation gives 5.445%5.445\%5.445%, a cubic spline through all ten points gives 5.450%5.450\%5.450% and PCHIP gives 5.493%5.493\%5.493%. Beyond the data it goes wrong: the same spline at 404040 years says 4.26%4.26\%4.26%, although the 303030-year yield is 5.60%5.60\%5.60%.
  • Interpolate inside, be careful outside: linear, cubic spline and PCHIP agree inside the data and part ways outside it.
  • Choose what to interpolate: the log of the discount factor keeps forward rates steady between knots.
  • Check the forward curve: a good interpolation gives sensible forward rates, not just a pretty line through the points.

Lab · what's graded

  • A linear interpolation10 pts
  • Interpolating the discount factor15 pts
  • A forward rate15 pts
  • A spline value10 pts
  • The right tool15 pts
  • Building a curve10 pts
  • Why not extrapolate a cubic spline25 pts
0/ 7 answered
Tasks
1Numeric answer10 ptsNot answered

The yield is 4.0%4.0\%4.0% at 222 years and 4.5%4.5\%4.5% at 333 years. What does linear interpolation give at 2.52.52.5 years, in percent? Give two decimals.

y=y =y=accepted: ± 0.01
2Numeric answer15 ptsNot answered

With ln⁡D=−yt\ln D=-ytlnD=−yt equal to −0.080-0.080−0.080 at 222 years and −0.135-0.135−0.135 at 333 years, linear interpolation gives ln⁡D(2.5)=−0.1075\ln D(2.5)=-0.1075lnD(2.5)=−0.1075. What yield y=−ln⁡D/ty=-\ln D/ty=−lnD/t at 2.52.52.5 years does this mean, in percent? Give two decimals.

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

What is the forward rate between 222 and 333 years, f=(y3⋅3−y2⋅2)/(3−2)f=(y_3\cdot3-y_2\cdot2)/(3-2)f=(y3​⋅3−y2​⋅2)/(3−2), when y2=4.0%y_2=4.0\%y2​=4.0% and y3=4.5%y_3=4.5\%y3​=4.5%? Give one decimal, in percent.

f=f =f=accepted: ± 0.05
4Numeric answer10 ptsNot answered

The natural cubic spline through (0,0)(0,0)(0,0), (1,1)(1,1)(1,1) and (2,0)(2,0)(2,0) is S(x)=−0.5x3+1.5xS(x)=-0.5x^{3}+1.5xS(x)=−0.5x3+1.5x on [0,1][0,1][0,1]. What is S(0.5)S(0.5)S(0.5)? Give four decimals.

s=s =s=accepted: ± 0.001
5Match15 ptsNot answered

Match each tool with the situation where it fits best.

Pick the partner of each item on the left.

np.interp
CubicSpline
PchipInterpolator
bounds_error=False, fill_value
6Order the steps10 ptsNot answered

Put the steps of building a discount curve from quoted yields 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. 1Check the implied forward rates for jumps or negatives
  2. 2Interpolate between the knots
  3. 3Turn each yield into log discount factors
  4. 4Collect the quoted yields and maturities
7Explain in words25 ptsNot answered

The cubic spline through ten Treasury yields says the 40-year yield is 4.26%4.26\%4.26% although the 30-year is 5.60%5.60\%5.60%. In one or two sentences, explain why extrapolating a cubic spline is risky.

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