source('../Chapter3/utils.R') source('../Chapter12/utils.R') source('../Chapter14/utils.R') r0 = 0.07 u = 1.3 d = 0.9 ## Get the grid of short rates: ## n_years = 5 rrate = make_spot_table(r0, u, d, n_years) print('Short rate grid=') rrate = upper_triangular_to_ascending_triangle(rrate) print(round(rrate, 4)) ## Get the "Payment rate" lattice (the left lattice in Figure 14.8): ## Payments = matrix(NA, nrow=(n_years+1), ncol=(n_years+1)) for( jj in seq(n_years+1, 1, -1) ){ n = (n_years+1 - jj) ## number of years for( ii in (2+n_years-jj):(n_years+1) ){ if( jj==(n_years+1) ){ Payments[ii, jj] = 100 }else{ r = rrate[ii, jj] + 0.02 ## 2% over the short rate Payments[ii, jj] = annuity_payment(100, n, r) } } } colnames(Payments) = paste0('t_', seq(0, n_years)) print('Payments Rate Lattice=') print(round(Payments, 2)) ## Get the "Value grid": ## Value = matrix(NA, nrow=(n_years+1), ncol=(n_years+1)) for( jj in seq(n_years+1, 1, -1) ){ for( ii in (2+n_years-jj):(n_years+1) ){ if( jj==(n_years+1) ){ Value[ii, jj] = 0 }else if( jj==n_years ){ r = rrate[ii, jj] + 0.02 ## 2% over the short rate R = 1 + r Value[ii, jj] = 100 * R / ( 1 + rrate[ii, jj] ) - 100 }else{ YP = Payments[ii, jj] ## what payment would the bank receive at the end of the year r = rrate[ii, jj] + 0.02 ## 2% over the short rate Interest_Part = 100 * r New_Loan_Value = 100 - YP + Interest_Part numer = New_Loan_Value * ( 1 + (0.5 * Value[ii-1, jj+1] + 0.5 * Value[ii, jj+1])/100 ) + YP denom = 1 + rrate[ii, jj] Value[ii, jj] = numer / denom - 100 } } } colnames(Value) = paste0('t_', seq(0, n_years)) print('Value Per 100 Lattice=') print(round(Value, 3))