make_spot_table = function(S0, u, d, n_times){ ## ## make_spot_table(62, 1.05943, 0.9439, 5) # duplicates the spot grid in Figure 12.7 on page 332. ## n_steps = n_times+1 ## = number of columns = number of time units + one column for the initial spot value n_rows = n_steps Spot = matrix(NA, nrow=n_rows, ncol=n_steps) Spot[1, 1] = S0 for( jj in 2:n_steps ){ last_ii = jj ## the last row we will compute for in column jj for( ii in 1:last_ii ){ if( ii == 1 ){ Spot[ii, jj] = u * Spot[ii, jj-1] }else{ Spot[ii, jj] = d * Spot[ii-1, jj-1] } } } ## Give this table descriptive column names: ## colnames(Spot) = paste0('t_', seq(0, n_times)) return(Spot) } evaluate_call_option = function(SSpot, K, q, R, type='European'){ ## ## Working backwards evaluate a call option. ## n_steps = dim(SSpot)[2] # = number of columns = number of time units + one column for the initial spot value n_rows = n_steps V = matrix(NA, nrow=n_rows, ncol=n_steps) for( jj in seq(n_rows, 1, -1) ){ for( ii in 1:jj ){ if( jj == n_steps ){ ## the final column/timestep V[ii, jj] = max( c(SSpot[ii, jj]-K, 0) ) }else{ if( type=='European' ){ V[ii, jj] = (q*V[ii, jj+1] + (1-q)*V[ii+1, jj+1])/R } if( type=='American' ){ pv_option = (q*V[ii, jj+1] + (1-q) *V[ii+1, jj+1])/R early_exercise_value = max( c(SSpot[ii, jj]-K, 0) ) V[ii, jj] = max( c(pv_option, early_exercise_value) ) } } } } return(V) } evaluate_put_option = function(SSpot, K, q, R, type='European'){ ## ## Working backwards evaluate a put option. ## n_steps = dim(SSpot)[2] # = number of columns = number of time units + one column for the initial spot_value n_rows = n_steps V = matrix(NA, nrow=n_rows, ncol=n_steps) for( jj in seq(n_rows, 1, -1) ){ for( ii in 1:jj ){ if( jj == n_steps ){ ## the final column/timestep V[ii, jj] = max( c(K-SSpot[ii, jj], 0) ) }else{ if( type=='European' ){ V[ii, jj] = (q*V[ii, jj+1] + (1-q)*V[ii+1, jj+1])/R } if( type=='American' ){ pv_option = (q*V[ii, jj+1] + (1-q)*V[ii+1, jj+1])/R early_exercise_value = max( c(K-SSpot[ii, jj], 0) ) V[ii, jj] = max( c(pv_option, early_exercise_value) ) } } } } return(V) }