-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMCTest.cpp
183 lines (144 loc) · 5.29 KB
/
MCTest.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//This is the test cpp file
// for MC method on option pricing
#include "MCOptionData.hpp"
#include <cmath>
#include <iostream>
#include <vector>
#include<numeric> //find mean and sd
//we need to define some functions
using namespace std;
struct SimOut{
double mean;
double sd;
double se;
};
double getMean(const std::vector<double>& v)
{ //get mean of a double vector
double sum = std::accumulate(v.begin(), v.end(), 0.0);
double out = sum / v.size();
return(out);
}
double getSD(const std::vector<double>& v)
// No need disconted factor here since I have done it in calculating option price
{ //get std of a double vector
double sq_sum = std::inner_product(v.begin(), v.end(), v.begin(), 0.0);
double mu = getMean(v);
double out = std::sqrt((sq_sum - (1/ v.size()) * mu * mu)/(v.size() - 1));
return(out);
}
double getSE(const std::vector<double>& v)
{
double out = getSD(v)/sqrt(v.size());
return(out);
}
SimOut getMCOptionPrice(long NT, long NSIM, const OptionData& op, double S)
{ // This is a function to get MC simulated mean and std on one option at current price S
double dt = op.T/NT;// parameters needed in the formula
double A = op.r - op.b;
double B = op.sig;
double sqrt_dt = sqrt(dt);
SimOut out; // output holder
vector<double> SimsHolder(NSIM);
double S_old = S;
double S_new;
for(long j =0; j<NSIM; j++)
{
for(long i =0; i<NT; i++)
{
S_new = S_old + A*S_old*dt + B*S_old*sqrt_dt*getz(); // the function
S_old = S_new;
}
SimsHolder[j] = exp(-op.r*op.T)*op.PayOff(S_new); // for each sim, the pay-off is obtained
S_old = S; //Set the S_old to the original value
}
out.mean = getMean(SimsHolder);
out.sd = getSD(SimsHolder);
out.se = getSE(SimsHolder);
return(out);
}
vector<vector<double> > MC_MeanPrice_NTvsNSIM(const vector<long>& NT, const vector<long>& NSIM, const OptionData& op, double S, string OutPutMetric = "mean")
{ //this is a function doing simultion across NT and NSIM
// you can also choose the metric you want to measure the simulation performance
long rowsize = NSIM.size();
long colsize = NT.size();
vector<vector<double> > out(rowsize, vector<double>(colsize));
SimOut temp;
for(long i = 0; i<rowsize; i++)
{
for(long j = 0; j<colsize; j++)
{
temp = getMCOptionPrice(NT[j], NSIM[i], op, S);
if (OutPutMetric == "mean"){
out[i][j] = temp.mean;
} else if (OutPutMetric == "sd"){
out[i][j] = temp.sd;
} else if (OutPutMetric == "se"){
out[i][j] = temp.se;
} else {
out[i][j] = temp.mean;
}
}
}
return(out);
}
int main(){
////////////////////////////////////////////////////////////////////////////
//////////// Group C ////////
/////////// ////////
////////////////////////////////////////////////////////////////////////////
//////////////////////////// Question a and b ////////////////////////////////
// Bacth 1 and Batch 2
OptionData op1;
op1.T = 1.5;
op1.K =120.0;
op1.sig = .4;
op1.r =0.04;
op1.b = 0.0;
op1.OptionType = "P";
// OptionData op2;
// op2.T = 1;
// op2.K =100;
// op2.sig = .20;
// op2.r =0.0;
// op2.b = 0.0;
// op2.OptionType = "C";
long arr1[] = {10, 100, 500};
vector<long> NTs(arr1, arr1 + (sizeof(arr1)/sizeof(long)));
long arr2[] = {10, 50, 100, 500, 1000, 5000, 10000};
vector<long> NSIMs(arr2, arr2 + (sizeof(arr2)/sizeof(long)));
vector<vector<double> > NT_vs_NSIM_op1 = MC_MeanPrice_NTvsNSIM(NTs, NSIMs, op1, 100);
// vector<vector<double> > NT_vs_NSIM_op2 = MC_MeanPrice_NTvsNSIM(NTs, NSIMs, op2, 100);
cout << "MC simluated Call prices for Batch 1 across conditions: " << endl;
Print2d(NT_vs_NSIM_op1);
// cout << "\nMC simluated Call prices for Batch 2 across conditions: " << endl;
// Print2d(NT_vs_NSIM_op2);
// //////////////////////////// Question c ////////////////////////////////
// OptionData Batch4;
// Batch4.T = 30;
// Batch4.K =100.0;
// Batch4.sig = .30;
// Batch4.r =0.08;
// Batch4.b = 0.0;
// Batch4.OptionType = "C";
// vector<vector<double> > NT_vs_NSIM_Batch4 = MC_MeanPrice_NTvsNSIM(NTs, NSIMs, Batch4, 100);
// cout << "\n\nMC simluated Call prices for Batch 4 across conditions: " << endl;
// Print2d(NT_vs_NSIM_Batch4);
// ////////////////////////////////////////////////////////////////////////////
// //////////// Group D ////////
// /////////// /////////
// ////////////////////////////////////////////////////////////////////////////
// //////////////////////////// Question a&b ////////////////////////////////
// vector<vector<double> > NT_vs_NSIM_op1_sd = MC_MeanPrice_NTvsNSIM(NTs, NSIMs, op1, 60, "sd");
// vector<vector<double> > NT_vs_NSIM_op2_sd = MC_MeanPrice_NTvsNSIM(NTs, NSIMs, op2, 100, "sd");
// cout << "\nMC simluated Call StDev for Batch 1 across conditions: " << endl;
// Print2d(NT_vs_NSIM_op1_sd);
// cout << "\nMC simluated Call StDev for Batch 2 across conditions: " << endl;
// Print2d(NT_vs_NSIM_op2_sd);
// vector<vector<double> > NT_vs_NSIM_op1_se = MC_MeanPrice_NTvsNSIM(NTs, NSIMs, op1, 60, "se");
// vector<vector<double> > NT_vs_NSIM_op2_se = MC_MeanPrice_NTvsNSIM(NTs, NSIMs, op2, 100, "se");
// cout << "\n\nMC simluated Call StErr for Batch 1 across conditions: " << endl;
// Print2d(NT_vs_NSIM_op1_se);
// cout << "\nMC simluated Call StErr for Batch 2 across conditions: " << endl;
// Print2d(NT_vs_NSIM_op2_se);
return 0;
}