/* A procedure that generates evaluates a value function using Monte Carlo and writes the values in a .csv file for plotting in Excel. Written by Jonathan Goodman for assignment 8 in the class http://www.math.nyu.edu/faculty/goodman/teaching/DerivSec09/index.html It uses the native C random number generator rand(), which may not be that good. Feel free to replace this with a better one, but remember to set the seed. */ #include #include using namespace std; int path( double X[], int n, double tStart, double T, double xStart); #define NT 50 // The number of time steps in a path #define NP 100000 // The number of paths per Monte Carlo #define NV 20 // The number of values of f computed #define XMIN 80 // The smallest x value for which to calculate f #define XSTEP 10 // The distance between x values at which f is evaluated int main() { ofstream CSVfile; // Write the output in csv format for Excel to read. CSVfile.open( "f.csv"); double X[NT+1]; // The sample path double f; // The expected payout double xStart; // The starting point for paths. xStart = XMIN; for (int val = 0; val < NV; val++) { // The loop over x values where f is evaluated f = 0; for (int p = 0; p < NP; p++) { // The loop over sample paths. path( X, NT, 0, 1, xStart); f += X[NT]; } f = f / NP; CSVfile << xStart << " , " << f << endl; // Write the xtarting point and value of f into the .csv file xStart += XSTEP; // Get ready for the next f evaluation } CSVfile.close(); return 0; }