source('utils.R') sigma = 0.2 S0 = 62 K = 60 delta_t = 1/12 r = 0.10 u = exp(sigma*sqrt(delta_t)) d = 1/u R = 1+ r/12 # total return over the next month q = (R - d)/(u - d) print(c(u, d, R, q)) n_months = 5 ## Build the stock lattice: ## SSpot = make_spot_table(S0, u, d, n_months) print('Spot lattice=') print(round(SSpot, 2)) C_European = evaluate_call_option(SSpot, K, q, R, type='European') print('European call option value=') print(round(C_European, 2)) ## Build the call and put lattices: ## P_European = evaluate_put_option(SSpot, K, q, R, type='European') print('European put option value=') print(round(P_European, 2)) ## Price the "as you like it" option by working backwards: ## n_months = 3 n_steps = n_months+1 asYouLikeIt = matrix(NA, nrow=n_steps, ncol=n_steps) for( jj in seq(n_months+1, 1, -1) ){ for( ii in 1:jj ){ if( jj == (n_months+1) ){ ## the final column/timestep asYouLikeIt[ii, jj] = max( c(C_European[ii, jj], P_European[ii, jj]) ) }else{ asYouLikeIt[ii, jj] = (q*asYouLikeIt[ii, jj+1] + (1-q)*asYouLikeIt[ii+1, jj+1])/R } } } print(round(asYouLikeIt, 2))