Get startedGet started for free

Predicted vs. actual prices I

Plotting the predicted prices of bonds for different levels of yields using duration, then comparing these predicted prices to the actual prices of the bond is a great way of visualizing the accuracy of duration.

In this exercise, you will begin by finding the duration of the bond, as well as the price of the bond at different yield levels. In the next exercise, you will find the predicted price from duration and plot the difference.

The bond you will consider is a 10 year bond paying an annual coupon of 5% and a yield to maturity of 5%.

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

View Course

Exercise instructions

  • Find the duration and dollar duration of the bond.
  • Create an array of bond yields from 0 to 10 in increments of 0.1 and convert this array to a pandas DataFrame called bond_yield.
  • Add the column price containing the bond price for each level of yield.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Price a 10 year bond with 5% coupon and 5% yield, reprice at higher and lower yields
price = ____
price_up = ____
price_down = ____

# Find the duration and dollar duration of the bond
duration = ____
dollar_duration = ____ * ____ * ____

# Create an array of yields from 0 to 10 in steps of 0.1, convert to DataFrame
bond_yields = np.arange(____, ____, ____)
bond = pd.DataFrame(____, columns=['bond_yield'])

# Add a column called price with the bond price for each yield level
bond['price'] = -npf.pv(rate=bond['bond_yield'] / 100, ____, ____, ____)
Edit and Run Code