σμ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
  • 日本語
  • 中文
QuantLib skill›Module 3 · Curves, instruments and engines›Topic 3.1·~1 hr 5 minAdvanced

Yield term structures and bootstrapping

Skill positionModule 3 of 5 · Topic 1 of 4
3.1 Yield term structures and bootstrapping
—Lab score— / 100Total XP—Streak—
Lecture0% read

9 min readCode examples: 5

  1. 01A curve is a function of time
  2. 02Forward rates
  3. 03Bootstrapping a curve from bond prices
  4. 04Curves from real Treasuries
  5. 05What you will build

On 9 October 2026 the 10-year Treasury par yield is 5.24%, yet the 10-year zero rate is 5.15%, and the market's forward rate for the five years between year 5 and year 10 is 5.38%. A 10-year bond with a 4% coupon is worth 90.476 when each cash flow is discounted with its own rate from the curve, and 90.536 at one flat yield. A pricing library does not hold one rate; it holds a curve that can give the discount factor of any date. This topic builds that curve from bond quotes.

01

A curve is a function of time

Where we are. The bond of topic 4 was discounted at a single yield, and the option of topic 5 used a single rate. But a rate for one year and a rate for ten years are different numbers. A yield term structure (a yield curve) lists the zero rate for each term: the rate of a payment of 1 made at that term and nothing before. From the zero rate comes the discount factor, D(t) = e^(−z(t) · t) with continuous compounding, as in topic 3.

A curve is known only at a few nodes, so you need a rule for the terms in between. The simplest is linear interpolation: join the nodes with straight lines. Outside the nodes keep the rate flat. QuantLib offers many rules, and the choice matters; here the zero rates are interpolated, not the discount factors.

D(t) = e^(−z(t) · t), z(t) = z_i + (t − t_i) ÷ (t_(i+1) − t_i) × (z_(i+1) − z_i) for t_i ≤ t ≤ t_(i+1)(1)D is the discount factor, z the zero rate read from the straight line between the two nodes around t; before the first node and after the last one z stays flat

Worked example 1. *Problem:* a curve has zero rates of 2%, 3%, 4% and 4.5% at 1, 2, 5 and 10 years. Find the zero rate at 0.5, 1.5, 3.5, 7 and 12 years, then the discount factor at 0.5, 3.5 and 12 years. *Plan:* use linear_interp and curve_discount. *Steps:* below.

main.pyPython
times, zeros = [1.0, 2.0, 5.0, 10.0], [0.02, 0.03, 0.04, 0.045]print([round(linear_interp(times, zeros, t), 5) for t in (0.5, 1.5, 3.5, 7.0, 12.0)])1print([round(curve_discount(times, zeros, t), 5) for t in (0.5, 3.5, 12.0)])2
[0.02, 0.025, 0.035, 0.042, 0.045]
[0.99005, 0.88471, 0.58275]
  1. 1Straight lines between the nodes, flat outside.
  2. 2exp(-z t) with the interpolated z.

*Check:* the zero rates are 0.02, 0.025, 0.035, 0.042 and 0.045: flat before the first node and after the last. The discount factors are 0.99005, 0.88471 and 0.58275; the last is e^(−0.045 × 12). *What to notice:* the curve is a function: ask it for any term and it answers, which is the one thing the pricing code needs.

02

Forward rates

A curve also tells you what the market expects to earn in the future. If one year costs 2% a year and two years cost 3% a year, then money locked for the second year earns more than 3%. The rate for that second year alone, fixed today, is the forward rate. It is the rate that makes the two routes equal: lock for t2 years, or lock for t1 years and then roll over at the forward rate. In discount factors, the forward rate is the growth of D(t1) into D(t2).

f(t1, t2) = ln( D(t1) ÷ D(t2) ) ÷ ( t2 − t1 ) = ( z2 t2 − z1 t1 ) ÷ ( t2 − t1 )(2)f is the continuously compounded forward rate between t1 and t2; z1 and z2 are the zero rates of the two terms

Worked example 2. *Problem:* on the curve of example 1, find the forward rates for years 1 to 2, 2 to 5, 5 to 10 and 1 to 10, and check that growing D(10) back by the forward from year 5 to year 10 gives D(5). *Plan:* use curve_forward. *Steps:* below.

main.pyPython
times, zeros = [1.0, 2.0, 5.0, 10.0], [0.02, 0.03, 0.04, 0.045]print([round(curve_forward(times, zeros, a, b), 5) for a, b in ((1.0, 2.0), (2.0, 5.0), (5.0, 10.0), (1.0, 10.0))])1print(round(curve_discount(times, zeros, 5.0) / curve_discount(times, zeros, 10.0), 5), round(math.exp(curve_forward(times, zeros, 5.0, 10.0) * 5.0), 5))2
[0.04, 0.04667, 0.05, 0.04778]
1.28403 1.28403
  1. 1Four forward rates from one curve.
  2. 2The two routes to year 10 are equal.

