[Problem]
There is a bond whose face value is $100. It has 5% coupon rate, paying annually. If the par value of the bond is $100, what is Yield To Maturity(YTM) of this bond?
[Explanation]
YTM is nothing but to quote the bond in terms of the rate. As you might know, the discount rate is different from year to year. By using only one discount rate, we can get a broad sense of the discount rate that this bond bears. The higher YTM, the cheaper the bond price is.
When you use Excel, you should use solver. Yeah, it's hassle. But with R, you can solve this problem with "uniroot" command, which allows you to get a root for single variable equation. You can use this command to any type of single variable equation. (or polynomials)
[Codes]
Here is the source code
#[How to calculate YTM in r]
#You don't need any library in here.
f.ytm = function(ytm)
{
E = NULL
price <- 100
maturity <- 10
coupon <- 5
par <- 100
for (i in 1 : mature)
{
E[i] <- coupon/(1+ytm)^i
}
E[maturity] <- E[maturity] + par/(1+ytm)^maturity
sum_e <- sum(E)
return(sum_e - price)
}
solution <- uniroot(f.ytm, interval=c(0,1))
ytm <- solution$root
print(ytm)
No comments:
Post a Comment