source('../Chapter12/utils.R') source('../Chapter14/utils.R') r0 = 0.06 u = 1.2 d = 0.9 ## Get the grid of spot rates: ## n_years = 9 rrate = make_spot_table(r0, u, d, n_years) print('Spot rate grid=') print(round(upper_triangular_to_ascending_triangle(rrate), 3)) ## print in the format of this chapter ## Solve for the_value of the bond: ## n_steps = n_years+1 ## = number of columns = number of years + the initial spot_value n_rows = n_steps q = 0.5 ## Part (a): ## V = matrix(NA, nrow=n_rows, ncol=n_steps) for( jj in seq(n_rows, 1, -1) ){ for( ii in 1:jj ){ R = 1 + rrate[ii, jj] if( jj == n_steps ){ ## this is t=T-1 V[ii, jj] = 106 / R + 6 ## at t=T we get the face value plus the only coupon then we discount till t=T-1 and get another coupon }else if( jj == 1 ){ ## this is t=1 V[ii, jj] = ( q*V[ii, jj+1] + (1-q)*V[ii+1, jj+1] )/R ## at t=1 we dont get a coupon payment }else{ V[ii, jj] = ( q*V[ii, jj+1] + (1-q)*V[ii+1, jj+1] )/R + 6 ## for other time points you get the bond plus the coupon } } } colnames(V) = paste0('t_', seq(0, n_years)) V_print = upper_triangular_to_ascending_triangle(V) ## print in the format of this chapter print(round(V_print, 2)) ## Part (b): ## V = matrix(NA, nrow=n_rows, ncol=n_steps) for( jj in seq(n_rows, 1, -1) ){ for( ii in 1:jj ){ R = 1 + rrate[ii, jj] if( jj == n_steps ){ ## this is t=T-1 pv = 106 / R + 6 ## at t=T we get the face value plus the only coupon then we discount till t=T-1 and get another coupon call_value = 100 + 6 ## OR we get the face value plus the due coupon V[ii, jj] = min(pv, call_value) }else if( jj == 1 ){ ## this is t=1 V[ii, jj] = ( q*V[ii, jj+1] + (1-q)*V[ii+1, jj+1] )/R ## at t=1 we dont get a coupon payment }else{ pv = ( q*V[ii, jj+1] + (1-q)*V[ii+1, jj+1] )/R + 6 ## for other time points you get the bond plus the coupon call_value = 100 + 6 ## OR we get the face value plus the due coupon if( jj-1 < 5 ){ V[ii, jj] = pv ## the bond is not callable yet }else{ V[ii, jj] = min(pv, call_value) } } } } colnames(V) = paste0('t_', seq(0, n_years)) V_print = upper_triangular_to_ascending_triangle(V) ## print in the format of this chapter print(round(V_print, 2))