LINEBURG


<< . .

 3
( 20)



. . >>


Code 2.4: Test for uniqueness of IRR




15
2.3 Bonds

In this part we look at the treatment of bonds and similar ¬xed income securities. What distinguishes bonds
is that the future payments (of coupon, principal) are decided when the security is issued. We again limit
ourselves to the case of a ¬‚at term structure, and return to bond pricing with more general term structures
later.

2.3.1 Bond Price

The price of a bond is the present value of its future cash¬‚ows. If we consider a coupon bond like a US
goverment bond (T-Bond), the cash ¬‚ows look like

··· T
t= 01 2 3
··· C
Coupon C C C
Face value F

The current price of the bond is
T
C F
P0 = +
(1 + r)t (1 + r)T
t=1

with discrete compounding, and
T
e’rt C + e’rT F
P0 =
t=1

with continous compounding. The interest rate r is ¬xed, which means that the term structure is “¬‚at.”
Let us look at two versions of the bond price algorithm for the continous case.

#include <cmath>
#include <vector>
using namespace std;

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) {
double p = 0;
for (int i=0;i<coupon times.size();i++) {
p += exp(’r*coupon times[i])*coupon amounts[i];
};
for (int i=0;i<principal times.size();i++) {
p += exp(’r*principal times[i])*principal amounts[i];
};
return p;
};


Code 2.5: Bond price

There are two version of the routine listed, one which is called with both interest and principal vectors
(code 2.5) and another (code 2.6) which is simply called with the cash¬‚ows. I show 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 cash¬‚ows, 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


16
#include <cmath>
#include <vector>
using namespace std;

double bonds price(const vector<double>& cash¬‚ow times,
const vector<double>& cash¬‚ows,
const double& r) {
double p=0;
for (int i=0;i<cash¬‚ow times.size();i++) {
p += exp(’r*cash¬‚ow times[i])*cash¬‚ows[i];
};
return p;
};


Code 2.6: Bond price


and what is principal. But in the simple cases considered here I stick to the case of one set of cash¬‚ows, it
makes the routines easier to follow.
Let us also look at the case of discrete (annual) compounding, shown in code 2.7.

#include <cmath>
#include <vector>
using namespace std;

double bonds price discrete(const vector<double>& cash¬‚ow times,
const vector<double>& cash¬‚ows,
const double& r) {
double p=0;
for (int i=0;i<cash¬‚ow times.size();i++) {
p +=cash¬‚ows[i]/(pow((1+r),cash¬‚ow times[i]));
};
return p;
};


Code 2.7: Bond price


2.3.2 Yield to maturity.

The yield to maturity is the interest rate that makes the present value of the future coupon payments equal
to the current bondprice, that is, for a known price P0 , the yield is the solution y to the equation
T
e’yt C + e’yT F
P0 =
t=1

Note that this is the same as ¬nding the internal rate of return, if we include in the cash ¬‚ows the price of
the bond as a negative cash ¬‚ow “today”. The algoritm shown in code 2.8 is simple bisection, we know that
the yield is above zero, and ¬nd a maximium yield which the yield is below, and then bisect the interval
until we are “close enough.”




17
#include <cmath>
#include "fin_recipes.h"

double bonds yield to maturity( const vector<double>& cash¬‚ow times,
const vector<double>& cash¬‚ow amounts,
const double& bondprice) {
const ¬‚oat ACCURACY = 1e’5;
const int MAX ITERATIONS = 200;
double bot=0, top=1.0;
while (bonds price(cash¬‚ow times, cash¬‚ow amounts, top) > bondprice) {
top = top*2;
};
double r = 0.5 * (top+bot);
for (int i=0;i<MAX ITERATIONS;i++){
double di¬ = bonds price(cash¬‚ow times, cash¬‚ow amounts,r) ’ bondprice;
if (std::fabs(di¬)<ACCURACY) return r;
if (di¬>0.0) { bot=r;}
{ top=r; };
else
r = 0.5 * (top+bot);
};
return r;
};


Code 2.8: Bond yield




18
2.3.3 Duration.

The duration of a bond is the “weighted average maturity” of the bond.

tCt Pt
Duration =
Bond Price
t


where

• Ct is the cash ¬‚ow in period t, and
• Pt is the discount factor, the current price of a discount bond paying $1 at time t.

There are two versions of the duration calculation, based on how one estimate the bond price. One assumes
the current interest rate r and calculates
T
e’rt Ct
Bond price =
t=1

which gives the duration calculation shown in code 2.9

#include <cmath>
#include <vector>
using namespace std;

double bonds duration(const vector<double>& cash¬‚ow times,
const vector<double>& cash¬‚ows,
const double& r) {
double S=0;
double D1=0;
for (int i=0;i<cash¬‚ow times.size();i++){
S += cash¬‚ows[i] * exp(’r*cash¬‚ow times[i]);
D1 += cash¬‚ow times[i] * cash¬‚ows[i] * exp(’r*cash¬‚ow times[i]);
};
return D1 / S;
};


Code 2.9: Bond duration

Alternatively one can calculate the yield to maturity for the bond, and use that in estimating the bond price.
This is called Macaulay Duration First one calculates y, the yield to maturity, from
T
e’yt Ct
Bond price =
t=1

and then use this y in the duration calculation:

tCt e’yt
Macaulay duration =
e’yt Ct

Code 2.10

Modi¬ed Duration
duration
Modi¬ed Duration =
yield



19
#include "fin_recipes.h"

double bonds duration macaulay(const vector<double>& cash¬‚ow times,
const vector<double>& cash¬‚ows,
const double& bond price) {
double y = bonds yield to maturity(cash¬‚ow times, cash¬‚ows, bond price);
return bonds duration(cash¬‚ow times, cash¬‚ows, y); // use YTM in duration
};




Code 2.10: Calculating the Macaulay duration of a bond

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

double bonds duration modi¬ed (const vector<double>& cash¬‚ow times,
const vector<double>& cash¬‚ow amounts,
const double& bond price,
const double& r){
double dur = bonds duration(cash¬‚ow times, cash¬‚ow amounts, r);
double y = bonds yield to maturity(cash¬‚ow times, cash¬‚ow amounts, bond price);
return dur/(1+y);
};


Code 2.11: Modi¬ed duration

Bond convexity

Convexity measures the curvature of the approximation done when using duration. It is calculated as
n
ci t2 e’yti
i
i=1




#include <cmath>
#include <vector>
using namespace std;

double bonds convexity(const vector<double>& cash¬‚ow times,
const vector<double>& cash¬‚ow amounts,
const double& y ) {
double C=0;
for (int i=0;i<cash¬‚ow times.size();i++){
double t = cash¬‚ow times[i];
C+= cash¬‚ow amounts[i] * t * t * exp(’y*t);
};
return C;
};


Code 2.12: Bond convexity




20
Chapter 3

The term structure of interest rates and an object lesson
Contents
3.1 Term structure calculations . . . . . . . . . . . . . . . . . . .......... . . 21
3.2 Using the currently observed term structure. . . . . . . . .......... . . 22
3.2.1 Linear Interpolation. . . . . . . . . . . . . . . . . . . . . . . ........... . . 22
3.3 The term structure as an object . . . . . . . . . . . . . . . .......... . . 22
3.4 Implementing a term structure class . . . . . . . . . . . . . .......... . . 23
3.4.1 Base class . . . . . . . . . . . . . . . . . . . . . . . . . . . . ........... . . 23
3.4.2 Flat term structure. . . . . . . . . . . . . . . . . . . . . . . ........... . . 25
3.4.3 Interpolated term structure. . . . . . . . . . . . . . . . . . . ........... . . 26
3.5 Bond calculations using the term structure class . . . . . .......... . . 28


In this chapter we look at various algorithms that has been used to estimate a “term structure,” i e a relation
between length of period for investment and interest rate.
The term structure is the current price for a future (time t) payment of one dollar (discount factor). It can
also be viewed as the yield on a zero coupon bond paying one dollar at time t. Alternatively one can think
about forward interest rates, the yield on borrowing at some future date t1 and repaying it at a later date
t2 . Knowing one of these one also knows the others, since there are one-to-one transforms moving one into
the other.


3.1 Term structure calculations

Let us show some useful transformations for moving between these three alternative views of the term
structure. . Let r(t) be the yield on a t“period discount bond, and d(t) the discount factor for time t (the
current price of a bond that pays $1 at time t. Then

d(t) = e’r(t)t

’ ln(d(t))
r(t) =
t

Also, the forward rate for borrowing at time t1 for delivery at time T is calculated as

d(T ) d(t1 )
’ ln ln
d(t1 ) d(T )
ft (t1 , T ) = =
T ’ t1 T ’ t1

The forward rate can also be calculated directly from yields as

T ’t t1 ’ t
’ rd (t, t1 )
fd (t, t1 , T ) = rd (t, T )
T ’ t1 T ’ t1

Note that this assumes continously compounded interest rates.
Code 3.1 shows the implementation of these transformations.




21
#include <cmath>
using namespace std;

double term structure yield from discount factor(const double& dfact,
const double& t) {
return (’log(dfact)/t);
}

double term structure discount factor from yield(const double& r,
const double& t) {
return exp(’r*t);
};

double term structure forward rate from disc facts(const double& d t,
const double& d T,
const double& time) {
return (log (d t/d T))/time;
};

double term structure forward rate from yields(const double& r t1,
const double& r T,
const double& t1,
const double& T) {
return (r T*(T/(T’t1))’r t1*(t1/T));
};


Code 3.1: Term structure transformations

3.2 Using the currently observed term structure.

To just use todays term structure, we need to take the observations of yields that is observed in the market
and use these to generate a term structure. The simplest possible way of doing this is to linearly interpolate
the currently observable zero coupon yields.

3.2.1 Linear Interpolation.

If we are given a set of yields for various maturities, the simplest way to construct a term structure is by
straightforward linear interpolation between the observations we have to ¬nd an intermediate time. For
many purposes this is “good enough.” This interpolation can be on either yields, discount factors or forward
rates, we illustrate the case of linear interpolation of spot rates.

Computer algorithm, linear interpolation of yields. Note that the algorithm assumes the yields are
ordered in increasing order of time to maturity.


3.3 The term structure as an object

To actually use the term structure one has to specify one of these three alternative views of the term stucuture
for all possible future dates. This is of course not feasible, so one will need to have a method for specifying
the term structure, such as the linear interpolation above. Next this term structure will have to be made
available for use in other contexts. This is perfect for specifying a class, so we will use this as the prime
example of the uses of a class. One can think of the term structure class as an abstract function that either
return a discount factor or a yield.

<< . .

 3
( 20)



. . >>

Copyright Design by: Sunlight webdesign