σμ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
  • 日本語
  • 中文
Derivatives Pricing skill›Module 1 · Rates, bonds and swaps›Topic 1.6·~55 minIntermediate

Swaps and forward rates

Skill positionModule 1 of 6 · Topic 6 of 7
1.6 Swaps and forward rates
—Lab score— / 100Total XP—Streak—
Lecture0% read

9 min readCode examples: 5

  1. 01From a par curve to discount factors
  2. 02Forward rates
  3. 03A swap is two legs
  4. 04What one basis point does
  5. 05The Treasury curve as a swap curve
  6. 06What you will build

On 3 January 2022 the 5-year US Treasury par yield was 1.37%; on 9 October 2026 it is 5.02%. A five-year swap struck at the old rate is worth 158,401 to the side that pays fixed, on a notional of one million, and one more basis point of interest rate adds another 390 to it. This topic builds the machinery that turns a curve into those numbers.

01

From a par curve to discount factors

Where we are. The Python course priced a bond by discounting its cash flows, and read yields and curves. This skill goes from there to the products banks actually trade. The first step is to write a curve in the form every later topic uses: a list of discount factors.

DefinitionDiscount factor

P_i is the price today of 1 paid in i years. If P_5 = 0.80, a promise of 100 in five years costs 80 today. Pricing anything that pays fixed amounts at known dates is a sum of amounts times discount factors, so once you have the P_i you can price almost everything.

Markets do not quote discount factors. The US Treasury quotes par yields: the coupon rate at which a bond of that maturity costs exactly its face value of 1. A 3-year bond with an annual coupon c_3 pays c_3 at the end of each of the three years and 1 at the end of the third, and costs 1. Written with discount factors, that sentence is an equation.

c_n × (P_1 + P_2 + ... + P_n) + P_n = 1 so P_n = (1 − c_n × (P_1 + ... + P_(n−1))) / (1 + c_n)(1)one equation per year n; the right-hand side only needs the discount factors of the earlier years, so solving for them one year at a time is called bootstrapping

Worked example 1. *Problem:* the par rates of years 1, 2 and 3 are 2%, 3% and 4%. What are the discount factors? *Plan:* year 1 has no earlier factor; each later year uses the sum of the factors found so far. *Steps:* below.

main.pyPython
par = np.array([0.02, 0.03, 0.04])discount = bootstrap_discount(par)1print(discount.round(6).tolist())2print([round(float(par[k] * discount[:k + 1].sum() + discount[k]), 6) for k in range(3)])
[0.980392, 0.942319, 0.887588]
[1.0, 1.0, 1.0]
  1. 1One factor per year, found one after the other.
  2. 2Every par bond must cost 1.

*Check:* P_1 = 1 / 1.02 = 0.980392; P_2 = (1 − 0.03 × 0.980392) / 1.03 = 0.942319; P_3 = (1 − 0.04 × (0.980392 + 0.942319)) / 1.04 = 0.887588. The second line prices the three par bonds with these factors and gets 1.0 each time. *What to notice:* the factors fall faster than the rates alone suggest because each year's coupon is discounted with the earlier factors. A flat par curve of 5% gives P_n = 1 / 1.05^n; a rising one gives smaller factors further out.

Where the six yields become ten

Treasury quotes six tenors (1, 2, 3, 5, 7 and 10 years). A bond that pays once a year needs a par rate for every year, so the lab first joins the quoted points with straight lines: that is interpolate_par. Treasury par yields are really quoted with half-yearly coupons; the lab uses annual ones so that every formula stays short.

02

Forward rates

A discount factor for five years tells you what five years of waiting costs, but not what year five alone costs. The forward rate f_i is the interest rate for year i that the curve implies today: the rate at which you could lock in today to lend from the end of year i − 1 to the end of year i.

f_i = P_(i−1) / P_i − 1 with P_0 = 1(2)lending 1 for i years at the curve and rolling it year by year must give the same money as the one-step factor: (1 + f_1) × ... × (1 + f_n) = 1 / P_n

Worked example 2. *Problem:* what forward rates are hidden in the curve of example 1? *Plan:* divide each factor by the next one and subtract 1. *Steps:* below.

main.pyPython
forwards = forward_rates(discount)1print(forwards.round(6).tolist())print(round(float(np.prod(1 + forwards)), 6), round(float(1 / discount[-1]), 6))
[0.02, 0.040404, 0.061662]
1.126649 1.126649
  1. 1The rate implied for each single year.

*Check:* the forwards are 2.00%, 4.04% and 6.17%, while the par rates are 2%, 3% and 4%. Compounding the three forwards gives 1.126649, the same as 1 / P_3. *What to notice:* a rising curve means the future years cost more than the average: the forward for year 3 is far above the 3-year par rate. Forwards are what the market expects plus a price for waiting, and they are the building block of every rate product.

