# # Written by: # -- # John L. Weatherwax 2009-04-21 # # email: wax@alum.mit.edu # # Please send comments and especially bug reports to the # above email address. # #----- save_plots = F set.seed(0) # P 1: # bondvalue = function(c, T, r, par){ # # Computes bv = bond values (current prices) corresponding # to all values of yield to maturity in the input vector r # # INPUT # c = coupon payment (semiannual) # T = time to maturity (in years) # r = vector of yields to maturity (semiannual rates) # par = par value # bv = c/r + (par - c/r) * (1+r)^(-2*T) bv } # Computes the yield to maturity of a bond paying semiannual coupon payments # # price, coupon payment, and time to maturity (in years) are set below # # Uses the function "bondvalue" # price = 1200 # current price of the bond C = 40 # coupon payment T = 30 # time to maturity par = 1000 # par value of the bond r = seq( 0.02, 0.05, length.out=500 ) value = bondvalue(C, T, r, par) yield2M = spline(value, r, xout=price) # spline interpolation if( save_plots ){ postscript("../../WriteUp/Graphics/Chapter3/chap_3_prob_1_plot.eps", onefile=FALSE, horizontal=FALSE) } plot( r, value, xlab='yield to maturity', ylab='price of bond', type='l', main='par= 1000, coupon payment= 40, T= 30', lwd=2 ) abline(h=price) abline(v=yield2M$y) if( save_plots ){ dev.off() } # P 2: # uniroot( function(r) r^2 - 0.5, c(0.7,0.8) ) # P 3: # uniroot( function(r) bondvalue(C, T, r, par) - 1200, c(0.03,0.04) ) # P 4: # uniroot( function(r) bondvalue(280, 8, r, 10000) - 9800, c(0.01,0.04) ) # P 5: # library(fEcofin) if( save_plots ){ postscript("../../WriteUp/Graphics/Chapter3/chap_3_prob_5_short_end.eps", onefile=FALSE, horizontal=FALSE) } plot(mk.maturity[,1], mk.zero2[5,2:56], type="l", xlab="maturity", ylab="yield", xlim=c(0,3) ) lines(mk.maturity[,1], mk.zero2[6,2:56], lty=2, type="l") lines(mk.maturity[,1], mk.zero2[7,2:56], lty=3, type="l") lines(mk.maturity[,1], mk.zero2[8,2:56], lty=4, type="l") legend("topleft", c("1985-12-01", "1986-01-01", "1986-02-01", "1986-03-01"), lty=1:4) if( save_plots ){ dev.off() } if( save_plots ){ postscript("../../WriteUp/Graphics/Chapter3/chap_3_prob_5_long_end.eps", onefile=FALSE, horizontal=FALSE) } plot(mk.maturity[,1], mk.zero2[5,2:56], type="l", xlab="maturity", ylab="yield", xlim=c(20,30) ) lines(mk.maturity[,1], mk.zero2[6,2:56], lty=2, type="l") lines(mk.maturity[,1], mk.zero2[7,2:56], lty=3, type="l") lines(mk.maturity[,1], mk.zero2[8,2:56], lty=4, type="l") legend("bottomleft", c("1985-12-01", "1986-01-01", "1986-02-01", "1986-03-01"), lty=1:4) if( save_plots ){ dev.off() } # P 6: # if( save_plots ){ postscript("../../WriteUp/Graphics/Chapter3/chap_3_prob_6.eps", onefile=FALSE, horizontal=FALSE) } plot(mk.maturity[,1], mk.zero2[29,2:56], type="l", xlab="maturity", ylab="yield", xlim=c(10,30), ylim=c(0.06,0.1) ) lines(mk.maturity[,1], mk.zero2[30,2:56], lty=2, type="l") lines(mk.maturity[,1], mk.zero2[31,2:56], lty=3, type="l") lines(mk.maturity[,1], mk.zero2[32,2:56], lty=4, type="l") legend("bottomleft", c("1987-12-01", "1988-01-01", "1988-02-01", "1988-03-01"), lty=1:4) if( save_plots ){ dev.off() }