*Check:* the forwards are 0.04, 0.04667, 0.05 and 0.04778. From year 1 to 2 it is 2 × 3% − 1 × 2% = 4%. Both sides of the second line are 1.28403. *What to notice:* the forward rates are above the zero rates because the curve slopes up; on an inverted curve (zero rates falling with the term) they would be below.

03

Bootstrapping a curve from bond prices

Nobody quotes zero rates. The market quotes bonds, and the Treasury publishes par yields: the coupon rate at which a bond of that term is worth exactly 100. Bootstrapping turns these quotes into discount factors one term at a time. The one-year par bond pays 1 + c at the end of year 1 and is worth 1, so D1 = 1 ÷ (1 + c). The two-year par bond pays c in year 1 and 1 + c in year 2, so once D1 is known the only unknown is D2. Each new bond brings exactly one new unknown.

c · ( D1 + D2 + … + Dn ) + Dn = 1 so Dn = ( 1 − c · ( D1 + … + D(n−1) ) ) ÷ ( 1 + c )(3)c is the annual coupon rate of the n-year par bond and D1 … Dn are the discount factors of the years 1 … n

The quotes exist only for a few terms (1, 2, 3, 5, 7, 10 years), but the equations need a bond for every whole year. The par rate of the years in between is read by linear interpolation, with the same function as before. Once the discount factors are known, zero_rates turns each one into a zero rate, -ln(D) / n, and par_rate does the reverse: the coupon that makes an n-year bond worth par. If the bootstrap is right, the par rates of the curve give back the quotes.

Worked example 3. *Problem:* bootstrap par yields of 3%, 3.5% and 4.5% at 1, 2 and 5 years. Find the discount factors of the years 1 to 5, their zero rates, and the par rates of the curve. *Plan:* use bootstrap, zero_rates and par_rate. *Steps:* below.

main.pyPython
dfs = bootstrap([1, 2, 5], [0.03, 0.035, 0.045])1print([round(d, 5) for d in dfs])2print([round(z, 5) for z in zero_rates(dfs)])3print([round(par_rate(dfs, n), 5) for n in (1, 2, 3, 4, 5)])4
[0.97087, 0.93335, 0.89278, 0.84812, 0.79997]
[0.02956, 0.03449, 0.0378, 0.04118, 0.04464]
[0.03, 0.035, 0.03833, 0.04167, 0.045]
  1. 1One factor for each whole year up to 5.
  2. 2The factors.
  3. 3The zero rates.
  4. 4The par rates: the quotes, and the interpolated years between them.

*Check:* the discount factors are 0.97087, 0.93335, 0.89278, 0.84812 and 0.79997. The first is 1 ÷ 1.03 and the second is (1 − 0.035 ÷ 1.03) ÷ 1.035 = 0.93335. The zero rates are 0.02956, 0.03449, 0.0378, 0.04118 and 0.04464, and the par rates are 0.03, 0.035, 0.03833, 0.04167 and 0.045: the quotes come back, and the years between them lie on the straight line. *What to notice:* the zero rate of year 1 is ln 1.03 = 0.02956, the same 3% written with continuous compounding; from year 2 on the zero rates rise faster than the par yields, because a par yield is an average that includes early coupons discounted at lower rates.

Worked example 4. *Problem:* a flat 5% par curve must give a flat 5% curve of annual rates. Then price a 5-year bond with a 4% coupon on the curve of example 3 and at a flat yield of 4.5%. *Plan:* sum the discount factors. *Steps:* below.

main.pyPython
dfs = bootstrap([1, 2, 5], [0.03, 0.035, 0.045])flat = bootstrap([1, 10], [0.05, 0.05])print(round(flat[9], 5), round(1.05 ** -10, 5), round(zero_rates(flat)[9], 5))1print(round(4.0 * sum(dfs) + 100.0 * dfs[4], 4), round(sum(4.0 / 1.045 ** k for k in range(1, 6)) + 100.0 / 1.045 ** 5, 4))2
0.61391 0.61391 0.04879
97.7775 97.805
  1. 1A flat par curve is a flat yield.
  2. 2The bond from the curve, then at one flat yield.

*Check:* the ten-year factor of the flat curve is 0.61391, which is 1.05^(−10), and its zero rate is 0.04879 (ln 1.05, the same 5% written with continuous compounding). The bond is worth 97.7775 on the curve and 97.805 at the flat 4.5% yield. *What to notice:* the two prices are close but not equal: the curve discounts the early coupons at lower rates and the last payment at 4.46%, while the flat yield uses one rate for everything.

04

Curves from real Treasuries

Worked example 5. *Problem:* bootstrap the real Treasury par yields (1, 2, 3, 5, 7 and 10 years) on three days, 2 July 2007, 3 August 2020 and the last day of the table. For each day find the zero rates at 1, 5 and 10 years, the discount factor at 7.5 years, the forward rates for years 5 to 10 and 1 to 2, the largest repricing error of the input bonds, and the price of a 10-year 4% bond from the curve and at the flat 10-year yield. *Plan:* study combines the functions. *Steps:* below.