03

A swap is two legs

An interest-rate swap is an agreement to exchange interest payments on a notional amount that is never paid. One side pays a fixed rate K agreed today; the other pays the floating rate, which is reset each year to the market rate. The side that pays fixed is called the *payer*; the other is the *receiver*.

DefinitionSwap legs

The fixed leg is a bond-like stream: K per year for n years, worth K times the annuity P_1 + ... + P_n. The floating leg is worth 1 − P_n per unit of notional: a floating-rate bond that pays the market rate is always worth its face value, so receiving the floating payments for n years is worth 1 now minus the notional you would get back at the end, P_n.

value to the payer of fixed = notional × ( (1 − P_n) − K × (P_1 + ... + P_n) )(3)the receiver of fixed has the opposite sign; the swap is worth nothing at the par swap rate K = (1 − P_n) / (P_1 + ... + P_n)

Worked example 3. *Problem:* on the curve of example 1, what is the par swap rate for 3 years, and what is a 3-year swap with a fixed rate of 3.5% on one million worth to each side? *Plan:* compute the annuity, the two legs and subtract. *Steps:* below.

main.pyPython
print(round(annuity(discount, 3), 6), round(swap_rate(discount, 3), 6), round(swap_rate(discount, 2), 6))1print(round(1e6 * (1 - discount[2]), 2), round(1e6 * 0.035 * annuity(discount, 3), 2))2print(round(swap_value(discount, 0.035, 3, 1e6), 2), round(swap_value(discount, 0.035, 3, 1e6, pay_fixed=False), 2))print(round(swap_value(discount, swap_rate(discount, 3), 3, 1e6), 6))
2.810299 0.04 0.03
112411.96 98360.46
14051.49 -14051.49
0.0
  1. 1The annuity and the par swap rates.
  2. 2The floating and the fixed leg of the 3.5% swap.

*Check:* the annuity is 2.810299. The par swap rate for 3 years is 4.00% and for 2 years 3.00%: they equal the par rates we started from, as they must, because a swap is a bond in disguise. The floating leg of the 3.5% swap is worth 112,411.96 and the fixed leg 98,360.46, so the payer of fixed gains 14,051.49 and the receiver loses the same amount. At the par swap rate the value is 0. *What to notice:* the 3.5% swap pays less fixed than the market rate of 4%, so it is an asset to the payer: the gain is about 0.5% times the annuity times the notional.

Real swap curves

Banks build their swap curve from overnight rates (SOFR) and swap quotes, not from Treasuries, and they add small adjustments. The mechanics you write here are the same; only the input curve changes. Using the Treasury curve keeps the lab on real, public data.

04

What one basis point does

A trader who owns a swap asks the same question about every position: *how much do I gain or lose if rates move a little?* The standard answer is the DV01: the change in value for a rise of one basis point (0.0001, a hundredth of a percent) in the curve. It is found by bumping the quoted rates, building the curve again, and valuing the same swap on both curves.

Worked example 4. *Problem:* what is the DV01 of the 3.5% swap of example 3 for the payer of fixed? *Plan:* shift all par rates by 0.0001, bootstrap again, and take the difference of the two values. *Steps:* below.

main.pyPython
print(round(swap_dv01(par, 0.035, 3, 1e6), 2))1print(round(swap_value(bootstrap_discount(par + 0.0001), 0.035, 3, 1e6) - swap_value(discount, 0.035, 3, 1e6), 2))
278.28
278.28
  1. 1The function and the same thing written out.

*Check:* both lines give 278.28: a rise of one basis point in all rates adds 278 to the value of this swap for the payer of fixed, who is paying a rate that is now further below the market. *What to notice:* rates up helps the payer of fixed, and the amount is close to the annuity (2.81) times the notional times 0.0001 (281), a little less because every discount factor also falls.

05

The Treasury curve as a swap curve

Worked example 5. *Problem:* build the discount curve and the swap rates for three days: 3 January 2022, 3 July 2023 and the last day of the file, and value a swap struck in January 2022 on the last day. *Plan:* study interpolates and bootstraps each day, reads the 2-, 5- and 10-year swap rates and the forwards, and values a 5-year swap struck at the January 2022 rate. *Steps:* below.

