spot_rates_to_discount_factors = function(sk){ ## ## Converts spot rates s_k into discount factors d_k ## ks = 1:length(sk) dk = (1+sk)^(-ks) return(dk) } spot_rates_to_short_rates = function(sk){ ## ## Converts spot rates s_k into short rates r_k ## rk = c() for( k in 1:length(sk) ){ numer = (1+sk[k])^k if( length(rk)==0 ){ denom = 1 }else{ denom = prod(1+rk) } r_next = numer / denom - 1 rk = c(rk, r_next) } return(rk) } spot_rates_to_next_years_spot_rates = function(sk){ n_spots = length(sk) js = 2:n_spots numer = (1+sk[js])^js denom = (1+sk[1])^1 f_1t = ( numer/denom )^(1/(js-1)) - 1 return(f_1t) } ## The first row of the forward rate table (the first row are spot rates): s_k = f_{0, k} ## sk = c(6.00, 6.45, 6.80, 7.10, 7.36, 7.56, 7.77) sk = sk/100 print('spot rates:') print(round(sk * 100, 2)) ## The first row of the discount factor table: d_k ## dk = spot_rates_to_discount_factors(sk) print('discount rates:') print(round(dk, 3)) ## The first row of the short rate table: r_k ## rk = spot_rates_to_short_rates(sk) print('short rates:') print(round(rk * 100, 2)) ## Produce the spot rates in each expected year: ## print("Expected spot rates each year:") print(round(sk * 100, 2)) for( rowi in 1:6 ){ sk_next_year = spot_rates_to_next_years_spot_rates(sk) print(round(sk_next_year * 100, 2)) sk = sk_next_year ## go to the next year ##print(round(spot_rates_to_discount_factors(sk), 3)) ## these are the entries in the Discount factor table }