#========================================================# # Quantitative ALM, Financial Econometrics & Derivatives # ML/DL using R, Python, Tensorflow by Sang-Heon Lee # # https://kiandlee.blogspot.com #——————————————————–# # Bond Modified Duration Calculation #========================================================# graphics.off() # clear all graphs rm(list = ls()) # remove all files from your workspace library(derivmkts) # price, yield, duration, convexity #——————————————————- # Input #——————————————————- C – 0.05 # coupon rate y – 0.03 # YTM m – 5 # maturity freq – 1 # payment frequency PA – 1 # principal amount cpn – C*PA # annual coupon amount # P0 : initial price(P0), # D and C : duration and convexity P0 – bondpv(cpn, m, y, PA, freq) D – duration(P0, cpn, m, PA, freq, modified = TRUE) C – convexity(P0, cpn, m, PA, freq) cat(paste0( “P0 = “, P0, “\n”, “Duration = “, D, “\n”, “Convexity = “, C, “\n”)) # % price change using # 1) full calculation # 2) approximation with D # 3) approximation with D and C Pd – bondpv(cpn, m, y+0.0001, PA, freq) per_ch_P_full – (Pd – P0)/P0 per_ch_P_app_D – –D*0.0001 per_ch_P_app_DC – –D*0.0001 + 0.5*C*(0.0001)^2 cat(paste0( “% bond price change \n”, “full = “, round(per_ch_P_full*100,8), “%\n”, “with duration = “, round(per_ch_P_app_D*100,8), “%\n”, “with duration and convexity = “, round(per_ch_P_app_DC*100,8), “%\n”)) Favorite