main.pyPython
treasury = load_treasury()out = study(treasury)1for date, curve in out["curves"].items():    print(date, [round(curve[k], 4) for k in ("swap_2y", "swap_5y", "swap_10y")], [round(curve["forwards"][i], 4) for i in (0, 4, 9)])print(round(out["value"], 2), round(out["dv01"], 2), round(out["par_value"], 6) + 0.0)
2022-01-03 [0.0078, 0.0137, 0.0163] [0.004, 0.0206, 0.0189]
2023-07-03 [0.0494, 0.0419, 0.0386] [0.0543, 0.0338, 0.0325]
2026-10-09 [0.048, 0.0502, 0.0524] [0.0447, 0.0532, 0.0567]
158400.8 389.98 0.0
  1. 1The curve, the forwards and one old swap, from real yields.

*Check:* in January 2022 rates were near zero: swap rates of 0.78%, 1.37% and 1.63% for 2, 5 and 10 years. In July 2023 the curve was inverted: 4.94% for 2 years but 3.86% for 10, and the forwards fall from 5.43% in year 1 to 3.25% in year 10, which is the market pricing rate cuts. On 9 October 2026 the curve rises again: 4.80%, 5.02% and 5.24%. A swap struck at 1.37% in 2022 is now worth 158,400.80 to the payer of fixed on one million, with a DV01 of 389.98; a swap struck at today's rate is worth 0. *What to notice:* the swap did not change; the curve did. Everything you computed came from the discount factors, which is why a bank can value ten thousand swaps from one curve.

Interpolation is a model

Between the quoted tenors the lab draws straight lines in par yields. Another interpolation (on zero rates, or on the log of discount factors) gives slightly different forwards, and forwards are sensitive to it. Two banks with the same quotes can value the same swap a few basis points apart because of such choices.

  • FunctionQuestion it answers
  • interpolate_par(tenors, yields)What is the par rate of every year?fractions, years 1 to N
  • bootstrap_discount(par)What does 1 paid in n years cost?every par bond costs 1
  • forward_rates(discount)What rate does the curve imply for each year?P_(i-1) / P_i - 1
  • annuity(discount, n)What does 1 per year for n years cost?sum of the first n factors
  • swap_rate(discount, n)Which fixed rate makes the swap worth nothing?equals the par rate here
  • swap_value(discount, K, n, notional, pay_fixed)What is the swap worth to each side?floating minus fixed
  • swap_dv01(par, K, n, notional)What does one basis point do?bump, bootstrap, revalue
What the real data saysUS Treasury par yields, daily, 2000 to October 2026; annual-pay swaps on a notional of 1,000,000
Swap rates, 3 Jan 20220.78 / 1.37 / 1.63 %2, 5 and 10 years: rates near zero
Swap rates, 3 Jul 20234.94 / 4.19 / 3.86 %an inverted curve; forwards fall from 5.43% to 3.25%
Swap rates, 9 Oct 20264.80 / 5.02 / 5.24 %an upward curve; forwards rise from 4.47% to 5.67%
A 2022 swap today158,4015 years, fixed 1.37%, paying fixed: in the money
Its DV01390per basis point, for the payer of fixed

Key takeaways

  • A curve is a list of prices. The discount factors P_i price every fixed cash flow; bootstrapping turns par yields into them one year at a time.
  • Forwards live inside the curve. f_i = P_(i-1) / P_i - 1 is the rate for year i that you can lock in today; a falling forward curve is the market pricing cuts.
  • A swap is two legs. The floating leg is worth 1 - P_n and the fixed leg K times the annuity; the par swap rate makes them equal and a swap struck elsewhere is worth the difference.
  • DV01 is a bump. Shift the quoted rates by one basis point, rebuild the curve, revalue: the change is the exposure per basis point.
Quick check
Your turn. A 2-year swap on a notional of 1,000,000 has a fixed rate of 4%. The discount factors are P_1 = 0.96 and P_2 = 0.92. What is it worth to the payer of fixed?
Pick an answer.
06

What you will build

The lab follows the lecture. Task 1 interpolates the par yields to one rate for each year. Task 2 bootstraps the discount factors, the heart of the topic. Tasks 3 to 5 read the forward rates, the annuity and the par swap rate off the factors. Task 6 values a swap for either side and task 7 finds its DV01 by bumping the curve. Task 8 runs everything on the real Treasury file for three days.

Next topic: swaps are a contract on interest. The next one, The forward price and the futures basis, prices a contract to buy an asset later, and shows why a future and a forward on the same thing can differ.

Lab · what's graded

8 tasks · 100 pts
  • Par rates for every year10 pts
  • Bootstrapping the discount factors20 pts
  • Forward rates10 pts
  • The annuity5 pts
  • The par swap rate10 pts
  • The value of a swap15 pts
  • The DV01 of a swap10 pts
  • Three days of the Treasury curve20 pts
Explorer · lab-dv-01
▾ lab-dv-01
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-dv-01$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-dv-01 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