//10.11.js // 2024-09-04 // $Id: 10.11.js 1.1 2024/11/03 06:30:00 s Exp $ // // a : amount // d : discount factor, d[k] = d(0,k) // f : forward rate, f[k] = f(k-1,k) // g(a,r,pv1) : pv2 - pv1 // pv1 : present value in variable rate // pv2 : present value in fixed rate // r : fixed rate // s : spot rate, s[k] = s(k) // x : cash flow in variable rate, x[0] = x(k) // y : cash flow in fixed rate, y = a * r 'use strict' // function const g = (a, r, pv1) => { // g = pv2 - pv1 let d=[]; d[1] = 1 / (1 + r) ** 1; d[2] = 1 / (1 + r) ** 2; d[3] = 1 / (1 + r) ** 3; d[4] = 1 / (1 + r) ** 4; d[5] = 1 / (1 + r) ** 5; d[6] = 1 / (1 + r) ** 6; const y = a * r; const pv2 = y * (d[1] + d[2] + d[3] + d[4] + d[5] + d[6]); return pv2 - pv1; } const sum = (x, y) => x + y; // input const a = 1000; const s = [0.00, 0.070, 0.073, 0.077, 0.081, 0.084, 0.088]; // calculation let f = [0, 0, 0, 0, 0, 0, 0]; for(let i = 0; i < s.length; i += 1){ if(i == 0){ f[0] = 0; }else if (i == 1){ f[1] = s[1]; }else { f[i] = (1 + s[i]) ** i / (1 + s[i - 1]) ** (i - 1) - 1; } } const x = f.map((x) => a * x); const d = s.map((_, i) => 1 / (1 + s[i]) ** i); const pv1 = x.map((_, i) => x[i] * d[i]).reduce(sum, 0); let r; for(let i = 8700; i < 8900; i += 1){ r = i / 1e5; if( g(a, r, pv1) > 0){ break; } } // output console.log('10.11.js'); console.log('a', a); console.log('s', ...s.map(x => x.toFixed(3).padStart(7))); console.log('f', ...f.map(x => x.toFixed(3).padStart(7))); console.log('x', ...x.map(x => x.toFixed(3).padStart(7))); console.log('d', ...d.map(x => x.toFixed(3).padStart(7))); console.log('pv1 in variable rates ', pv1); console.log('constant r, g(r)', r, g(a,r,pv1)); // end