#include #include #include #include #include using namespace std; /* Template code for the binomial tree * Instructions to build the code are here * http://www.maths.manchester.ac.uk/~pjohnson/Tutorials/node1.html */ // return the payoff of the function you want to evaluate double payoff(double S,double X) { return max(S-X,0.); } // return the value of the binomial tree double binomialTree(double S0,double X,double T,double r,double sigma,int n) { // declare and initialise local variables (u,d,q) double dt,u,d,q; dt = T/n; u = exp(sigma*sqrt(dt)); d = exp(-sigma*sqrt(dt)); q = (exp(r*dt)-d)/(u-d); // create storage for the stock price tree and option price tree vector> stockTree(n+1,vector(n+1)); // setup and initialise the stock price tree for(int i=0;i<=n;i++) { for(int j=0;j<=i;j++) { stockTree[i][j]=S0*pow(u,j)*pow(d,i-j); } } vector> valueTree(n+1,vector(n+1)); for(int j=0;j<=n;j++) { valueTree[n][j]=payoff(stockTree[n][j],X); } for(int i=n-1;i>=0;i--) { for(int j=0;j<=i;j++) { valueTree[i][j] = exp(-r*dt)*( q*valueTree[i+1][j+1] + (1-q)*valueTree[i+1][j]); } } return valueTree[0][0]; } int main() { // declare and initialise Black Scholes parameters // declare and initialise Black Scholes parameters double S0=100.,X=100.,T=1.,r=0.06,sigma=0.2; // declare and initialise tree paramaters (steps in tree) int n=3; cout << " V(S="<