source('../Chapter12/utils.R') source('../Chapter14/utils.R') ## Generate the entries in Figure 14.11 (this verifies our calculations are correct) ## spot_rates = c(7.67, 8.27, 8.81, 9.31, 9.75, 10.16, 10.52, 10.85, 11.15, 11.42, 11.67, 11.89, 12.09, 12.27) ak = c(7.67, 8.863, 9.878, 10.79, 11.49, 12.18, 12.64, 13.12, 13.5, 13.79, 14.1, 14.23, 14.4, 14.51) ##b = 0.01 ## does not seem to match the numbers b = 0.02 ## this will match the numbers in the book n_years = length(spot_rates)-1 rrate = constant_b_ho_lee_model_to_short_rate(ak, b, n_years) print('rrate=') print(round(rrate, 2)) E0 = short_rate_lattice_to_elementary_prices(rrate) print('Elementary prices=') print(round(E0, 3)) E0[is.na(E0)] = 0 ## sum the columns of the elemntary price grid E00 = colSums(E0) print('Bond prices=') print(E00) ks = 0:(n_years+1) sk = (1/E00)^(1/ks) - 1 sk_pct = sk[-1] * 100 ## drop the first element and conver to a percent print('Forward rates=') print(round(sk_pct, 2)) ## Lets us a minimization routine to estimate ak: ## print(model_term_structure_match_min_fn(spot_rates, ak, b, model_to_use='ho_lee')) set.seed(42) par0 = 5:(n_years+5) sol = optim(par=par0, function(x){model_term_structure_match_min_fn(spot_rates, x, b, model_to_use='ho_lee')}) ak_est = sol$par print('ak estimate from optim=') print(round(ak_est, 3))