-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExactOptionGlobal.hpp
181 lines (146 loc) · 3.87 KB
/
ExactOptionGlobal.hpp
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
#ifndef ExactOptionGlobal_hpp
#define ExactOptionGlobal_hpp
#include<vector>
#include<string>
#include<random>
#include<iostream>
#include<boost/random.hpp>
#include <iomanip>
using namespace std;
// Data Encapsulate Struct
struct OptionPara
{
double T; // Expiry date
double K; // Strike price
double sig; // Volatility
double r; // Interest rate
double b; // Cost of carry
};
// BASE CLASS ExactOption
class ExactOption{
private:
OptionPara m_para;
string optType;
void init(){ // Initialise all default values
// Default values
m_para.r = 0.05;
m_para.sig = 0.2;
m_para.K = 110.0;
m_para.T = 0.5;
m_para.b = m_para.r; // Black and Scholes stock option model (1973)
optType = "C"; // European Call Option (this is the default type)
}
public: // Public functions
ExactOption (){
init();
}
ExactOption (const OptionPara& para, string optionType = "C"){
m_para = para;
optType = optionType;
if (optType == "c")
optType = "C";
} // Specifier
virtual ~ExactOption(){}
virtual vector<vector<double> > Price(const vector<double>& U, const vector<vector<double> > & ParaMatrix) const{
vector<vector<double> > out;
return out;
}
virtual vector<vector<double> > Delta(const vector<double>& U, const vector<vector<double> > & ParaMatrix) const{
vector<vector<double> > out;
return out;
}
virtual vector<vector<double> > Gamma(const vector<double>& U, const vector<vector<double> > & ParaMatrix) const{
vector<vector<double> > out;
return out;
}
//Getter
OptionPara GetPara() const{
return(m_para);
}
string GetType() const{
return(optType);
}
// Modifier functions
void toggle(){
// Change option type (C/P, P/C)
if (optType == "C")
optType = "P";
else
optType = "C";
}
// Change option type (C/P, P/C)
void SetPara(const OptionPara& para){
//set parametrs by struct OptionPara
m_para = para;
}
void SetType(const string& optionType){
optType = optionType;
}
};
//Interfaces
//Printer
inline void Print1d(const vector<double>& v) // Print a double vector
{
for(int i = 0; i < v.size(); i++){
cout << v[i] << " ";
}
}
inline void Print2d(const vector<vector<double> >& v) // Print a matrix
{
int d1 = v[0].size();
int d2 = v.size();
for(int i = 0; i < d1; i++)
{
for (int j = 0; j < d2; j++)
{
cout << std::setprecision(3) << v[j][i] << " ";
}
cout << endl;
}
}
// Mesh Generator
inline vector<double> getMesh(double start, double end, double h){
// Global Function of getting Meshes by size
vector<double> out;
while (start <= end){
out.push_back(start);
start += h;
}
return out;
}
static std::random_device rd;
static boost::mt19937 gen(rd());
inline vector<double> getMesh(double start, double end, int n_rand){
// Global Function of getting Meshes by unif random numbers
vector<double> out;
int i = 0;
boost::random::uniform_real_distribution<> runif(start, end);
while ( i < n_rand){
out.push_back(runif(gen));
i += 1;
}
return out;
}
inline vector<double> getMesh(){
// Global Function for getting Meshes by console input
std::vector<double> out;
double input;
cout << "Please give a number and type in anything else to stop: " << endl;
while (cin >> input){
out.push_back(input);
cin.clear();
cout << "Please give a number and type in anything else to stop: " << endl;
}
return out;
}
// Pricer Functions
inline vector<vector<double> > OptionMatrixPricer (const ExactOption& option, const vector<double>& U, const vector<vector<double> > & ParaMatrix){
return option.Price(U, ParaMatrix);
}
inline vector<vector<double> > OptionMatrixDelta (const ExactOption& option, const vector<double>& U, const vector<vector<double> > & ParaMatrix){
return option.Delta(U, ParaMatrix);
}
inline vector<vector<double> > OptionMatrixGamma (const ExactOption& option, const vector<double>& U, const vector<vector<double> > & ParaMatrix){
return option.Gamma(U, ParaMatrix);
}
#endif