next up previous contents index
Next: Convexity Up: Bond Algoritms. Previous: Yield to maturity.   Contents   Index

Subsections


Duration.


Simple duration.


\begin{displaymath}D_1 = \sum \frac{t C(t)P(t)}{\sum C(t)P(t)} \end{displaymath}

where First, case where term structure is flat



// file bonds_duration.cc
// author: Bernt Arne Odegaard

#include <cmath>
#include <vector>

double bonds_duration(const vector<double>& cashflow_times,
		      const vector<double>& cashflows,
		      const double& r) 
//   calculate the duration of a bond, simple case where the term 
//   structure is flat, interest rate r.
{
    double S=0;
    double D1=0;
    for (unsigned i=0;i<cashflow_times.size();i++){
	S  +=                     cashflows[i] * exp(-r*cashflow_times[i]);
 	D1 += cashflow_times[i] * cashflows[i] * exp(-r*cashflow_times[i]);
    };
    return D1 / S;
};


More general term structure:



// file duration.cc
// author: Bernt A Oedegaard.

#include <vector>
#include "term_structure_class.h"

double bonds_duration(const vector<double>& cashflow_times,
		      const vector<double>& cashflow_amounts,
		      const term_structure_class& d ) {
    double S=0;
    double D1=0;
    for (unsigned i=0;i<cashflow_times.size();i++){
	S += cashflow_amounts[i] * d.discount_factor(cashflow_times[i]);
	D1 += cashflow_times[i] * cashflow_amounts[i] * d.discount_factor(cashflow_times[i]);
    };
    return D1/S;
};



Macaulay Duration


\begin{displaymath}D_2 = \sum \frac{t C(t)e^{-yt}}{\sum C(t)e^{-yt}} \end{displaymath}

where



// file bonds_duration_macaulay.cc
// author: Bernt A Oedegaard.
// calculate Macaulay duration.


#include "fin_algoritms.h"
 
double bonds_duration_macaulay(const vector<double>& cashflow_times,
			       const vector<double>& cashflows,
			       const double& bond_price) {
  double y = bonds_yield_to_maturity(cashflow_times, cashflows, bond_price);
  return bonds_duration(cashflow_times, cashflows, y); // use YTM in duration 
};



Modified Duration


\begin{displaymath}\textrm{Modified Duration} = \frac{\textrm{duration}}{\textrm{yield}} \end{displaymath}



// file bonds_duration_modified.cc
// author: Bernt A Oedegaard.

#include <vector>
#include "fin_algoritms.h"

double bonds_duration_modified (const vector<double>& cashflow_times,
				const vector<double>& cashflow_amounts,
				const double& bond_price,
				const double& r)
{
    double dur = bonds_duration(cashflow_times, cashflow_amounts, r);
    double y = bonds_yield_to_maturity(cashflow_times,cashflow_amounts,
				       bond_price);
    return dur/y;
};



next up previous contents index
Next: Convexity Up: Bond Algoritms. Previous: Yield to maturity.   Contents   Index
Bernt Arne Odegaard
1999-09-09