source('../Chapter12/utils.R') ## Part (b): ## r0 = 0.1 u = 1.2 d = 0.9 ## Get the grid of possible short rates: ## n_years = 4 rrate = make_spot_table(r0, u, d, n_years) print('Spot rates=') print(round(rrate, 4)) p_default = 0.1 p_no_default = 1 - p_default ## Solve for the_value of the bond that might default: ## n_steps = n_years+1 ## = number of columns = number of years + the initial spot_value n_rows = n_steps B = 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] ## the total discount rate if( jj == n_steps ){ ## the final column/timestep B[ii, jj] = ( p_no_default * 110 + p_default * 0 ) / R }else{ B[ii, jj] = ( p_no_default * ( 0.5 * B[ii, jj+1] + 0.5 * B[ii+1, jj+1] + 10 ) )/R } } } ## Give this table descriptive column names: ## colnames(B) = paste0('t_', seq(0, n_years)) print(round(B, 6))