Bond Valuation and Analysis in Python
Joshua Mayhew
Options Trader
Recall the formula for the price of a zero coupon bond:
$PV = \frac{FV}{(1 + r)^n}$
We can rearrange this equation to solve for yield $(r)$:
$FV = PV \times (1 + r)^n$
$ \frac{FV}{PV} = (1 + r)^n$
$ \sqrt[n]\frac{FV}{PV} = (1 + r)$
$ \sqrt[n]\frac{FV}{PV} -1 = r$
Let's look at the same zero coupon bond from earlier working backwards:
What is the yield of this bond?
3 year zero coupon bond, price USD 90.19, face value USD 100:
$r = \sqrt[n]\frac{FV}{PV} -1$
ytm = (100 / 90.19) ** (1/3) - 1
print(ytm)
0.035
We will use ytm
for 'yield to maturity'.
Coupon bond formula:
$ PV = \frac{C}{(1 + r)^1} + \frac{C}{(1 + r)^2} + ... +\frac{C}{(1 + r)^n} + \frac{P}{(1 + r)^n}$
$ = \sum_{i=1}^n \frac{C}{(1 + r)^i} + \frac{P}{(1 + r)^n}$
This equation cannot be rearranged in terms of $r$
We use trial and error to find $r$
This is how the npf.rate()
function works
Let's consider our coupon paying bond earlier which:
What is its yield to maturity?
3 year coupon bond, 3% annual coupon with a price of USD 97.22:
import numpy_financial as npf
npf.rate(nper=3, pmt=3, pv=-97.22, fv=100)
0.04
Remember we need to set the PV to a negative number.
This is because the price of the bond is money we pay (negative cash-flow).
Bond Valuation and Analysis in Python