-
Notifications
You must be signed in to change notification settings - Fork 1
/
EUoption.cpp
380 lines (316 loc) · 8.5 KB
/
EUoption.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// EUoption.cpp
//
// Constructors and Member Functions of EUoption Class
#include "EUoption.hpp"
#include <cmath>
#include <iostream>
//Gaussian Pdf and Cdf
double EUoption::n(double x) const
{
boost::math::normal_distribution<double> EUoptionStandardNormal;
return(pdf(EUoptionStandardNormal, x)); // I use boost package
};
double EUoption::N(double x) const
{
boost::math::normal_distribution<double> EUoptionStandardNormal;
return(cdf(EUoptionStandardNormal, x)); // I use boost package
};
// Kernel Functions (Haug)
double EUoption::CallPrice(double U, const OptionPara& para) const
{
double _T = para.T;
double _K = para.K;
double _sig = para.sig;
double _r = para.r;
double _b = para.b;
double tmp = _sig * sqrt(_T);
double d1 = ( log(U/_K) + (_b + (_sig*_sig)*0.5 ) * _T )/ tmp;
double d2 = d1 - tmp;
return (U * exp((_b-_r)*_T) * N(d1)) - (_K * exp(-_r * _T)* N(d2));
}
double EUoption::PutPrice(double U, const OptionPara& para) const
{
double _T = para.T;
double _K = para.K;
double _sig = para.sig;
double _r = para.r;
double _b = para.b;
double tmp = _sig * sqrt(_T);
double d1 = ( log(U/_K) + (_b + (_sig*_sig)*0.5 ) * _T )/ tmp;
double d2 = d1 - tmp;;
return (_K * exp(-_r * _T)* N(-d2)) - (U * exp((_b-_r)*_T) * N(-d1));
}
double EUoption::CallDelta(double U, const OptionPara& para) const
{
double _T = para.T;
double _K = para.K;
double _sig = para.sig;
double _r = para.r;
double _b = para.b;
double tmp = _sig * sqrt(_T);
double d1 = ( log(U/_K) + (_b + (_sig*_sig)*0.5 ) * _T )/ tmp;
return exp((_b-_r)*_T) * N(d1);
}
double EUoption::PutDelta(double U, const OptionPara& para) const
{
double _T = para.T;
double _K = para.K;
double _sig = para.sig;
double _r = para.r;
double _b = para.b;
double tmp = _sig * sqrt(_T);
double d1 = ( log(U/_K) + (_b + (_sig*_sig)*0.5 ) * _T )/ tmp;
return exp((_b-_r)*_T) * (N(d1) - 1.0);
}
double EUoption::CallGamma(double U, const OptionPara& para) const
{
double _T = para.T;
double _K = para.K;
double _sig = para.sig;
double _r = para.r;
double _b = para.b;
double tmp = _sig * sqrt(_T);
double d1 = ( log(U/_K) + (_b + (_sig*_sig)*0.5 ) * _T )/ tmp;
double d2 = d1 - tmp;
return ( n(d1) * exp((_b-_r)*_T) ) / (U * tmp);
}
double EUoption::PutGamma(double U, const OptionPara& para) const
{
return CallGamma(U, para);
}
/////////////////////////////////////////////////////////////////////////////////////
void EUoption::copy( const EUoption& o2)
{
this->SetPara(o2.GetPara());
this->SetType(o2.GetType());
}
EUoption::EUoption(){
ExactOption();
}
EUoption::EUoption(const OptionPara& para, string optionType){
//specific constructor
this->SetPara(para);
this->SetType(optionType);
if (this->GetType() == "c")
this->SetType("C");
}
EUoption::EUoption(const EUoption& o2)
{ // Copy constructor
copy(o2);
}
EUoption::~EUoption()
{
}
EUoption& EUoption::operator = (const EUoption& option2)
{
if (this == &option2) return *this;
copy (option2);
return *this;
}
// Functions that calculate option price and sensitivities
double EUoption::Price(double U) const
{
if (this->GetType() == "C")
{
//cout << "calling call\n";
return CallPrice(U, this->GetPara()); //uing member parameters
}
else
{
//cout << "calling put\n";
return PutPrice(U, this->GetPara());
}
}
//Overload Functions for Pricer
// Vector Pricer
vector<double> EUoption::Price(const vector<double>& U) const
{
int size_v = U.size();
vector<double> out(size_v);
for (int i = 0; i < size_v; i++){
out[i] = Price(U[i]);
}
return(out);
}
// Matrix Pricer
vector<vector<double> > EUoption::Price(const vector<double>& U, const vector<vector<double> >& ParaMatrix) const
{
int row_size = ParaMatrix.size(); // row size of output matrix
int num_para = ParaMatrix[0].size(); // col num of input matrix
int col_size = U.size(); // col size of output matrix
if (num_para != 5)
{ // To see if the input matrix is valid
cout << "The parameter matrix requires 5 columns!" << endl;
cout << "Colums are T, K, sig, r, b" << endl;
exit(1);
}
vector<vector<double> > out(row_size, vector<double> (col_size));
for (int i = 0; i < row_size; i++)
{
// the column order does matter
OptionPara TmpPara;
TmpPara.T = ParaMatrix[i][0];
TmpPara.K = ParaMatrix[i][1];
TmpPara.sig = ParaMatrix[i][2];
TmpPara.r = ParaMatrix[i][3];
TmpPara.b = ParaMatrix[i][4];
for(int j=0; j < col_size; j++)
{
if (this->GetType() == "C")
{
out[i][j] = CallPrice(U[j], TmpPara);
} else {
out[i][j] = PutPrice(U[j], TmpPara);
}
}
}
return(out);
}
//Overload Functions for Delta
// Vector Delta
double EUoption::Delta(double U) const
{
if (this->GetType() == "C")
return CallDelta(U, this->GetPara());
else
return PutDelta(U, this->GetPara());
}
vector<double> EUoption::Delta(const vector<double>& U) const
{
int size_v = U.size();
vector<double> out(size_v);
for (int i = 0; i < size_v; i++){
out[i] = Delta(U[i]);
}
return(out);
}
vector<vector<double> > EUoption::Delta(const vector<double>& U, const vector<vector<double> >& ParaMatrix) const
{
int row_size = ParaMatrix.size(); // row size of output matrix
int num_para = ParaMatrix[0].size(); // col num of input matrix
int col_size = U.size(); // col size of output matrix
if (num_para != 5)
{ // To see if the input matrix is valid
cout << "The parameter matrix requires 5 columns!" << endl;
cout << "Colums are T, K, sig, r, b" << endl;
exit(1);
}
vector<vector<double> > out(row_size, vector<double> (col_size));
for (int i = 0; i < row_size; i++)
{
// the column order does matter
OptionPara TmpPara;
TmpPara.T = ParaMatrix[i][0];
TmpPara.K = ParaMatrix[i][1];
TmpPara.sig = ParaMatrix[i][2];
TmpPara.r = ParaMatrix[i][3];
TmpPara.b = ParaMatrix[i][4];
for(int j=0; j < col_size; j++)
{
if (this->GetType() == "C")
{
out[i][j] = CallDelta(U[j], TmpPara);
} else {
out[i][j] = PutGamma(U[j], TmpPara);
}
}
}
return(out);
}
//Overload Functions for Gamma
double EUoption::Gamma(double U) const
{
if (this->GetType() == "C")
return CallGamma(U, this->GetPara());
else
return PutGamma(U, this->GetPara());
}
// Matrix Delta
vector<vector<double> > EUoption::Gamma(const vector<double>& U, const vector<vector<double> >& ParaMatrix) const
{
int row_size = ParaMatrix.size(); // row size of output matrix
int num_para = ParaMatrix[0].size(); // col num of input matrix
int col_size = U.size(); // col size of output matrix
if (num_para != 5)
{ // To see if the input matrix is valid
cout << "The parameter matrix requires 5 columns!" << endl;
cout << "Colums are T, K, sig, r, b" << endl;
exit(1);
}
vector<vector<double> > out(row_size, vector<double> (col_size));
for (int i = 0; i < row_size; i++)
{
// the column order does matter
OptionPara TmpPara;
TmpPara.T = ParaMatrix[i][0];
TmpPara.K = ParaMatrix[i][1];
TmpPara.sig = ParaMatrix[i][2];
TmpPara.r = ParaMatrix[i][3];
TmpPara.b = ParaMatrix[i][4];
for(int j=0; j < col_size; j++)
{
if (this->GetType() == "C")
{
out[i][j] = CallGamma(U[j], TmpPara);
} else {
out[i][j] = PutGamma(U[j], TmpPara);
}
}
}
return(out);
}
// Call-Put Partity Functions
double EUoption::PriceUsingPutCallParity (double U) const
{
OptionPara para = this->GetPara();
string optiontype = this->GetType();
if (optiontype == "C")
return(CallPrice(U, para) + para.K*exp(-para.r*para.T) - U);
else
return (PutPrice(U, para) + U - para.K*exp(-para.r*para.T));
}
void EUoption::CheckPutCallParity(double U) const
{
OptionPara para = this->GetPara();
// float is employed here to get off the round error
if ((float)(CallPrice(U, para) + para.K*exp(-para.r*para.T)) == (float)(PutPrice(U, para) + U)){
cout << "The Call-Put Parity is hold." << endl;
} else {
cout << "The Call-Put Parity is not hold." << endl;
}
}
//Exact Vs Numerical
vector<double> EUoption::ExactVSNumericalDelta(double U, const vector<double>& h) const
{
int row_size = h.size();
vector<double> out(row_size);
double v1;
double v2;
double vE;
for (int i = 0; i < row_size; i++)
{
v2 = Price(U + h[i]);
v1 = Price(U - h[i]);
vE = Delta(U);
out[i] = (v2-v1)/(2*h[i]) - vE;
}
return (out);
}
vector<double> EUoption::ExactVSNumericalGamma(double U, const vector<double>& h) const
{
int row_size = h.size();
vector<double> out(row_size);
double v1;
double v2;
double v3;
double vE;
for (int i = 0; i < row_size; i++)
{
v1 = Price(U - h[i]);
v2 = Price(U);
v3 = Price(U + h[i]);
vE = Gamma(U);
out[i] = (v3-2*v2+v1)/(h[i]*h[i]) - vE;
}
return (out);
}