{"id":205664,"date":"2024-04-26T11:02:56","date_gmt":"2024-04-26T15:02:56","guid":{"rendered":"https:\/\/ibkrcampus.com\/?p=205664"},"modified":"2024-04-26T11:02:38","modified_gmt":"2024-04-26T15:02:38","slug":"bond-modified-duration-in-excel-and-r","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/bond-modified-duration-in-excel-and-r\/","title":{"rendered":"Bond Modified Duration in Excel and R"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<p>In this post, a bond price is calculated by discounting future cash flows using&nbsp;<strong>YTM<\/strong>&nbsp;which is the abbreviation of&nbsp;<strong>Yield To Maturity<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-bond-price\">Bond Price<\/h3>\n\n\n\n<p>Bond price with unit notional amount, coupon C, YTM y, annual frequency is as follows.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"572\" height=\"64\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-1.jpg\" alt=\"\" class=\"wp-image-205679 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-1.jpg 572w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-1-300x34.jpg 300w\" data-sizes=\"(max-width: 572px) 100vw, 572px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 572px; aspect-ratio: 572\/64;\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">(Modified) Duration<\/h3>\n\n\n\n<p>Duration is a measure of price sensitivity to interest rates. At first, we need to calculate an interest rate sensitivity of a bond price.<\/p>\n\n\n\n<p>The first derivative of&nbsp;<strong><em>P<\/em><\/strong>&nbsp;with respect to&nbsp;<em><strong>y&nbsp;<\/strong><\/em>is as follows.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"579\" height=\"74\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-2.jpg\" alt=\"\" class=\"wp-image-205680 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-2.jpg 579w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-2-300x38.jpg 300w\" data-sizes=\"(max-width: 579px) 100vw, 579px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 579px; aspect-ratio: 579\/74;\" \/><\/figure>\n\n\n\n<p>Factoring out <img decoding=\"async\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-3.jpg\" alt=\"\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\">in the above equation yields<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"591\" height=\"134\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-4.jpg\" alt=\"\" class=\"wp-image-205683 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-4.jpg 591w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-4-300x68.jpg 300w\" data-sizes=\"(max-width: 591px) 100vw, 591px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 591px; aspect-ratio: 591\/134;\" \/><\/figure>\n\n\n\n<p>To avoid lengthy expression, we substitute&nbsp;<strong>STDC<\/strong>&nbsp;for the bracketed term.&nbsp;<strong>STDC<\/strong>&nbsp;is the abbreviation of the&nbsp;<strong>s<\/strong>um of multiplications of&nbsp;<strong>t<\/strong>ime and&nbsp;<strong>d<\/strong>iscounted&nbsp;<strong>c<\/strong>ash flow (only coupon or coupon + principal amount). Of course, this abbreviation is not official<\/p>\n\n\n\n<p>It is noting that the above amount is a kind of&nbsp;<strong>a change of bond price<\/strong>. But it is more convenient to use the expression of&nbsp;<strong>a percentage change<\/strong>&nbsp;for comparison or analysis. For this reason, let&#8217;s modify it by&nbsp;<strong>a ratio of its initial bond price<\/strong>.<\/p>\n\n\n\n<p>Through this modification, we can use this measure as a % change of bond price which resulted from a change of interest rate.&nbsp;<strong>Dividing<\/strong> <img decoding=\"async\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-5.jpg\" alt=\"\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\"> <strong>by&nbsp;<em>P<\/em><\/strong>&nbsp;gives the following expression.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"561\" height=\"88\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-6.jpg\" alt=\"\" class=\"wp-image-205687 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-6.jpg 561w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-6-300x47.jpg 300w\" data-sizes=\"(max-width: 561px) 100vw, 561px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 561px; aspect-ratio: 561\/88;\" \/><\/figure>\n\n\n\n<p>This is the&nbsp;<strong>modified duration (D)<\/strong>. Modified duration is defined without negative sign since this &#8220;minus&#8221; sign is popped out of discounting, which indicates that an increase in interest rate lowers a bond price.<\/p>\n\n\n\n<p>Rearranging this equation, we can find that&nbsp;<strong>% change of bond price results from the multiplication of modified duration and interest rate change<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"582\" height=\"90\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-7.jpg\" alt=\"\" class=\"wp-image-205689 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-7.jpg 582w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-7-300x46.jpg 300w\" data-sizes=\"(max-width: 582px) 100vw, 582px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 582px; aspect-ratio: 582\/90;\" \/><\/figure>\n\n\n\n<p>For example,<\/p>\n\n\n\n<p>1) If modified duration is&nbsp;<strong>1 (year)<\/strong>&nbsp;and interest rate change is +25bp (=&nbsp;<strong>+0.25%<\/strong>), % change of bond price is equal to&nbsp;<strong>-0.25%<\/strong>&nbsp;(= -1*0.25%).<\/p>\n\n\n\n<p>2) If modified duration is&nbsp;<strong>10 (year)<\/strong>&nbsp;and interest rate change is +25bp (=&nbsp;<strong>+0.25%<\/strong>), % change of bond price is equal to&nbsp;<strong>-2.5%<\/strong>&nbsp;(= -10*0.25%).<\/p>\n\n\n\n<p>In other words,&nbsp;<strong>the longer the duration or maturity of a bond, the more sensitive is it&#8217;s price to a change in interest rates<\/strong>. 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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Macaulay Duration<\/h3>\n\n\n\n<p>In fact, Macaulay originally invented the concept of duration. This is called as&nbsp;<strong>Macaulay duration<\/strong>&nbsp;which is the average time until receipt of a bond&#8217;s cash flows, weighted according to the present values of these cash flows, measured in years.<\/p>\n\n\n\n<p>Interestingly, it is already calculated in the previous equation. Macaulay duration <img decoding=\"async\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-8.jpg\" alt=\"\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\"> is as follows.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"573\" height=\"66\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-9.jpg\" alt=\"\" class=\"wp-image-205693 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-9.jpg 573w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-9-300x35.jpg 300w\" data-sizes=\"(max-width: 573px) 100vw, 573px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 573px; aspect-ratio: 573\/66;\" \/><\/figure>\n\n\n\n<p>The relationship between modified duration and Macaulay duration is<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"568\" height=\"73\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-10.jpg\" alt=\"\" class=\"wp-image-205695 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-10.jpg 568w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-10-300x39.jpg 300w\" data-sizes=\"(max-width: 568px) 100vw, 568px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 568px; aspect-ratio: 568\/73;\" \/><\/figure>\n\n\n\n<p>It is worth while to note that the meaning of &#8220;modified&#8221; is the modification of Macaulay duration by dividing&nbsp;<strong>1 + y<\/strong>.<\/p>\n\n\n\n<p>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&nbsp;<strong><em>D<\/em><\/strong>&nbsp;for the modified duration and&nbsp;<strong><em>D<sub>mac<\/sub><\/em><\/strong>&nbsp;for Macaulay duration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Generalization<\/h3>\n\n\n\n<p>When interest conversion period is less than one year such as one quarter, duration is redefined as follows.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"569\" height=\"67\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-11.jpg\" alt=\"\" class=\"wp-image-205698 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-11.jpg 569w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/bond-duration-excel-r-shlee-11-300x35.jpg 300w\" data-sizes=\"(max-width: 569px) 100vw, 569px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 569px; aspect-ratio: 569\/67;\" \/><\/figure>\n\n\n\n<p>Here, k is the number of compounding periods per year.<\/p>\n\n\n\n<p><strong>Excel Illustration<\/strong><\/p>\n\n\n\n<p>Excel provides<strong>&nbsp;MDURATION()<\/strong>&nbsp;function for the calculation of modified duration.<\/p>\n\n\n\n<p><strong>&nbsp; &nbsp;&nbsp;MDURATION (settlement, maturity, coupon, yld, freq)<\/strong><\/p>\n\n\n\n<p>Arguments of MDURATION() are&nbsp;<strong>pricing date, maturity date, coupon rate, YTM, compounding frequency<\/strong>&nbsp;in order.<\/p>\n\n\n\n<p>Besides MDURATION(), we calculate duration by using definition and numerical differentiation for clear understanding.<\/p>\n\n\n\n<p>The following Excel spreadsheet shows the case of k=1.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"976\" height=\"404\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/excel_dur_annual-shlee.png\" alt=\"\" class=\"wp-image-205701 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/excel_dur_annual-shlee.png 976w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/excel_dur_annual-shlee-700x290.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/excel_dur_annual-shlee-300x124.png 300w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/excel_dur_annual-shlee-768x318.png 768w\" data-sizes=\"(max-width: 976px) 100vw, 976px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 976px; aspect-ratio: 976\/404;\" \/><\/figure>\n\n\n\n<p>The following Excel spreadsheet shows the case of k=4.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1057\" height=\"1010\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/excel_dur_quarterly-shlee.png\" alt=\"\" class=\"wp-image-205702 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/excel_dur_quarterly-shlee.png 1057w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/excel_dur_quarterly-shlee-700x669.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/excel_dur_quarterly-shlee-300x287.png 300w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/excel_dur_quarterly-shlee-768x734.png 768w\" data-sizes=\"(max-width: 1057px) 100vw, 1057px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1057px; aspect-ratio: 1057\/1010;\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">R code<\/h3>\n\n\n\n<p>No sooner had we explained the concept of duration than we made the following R code.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#========================================================#\n# Quantitative ALM, Financial Econometrics &amp; Derivatives \n# ML\/DL using R, Python, Tensorflow by Sang-Heon Lee \n#\n# https:\/\/shleeai.blogspot.com\n#--------------------------------------------------------#\n# Bond Modified Duration Calculation\n#========================================================#\n \ngraphics.off()  # clear all graphs\nrm(list = ls()) # remove all files from your workspace\n \n#-------------------------------------------------------\n# Input\n#-------------------------------------------------------\n    C  &lt;- 0.05       # coupon rate\n    y  &lt;- 0.03       # YTM\n    m  &lt;- 5          # maturity\n    dr &lt;- 0.000001   # interest rate change\n \n#-------------------------------------------------------\n# Duration by Definition (k=1)\n#-------------------------------------------------------\n    \n    # data.frame for calculation\n    df &lt;- data.frame(T = 1:m, \n                     CF = C + c(0,0,0,0,1))\n    \n    df$DF &lt;- 1\/(1+y)^(df$T)\n    df$DC &lt;- df$DF*df$CF\n    \n    P0 &lt;- sum(df$DC) # initial bond price\n    \n    df$TDC &lt;- df$T*df$DC # time * discounted CF\n    \n    STDC &lt;- sum(df$TDC)\n    \n    # duration\n    D_mac_dk1 &lt;- STDC\/P0         # Macaulay Dur\n    D_mod_dk1 &lt;- D_mac_dk1\/(1+y) # Modified Dur\n    \n#-------------------------------------------------------\n# Duration by Numerical Differentiation (k=1)\n#-------------------------------------------------------\n    \n    df &lt;- data.frame(T = 1:m, \n                     CF = C + c(rep(0,m-1),1))\n    \n    df$DFu &lt;- 1\/(1+y-dr)^(df$T)\n    df$DCu &lt;- df$DFu*df$CF\n    \n    Pu &lt;- sum(df$DCu) # price up\n    \n    df$DFd &lt;- 1\/(1+y+dr)^(df$T)\n    df$DCd &lt;- df$DFd*df$CF\n    \n    Pd &lt;- sum(df$DCd) # price down\n    \n    # duration\n    D_mod_nk1 &lt;- (Pu-Pd)\/(2*dr)\/P0 # Modified Dur\n    \n#-------------------------------------------------------\n# Duration by Definition (k=4)\n#-------------------------------------------------------\n    \n    k  &lt;- 4 # Compouding period = Q\n    \n    df &lt;- data.frame(T = seq(1\/k,m,1\/k), \n                     CF = C\/k + c(rep(0,k*m-1),1))\n    \n    df$DF &lt;- 1\/(1+y\/k)^(df$T*k)\n    df$DC &lt;- df$DF*df$CF\n    \n    P0 &lt;- sum(df$DC)\n    \n    df$TDC &lt;- df$T*df$DC\n    STDC &lt;- sum(df$TDC)\n    \n    D_mac_dk4 &lt;- STDC\/P0           # Macaulay Dur\n    D_mod_dk4 &lt;- D_mac_dk4\/(1+y\/k) # Modified Dur\n    \n#-------------------------------------------------------\n# Duration by Numerical Differentiation (k=4)\n#-------------------------------------------------------\n \n    df &lt;- data.frame(T = seq(1\/k,m,1\/k),\n                     CF = C\/k + c(rep(0,k*m-1),1))\n \n    df$DFu &lt;- 1\/(1+(y-dr)\/k)^(df$T*k)\n    df$DCu &lt;- df$DFu*df$CF\n \n    Pu &lt;- sum(df$DCu)\n \n    df$DFd &lt;- 1\/(1+(y+dr)\/k)^(df$T*k)\n    df$DCd &lt;- df$DFd*df$CF\n \n    Pd &lt;- sum(df$DCd)\n    D_mod_nk4 &lt;- (Pu-Pd)\/(2*dr)\/P0 # Modified Dur\n \n    \n    # Print\n    cat(paste0(\"\\nDuration Calculation Results \\n\\n\",\n        \"Macaulay Dur (k=1) def = \", D_mac_dk1, \"\\n\",\n        \"Modified Dur (k=1) def = \", D_mod_dk1, \"\\n\",\n        \"Modified Dur (k=1) num = \", D_mod_nk1, \"\\n\",\n        \"\\n\",\n        \"Macaulay Dur (k=4) def = \", D_mac_dk4, \"\\n\",\n        \"Modified Dur (k=4) def = \", D_mod_dk4, \"\\n\",\n        \"Modified Dur (k=4) num = \", D_mod_nk4))<\/pre>\n\n\n\n<p>The above R code produces the same results as those of Excel exercises.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Duration Calculation Results \n \nMacaulay Dur (k=1) def = 4.56806046946571\nModified Dur (k=1) def = 4.43501016452982\nModified Dur (k=1) num = 4.43501016417148\n \nMacaulay Dur (k=4) def = 4.48393573818857\nModified Dur (k=4) def = 4.45055656395888\nModified Dur (k=4) num = 4.45055656459531<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Concluding Remarks<\/h3>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>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&nbsp;<strong>linearly approximation<\/strong>. To make up for this shortcoming, the second order derivative (<strong>convexity<\/strong>) can be considered when the first approximation shows poor result. It depends on the product types and specifications.<\/p>\n\n\n\n<p><em>Originally posted on <a href=\"https:\/\/shleeai.blogspot.com\/2021\/09\/bond-modified-duration-in-excel-and-r.html\">SHLee AI Financial Model<\/a> blog.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post explains the meaning of duration and calculation of this risk measure by using Excel and R.<\/p>\n","protected":false},"author":662,"featured_media":205705,"comment_status":"open","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[339,343,338,341,342],"tags":[45,806,5878,17052,487,6591],"contributors-categories":[13728],"class_list":{"0":"post-205664","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-science","8":"category-programing-languages","9":"category-ibkr-quant-news","10":"category-quant-development","11":"category-r-development","12":"tag-bonds","13":"tag-data-science","14":"tag-excel","15":"tag-macaulay-duration","16":"tag-r","17":"tag-rstats","18":"contributors-categories-sh-fintech-modeling"},"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Bond Modified Duration in Excel and R | IBKR Quant<\/title>\n<meta name=\"description\" content=\"This post explains the meaning of duration and calculation of this risk measure by using Excel and R.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/205664\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Bond Modified Duration in Excel and R\" \/>\n<meta property=\"og:description\" content=\"This post explains the meaning of duration and calculation of this risk measure by using Excel and R.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/bond-modified-duration-in-excel-and-r\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2024-04-26T15:02:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/market-charts-coins-light-blue.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"563\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sang-Heon Lee\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sang-Heon Lee\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/bond-modified-duration-in-excel-and-r\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/bond-modified-duration-in-excel-and-r\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Sang-Heon Lee\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/0a959ff9de7f0465a07baa1fe1ae0200\"\n\t            },\n\t            \"headline\": \"Bond Modified Duration in Excel and R\",\n\t            \"datePublished\": \"2024-04-26T15:02:56+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/bond-modified-duration-in-excel-and-r\\\/\"\n\t            },\n\t            \"wordCount\": 818,\n\t            \"commentCount\": 0,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/bond-modified-duration-in-excel-and-r\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/04\\\/market-charts-coins-light-blue.jpg\",\n\t            \"keywords\": [\n\t                \"bonds\",\n\t                \"Data Science\",\n\t                \"Excel\",\n\t                \"Macaulay Duration\",\n\t                \"R\",\n\t                \"rstats\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Quant\",\n\t                \"Quant Development\",\n\t                \"R Development\"\n\t            ],\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"CommentAction\",\n\t                    \"name\": \"Comment\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/bond-modified-duration-in-excel-and-r\\\/#respond\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/bond-modified-duration-in-excel-and-r\\\/\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/bond-modified-duration-in-excel-and-r\\\/\",\n\t            \"name\": \"Bond Modified Duration in Excel and R | IBKR Campus US\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/bond-modified-duration-in-excel-and-r\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/bond-modified-duration-in-excel-and-r\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/04\\\/market-charts-coins-light-blue.jpg\",\n\t            \"datePublished\": \"2024-04-26T15:02:56+00:00\",\n\t            \"description\": \"This post explains the meaning of duration and calculation of this risk measure by using Excel and R.\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/bond-modified-duration-in-excel-and-r\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/bond-modified-duration-in-excel-and-r\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/04\\\/market-charts-coins-light-blue.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/04\\\/market-charts-coins-light-blue.jpg\",\n\t            \"width\": 1000,\n\t            \"height\": 563,\n\t            \"caption\": \"Quant\"\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/0a959ff9de7f0465a07baa1fe1ae0200\",\n\t            \"name\": \"Sang-Heon Lee\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/sang-heonlee\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Bond Modified Duration in Excel and R | IBKR Quant","description":"This post explains the meaning of duration and calculation of this risk measure by using Excel and R.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/205664\/","og_locale":"en_US","og_type":"article","og_title":"Bond Modified Duration in Excel and R","og_description":"This post explains the meaning of duration and calculation of this risk measure by using Excel and R.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/bond-modified-duration-in-excel-and-r\/","og_site_name":"IBKR Campus US","article_published_time":"2024-04-26T15:02:56+00:00","og_image":[{"width":1000,"height":563,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/market-charts-coins-light-blue.jpg","type":"image\/jpeg"}],"author":"Sang-Heon Lee","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Sang-Heon Lee","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/bond-modified-duration-in-excel-and-r\/#article","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/bond-modified-duration-in-excel-and-r\/"},"author":{"name":"Sang-Heon Lee","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/0a959ff9de7f0465a07baa1fe1ae0200"},"headline":"Bond Modified Duration in Excel and R","datePublished":"2024-04-26T15:02:56+00:00","mainEntityOfPage":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/bond-modified-duration-in-excel-and-r\/"},"wordCount":818,"commentCount":0,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/bond-modified-duration-in-excel-and-r\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/market-charts-coins-light-blue.jpg","keywords":["bonds","Data Science","Excel","Macaulay Duration","R","rstats"],"articleSection":["Data Science","Programming Languages","Quant","Quant Development","R Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/bond-modified-duration-in-excel-and-r\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/bond-modified-duration-in-excel-and-r\/","url":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/bond-modified-duration-in-excel-and-r\/","name":"Bond Modified Duration in Excel and R | IBKR Campus US","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/bond-modified-duration-in-excel-and-r\/#primaryimage"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/bond-modified-duration-in-excel-and-r\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/market-charts-coins-light-blue.jpg","datePublished":"2024-04-26T15:02:56+00:00","description":"This post explains the meaning of duration and calculation of this risk measure by using Excel and R.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/bond-modified-duration-in-excel-and-r\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/bond-modified-duration-in-excel-and-r\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/market-charts-coins-light-blue.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/market-charts-coins-light-blue.jpg","width":1000,"height":563,"caption":"Quant"},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/0a959ff9de7f0465a07baa1fe1ae0200","name":"Sang-Heon Lee","url":"https:\/\/www.interactivebrokers.com\/campus\/author\/sang-heonlee\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/04\/market-charts-coins-light-blue.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/205664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/662"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=205664"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/205664\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/205705"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=205664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=205664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=205664"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=205664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}