Bond Price.

The current price of the bond is

with discrete compounding, and

with continous compounding. The interest rate is fixed, which means that the term structure is ``flat.''

Let us look at two versions of the bond price algorithm for the continous case.

// file bonds_price_both.cc // author: Bernt A Oedegaard. #include <cmath> #include <vector> double bonds_price(const vector<double>& coupon_times, const vector<double>& coupon_amounts, const vector<double>& principal_times, const vector<double>& principal_amounts, const double& r) // calculate bond price when term structure is flat, // given both coupon and principals { double p = 0; for (unsigned i=0;i<coupon_times.size();i++) { p += exp(-r*coupon_times[i])*coupon_amounts[i]; }; for (unsigned i=0;i<principal_times.size();i++) { p += exp(-r*principal_times[i])*principal_amounts[i]; }; return p; };

// file bonds_price.cc // author: Bernt A Oedegaard. #include <cmath> #include <vector> double bonds_price(const vector<double>& cashflow_times, const vector<double>& cashflows, const double& r) { // calculate bond price when term structure is flat, double p=0; for (unsigned i=0;i<cashflow_times.size();i++) { p += exp(-r*cashflow_times[i])*cashflows[i]; }; return p; };

There are two version of the routine listed, one which is called with both interest and principal vectors, another which is simply called with the cashflows. I put up both to make one think about the fact that for most purposes the distinction between coupons and principals is not necessary to make, what counts is the cashflows, which is the sum of coupons and principal. There are cases where the distinction is important, for example when taxes are involved. Then we need to keep track of what is interest and what is principal. But in the simple cases considered here we stick to the case of cashflows, it makes the routines easier to follow.

Let us also look at the case of discrete (annual) compounding:

// file bonds_price.cc // author: Bernt A Oedegaard. #include <cmath> #include <vector> double bonds_price_discrete(const vector<double>& cashflow_times, const vector<double>& cashflows, const double& r) { // calculate bond price when term structure is flat, double p=0; for (unsigned i=0;i<cashflow_times.size();i++) { p +=cashflows[i]/(pow((1+r),cashflow_times[i])); }; return p; };

In the previous we assumed the term structure was flat. In most cases
that is not a reasonable assumption, we need to use a general
`term_structure` for discounting.

where is a function that discounts a payment at time to its value today. For example, a flat term structure has the following simple discount function:

For routines involving the term structure a

// file bonds_price_termstru.cc // author: Bernt Arne Odegaard #include <vector> #include "term_structure_class.h" double bonds_price(const vector<double>& cashflow_times, const vector<double>& cashflows, const term_structure_class& d) { double p = 0; for (unsigned i=0;i<cashflow_times.size();i++) { p += d.discount_factor(cashflow_times[i])*cashflows[i]; }; return p; };