Plotting duration vs. the factor
Plotting a graph of duration against a factor such as maturity, coupons, or yields is a great way to see how the factor affects the duration of a bond.
In the video, we plotted a graph of duration against maturity. In this exercise, you are going to do the same thing for the coupon rate. You will use a 10 year bond with a yield of 5% and face value of USD 100.
numpy
, numpy_financial
, pandas
, and matplotlib
have already been imported for you as np
, npf
, pd
, and plt
, respectively.
This exercise is part of the course
Bond Valuation and Analysis in Python
Exercise instructions
- Create an array of coupons from 0 to 10 in increment sizes of 0.1, and convert to a
pandas
DataFrame. - Add four additional columns to the DataFrame;
price
,price_up
,price_down
, andduration
for the bond. - Plot a graph with
bond_coupon
on the x-axis andduration
on the y-axis.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create array of coupon rates and assign to pandas DataFrame
bond_coupon = np.arange(____, ____, ____)
bond = pd.DataFrame(____, columns=['____'])
# Calculate bond price, price_up, price_down, and duration
bond['price'] = -npf.pv(rate=0.05, nper=10, pmt=bond['bond_coupon'], fv=100)
bond['price_up'] = ____
bond['price_down'] = ____
bond['duration'] = (bond['____'] - bond['____']) / (2 * bond['____'] * 0.01)
# Plot coupon vs. duration, add labels & title, show plot
plt.plot(____, ____)
plt.xlabel('Coupon (%)')
plt.ylabel('Duration (%)')
plt.show()