Introduction
In our last lesson, you implemented the second-order Taylor expansion to numerically compare bond price changes. You saw how the duration-plus-convexity model provides a much more accurate estimate than the linear duration-only approximation, especially for significant shifts in yield. Today, we will bring this comparison to life visually.
Your objective is to create a plot that displays the actual bond price-yield curve alongside its first-order (duration) and second-order (duration + convexity) approximations. This visualization will offer a powerful and intuitive confirmation of the concepts we've explored, solidifying your understanding of how these crucial risk measures relate to the fundamental behavior of bond prices.
1. The Mathematics of Price Approximation
Recall the Taylor series expansion around a central yield, , which we used to approximate the percentage price change. We can also use it to approximate the new price level, , directly.
Given an initial price , Modified Duration , and Convexity , all calculated at :
-
Actual Price: The true price of the bond, calculated using the standard present value formula for any given yield . We'll denote this as .
-
Linear (Duration) Approximation: The first-order Taylor approximation, which represents the tangent line to the price-yield curve at .
Since we know that , we can substitute to get:
-
Quadratic (Duration + Convexity) Approximation: The second-order Taylor approximation, which adds a quadratic term to better match the curvature of the actual price function.
Using our definition of convexity, , we substitute to arrive at:
Your task will be to plot these three functions—, , and —on the same graph.
2. A Reference Visualization
Before you start coding, let's look at a professionally produced chart that illustrates exactly what you aim to create. This will serve as a clear visual target.
Relative Impact of Duration and Convexity on Bond Price Changes
This paper by Brooks and Livingston provides a concise analysis of bond price approximations. We'll focus on the introductory text and its key graphic.
Please examine Exhibit 1: Price-Yield Relation Based on Duration, Convexity and Actual on the second page. Notice how it plots three distinct curves: The solid "Actual" line shows the true, convex relationship between price and yield. The dashed "Duration" line is a straight line, tangent to the actual curve at a single point. The "++ Convexity" line is a parabola that closely tracks the actual price curve over a much wider range of yields. Pay attention to the accompanying text, which explains that adding higher derivative terms (like convexity) increases the accuracy of the approximation. This exhibit is precisely the output you will generate in the next step.
3. Python Implementation: Plotting the Curves
Now it's time to create your own version of that exhibit. You will write a Python script using a library like matplotlib to plot the three price functions.
You can leverage the functions you built in previous lessons:
generate_bond_cash_flows(maturity, principal, coupon_rate, freq)bond_price(ytm, cash_flows, times, freq)modified_duration(mac_dur, ytm, freq)(and its dependency,macaulay_duration)calculate_convexity(bond_price, cash_flows, times, ytm, freq)
Your Task
Write a Python script that performs the following steps:
- Set up parameters: Define a sample bond and a central yield ().
- Bond: 20-year maturity, $1000 principal, 8% coupon rate, semi-annual payments.
- Central Yield (): 8% (i.e., the bond starts at par).
- Calculate base metrics: At the central yield , calculate the initial price (), Modified Duration (), and Convexity ().
- Generate data for plotting:
- Create a range of yields (e.g., from 4% to 12%) to serve as the x-axis.
- For each yield in this range, calculate the corresponding price for all three functions: Actual, Duration Approx., and Duration + Convexity Approx.
- Plot the results: Use
matplotlibto plot the three price series against the yield range. Make sure to label your axes and curves clearly.
Here is a skeleton to guide your implementation.
import numpy as np
import matplotlib.pyplot as plt
# Assume your previously defined functions are available here:
# generate_bond_cash_flows, bond_price, macaulay_duration,
# modified_duration, calculate_convexity
# 1. Define Bond and Market Parameters
maturity = 20
principal = 1000
coupon_rate = 0.08
freq = 2
y0 = 0.08 # Central yield
# 2. Calculate Base Metrics at the central yield y0
cash_flows, times = generate_bond_cash_flows(maturity, principal, coupon_rate, freq)
P0 = bond_price(y0, cash_flows, times, freq)
mac_dur = macaulay_duration(P0, cash_flows, times, y0, freq)
D_mod = modified_duration(mac_dur, y0, freq)
C = calculate_convexity(P0, cash_flows, times, y0, freq)
print(f"Initial Price (P0): {P0:.2f}")
print(f"Modified Duration (D_mod): {D_mod:.4f}")
print(f"Convexity (C): {C:.4f}")
# 3. Generate data for plotting
# Create a range of yields around y0
y_range = np.linspace(y0 - 0.04, y0 + 0.04, 100)
delta_y = y_range - y0
# Calculate the three price curves
# a) Actual Price (re-price the bond at each yield)
actual_prices = [bond_price(y, cash_flows, times, freq) for y in y_range]
# b) Duration Approximation (Linear)
duration_approx_prices = P0 - D_mod * P0 * delta_y
# c) Duration + Convexity Approximation (Quadratic)
convexity_approx_prices = duration_approx_prices + 0.5 * C * P0 * (delta_y**2)
# 4. Plot the results
plt.style.use('seaborn-v0_8-darkgrid')
plt.figure(figsize=(12, 8))
plt.plot(y_range * 100, actual_prices, label='Actual Price (Full Re-pricing)', linewidth=2.5, color='blue')
plt.plot(y_range * 100, duration_approx_prices, label='Duration Approx. (Linear)', linestyle='--', color='red')
plt.plot(y_range * 100, convexity_approx_prices, label='Duration + Convexity Approx. (Quadratic)', linestyle=':', color='green', linewidth=2)
plt.title('Bond Price vs. Yield: Actual and Approximations', fontsize=16)
plt.xlabel('Yield to Maturity (%)', fontsize=12)
plt.ylabel('Bond Price ($)', fontsize=12)
plt.legend(fontsize=11)
plt.grid(True)
plt.show()
4. Interpreting the Visualization
When you run your script, the resulting plot should clearly illustrate the following key points:
- Tangent Line: The duration approximation is a straight line that is perfectly tangent to the actual price curve at the initial yield . For infinitesimally small changes in yield, it is a perfect approximation.
- Approximation Error: As you move away from in either direction, the linear approximation deviates from the actual price. This "error" is due to the bond's convexity. The duration-only model consistently underestimates the bond's price.
- The Superiority of the Quadratic Model: The duration-plus-convexity approximation, a parabola, "hugs" the actual price curve far more closely and over a much wider range of yields. It effectively captures the curvature that the linear model misses. This visually demonstrates why the convexity adjustment is so critical for accurate risk assessment, especially in volatile rate environments.
This visualization serves as the definitive intuitive proof for the entire module on duration and convexity. It moves the concepts from abstract formulas to a tangible, graphical representation of bond risk dynamics.
Conclusion
In this lesson, you successfully visualized the relationship between a bond's actual price-yield curve and its Taylor series approximations. By plotting the linear (duration) and quadratic (duration + convexity) models, you've created a powerful visual tool that demonstrates the limitations of duration in isolation and the significant improvement in accuracy gained by incorporating convexity.
Key Takeaways:
- The relationship between a bond's price and its yield is inherently non-linear (convex).
- Modified Duration provides a linear approximation of this relationship, which is only accurate for very small yield changes.
- Adding the convexity term creates a quadratic approximation that captures the curve's shape, resulting in a much more accurate estimate of price changes over a wider range of yields.
- Visualizing these functions provides a deep, intuitive understanding of these fundamental risk measures.
This lesson concludes our detailed study of duration and convexity. In the next module, we will build upon this foundation to explore practical, industry-standard risk metrics. You will start by deriving and implementing the Price Value of a Basis Point (PVBP), a metric that translates the abstract concept of duration into a concrete dollar value of risk.