Skip to content

Instantly share code, notes, and snippets.

@honake
Last active October 21, 2016 05:48
Show Gist options
  • Save honake/77f35b4a653839fc366ef10088cd15ce to your computer and use it in GitHub Desktop.
Save honake/77f35b4a653839fc366ef10088cd15ce to your computer and use it in GitHub Desktop.
compare MC and BS
#include <iostream>
#include <cmath>
#include "UTRandom1.h"
#include "UTNormals.h"
using namespace std;
double SimpleMonteCarlo1(double Expiry,
double Strike,
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*std::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 = thisSpot - Strike;
thisPayoff = thisPayoff >0 ? thisPayoff : 0;
runningSum += thisPayoff;
}
double mean = runningSum / NumberOfPaths;
mean *= exp(-r*Expiry);
return mean;
}
double blackSholesCall(double Expiry,
double Strike,
double Spot,
double Vol,
double r)
{
double d1=(log(Spot/Strike) + (r+Vol*Vol/2.)*(Expiry))/(Vol*sqrt(Expiry));
double d2=(log(Spot/Strike) + (r-Vol*Vol/2.)*(Expiry))/(Vol*sqrt(Expiry));
return CumulativeNormal(d1)*Spot - CumulativeNormal(d2)*Strike*exp(-r*(Expiry));
}
int main()
{
double Expiry;
double Strike;
double Spot;
double Vol;
double r;
unsigned long NumberOfPaths;
cout << "\nEnter expiry\n";
cin >> Expiry;
cout << "\nEnter strike\n";
cin >> Strike;
cout << "\nEnter spot\n";
cin >> Spot;
cout << "\nEnter vol\n";
cin >> Vol;
cout << "\nr\n";
cin >> r;
cout << "\nNumber of paths\n";
cin >> NumberOfPaths;
double mc = SimpleMonteCarlo1(Expiry,
Strike,
Spot,
Vol,
r,
NumberOfPaths);
double ana = blackSholesCall(Expiry,
Strike,
Spot,
Vol,
r);
cout << "Monte Carlo: " << mc << "\n";
cout << "Analytic: " << ana << "\n";
double tmp;
cin >> tmp;
return 0;
}
/* Output
Enter expiry
2
Enter strike
14000
Enter spot
14500
Enter vol
0.38
r
0.06
Number of paths
10000
Monte Carlo: 4055.57
Analytic: 4007.4
done
Program ended with exit code: 0
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment