Share Brilliantly
fixed income risk
let cashFlows = [
{t:0.5, value:100},
{t:1, value:100},
];
let rates = function(t){
//returns the appropriate rate
}
function pv(cashFlows, rates){
let sum = 0;
for(let flow of cashFlows){
sum += flow.value*Math.exp(-1*flow.t*rates(flow.t));
}
return sum;
}
function duration(cashFlows, rates){
let P = pv(cashFlows, rates);
let rates2 = function(t){
return rates(t)+0.00001;
}
let deltaP = pv(cashFlows, rates2) - P;
let D = -1 * deltaP/(0.00001 * P);
return D;
}
Try it!