Numeric Methods for Duration

Overview


Given the defintion of Duration
{% \frac{dP}{dy} = -D \times P %}
The duration can be approximated by
{% D \approx - \frac{\Delta P}{P \times \Delta y} %}
D can be computed by finding the Present Value, of the set of cash flows using the current rate curve, and then computing the present value of the cash flows to the same curve with a small {% \Delta y %} added.

{% \Delta P %} is the the difference between the two present values.

The same methodology can be applied when calculating key rate durations or any other custom defined duration.

Implementation



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!

Contents