Price, Duration, Convexity, and Yield to Maturity of Bond

Installing the Packages

Install the required packages that are important to run the code. The following code reads the list of packages from the ``packages” list provided. If the package is not installed, then it installs, and if already installed then it uses it directly.

if (!require(pacman)) install.packages("pacman")
## Loading required package: pacman
pacman::p_load(utils,ITNr,RColorBrewer,scales,graphsim,igraph,readxl,plyr,quantreg,bayesQR, spatstat,ggplot2,ggpubr, gtools, car, devtools,statar,reshape2,MASS,dynpanel,plm,dummies,ggridges,ggthemes)

Calculation of Bond Price

A bond issued for \(T\) years with a face value \(F\) and coupon rate \(c\) % per annum. If currently, the yield to maturity is \(r\) % per annum. The bond pays \(n\) times in a year, i.e., \(n=2\) the bond pays half-yearly. The price of the bond \(P\) is

\[P=\sum_{t=1}^T \frac{(c/n)F}{(1+r/n)^t} + \frac{F}{(1+r/n)^T}\] The code for the above formula is given below.

##### CALCULATION OF PRICE
Price=function(FV, CR, Y, M, fr){
TP=fr*M
Price= ( (CR*FV/(fr*100))/(1+Y/(fr*100)))*((1-(1/(1+Y/(fr*100))^(TP-1)))/(1-(1/(1+Y/(fr*100))))) + (FV/(1+Y/(100*fr))^TP)
sprintf("Price=%f", Price )
}

### AN EXAMPLE ####
FV= 1000   #Face Value
CR= 8   #Coupon Rate
Y=  8.1   # Yield
M=  10   # Maturing Time in Years
fr=  2   # Frequency of payment. Foe example, 2 means twice in a year
Price(1000,8,8.1,10,2)
## [1] "Price=975.153996"

Duration and Convexity of a Bond

The duration of a bond is a time period where the cashflow of the bond is equivalent to a zero-coupon bond.It is calculated as follows.Assuminga simple bond which pays annually, \[\frac{dP}{d(1+r)}=-\sum_{t=1}^T \frac{cFt}{(1+r)^{t+1}} + \frac{TF}{(1+r)^{T+1}}\] The Maculay duration is defined by \[\text{Maculay Duration(D)}=-\frac{dP}{P} \times \frac{1+r}{d(1+r)} =\sum_{t=1}^Ttw_t \ \ , \ \ w_t=\frac{cF}{P(1+r)^{t}}, w_T=\frac{cF+F}{P(1+r)^{T}}\] The modified duration is given by \[\text{Modified Duration (MdD)}=-\frac{dP}{dr} \times \frac{1}{P} =\sum_{t=1}^T \frac{cFt}{(1+r)^{t+1}} + \frac{TF}{(1+r)^{T+1}}\]

Noe let us discuss how this is equivalent to a Zero coupon bond. Suppose an individual invests in a above kind of bond by borrowing from a friend to who he will pay in the maturity \(M\). The current price of the bond is \(P\), so he will borrow \(P\) amount and will pay liability \(L=P(1+r)^M\) in the maturity. If interest rate increases value of \(P\) will fall, but her liability will increase. Similarly, if \(r\) falls, his liability will decrease but price of the bond will increase. Thus, there id atrade-off between \(P\) and \(r\). Thus, \[\frac{dL}{dr}=\frac{dL}{d(1+r)}=(1+r)^M\frac{dP}{d(r+1)}+PM(1+r)^{M-1}=(D-M)(1+r)^{M-1}P\] Thus the loss is zero if the buyer chooses a zero-coupon bond with maturity \(M=D\). Thus duration is helpful in managing the interest rate risk in asset-liability mismatch.

Convexity of a Bond

The convexity of a bond is the curvature of the price-yield curve. It shows the second order sensitivity of price to change in yield to maturity \[\text{Maturity}=\frac{1}{P}\frac{d^2P}{dr^2}\]

##### CALCULATION OF DURATION

Duration=function(FV, CR,r0,r1, M, fr){
Price=function(FV, CR, r0, M, fr){
TP=fr*M
Y=r0
Price= ( (CR*FV/(fr*100))/(1+Y/(fr*100)))*((1-(1/(1+Y/(fr*100))^(TP-1)))/(1-(1/(1+Y/(fr*100))))) + (FV/(1+Y/(100*fr))^TP)
}
P0=Price(1000,8,r0,10,2)
P1=Price(1000,8,r1,10,2)
D=(P1-P0)*(1+r0/(100))/((r0-r1)*P0)*100
sprintf("Duration=%g years", D)
}

