Aan de slagGa gratis aan de slag

Plotting convexity vs. the factor

Another way to check what effect a certain factor has on bond convexity is to directly plot a graph of this factor against the bond's convexity.

In this exercise, you will price a 20 year bond with a 6% coupon and face value of USD 100, then find the convexity of this bond for different levels of yields.

numpy, numpy_financial, pandas, and matplotlib have already been imported for you as np, npf, pd, and plt, respectively.

Deze oefening maakt deel uit van de cursus

Bond Valuation and Analysis in Python

Cursus bekijken

Oefeninstructies

  • Create an array of bond yields from 0 to 20 in increments of 0.1 and convert to a pandas DataFrame.
  • Find the price of the bond, shift the yields up and down and reprice, then calculate the convexity of the bond.
  • Plot a graph of bond yields on the x-axis against convexity on the y-axis.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Create array of bond yields and covert to pandas DataFrame
bond_yields = np.arange(____, ____, ____)
bond = pd.DataFrame(____, columns=['bond_yield'])

# Find price of bond, reprice for higher and lower yields, calculate convexity
bond['price'] = -npf.pv(rate=bond['____'] / 100, nper=____, pmt=____, fv=____)
bond['price_up'] = ____
bond['price_down'] = ____
bond['convexity'] = (bond['____'] + bond['____'] - 2 * bond['____']) / (bond['____'] * 0.01 ** 2)

# Create plot of bond yields against convexity, add labels to axes, display plot
plt.plot(bond['____'], bond['____'])
plt.xlabel('Yield (%)')
plt.ylabel('Convexity')
____
Code bewerken en uitvoeren