LINEBURG


<< . .

 4
( 20)



. . >>





22
#include <vector>
using namespace std;
#include "fin_recipes.h"

double term structure yield linearly interpolated(const double& time,
const vector<double>& obs times,
const vector<double>& obs yields) {
// assume the yields are in increasing time to maturity order.
int no obs = obs times.size();
if (no obs<1) return 0;
double t min = obs times[0];
if (time <= t min) return obs yields[0]; // earlier than lowest obs.

double t max = obs times[no obs’1];
if (time >= t max) return obs yields[no obs’1]; // later than latest obs

int t=1; // ¬nd which two observations we are between
while ( (t<no obs) && (time>obs times[t])) { ++t; };
double lambda = (obs times[t]’time)/(obs times[t]’obs times[t’1]);
// by ordering assumption, time is between t-1,t
double r = obs yields[t’1] * lambda + obs yields[t] * (1.0’lambda);
return r;
};


Code 3.2: Interpolated term structure from spot rates


3.4 Implementing a term structure class

A term structure can thus be abstractly described as a function of time. The user of a term structure will
not need to know the underlying model of term structures, all that is needed is an interface that speci¬es
functions for

• prices of zero coupon bonds (discount factors).
• yields of zero coupon bonds (spot rates).
• forward rates.

These will for given parameters and term structure models provide all that a user will want to use for
calculations.

3.4.1 Base class


#ifndef TERM STRUCTURE CLASS H
#de¬ne TERM STRUCTURE CLASS H

class term structure class {
public:
virtual double yield(const double& T) const;
virtual double discount factor(const double& T) const;
virtual double forward rate(const double&, const double&) const;
};

#endif


Code 3.3:

The code for these functions uses algorithms that are described earlier in this chapter for transforming


23
between various views of the term structure. The term structure class merely provide a convenient interface
to these algoritms.

#include "term_structure_class.h"
#include "fin_recipes.h"

double term structure class::forward rate(const double& t1, const double& T) const{
double d1 = discount factor(t1);
double dT = discount factor(T);
return term structure forward rate from disc facts(d1,dT,T’t1);
};

double term structure class::yield(const double& t) const{
return term structure yield from discount factor(discount factor(t),t);
};

double term structure class::discount factor(const double& t) const {
return term structure discount factor from yield(yield(t),t);
};


Code 3.4:

Note that the de¬nitions of calculations are circular. Any given speci¬c type of term structure has to over-ride
at least one of the functions yield, discount factor or forward rate.
We next consider two examples of speci¬c term structures.




24
3.4.2 Flat term structure.

The ¬‚at term structure overrides both the yield member function of the base class.
The only piece of data this type of term structure needs is an interest rate.

#ifndef TERM STRUCTURE CLASS FLAT
#de¬ne TERM STRUCTURE CLASS FLAT

#include "term_structure_class.h"

class term structure class ¬‚at : public term structure class {
private:
double R ; // interest rate
public:
term structure class ¬‚at(const double& r);
virtual double yield(const double& t) const;
void set int rate(const double& r);
};

#endif


Code 3.5:


#include "term_structure_class_flat.h"

term structure class ¬‚at::term structure class ¬‚at(const double& r){ R = r; }

double term structure class ¬‚at::yield(const double& T) const { if (T>=0) return R ; return 0;};

void term structure class ¬‚at::set int rate(const double& r) { R = r; };


Code 3.6:




25
3.4.3 Interpolated term structure.

The interpolated term structure implemented here uses a set of observations of yields as a basis, and for
observations in between observations will interpolate between the two closest. The following only provides
implementations of calculation of the yield, for the other two rely on the base class code.
There is some more book-keeping involved here, need to have code that stores observations of times and
yields.

#ifndef TERM STRUCTURE CLASS INTERPOLATED
#de¬ne TERM STRUCTURE CLASS INTERPOLATED

#include "term_structure_class.h"
#include <vector>
using namespace std;

class term structure class interpolated : public term structure class {
private:
vector<double> times ; // use to keep a list of yields
vector<double> yields ;
void clear();
public:
term structure class interpolated();
term structure class interpolated(const vector<double>& times, const vector<double>& yields);
virtual ˜term structure class interpolated();
term structure class interpolated(const term structure class interpolated&);
term structure class interpolated operator= (const term structure class interpolated&);

int no observations() const { return times .size(); };
virtual double yield(const double& T) const;
void set interpolated observations(vector<double>& times, vector<double>& yields);
};

#endif


Code 3.7:




26
#include "term_structure_class_interpolated.h"
#include "fin_recipes.h"

void term structure class interpolated::clear(){
times .erase(times .begin(), times .end());
yields .erase(yields .begin(), yields .end());
};

term structure class interpolated::term structure class interpolated():term structure class(){clear();};

term structure class interpolated::term structure class interpolated(const vector<double>& in times,
const vector<double>& in yields) {
clear();
if (in times.size()!=in yields.size()) return;
times = vector<double>(in times.size());
yields = vector<double>(in yields.size());
for (int i=0;i<in times.size();i++) {
times [i]=in times[i];
yields [i]=in yields[i];
};
};

term structure class interpolated::˜term structure class interpolated(){ clear();};

term structure class interpolated::term structure class interpolated(const term structure class interpolated& term) {
times = vector<double> (term.no observations());
yields = vector<double> (term.no observations());
for (int i=0;i<term.no observations();i++){
times [i] = term.times [i];
yields [i] = term.yields [i];
};
};

