Get startedGet started for free

Convert your code into a function

1. Convert your code into a function

You are going to

2. Bond valuation function

value many bonds in the rest of this course using the same steps we outlined in the previous set of exercises. To simplify this process, it makes sense to create a bond valuation function. This way, you limit the possibility of making mistakes as you don't need to keep re-writing multiple lines of code each time you value a bond.

3. Steps in bond valuation

We still keep the step-by-step approach from before, but this time we have to generalize the inputs so the function can value bonds with different coupons and maturities. The first thing we do is to use variable names instead of actual values. So, we use "p" for par value instead of, say, $100. We use "r" for the coupon rate instead of, say, 5% or 0(point)05. We use "ttm" for time to maturity instead of, say, 5 years. We use "y" for yield instead of, say, 4%. These variables - p, r, ttm, and y - are the required inputs by the bond valuation function. The code is also modified to make some of the steps more generic. Now, let's go through each of these steps to see what we changed.

4. Steps in bond valuation

The first step is to construct a cash flow vector 'cf'. For the cash flow vector 'cf', we need to allow the code to be flexible and generate the coupon payments and the principal payment automatically given the bond's par value, coupon rate, and time to maturity. We use the rep() command, which takes two inputs: X and Y. It basically repeats Y times the value of X. This fits how we model the coupon payments prior to maturity. So, X is equal to the par value times the coupon rate and Y is equal to the time to maturity minus 1. Then, the cash flow vector's final element should equal the last coupon payment plus principal. Mathematically, this is equivalent to the par value times one plus the coupon rate 'r'.

5. Steps in bond valuation

The next step is to convert the 'cf' vector into a data frame so we can add variables to the data. This is similar to the step we used in the last section. Next, we create a time index 't'. To automate this process, we need to find an object that has values equal to 1, 2, 3, 4, etc. until the time to maturity of the bond. Fortunately, the label of the rows of the cash flow vector 'cf' fits this purpose. So, using the rownames command, we can extract those values and put them into the time index 't' variable. Then, to ensure that the values are read-in as numbers, we use the as numeric command.

6. Steps in bond valuation

The last three steps are similar to the discussion in the last section when we did the step-by-step valuation. First, we calculate a present value factor 'pv_factor'. Next, we calculate the present value of each cash flow 'pv' by multiplying each cash flow by the appropriate pv_factor. Finally, we sum the present value of each cash flow to arrive at the bond's value.

7. Wrap the code

The final step in the function-writing process is to wrap the code with one line at the beginning and another line at the end. In the first line, we setup the bond valuation function 'bondprc()'. The first line of code shows the bondprc() function takes as inputs "p," "r," "ttm," and "y". The first line ends with an open curly brace, after which we see the 6 lines of bond valuation code we discussed previously. Finally, we add the last line of the code below, which is simply a closed curly brace to end the function. This completes our bond valuation function!

8. Let's practice!

Now it's time for you to create your own bond valuation function. Let's practice!