Bond Modified Duration in Excel and R

Bond duration is a basic building block for bond portfolio management and asset-liability management (ALM). This post explains the meaning of duration and calculation of this risk measure by using Excel and R.


In this post, a bond price is calculated by discounting future cash flows using YTM which is the abbreviation of Yield To Maturity.

Bond Price


Bond price with unit notional amount, coupon C, YTM y, annual frequency is as follows. \[\begin{align} P = \frac{C}{1+y} + \frac{C}{(1+y)^2} + ... + \frac{C}{(1+y)^{n-1}} + \frac{1+C}{(1+y)^n} \end{align}\]

(Modified) Duration


Duration is a measure of price sensitivity to interest rates. At first, we need to calculate an interest rate sensitivity of a bond price.

The first derivative of \(P\) with respect to \(y\) is as follows.
\[\begin{align} \frac{d P}{d y} = -\frac{1C}{(1+y)^2} - \frac{2C}{(1+y)^3} - ... - \frac{(n-1)C}{(1+y)^{n}} - \frac{n(1+C)}{(1+y)^{n+1}} \end{align}\]
Factoring out \(-\frac{1}{1+y}\) in the above equation yields

\[\begin{align} \frac{d P}{d y} &= -\frac{1}{1+y} \left[\frac{1C}{(1+y)} + \frac{2C}{(1+y)^2} + ... + \frac{(n-1)C}{(1+y)^{n-1}} + \frac{n(1+C)}{(1+y)^{n}} \right] \\ &= -\frac{1}{1+y} STDC \end{align}\]
To avoid lengthy expression, we substitute STDC for the bracketed term. STDC is the abbreviation of the sum of multiplications of time and discounted cash flow (only coupon or coupon + principal amount). Of course, this abbreviation is not official :-)

It is noting that the above amount is a kind of a change of bond price. But it is more convenient to use the expression of a percentage change for comparison or analysis. For this reason, let's modify it by a ratio of its initial bond price.

Through this modification, we can use this measure as a % change of bond price which resulted from a change of interest rate. Dividing \(\left(\frac{d P}{d y}\right)\) by \(P\) gives the following expression.

\[\begin{align} \frac{d P}{d y}\frac{1}{P} = -\left[\frac{1}{1+y} STDC \frac{1}{P}\right] = -D \end{align}\]
This is the modified duration (\(D\)). Modified duration is defined without negative sign since this "minus" sign is popped out of discounting, which indicates that an increase in interest rate lowers a bond price.

Rearranging this equation, we can find that % change of bond price results from the multiplication of modified duration and interest rate change.

\[\begin{align} D \times {\Delta y} = -\frac{\Delta P}{P} \end{align}\]
For example,

1) If modified duration is 1 (year) and interest rate change is +25bp (= +0.25%), % change of bond price is equal to -0.25% (= -1*0.25%).

2) If modified duration is 10 (year) and interest rate change is +25bp (= +0.25%), % change of bond price is equal to -2.5% (= -10*0.25%).

In other words, the longer the duration or maturity of a bond, the more sensitive is it's price to a change in interest rates. For this reason, when the central bank increases the target rate, bond portfolio managers have a tendency to make bond portfolio duration shorter to avoid subsequent capital loss due to interest rate hikes. But it is not always.


Macaulay Duration


In fact, Macaulay originally invented the concept of duration. This is called as Macaulay duration which is the average time until receipt of a bond's cash flows, weighted according to the present values of these cash flows, measured in years.

Interestingly, it is already calculated in the previous equation. Macaulay duration (\(D_{mac}\)) is as follows.
\[\begin{align} D_{mac} = STDC \frac{1}{P} \end{align}\]
The relationship between modified duration and Macaulay duration is

\[\begin{align} D = \frac{1}{1+y} STDC \frac{1}{P} = \frac{D_{mac}}{1+y} \end{align}\]
It is worth while to note that the meaning of "modified" is the modification of Macaulay duration by dividing \({1+y}\).

As modified duration is widely used than Macaulay duration, when we use the term of duration, it is the modified duration. For this reason, we use \(D\) for the modified duration and \(D_{mac}\) for Macaulay duration.

Generalization


When interest conversion period is less than one year such as one quarter, duration is redefined as follows. \[\begin{align} D = \frac{D_{mac}}{1+y/k} \end{align}\] Here, k is the number of compounding periods per year.


Excel Illustration


Excel provides MDURATION() function for the calculation of modified duration.

    MDURATION (settlement, maturity, coupon, yld, freq)

Arguments of MDURATION() are pricing date, maturity date, coupon rate, YTM, compounding frequency in order.

Besides MDURATION(), we calculate duration by using definition and numerical differentiation for clear understanding.

The following Excel spreadsheet shows the case of k=1.
modified duration calculation Excel spreadsheet and R code


The following Excel spreadsheet shows the case of k=4.
modified duration calculation Excel spreadsheet and R code


R code


