source('utils.R') r = 0.1 R = 1 + r ## one-year discount factor u = 1.2 ## the original example p_up = 0.75 d = 0.9 p_down = 0.27 q = (R - d)/(u - d) S0 = 400 ## current spot price ## Get the grid of gold spot prices: ## n_years = 10 SSpot = make_spot_table(S0, u, d, n_years) print('Spot gold price=') print(round(SSpot, 1)) ## Solve for the_value of the mine (in terms of K): ## n_steps = n_years+1 ## = number of columns = number of years + the initial spot_value n_rows = n_steps K = matrix(NA, nrow=n_rows, ncol=n_steps) for( jj in seq(n_steps, 1, -1) ){ for( ii in 1:jj ){ if( jj == n_steps ){ ## the final column/timestep K[ii, jj] = 0 }else{ if( jj == (n_steps-1) ){ ## the one before last column/timestep g_hat = q*SSpot[ii, jj+1] + (1-q)*SSpot[ii+1, jj+1] g = (SSpot[ii, jj] + g_hat)/2 K[ii, jj] = (g/R)^2 / 2000 }else{ K_right = K[ii, jj+1] ## the node just to the right of this spot K_prime = K[ii+1, jj+1] ## the node just to the right and below this spot K_hat = q*K_right + (1-q)*K_prime g_hat = q*SSpot[ii, jj+1] + (1-q)*SSpot[ii+1, jj+1] g = (SSpot[ii, jj] + g_hat)/2 K[ii, jj] = (g - K_hat)^2 / ( 2000 * R^2 ) + K_hat/R ## moving R will include the discount factor into the profit } } } } print('K=') print(round(K, 1)) x0 = 50000 ## initial amount of gold in the mine V0 = K[1, 1] * x0 print(sprintf('Value of the lease= %f (MM)', V0/1e6) )