#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 */ int main() { // 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; // 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]=max(stockTree[n][j]-X,0.); } 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]); } } cout << " V(S="<