Bond Valuation and Analysis in Python
Joshua Mayhew
Options Trader
The 'average' time taken to get your money back
Waiting longer = more exposed to interest rates
Duration is the derivative (rate of change) of price with respect to yield
The slope of the tangent line is the duration
We can investigate the different factors affecting duration by:
import numpy as np
import numpy_financial as npf
import pandas as pd
import matplotlib.pyplot as plt
bond_maturity = np.arange(0, 30, 0.1)
bond = pd.DataFrame(bond_maturity, columns=['bond_maturity'])
bond['price'] = -npf.pv(rate=0.05, nper=bond['bond_maturity'], pmt=5, fv=100)
bond['price_up'] = -npf.pv(rate=0.05 + 0.01, nper=bond['bond_maturity'], pmt=5, fv=100)
bond['price_down'] = -npf.pv(rate=0.05 - 0.01, nper=bond['bond_maturity'], pmt=5, fv=100)
bond['duration'] = (bond['price_down'] - bond['price_up']) / (2 * bond['price'] * 0.01)
plt.plot(bond['bond_maturity'], bond['duration'])
plt.xlabel('Maturity (Years)')
plt.ylabel('Duration (%)')
plt.title("Effect of Varying Maturity On Bond Duration")
plt.show()
The duration of a bond will increase for a:
Bond Valuation and Analysis in Python