No sooner had we explained the concept of duration than we made the following R code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#========================================================#
# Quantitative Financial Econometrics & Derivatives 
# ML/DL using R, Python, Tensorflow by Sang-Heon Lee 
#
# https://shleeai.blogspot.com
#--------------------------------------------------------#
# Bond Modified Duration Calculation
#========================================================#
 
graphics.off()  # clear all graphs
rm(list = ls()) # remove all files from your workspace
 
#-------------------------------------------------------
# Input
#-------------------------------------------------------
    C  <- 0.05       # coupon rate
    y  <- 0.03       # YTM
    m  <- 5          # maturity
    dr <- 0.000001   # interest rate change
 
#-------------------------------------------------------
# Duration by Definition (k=1)
#-------------------------------------------------------
    
    # data.frame for calculation
    df <- data.frame(T = 1:m, 
                     CF = C + c(0,0,0,0,1))
    
    df$DF <- 1/(1+y)^(df$T)
    df$DC <- df$DF*df$CF
    
    P0 <- sum(df$DC) # initial bond price
    
    df$TDC <- df$T*df$DC # time * discounted CF
    
    STDC <- sum(df$TDC)
    
    # duration
    D_mac_dk1 <- STDC/P0         # Macaulay Dur
    D_mod_dk1 <- D_mac_dk1/(1+y) # Modified Dur
    
#-------------------------------------------------------
# Duration by Numerical Differentiation (k=1)
#-------------------------------------------------------
    
    df <- data.frame(T = 1:m, 
                     CF = C + c(rep(0,m-1),1))
    
    df$DFu <- 1/(1+y-dr)^(df$T)
    df$DCu <- df$DFu*df$CF
    
    Pu <- sum(df$DCu) # price up
    
    df$DFd <- 1/(1+y+dr)^(df$T)
    df$DCd <- df$DFd*df$CF
    
    Pd <- sum(df$DCd) # price down
    
    # duration
    D_mod_nk1 <- (Pu-Pd)/(2*dr)/P0 # Modified Dur
    
#-------------------------------------------------------
# Duration by Definition (k=4)
#-------------------------------------------------------
    
    k  <- 4 # Compouding period = Q
    
    df <- data.frame(T = seq(1/k,m,1/k), 
                     CF = C/+ c(rep(0,k*m-1),1))
    
    df$DF <- 1/(1+y/k)^(df$T*k)
    df$DC <- df$DF*df$CF
    
    P0 <- sum(df$DC)
    
    df$TDC <- df$T*df$DC
    STDC <- sum(df$TDC)
    
    D_mac_dk4 <- STDC/P0           # Macaulay Dur
    D_mod_dk4 <- D_mac_dk4/(1+y/k) # Modified Dur
    
#-------------------------------------------------------
# Duration by Numerical Differentiation (k=4)
#-------------------------------------------------------
 
    df <- data.frame(T = seq(1/k,m,1/k),
                     CF = C/+ c(rep(0,k*m-1),1))
 
    df$DFu <- 1/(1+(y-dr)/k)^(df$T*k)
    df$DCu <- df$DFu*df$CF
 
    Pu <- sum(df$DCu)
 
    df$DFd <- 1/(1+(y+dr)/k)^(df$T*k)
    df$DCd <- df$DFd*df$CF
 
    Pd <- sum(df$DCd)
    D_mod_nk4 <- (Pu-Pd)/(2*dr)/P0 # Modified Dur
 
    
    # Print
    cat(paste0("\nDuration Calculation Results \n\n",
        "Macaulay Dur (k=1) def = ", D_mac_dk1, "\n",
        "Modified Dur (k=1) def = ", D_mod_dk1, "\n",
        "Modified Dur (k=1) num = ", D_mod_nk1, "\n",
        "\n",
        "Macaulay Dur (k=4) def = ", D_mac_dk4, "\n",
        "Modified Dur (k=4) def = ", D_mod_dk4, "\n",
        "Modified Dur (k=4) num = ", D_mod_nk4))
    
cs


The above R code produces the same results as those of Excel exercises.

1
2
3
4
5
6
7
8
9
10
Duration Calculation Results 
 
Macaulay Dur (k=1) def = 4.56806046946571
Modified Dur (k=1) def = 4.43501016452982
Modified Dur (k=1) num = 4.43501016417148
 
Macaulay Dur (k=4) def = 4.48393573818857
Modified Dur (k=4) def = 4.45055656395888
Modified Dur (k=4) num = 4.45055656459531
 
cs


Concluding Remarks


From this post, we can understand the meaning of duration, in particular modified duration, by using intuitive derivations and Excel illustrations. Finally we can easily implement R code for the calculation of duration. Upon this work, we can build up the advanced risk management or portfolio optimization model.

But care is taken with some limitation of duration so that duration is the first derivative of highly non-linear bond price function, in other words, or the first order or linearly approximation. To make up for this shortcoming, the second order derivative (convexity) can be considered when the first approximation shows poor result. It depends on the product types and specifications. \(\blacksquare\)


No comments:

Post a Comment