Next: Convexity Up: Bond Algoritms. Previous: Yield to maturity.   Contents   Index

Subsections

# Duration.

## Simple duration.

where
• is the cash flow in period , and
• is the discount factor, the current price of a discount bond paying \$1 at time .
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

where
• is the cashflow in period , and
• is the (continous) yield to maturity.

// 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

// 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: Convexity Up: Bond Algoritms. Previous: Yield to maturity.   Contents   Index
Bernt Arne Odegaard
1999-09-09