term structure class interpolated
term structure class interpolated::operator= (const term structure class interpolated& term) {
times = vector<double> (term.no observations());
yields = vector<double> (term.no observations());
for (int i=0;i<term.no observations();i++){
times [i] = term.times [i];
yields [i] = term.yields [i];
};
return (*this);
};

double term structure class interpolated::yield(const double& T) const {
return term structure yield linearly interpolated(T, times , yields );
};

void
term structure class interpolated::set interpolated observations(vector<double>& in times,
vector<double>& in yields) {
clear();
if (in times.size()!=in yields.size()) return;
times = vector<double>(in times.size());
yields = vector<double>(in yields.size());
for (int i=0;i<in times.size();i++) {
times [i]=in times[i];
yields [i]=in yields[i];
};
};


Code 3.8:




27
3.5 Bond calculations using the term structure class

Codes 3.9 and 3.10 illustrates how one would calculate bond prices and duration if one has a term structure
class.

#include <vector>
using namespace std;

#include "term_structure_class.h"

double bonds price(const vector<double>& cash¬‚ow times,
const vector<double>& cash¬‚ows,
const term structure class& d) {
double p = 0;
for (unsigned i=0;i<cash¬‚ow times.size();i++) {
p += d.discount factor(cash¬‚ow times[i])*cash¬‚ows[i];
};
return p;
};


Code 3.9: Pricing a bond with a term structure class


#include "term_structure_class.h"
#include <vector>
using namespace std;

double bonds duration(const vector<double>& cash¬‚ow times,
const vector<double>& cash¬‚ow amounts,
const term structure class& d ) {
double S=0;
double D1=0;
for (unsigned i=0;i<cash¬‚ow times.size();i++){
S += cash¬‚ow amounts[i] * d.discount factor(cash¬‚ow times[i]);
D1 += cash¬‚ow times[i] * cash¬‚ow amounts[i] * d.discount factor(cash¬‚ow times[i]);
};
return D1/S;
};


Code 3.10: Calculating a bonds duration with a term structure class


References Shiller [1990] is a good reference on the term structure.




28
Chapter 4

Futures algoritms.
Contents
4.1 Pricing of futures contract. .............................. 29


In this we discuss algoritms used in valuing futures contracts.


4.1 Pricing of futures contract.

The futures price of an asset without payouts is the future value of the current price of the assset.

ft = er(T ’t) St


#include <cmath>
using namespace std;

double futures price(const double& S, // current price of underlying asset
const double& r, // risk free interest rate
const double& time to maturity) {
return exp(r*time to maturity)*S;
};


Code 4.1: Futures price


The program

void test futures price(){
double S=100; double r=0.10; double time=0.5;
cout << " futures price = " << futures price(S,r, time) << endl;
};


provides the output
futures price = 105.127

Example 4.1: Futures price




29
Chapter 5

Binomial option pricing
Contents
5.1 Multiperiod binomial pricing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32


Option and other derivative pricing is one of the prime “success stories” of modern ¬nance. An option is a
derivative security, the cash ¬‚ows from the security is a function of the price of some other security, typically
called the underlying security. A call option is a right, but not obligation, to buy a given quantity of the
underlying security at a given price, called the exercise price K, within a certain time interval. A put option
is the right, but not obligation, to sell a given quantity of the underlying security to an agreed excercise
price within a given time interval. If an option can only be exercised (used) at a given date (the time interval
is one day), the option is called an European Option. If the option can be used in a whole time period up
to a given date, the option is called American.
An option will only be used if it is valuable to the option holder. In the case of a call option, this is when the
exercise price K is lower than the price one alternatively could buy the underlying security for, which is the
current price of the underlying security. Hence, options have never negative cash ¬‚ows at maturity. Thus,
for anybody to be willing to o¬er an option, they must have a cost when entered into. This cost, or price,
is typically called an option premium. As notation, let C signify the price of a call option, P the price of a
put option and S the price of the underlying security. All of these prices are indexed by time. We typically
let 0 be “now” and T the ¬nal maturity date of the option. From the de¬nition of the options, it is clear
that at their last possible exercise date, the maturity date, they have cash ¬‚ows.

CT = max(0, ST ’ K)

PT = max(0, K ’ ST )

The challenge of option pricing is to determine the option premium C0 and P0 .
All pricing considers that the cash¬‚ows from the derivative is a direct function of the price of the underlying
security. Pricing can therefore be done relative to the price of the underlying security. To price options it is
necessary to make assumptions about the probability distribution of movements of the underlying security.
We start by considering this in a particularly simple framework, the binomial assumption. The price of the
underlying is currently S0 . The price can next period only take on two values, Su and Sd .


¨ Su
¨
B
¨
¨¨
¨
¨¨
r S0
rr
r
rr
r Sd
rj


If one can ¬nd all possible future “states,” an enumeration of all possibilities, one can value a security by
constructing arti¬cial “probabilities”, called “state price probabilities,” which one use to ¬nd an arti¬cial
expected value of the underlying security, which is then discounted at the risk free interest rate. The binomial
framework is particularly simple, since there are only two possible states. If we ¬nd the “probability” q of

<< . .

 4
( 20)



. . >>

Copyright Design by: Sunlight webdesign