1. Learn
  2. /
  3. Courses
  4. /
  5. Bond Valuation and Analysis in R

Exercise

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.

Instructions

100 XP
  • 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 using uniroot().
  • Use ytm() with your cf vector to find the bond's yield to maturity.