-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBSMain.cpp
More file actions
107 lines (86 loc) · 3.66 KB
/
BSMain.cpp
File metadata and controls
107 lines (86 loc) · 3.66 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
#include "project/BSMain.hpp"
int main(int argc, char* argv[]) {
// Setup
if(argc != 2){
std::cout << "Please execute as ./Black-Scholes #threads" << std::endl;
return 1;
}
int thread_used = std::atoi(argv[1]);
// Declaration variables
std::vector<double> values; //expiry,strike,spot,vol,r,d
unsigned long numberOfPaths = 1 << 20; // number of samples for Monte Carlo
unsigned int numberOfDates = 50; // number of element of time division
FinArray times(numberOfDates); // Array with elements of time
// Variables to read input
std::string line;
std::string token;
// Variable used in loop
double final_result;
// Input file
std::string input_filename = "input.csv";
std::ifstream inputFile(input_filename);
// Output file
std::string output_filename = "output.csv";
std::ofstream outputFile(output_filename);
if(!inputFile.is_open() || !outputFile.is_open()){
std::cout << "Error while opening a file" << std::endl;
return 1;
}
std::vector<std::unique_ptr<RandomBase>> generators;
// Creates n random number generator with different seeds
for(int i = 0; i < thread_used; i++){
generators.emplace_back(std::make_unique<RandomParkMiller>(numberOfDates, i));
}
std::vector<AntiThetic> antiGens;
// The AntiThetic class is just an interface.
// Therefore the real generator needs to exists.
// Hence we need a vector for the generators and one for AntiThetic
for(int i = 0; i < thread_used; i++){
antiGens.emplace_back(*generators[i]);
}
std::vector<std::unique_ptr<StatisticMC>> gatherers;
// Creates n gatherers that store the means
for(int i = 0; i < thread_used; i++){
gatherers.emplace_back(std::make_unique<StatisticMean>());
}
// Main loop
while(std::getline(inputFile, line)){
// Inizialize the string like a stream
std::stringstream str(line);
values.clear();
while(std::getline(str,token,',')){
values.emplace_back(std::atof(token.c_str()));
}
for(int i = 0; i < 6; i++)
std::cout << values[i] << " ";
std::cout << std::endl;
// Generate finite element in time
for (unsigned long i=0; i < numberOfDates; i++)
times[i] = (i+1.0)*values[0]/numberOfDates;
ParameterConstant volParam(values[3]);
ParameterConstant rParam(values[4]);
ParameterConstant dParam(values[5]);
PayOffCall payOff(values[1]);
PathAsian option(times, values[0], payOff);
final_result = 0;
#pragma omp parallel shared(thread_used, antiGens, values, gatherers, option, rParam, dParam, volParam, numberOfPaths) num_threads(thread_used)
{
int thread_id = omp_get_thread_num();
ExoticBSEngine engine(option, rParam, dParam, volParam, antiGens[thread_id], values[2]);
gatherers[thread_id]->clear();
engine.doSimulation(*gatherers[thread_id], numberOfPaths/thread_used);
double result = gatherers[thread_id]->getResultSoFar();
#pragma omp atomic
final_result += result/thread_used;
}
outputFile << values[0] << ','
<< values[1] << ','
<< values[2] << ','
<< values[3] << ','
<< values[4] << ','
<< values[5] << ','
<< final_result << std::endl;
std::cout << final_result << std::endl;
}
return 0;
}