## ## Get the function spot_rates_to_discount_factors: ## source('dup_table_4_2.R') ## ## Get the spot rates: ## source('exercise_2.R') ## ## Part (a): ## dk = spot_rates_to_discount_factors(s) dk = c(1, dk) print(round(dk, 3)) x = c(-40, rep(10, 6)) ## the cash flow PV = sum( dk * x ) print(sprintf('PV (dicount method)= %f', PV)) ## ## Part (b): ## ndk = length(dk) dk_n_kp1 = c( dk[ndk] / dk[ndk-1] ) ndk = ndk - 1 while( ndk > 0 ){ dk_n_kp1 = c( dk[ndk] / dk[ndk-1], dk_n_kp1 ) ndk = ndk - 1 } print('d_{k, k+1}=') print(dk_n_kp1) nx = length(x) PV = x[nx] k = nx-1 while(k > 0){ PV = x[k] + dk_n_kp1[k] * PV k = k-1 } print(sprintf('PV (running method)= %f', PV))