-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleMC.cpp
36 lines (32 loc) · 915 Bytes
/
SimpleMC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "SimpleMC.h"
#include "Random1.h"
#include <cmath>
// the basic math functions should be in
// namespace std but arenft in VCPP6
#if !defined(_MSC_VER)
using namespace std;
#endif
double SimpleMonteCarlo2(const PayOff& thePayOff,
double Expiry,
double Spot,
double Vol,
double r,
unsigned long NumberOfPaths)
{
double variance = Vol*Vol*Expiry;
double rootVariance = sqrt(variance);
double itoCorrection = -0.5*variance;
double movedSpot = Spot*exp(r*Expiry + itoCorrection);
double thisSpot;
double runningSum = 0;
for (unsigned long i = 0; i < NumberOfPaths; i++)
{
double thisGaussian = GetOneGaussianByBoxMuller();
thisSpot = movedSpot*exp(rootVariance*thisGaussian);
double thisPayOff = thePayOff(thisSpot);
runningSum += thisPayOff;
}
double mean = runningSum / NumberOfPaths;
mean *= exp(-r*Expiry);
return mean;
}