LINEBURG


<< . .

 18
( 20)



. . >>

return ’99.9; // should never get here
};


Code A.3: Approximation to the cumulative bivariate normal


If one has more than two correlated variables, the calculation of cumulative probabilites is a nontrivial
problem. One common method involves Monte Carlo estimation of the de¬nite integral. We will consider
this, but then it is necessary to ¬rst consider simulation of random normal variables.




131
A.5 Simulating random normal numbers

Generation of random numbers is a large topic and is treated at length in such sources as Knuth [1997].
The generated numbers can never be truly random, only “pseudo”-random, they will be generated according
to some reproducible algorithm and after a (large) number of random number generations the sequence
will start repeating itself. The number of iterations before replication starts is a measure of the quality
of a random number generator. For anybody requiring high-quality random number generators the rand()
function provided by the standard C++ library should be avoided, but for not getting into involved discussion
of random number generations we use this function as a basis for the generation of uniformly distributed
numbers in the interval [0, 1), as shown in code A.4.

#include <cstdlib>

double random uniform 0 1(void){
return double(rand())/double(RAND MAX); // this uses the C library random number generator.
};


Code A.4: Pseudorandom numbers from an uniform [0, 1) distribution

These uniformly distributed distributed random variates are used as a basis for the Polar method for normal
densities discussed in Knuth [1997] and inplemented as shown in code A.5

#include <cmath>
#include <cstdlib>
//using namespace std;

double random uniform 0 1(void);

double random normal(void){
double U1, U2, V1, V2;
double S=2;
while (S>=1) {
U1 = random uniform 0 1();
U2 = random uniform 0 1();
V1 = 2.0*U1’1.0;
V2 = 2.0*U2’1.0;
S = pow(V1,2)+pow(V2,2);
};
double X1=V1*sqrt((’2.0*log(S))/S);
return X1;
};


Code A.5: Pseudorandom numbers from a normal (0, 1) distribution



A.6 Cumulative probabilities for general multivariate distributions

When moving beyond


A.7 References

Tong [1990] discusses the multivariate normal distribution, and is a good reference.




132
A.8 Exercises

Exercise 13.
Replace the random uniform function here by an alternative of higher quality, by looking into what numerical
libraries is available on your computing platform, or by downloading a high quality random number generator from
such places as mathlib or statlib.




133
Appendix B

C++ concepts
This chapter contains a listing of various C/C++ concepts and some notes on each of them.

class

const
double
exp(x) (C function). De¬ned in <cmath>. Returns the natural exponent e to the given power x, ex .
Indexation (in vectors and matrices). To access element number i in an array A, use A[i-1]. Well known
trap for people coming to C from other languages. Present in C for historical e¬ciency reasons. Arrays
in C were implemented using pointers. When referring to the ¬rst element in the array,
log(x) (C function). De¬ned in <cmath>
namespace
standard namespace
vector (C++ container class). De¬ned in <vector>




134
Appendix C

Summarizing routine names
In many of the algorithms use is made of other routines. To simplify the matter all routines are summarised
in one header ¬le, fin recipes.h. This appendix shows this ¬le.

// ¬le: ¬n recipes.h
// author: Bernt Arne Oedegaard
// de¬nes all routines in the ¬nancial numerical recipes “book”

#include <vector>
using namespace std;

///////// present value ////////////////////////////////////
// discrete coumpounding

double bonds price discrete(const vector<double>& cash¬‚ow times,
const vector<double>& cash¬‚ows,
const double& r);

double cash ¬‚ow pv discrete (const vector<double>& c¬‚ow times,
const vector<double>& c¬‚ow amounts,
const double& r);
// continous compounding
double cash ¬‚ow pv (const vector<double>& c¬‚ow times,
const vector<double>& c¬‚ow amounts,
const double& r);

double cash ¬‚ow irr(const vector<double>& c¬‚ow times,
const vector<double>& c¬‚ow amounts);

bool cash ¬‚ow unique irr(const vector<double>& c¬‚ow times,
const vector<double>& c¬‚ow amounts);

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 bonds price(const vector<double>& cash¬‚ow times,
const vector<double>& cash¬‚ows,
const double& r);


double bonds duration(const vector<double>& cash¬‚ow times,
const vector<double>& cash¬‚ows,
const double& r);

