-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathantithetic_variates.cpp
More file actions
99 lines (80 loc) · 2.6 KB
/
antithetic_variates.cpp
File metadata and controls
99 lines (80 loc) · 2.6 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <vector>
#include <cmath>
#include <random>
#include <algorithm>
#include <iomanip>
using namespace std;
const double S0 = 100.0;
const double K = 105.0;
const double r = 0.05;
const double sigma = 0.2;
const double T = 1.0;
const int n = 100000;
double callPayoff(double S, double K)
{
return max(S - K, 0.0);
}
void runStandardMonteCarlo(mt19937 &gen, normal_distribution<double> &dist)
{
double sumPayoff = 0.0;
double sumSqPayoff = 0.0;
double drift = (r - 0.5 * sigma * sigma) * T;
double vol = sigma * sqrt(T);
for (int i = 0; i < n; ++i)
{
double Z = dist(gen);
double ST = S0 * exp(drift + vol * Z);
double payoff = callPayoff(ST, K);
sumPayoff += payoff;
sumSqPayoff += payoff * payoff;
}
double meanPayoff = sumPayoff / n;
double meanPrice = exp(-r * T) * meanPayoff;
double variancePayoff = (sumSqPayoff / n) - meanPayoff * meanPayoff;
double varianceEstimator = variancePayoff / n;
double stdError = sqrt(varianceEstimator) * exp(-r * T);
cout << "--- Standard Monte Carlo ---" << endl;
cout << "Estimated Price: " << meanPrice << endl;
cout << "Std Error: " << stdError << endl;
}
void runAntitheticMonteCarlo(mt19937 &gen, normal_distribution<double> &dist)
{
double sumPayoff = 0.0;
double sumSqPayoff = 0.0;
double drift = (r - 0.5 * sigma * sigma) * T;
double vol = sigma * sqrt(T);
for (int i = 0; i < n / 2; ++i)
{
double Z = dist(gen);
double ST1 = S0 * exp(drift + vol * Z);
double payoff1 = callPayoff(ST1, K);
double ST2 = S0 * exp(drift + vol * (-Z));
double payoff2 = callPayoff(ST2, K);
double pairAverage = 0.5 * (payoff1 + payoff2);
sumPayoff += pairAverage;
sumSqPayoff += pairAverage * pairAverage;
}
int n_pairs = n / 2;
double meanPayoff = sumPayoff / n_pairs;
double meanPrice = exp(-r * T) * meanPayoff;
double variancePayoff = (sumSqPayoff / n_pairs) - meanPayoff * meanPayoff;
double varianceEstimator = variancePayoff / n_pairs;
double stdError = sqrt(varianceEstimator) * exp(-r * T);
cout << "--- Antithetic Variates ---" << endl;
cout << "Estimated Price: " << meanPrice << endl;
cout << "Std Error: " << stdError << endl;
}
int main()
{
random_device rd;
mt19937 gen(rd());
normal_distribution<double> dist(0.0, 1.0);
cout << fixed << setprecision(5);
cout << "Total Simulations: " << n << endl
<< endl;
runStandardMonteCarlo(gen, dist);
cout << endl;
runAntitheticMonteCarlo(gen, dist);
return 0;
}