### AN EXAMPLE ####
FV= 1000   #Face Value
CR= 8   #Coupon Rate
r0=  9.1   # Initial yield
r1= 9.0    # Final yield
M=  10   # Maturing Time in Years
fr=  2   # Frequency of payment. Foe example, 2 means twice in a year
Duration(1000, 8,9.1,9, 10,2)
## [1] "Duration=7.21932 years"
##### CALCULATION OF CONVEXITY  AT A POINT

Convexity=function(FV, CR,Y, M, fr){
Price=function(FV, CR,Y, M, fr){
TP=fr*M
Price= ( (CR*FV/(fr*100))/(1+Y/(fr*100)))*((1-(1/(1+Y/(fr*100))^(TP-1)))/(1-(1/(1+Y/(fr*100))))) + (FV/(1+Y/(100*fr))^TP)
}
Price=Price(FV, CR, Y,M,fr)
N=M*fr
c=rep(1, N)
for (i in 1:N-1){
c[i]=i*(i+1)*((CR*FV/(fr*100)))/( (1+ Y/(fr*100))^(i+2) )
c[N]=N*(N+1)*(((CR*FV+FV)/(fr*100)))/( (1+ Y/(fr*100))^(N+2) ) 
}
CN=sum(c)
CN=CN/Price
sprintf("Convexity=%g", CN)
}

### AN EXAMPLE ####
FV= 1000   #Face Value
CR= 8   #Coupon Rate
Y=  9.1   # Yield to maturity
M=  10   # Maturing Time in Years
fr=  2   # Frequency of payment. Foe example, 2 means twice in a year
Convexity(1000, 8, 9, 10,2)
## [1] "Convexity=64.7068"

Calculation of Yield to Maturity

Calculation of yield to maturity is simple reverse engineering of price calculation. In this case price of the bond \(P\) is given, based on non-linear equation, the yield to maturity \(r\) is calculated using the following equation \[\sum_{t=1}^T \frac{(c/n)F}{(1+r/n)^t} + \frac{F}{(1+r/n)^T}- P=0\]

##### CALCULATION OF YIELD TO MATURITY
Yield=function(FV,CR,M,fr,P){
TP=fr*M
f <- function (x) ( (CR*FV/(fr*100))/(1+x/fr))*((1-(1/(1+x/fr)^(TP-1)))/(1-(1/(1+x/fr)))) + (FV/(1+x/fr)^TP)-P
Y=uniroot(f, interval=c(0.001,1))
Y=Y$root*100
sprintf("Yield=%f",Y)
}

### AN EXAMPLE ####

FV= 1000   #Face Value
CR= 8  #Coupon Rate
M=  10   # Maturing Time in Years
fr=  2   # Frequency of payment. For example, 2 means twice in a year
P=  980    # Price
Yield(1000,8,10,2,980)
## [1] "Yield=8.026363"

Yield to Maturity of T-bills

Suppose a T-bill has a face value 100 and is issued for \(T \leq 365\) days. At time \(t\), the time to maturity is \(d=T-t\) days. Let \(r\) be the annualized yield to maturity compounded daily (i.e., continuously). If the bill is currently selling at price \(P\).

\[Pe^{\frac{rd}{365}}=100\] Taking the logarithm of both the sides \[\frac{rd}{365}+ \log (P)=\log (100)\] This implies \[\begin{aligned} r &=\log \Big( \frac{100}{P} \Big) \times \frac{365}{d} \\ &= \log \Big( \frac{100}{P} -1 +1 \Big) \times \frac{365}{d} \\ & \approx \Big( \frac{100}{P} -1 \Big) \times \frac{365}{d} \\ &= \Big( \frac{100 - P}{P} \Big) \times \frac{365}{d}\end{aligned}\] In percentage term \[r\approx \Big( \frac{100 - P}{P} \Big) \times \frac{365}{d} \times 100\] For example, the yield of a 91 days bill trading at price 99.5 on the 60th day (\(d=91-60=31\)) is \[r=\Big( \frac{100 -99.5}{99.5} \Big) \times \frac{365}{31} \times 100=5.92\%\]

##### CALCULATION OF YIELD TO MATURITY FOR T-Biils
TBillYield=function(FV, P, M, t){
d=M-t
r=((FV-P)/P)*(365/d)*100
sprintf("T-bill Yield=%f", r )
}
FV= 100 #Future value
P = 99.5#Trading price
M= 91 #maturity in days
t= 60 #current time in days
TBillYield(100,99.5,91,60)
## [1] "T-bill Yield=5.916680"