double bonds yield to maturity( const vector<double>& cash¬‚ow times,
const vector<double>& cash¬‚ow amounts,
const double& bondprice);

double bonds duration macaulay(const vector<double>& cash¬‚ow times,
const vector<double>& cash¬‚ows,
const double& bond price);

double bonds duration modi¬ed (const vector<double>& cash¬‚ow times,
const vector<double>& cash¬‚ow amounts,
const double& bond price,
const double& r);



135
double bonds convexity(const vector<double>& cash¬‚ow times,
const vector<double>& cash¬‚ow amounts,
const double& y);

/// term structure basics

double term structure
yield from discount factor(const double& dfact, const double& t);
double term structure
discount factor from yield(const double& r, const double& t);
double term structure
forward rate from disc facts(const double& d t, const double& d T, const double& T);
double term structure
forward rate from yields(const double& r t1, const double& r T,
const double& t1, const double& T);
double term structure yield linearly interpolated(const double& time,
const vector<double>& obs times,
const vector<double>& obs yields);
//// Futures pricing
double futures price(const double& S, const double& r, const double& time to maturity);

/// Binomial option pricing

double option price call european binomial( const double& S, const double& K, const double& r,
const double& u, const double& d); // one periode binomial

double option price call european binomial( const double& S, const double& K, const double& r,
const double& u, const double& d, const int& no periods);
// multiple periode binomial

vector< vector<double> > binomial tree(const double& S0,
const double& u,
const double& d,
const int& no steps);

/// Black Scholes formula //////////////////////////////////////////

double option price call black scholes(const double& S, const double& K, const double& r,
const double& sigma, const double& time) ;
double option price put black scholes (const double& S, const double& K, const double& r,
const double& sigma, const double& time) ;

double
option price implied volatility call black scholes newton( const double& S, const double& K,
const double& r, const double& time,
const double& option price);

double option price implied volatility call black scholes bisections( const double& S, const double& K,
const double& r, const double& time,
const double& option price);
double option price delta call black scholes(const double& S, const double& K, const double& r,
const double& sigma, const double& time);
double option price delta put black scholes (const double& S, const double& K, const double& r,
const double& sigma, const double& time);
void option price partials call black scholes(const double& S, const double& K, const double& r,
const double& sigma, const double& time,
double& Delta, double& Gamma, double& Theta,
double& Vega, double& Rho);
void option price partials put black scholes(const double& S, const double& K, const double& r,
const double& sigma, const double& time,
double& Delta, double& Gamma, double& Theta,
double& Vega, double& Rho);

/// warrant price
double warrant price adjusted black scholes(const double& S, const double& K,
const double& r, const double& sigma,
const double& time,
const double& no warrants outstanding,
const double& no shares outstanding);




136
double warrant price adjusted black scholes(const double& S, const double& K,
const double& r, const double& q,
const double& sigma, const double& time,
const double& no warrants outstanding,
const double& no shares outstanding);

/// Extensions of the Black Scholes model //////////////

double option price european call payout(const double& S, const double& K, const double& r,
const double& b, const double& sigma, const double& time);
double option price european put payout (const double& S, const double& K, const double& r,
const double& b, const double& sigma, const double& time);
double option price european call dividends(const double& S, const double& K, const double& r,
const double& sigma, const double& time,
const vector<double>& dividend times,
const vector<double>& dividend amounts );
double option price european put dividends( const double& S, const double& K, const double& r,
const double& sigma,const double& time,
const vector<double>& dividend times,
const vector<double>& dividend amounts);
double option price american call one dividend(const double& S, const double& K, const double& r,
const double& sigma,
const double& tau, const double& D1, const double& tau1);
double futures option price call european black(const double& F, const double& K, const double& r,
const double& sigma, const double& time);
double futures option price put european black(const double& F, const double& K, const double& r,
const double& sigma, const double& time);
double currency option price call european(const double& S, const double& K, const double& r,
const double& r f, const double& sigma, const double& time);
double currency option price put european(const double& S, const double& K, const double& r,
const double& r f, const double& sigma, const double& time);
double option price american perpetual call(const double& S, const double& K, const double& r,
const double& q, const double& sigma);
double option price american perpetual put(const double& S, const double& K, const double& r,
const double& q, const double& sigma);

// binomial option approximation ////////////////

