Plotting the price/yield relationship
Although there is an inverse relationship between price and yield, this relationship is not linear. This means that changes in price due to change in yield could differ substantially depending on whether the yield goes up or down. We will revisit this important concept when we discuss the concept of convexity in Chapter Three and how an adjustment is necessary to account for this curved relationship between a bond's price and yield. For now, just keep in mind that this inverse relationship is non-linear.
In this exercise, you will assume that you have a bond with $100 par value, 10% coupon rate, and 20 years to maturity. Note that this is different from the bond you've been working with to this point! Your goal is to value this bond at different levels of yield using your bondprc()
function, which is available in your workspace.
This exercise is part of the course
Bond Valuation and Analysis in R
Exercise instructions
- Create the vector
prc_yld
from 2% (0.02
) to 40% (0.40
) by increments of 1% (0.01
) by using the seq() function. - Use
data.frame()
to convertprc_yld
to a data frame. - Use the pre-written
for
loop withbondprc()
to calculate bond price at different yield levels inprc_yld
. Try to understand the behavior of the loop. - Your
prc_yld
object now contains a column for yield (prc_yld
) and a column for price (price
). Plot this object using the pre-written code to see the relationship between price and yield to maturity (YTM).
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Generate prc_yld
prc_yld <- seq(___, ___, ___)
# Convert prc_yld to data frame
prc_yld <- data.frame(___)
# Calculate bond price given different yields
for (i in 1:nrow(prc_yld)) {
prc_yld$price[i] <- bondprc(100, 0.10, 20, prc_yld$prc_yld[i])
}
# Plot P/YTM relationship
plot(___,
type = "l",
col = "blue",
main = "Price/YTM Relationship")