-
Notifications
You must be signed in to change notification settings - Fork 1
/
SimpleMC2.cpp
36 lines (30 loc) · 914 Bytes
/
SimpleMC2.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
#include "SimpleMC2.h"
#include "Random1.h"
#include <cmath>
// the basic math functions should be in
// namespace std but aren’t 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;
}