double option price call european binomial(const double& S, const double& K, const double& r,
const double& sigma, const double& t, const int& steps);
double option price put european binomial (const double& S, const double& K, const double& r,
const double& sigma, const double& t, const int& steps);
double option price call american binomial(const double& S, const double& K, const double& r,
const double& sigma, const double& t, const int& steps);
double option price put american binomial (const double& S, const double& K, const double& r,
const double& sigma, const double& t, const int& steps);
double option price call american binomial(const double& S, const double& K,
const double& r, const double& y,
const double& sigma, const double& t, const int& steps);
double option price put american binomial (const double& S, const double& K, const double& r,
const double& y, const double& sigma,
const double& t, const int& steps);

double option price call american discrete dividends binomial( const double& S, const double& K,
const double& r,
const double& sigma, const double& t,
const int& steps,
const vector<double>& dividend times,
const vector<double>& dividend amounts);

double option price put american discrete dividends binomial(const double& S, const double& K,
const double& r,
const double& sigma, const double& t,
const int& steps,
const vector<double>& dividend times,
const vector<double>& dividend amounts);




137
double option price call american proportional dividends binomial(const double& S, const double& K,
const double& r, const double& sigma,
const double& time, const int& no steps,
const vector<double>& dividend times,
const vector<double>& dividend yields);

double option price put american proportional dividends binomial( const double& S, const double& K, const double& r,
const double& sigma, const double& time, const int& no steps,
const vector<double>& dividend times,
const vector<double>& dividend yields);

double option price delta american call binomial(const double& S, const double& K, const double& r,
const double& sigma, const double& t, const int& no steps);
double option price delta american put binomial(const double& S, const double& K, const double& r,
const double& sigma, const double& t, const int& no steps);
void option price partials american call binomial(const double& S, const double& K, const double& r,
const double& sigma, const double& time, const int& no steps,
double& delta, double& gamma, double& theta,
double& vega, double& rho);

void option price partials american put binomial(const double& S, const double& K, const double& r,
const double& sigma, const double& time, const int& no steps,
double& delta, double& gamma, double& theta,
double& vega, double& rho);

double futures option price call american binomial(const double& F, const double& K, const double& r, const double& sigma,
const double& time, const int& no steps);

double futures option price put american binomial( const double& F, const double& K, const double& r, const double& sigma,
const double& time, const int& no steps);

double currency option price call american binomial( const double& S, const double& K, const double& r, const double& r f,
const double& sigma, const double& t, const int& n);

double currency option price put american binomial( const double& S, const double& K, const double& r, const double& r f,
const double& sigma, const double& t, const int& n);

//////////////////// ¬nite di¬erences //////////////////

double option price call american ¬nite di¬ explicit( const double& S, const double& K, const double& r,
const double& sigma, const double& time,
const int& no S steps, const int& no t steps);

double option price put american ¬nite di¬ explicit( const double& S, const double& K, const double& r,
const double& sigma, const double& time,
const int& no S steps, const int& no t steps);

double option price call european ¬nite di¬ explicit( const double& S, const double& K, const double& r,
const double& sigma, const double& time,
const int& no S steps, const int& no t steps);

double option price put european ¬nite di¬ explicit( const double& S, const double& K, const double& r,
const double& sigma, const double& time,
const int& no S steps, const int& no t steps);

double option price call american ¬nite di¬ implicit( const double& S, const double& K, const double& r,
const double& sigma, const double& time,
const int& no S steps, const int& no t steps);

double option price put american ¬nite di¬ implicit( const double& S, const double& K, const double& r,
const double& sigma, const double& time,
const int& no S steps, const int& no t steps);

double option price call european ¬nite di¬ implicit( const double& S, const double& K, const double& r,
const double& sigma, const double& time,
const int& no S steps, const int& no t steps);




138
double option price put european ¬nite di¬ implicit( const double& S, const double& K, const double& r,
const double& sigma, const double& time,
const int& no S steps, const int& no t steps);

///////////////////////// simulated option prices //////////////////////////////////////
// Payo¬ only function of terminal price
double option price call european simulated(const double& S, const double& K,
const double& r, const double& sigma,
const double& time to maturity, const int& no sims);
double option price put european simulated(const double& S, const double& K,

<< . .

 18
( 20)



. . >>

Copyright Design by: Sunlight webdesign