source('../Chapter12/utils.R') source('../Chapter3/utils.R') sigma = 0.18 u = exp(sigma) d = 1/u r = 0.10 S0 = 100 ## the inital value of the house R = 1 + r ## total return over the next year q = (R - d)/(u - d) print(c(u, d, R, q)) n_years = 15 delta_t = 1 ## yearly steps ## Build the value lattice: ## SSpot = make_spot_table(S0, u, d, n_years) print('Spot value of the house=') print(round(SSpot, 1)) ## Build the value of the loan (the mortgage balance i.e. what you still have to pay): ## LA = c(0.9*S0) ## finance 90% of the loan initially A = annuity_payment(LA[length(LA)], n_years, r) ## what we will pay each year print(sprintf('Yearly payment amount A= %f', A)) for( ye in 1:n_years ){ new_loan_value = LA[length(LA)] * R - A ## the loan value increases due to interest and then we pay off an amount A LA = c(LA, new_loan_value) } print("Value of loan='") print(data.frame(start_of_year=seq(1, 16), loan_amount=round(LA, 2))) ## ## Working backwards lets evaluate the option to default: ## 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(LA[jj] - 1.05 * SSpot[ii, jj], 0) ## amount saved if we default }else{ pv_option = (q*V[ii, jj+1] + (1-q) *V[ii+1, jj+1])/R amt_saved_if_default = LA[jj] - 1.05 * SSpot[ii, jj] V[ii, jj] = max( c(pv_option, amt_saved_if_default) ) } } } print(round(V, 2)) print(sprintf('Put value= %f', V[1, 1]))