Bond Valuation and Analysis in R
Clifford Ang
Senior Vice President, Compass Lexecon
bondprc() function to simplify calculationsp for par valuer for coupon ratettm for time to maturityy for yieldcf <- c(rep(p * r, ttm - 1), p * (1 + r))
rep(x, y) - repeats y times the value of xx = p * r =  coupon paymenty = ttm - 1 = bond's time to maturity minus one yearp * (1 + r) = principal + final coupon paymentcf <- data.frame(cf)
cf$t <- as.numeric(rownames(cf))
rownames() of cf vector is equal to 1, 2, 3, 4, until the ttm of bondas.numeric() needed to ensure values are read as numberscf$pv_factor <- 1 / (1 + y)^cf$t
cf$pv <- cf$cf * cf$pv_factor
sum(cf$pv)
bondprc() functionp, r, ttm, and ybondprc <- function(p, r, ttm, y){
   cf <- c(rep(p * r, ttm - 1), p * (1 + r))
   cf <- data.frame(cf)
   cf$t <- as.numeric(rownames(cf))
   cf$pv_factor <- 1 / (1 + y)^cf$t
   cf$pv <- cf$cf * cf$pv_factor
   sum(cf$pv)
}
Bond Valuation and Analysis in R