设计一个函数来计算债券的到期收益率,但要不断获得:
f(lower,...)中的错误: 缺少参数“ coupon_rate”,没有默认值
输入:
我不知道该如何调整以纠正此问题。有想法吗?
完整代码:
ytm <- function(face_value, coupon_rate, price, no_comp, tenure) {
bval <- function(i, face_value, coupon_rate, price, no_comp, tenure, t=seq(along = cf)) {
coupon_payment = face_value * coupon_rate
cf <- c(-face_value*(price/100),rep(c(coupon_payment), (no_comp * tenure -1)), face_value + coupon_payment)
sum(cf / (1 + i)^t)
}
coupon_payment = face_value * coupon_rate
cf <- c(-face_value*(price/100),rep(c(coupon_payment), (no_comp * tenure -1)), face_value + coupon_payment)
uniroot(ytm, interval=c(0, 20))$root
}
ytm(1000, 0.1, 101, 1, 3)