Use uniroot function to find YTM
Trial-and-error is a very cumbersome process. An alternative is to use an algorithm that does the work for you. In this particular case, the solution to the problem is the same as finding the root of a function.
In this exercise, you will use the unrioot() function to find the root.
The uniroot()
function requires us to setup a vector of cash flows, cf
, that begins with the price of the bond (as a negative number) as the first element and the cash flows you expect to receive from the bond (i.e., coupon and principal payments) as the remaining elements.
Recall that the price of the bond is $95.79 and the bond has a $100 par value, 5% coupon rate, and 5 years to maturity.
Este exercício faz parte do curso
Bond Valuation and Analysis in R
Instruções do exercício
- Create a vector of cash flows,
cf
, which includes the initial bond price (negative) and payments until maturity (positive). - Use the pre-written code to create a simple bond valuation function,
bval()
, which calculates the value of the bond at each time period. - Use the pre-written code to create the
ytm()
function usinguniroot()
. - Use
ytm()
with yourcf
vector to find the bond's yield to maturity.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Create cash flow vector
cf <- c(___, ___, ___, ___, ___, ___)
# Create bond valuation function
bval <- function(i, cf,
t=seq(along = cf))
sum(cf / (1 + i)^t)
# Create ytm() function using uniroot
ytm <- function(cf) {
uniroot(bval, c(0, 1), cf = cf)$root
}
# Use ytm() function to find yield