main.pyPython
out = study(load_data())for date, row in out.items():    print(date, round(row["zero_1"], 4), round(row["zero_5"], 4), round(row["zero_10"], 4), round(row["df_7_5"], 4), round(row["fwd_5_10"], 4), round(row["fwd_1_2"], 4), row["repricing"] < 1e-12, round(row["bond_curve"], 3), round(row["bond_flat"], 3))1
2007-07-02 0.0485 0.0479 0.049 0.6961 0.0501 0.0464 True 92.257 92.278
2020-08-03 0.0012 0.0022 0.0056 0.9684 0.0091 0.001 True 133.688 133.364
2026-10-09 0.0437 0.0491 0.0515 0.6848 0.0538 0.0502 True 90.476 90.536
  1. 1One row per day.

*Check:* in July 2007 the curve was flat: zero rates of 0.0485, 0.0479 and 0.049 at 1, 5 and 10 years, and a forward from year 5 to 10 of 0.0501. In August 2020 rates were close to zero and rose with the term: 0.0012, 0.0022 and 0.0056, with a forward of 0.0091 from year 5 to 10, and the 4% bond is worth 133.688 on the curve against 133.364 at a flat yield. On 9 October 2026 the zero rates are 0.0437, 0.0491 and 0.0515, the forward from year 5 to 10 is 0.0538, and the bond is 90.476 on the curve against 90.536 at a flat yield. The repricing check is true on all three days. *What to notice:* in 2020 the difference between the two prices is 32 cents on a bond that pays 4% when the market paid almost nothing: the shape of the curve matters most when it is steep.

The interpolation is a choice

Between two nodes the curve is a guess. Interpolating the zero rates, the discount factors or the logarithm of the discount factors gives the same value at the nodes and different values between them, and so different forward rates. The par yields here are also a simplification: the real Treasury quotes pay twice a year, and QuantLib builds the curve with helpers that know each instrument's own dates and conventions.

  • FunctionQuestion it answers
  • linear_interp(xs, ys, x)What is the value between two nodes?flat outside the nodes
  • curve_discount(times, zeros, t)What is a payment at t worth today?uses linear_interpQuantLib: YieldTermStructure.discount
  • curve_forward(times, zeros, t1, t2)What rate does the curve imply between two terms?uses curve_discountQuantLib: forwardRate
  • bootstrap(tenors, par_rates)Which discount factors price the par bonds?uses linear_interpQuantLib: PiecewiseLogLinearDiscount with bond helpers
  • zero_rates(dfs)What is the zero rate of each year?QuantLib: zeroRate
  • par_rate(dfs, n)Which coupon prices an n-year bond at par?the check of bootstrap
What the real data saysCurves bootstrapped from the 1, 2, 3, 5, 7 and 10-year Treasury par yields
10-year zero rate, 9 Oct 20265.15%par yield 5.24%
Forward rate, years 5 to 10, 9 Oct 20265.38%0.9% on 3 August 2020
10-year zero rate, 3 Aug 20200.56%par yield 0.56%
4% bond, curve against flat yield, 3 Aug 2020+0.32133.688 against 133.364
Repricing error0all six input bonds come back to par

Key takeaways

  • A curve is a function of time. It holds a few zero rates and answers for any term by interpolation, so every price can ask it for a discount factor.
  • A forward rate is read from two factors. The rate between t1 and t2 is the growth of D(t1) into D(t2); on a rising curve it is above the zero rates.
  • Bootstrapping goes one bond at a time. Each par bond brings one new discount factor, found from the factors before it.
  • Check by repricing. The par rates of the finished curve must give back the quotes you started with.
Quick check
Your turn. The zero rate (continuously compounded) is 3% for 1 year and 4% for 2 years. What is the forward rate for the second year?
Pick an answer.
05

What you will build

The lab follows the lecture. Task 1 is the linear interpolation with flat ends, and task 2 the discount factor of a curve. Task 3 is the forward rate between two terms. Task 4 bootstraps discount factors from par yields, task 5 turns them into zero rates, and task 6 finds the par rate that prices a bond at par. Task 7 builds curves from real Treasury yields on three days.

Next topic: Instruments and engines: the observer pattern. Look at the code you now have: the bond, the option and the curve are loose functions, and each one takes a pile of arguments. When the curve changes, you must call every pricing function again with the new factors, by hand. QuantLib solves this with a design: an instrument holds its terms, an engine prices it, and when a market quote changes everything that depends on it is told. The next topic rebuilds the bond and the option in that design.

Lab · what's graded

7 tasks · 100 pts
  • Linear interpolation8 pts
  • The discount factor10 pts
  • Forward rates10 pts
  • Bootstrapping26 pts
  • Zero rates8 pts
  • The par rate10 pts
  • Curves from Treasuries28 pts
Explorer · lab-sqt-06
▾ lab-sqt-06
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-sqt-06$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-sqt-06 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