-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptions.cpp
More file actions
209 lines (173 loc) · 5.7 KB
/
Copy pathOptions.cpp
File metadata and controls
209 lines (173 loc) · 5.7 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "Options.h"
#include "BinModel.h"
#include "BinDepLattice.h"
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <random>
#include <chrono>
using namespace std;
// Creates a tree of stock prices
void Option::StockPriceingTree(BinLatticeDep<double>& Price, BinModel Model) {
Price.SetN(N);
Price.SetNode(0, 0, Model.GetS0());
for (int z = 0; z < N; ++z) {
double it = pow(2, z) - 1;
for (int i = 0; i <= it; ++i) {
Price.SetNode(z + 1, 2 * i + 1, Price.GetNode(z, i) * (1 + Model.GetU()));
Price.SetNode(z + 1, 2 * i, Price.GetNode(z, i) * (1 + Model.GetD()));
}
}
}
// Prices the option using the CRR method
void Option::PriceByCRR(BinLatticeDep<double>& Price, BinModel Model) {
auto start = std::chrono::steady_clock::now();
double N = GetN();
StockPriceingTree(Price, Model);
PayoffTree(Price);
for (int n = N; n > 0; --n) {
for (int i = 0; i <= pow(2, n) - 1; i += 2) {
double z = (Price.GetNode(n, i) * (1 - Model.RiskNeutProb()) +
Price.GetNode(n, i + 1) * Model.RiskNeutProb()) /
(1 + Model.GetR());
Price.SetNode(n - 1, i / 2, z);
}
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
cout << "CRR Price: " << Price.GetNode(0, 0) << endl;
cout << "Time taken for CRR Price: " << elapsed_seconds.count() << " seconds" << endl;
Price.Clear();
}
// Prices the option using Monte Carlo simulation
void Option::PriceByMC(BinModel Model, double M) {
auto start = std::chrono::steady_clock::now();
double x = 0;
vector<double> StorePayOff(M);
for (int n = 0; n < M; ++n) {
vector<double> Path;
PathSim(Path, Model);
StockPathSim(Path, Model);
double payoff = Payoff(Path);
StorePayOff[n] = payoff;
x += payoff;
}
double Avg = x / M;
double riskNeutral = pow(1 / (1 + Model.GetR()), N);
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
cout << "MC Estimation: " << Avg * riskNeutral << endl;
double rootM = sqrt(M);
double unbiasedEst = 0;
for (int n = 0; n < M; ++n) {
unbiasedEst += pow(StorePayOff[n] - Avg, 2);
}
unbiasedEst = sqrt(unbiasedEst / (M - 1));
double StandardError = (unbiasedEst / rootM) * riskNeutral;
cout << "MC Standard Error: " << StandardError << endl;
cout << "Time taken for MC Price: " << elapsed_seconds.count() << " seconds" << endl;
StorePayOff.clear();
}
// Simulates a path of 1s and 0s for stock movements
void Option::PathSim(vector<double>& Path, BinModel Model) {
Path.resize(N);
Path[0] = Model.GetS0();
random_device rd;
mt19937 gen(rd());
uniform_real_distribution<double> dist(0.0, 1.0);
for (int i = 1; i <= N; ++i) {
double random = dist(gen);
Path[i] = (random <= Model.RiskNeutProb()) ? 1 : 0;
}
}
// Uses the path of 1s and 0s to model stock price movements
void Option::StockPathSim(vector<double>& Path, BinModel Model) {
double Initial = Model.GetS0();
double U = Model.GetU();
double D = Model.GetD();
for (int i = 1; i <= N; ++i) {
if (Path[i] == 1) {
Initial *= (1 + U);
} else if (Path[i] == 0) {
Initial *= (1 + D);
} else {
Path[0] = Model.GetS0();
}
Path[i] = Initial;
}
}
// Gets input for the Asian call option
void ArthAsianCall::GetInput() {
int N;
cout << "Enter Expiry time: "; cin >> N;
SetN(N);
cout << "Enter Strike Price: "; cin >> K;
SetK(K);
}
// Creates a payoff tree for the Asian call option
void ArthAsianCall::PayoffTree(BinLatticeDep<double>& Price) {
double N = GetN();
for (int i = 0; i <= pow(2, N) - 1; ++i) {
double Sum = Price.GetNode(N, i);
int z = i;
for (int n = N; n > 1; --n) {
if (z % 2 == 1) {
Sum += Price.GetNode(n - 1, (z - 1) / 2);
z = (z - 1) / 2;
} else {
Sum += Price.GetNode(n - 1, z / 2);
z = z / 2;
}
}
double Avg = Sum / N;
Price.SetNode(N, i, max(Avg - K, 0.0));
}
}
// Computes the payoff for a given path for the Asian call option
double ArthAsianCall::Payoff(const vector <double>& Path)
{
double N = GetN();
double pay = 0;
for (int i = 1; i <= N; i++)
{
pay += Path[i];
}
if((pay/N)-K>0){ return (pay/N)-K;}
else{ return 0;}
}
// Gets input for the LookBack option
void LookBack::GetInput() {
int N;
cout << "Enter Expiry time: "; cin >> N;
SetN(N);
cout << "Enter Strike Price: "; cin >> K;
SetK(K);
}
// Creates a payoff tree for the LookBack option
void LookBack::PayoffTree(BinLatticeDep<double>& Price) {
double N = GetN();
for (int i = 0; i < pow(2, N); ++i) {
double x = Price.GetNode(N, i);
double y = x;
int z = i;
for (int n = N; n >= 1; --n) {
if (z % 2 == 1) {
y = min(y, Price.GetNode(n - 1, (z - 1) / 2));
z = (z - 1) / 2;
} else {
y = min(y, Price.GetNode(n - 1, z / 2));
z = z / 2;
}
}
Price.SetNode(N, i, max(x - y, 0.0));
}
}
// Computes the payoff for a given path for the LookBack option
double LookBack::Payoff(const vector<double>& Path) {
double N = GetN();
double Terminal = Path[N];
double Min = *min_element(Path.begin(), Path.begin() + N + 1);
return max(Terminal - Min, 0.0);
}