source('http://waxworksmath.com/Authors/A_F/Conte/Code/Chapter3/utils.R') ##source('../../../../Conte/Code/Chapter3/utils.R') source('../Chapter12/utils.R') source('../Chapter14/utils.R') r0 = 0.07 u = 1.3 d = 0.9 ## Part (a): ## ## 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, 3)) N = 10 ## the notional dollar amount Payments = N * rrate ## these are the payments that must be made at the end of each year print('Payments= ') print(round(Payments, 3)) ## Price the SWAP: ## V = 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) ){ r = rrate[ii, jj] if( jj==(n_years+1) ){ V[ii, jj] = Payments[ii, jj] / ( 1 + r ) }else{ V[ii, jj] = ( Payments[ii, jj] + 0.5 * V[ii, jj+1] + 0.5 * V[ii-1, jj+1] ) / ( 1 + r ) } } } colnames(V) = paste0('t_', seq(0, n_years)) print('SWAP Value=') print(round(V, 3)) ## There are two ways of working this problem: ## ## 1) Using the Fixed rate == Variable rate equation ## rrate = make_spot_table(r0, u, d, n_years+1) rrate = upper_triangular_to_ascending_triangle(rrate) print('Short rate grid (one extra year)=') print(round(rrate, 3)) ## Compute the elementary prices from the short rates: ## E0 = short_rate_lattice_to_elementary_prices(rrate) print(round(E0, 3)) ## Sum the columns of the elementary price grid: ## E0[is.na(E0)] = 0 E00 = colSums(E0) E00 = E00[-1] print(E00) r = V[n_years+1, 1] / ( N * sum(E00) ) print(sprintf('Fixed rate SWAP==Variable rate SWAP: r= %f', r)) ## 2) Using the Fixed rate == Variable rate equation ## ## 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, 3)) fixed_rate_r_to_swap_value = function(rfixed, rrate){ ## ## For a fixed interest rate for payment evaluate a swap value ## n_years = dim(rrate)[1]-1 Payment = N * rfixed ## these are the fixed rate payments that must be made at the end of each year ## Price the SWAP with these payments: ## V = 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) ){ r = rrate[ii, jj] if( jj==(n_years+1) ){ V[ii, jj] = Payment / ( 1 + r ) }else{ V[ii, jj] = ( Payment + 0.5 * V[ii, jj+1] + 0.5 * V[ii-1, jj+1] ) / ( 1 + r ) } } } ##V[n_years+1, 1] ## return the initial price V ## return the full grid } ## ## Test this function: ##print(fixed_rate_r_to_swap_value(0.08, rrate)) ## rt = bisection_method(0.05, 0.1, function(x) {V=fixed_rate_r_to_swap_value(x, rrate); V[dim(V)[1], 1] - 3.9790}) print(sprintf('Build fixed rate payment lattice: r= %f', rt[1])) ## For Exercise 9 we need both grids: ## V_floating = V V_fixed = fixed_rate_r_to_swap_value(rt[1], rrate) print('V_floating=') print(round(V_floating, 3)) print('V_fixed=') print(round(V_fixed, 3))