CommencerCommencer gratuitement

Predicted vs. actual prices II

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 the previous exercise, you found the duration of the bond, and created a DataFrame with the actual bond prices at each level of yield. In this exercise, you will add columns to this DataFrame containing the predicted bond prices using duration, then plot the difference between the predicted price and the actual price.

numpy, numpy_financial, pandas, and matplotlib have already been imported for you as np, npf, pd, and plt, respectively, as well as the code from the previous exercise.

Cet exercice fait partie du cours

Bond Valuation and Analysis in Python

Afficher le cours

Instructions

  • Add the column yield_change with the current yield minus original yield.
  • Add the column price_change with the predicted bond price change using dollar duration.
  • Add the column predicted_price combining the original bond price with the price change.
  • Add a plot of bond yields against actual prices and against predicted prices on the same axes, and show the plot.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Add a column called yield_change with the current yield minus original yield
bond['yield_change'] = (bond['bond_yield'] - ____)

# Find the predicted bond price change using dollar duration then find predicted price
bond['price_change'] = -100 * ____ * bond['yield_change'] / 100
bond['predicted_price'] = ____ + bond['price_change'] 

# Plot bond yields against predicted and actual prices, add labels, legend, and display
plt.plot(bond['bond_yield'], bond['price'])
plt.plot(____, ____)
plt.xlabel('Yield (%)')
plt.ylabel('Price (USD)')
plt.legend(["Actual Price", "Predicted Price"])
____
Modifier et exécuter le code