-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
123 lines (110 loc) · 6.1 KB
/
main.cpp
File metadata and controls
123 lines (110 loc) · 6.1 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
/*
* Code for week 7 exercises of C++ for Finance.
*
* Copyright 2019 Laurence Alexander Hurst
*
* This file is part of C++ for Finance.
*
* C++ for Finance is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* C++ for Finance is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <https://www.gnu.org/licenses/>.
*
* See the file LICENCE in the original source code repository for the
* full licence.
*/
#include <ctime>
#include <iomanip>
#include <iostream>
#include <utility>
#include <vector>
#include "MonteCarlo.hpp"
namespace MC = MonteCarlo;
// Print out the MC simulation result - including value, error and calculation of time taken from (end-start)/(ticks/s)
void print_result(const double value, const double error, const time_t start, const time_t end) {
std::cout << std::setprecision(8) << std::fixed
<< "Calculated estimate value of " << value
<< " with error " << error
<< " in " << ((end-start)/CLOCKS_PER_SEC) << "s"
<< std::endl;
}
// Convenience function for IS benchmark - sets the rate to mu and sigma to sigma in both options passed by value
void set_mu_sigma_pair(MC::Data & opt1, MC::Data & opt2, const double mu, const double sigma) {
opt1.rate = mu; opt1.sigma = sigma;
opt2.rate = mu; opt2.sigma = sigma;
}
int main() {
// S_0, rate, sigma, maturity, strike, paths, steps
const MC::Data option {100, 0.05, 0.20, 1, 100, 1000000, 100};
std::cout << "Begin plain Monte Carlo:" << std::endl;
const time_t plain_mc_start {std::clock()};
const std::unique_ptr<MC::Result> plain_result {MC::Plain(option)};
const time_t plain_mc_end {std::clock()};
print_result(plain_result->value, plain_result->error, plain_mc_start, plain_mc_end);
std::cout << "Begin ln_S Monte Carlo:" << std::endl;
const time_t ln_S_mc_start {std::clock()};
const std::unique_ptr<MC::Result> ln_S_result {MC::Ln_S(option)};
const time_t ln_S_mc_end {std::clock()};
print_result(ln_S_result->value, ln_S_result->error, ln_S_mc_start, ln_S_mc_end);
std::cout << "Begin CV Monte Carlo:" << std::endl;
const time_t cv_mc_start {std::clock()};
double correlation;
const std::unique_ptr<MC::Result> cv_result {MC::Cv(option, correlation)};
const time_t cv_mc_end {std::clock()};
print_result(cv_result->value, cv_result->error, cv_mc_start, cv_mc_end);
std::cout << "Correlation was: " << correlation << std::endl;
// Everything but paths and steps is irrelevant for the plain IS benchmark
MC::Data is_bench_10k {0, 0, 0, 0, 0, 10000, 100};
MC::Data is_bench_100k {0, 0, 0, 0, 0, 100000, 100};
std::cout << "Begin IS benchmark Monte Carlo:" << std::endl;
const time_t is_b_p_10k_mc_start {std::clock()};
const std::unique_ptr<MC::Result> is_b_p_10k_result {MC::Is_Benchmark_Plain(is_bench_10k)};
const time_t is_b_p_10k_mc_end {std::clock()};
const time_t is_b_p_100k_mc_start {std::clock()};
const std::unique_ptr<MC::Result> is_b_p_100k_result {MC::Is_Benchmark_Plain(is_bench_100k)};
const time_t is_b_p_100k_mc_end {std::clock()};
std::cout << "plain results:" << std::endl;
std::cout << " 10k samples: "; print_result(is_b_p_10k_result->value, is_b_p_10k_result->error, is_b_p_10k_mc_start, is_b_p_10k_mc_end);
std::cout << "100k samples: "; print_result(is_b_p_100k_result->value, is_b_p_100k_result->error, is_b_p_100k_mc_start, is_b_p_100k_mc_end);
// Create a vector of pairs of mu, sigma values to try
std::vector<std::pair<double,double>> is_b_trials {
std::make_pair(1, 2), std::make_pair(2, 2), std::make_pair(2, 1), std::make_pair(2.3, 1), std::make_pair(2.3, 0.5)
};
std::cout << "IS results:" << std::endl;
// Do each trial
for (const auto item : is_b_trials) {
set_mu_sigma_pair(is_bench_10k, is_bench_100k, item.first, item.second);
const time_t trial_10k_start {std::clock()};
std::unique_ptr<MC::Result> trail_10k_result {MC::Is_Benchmark_Is(is_bench_10k)};
const time_t trial_10k_end {std::clock()};
const time_t trial_100k_start {std::clock()};
std::unique_ptr<MC::Result> trail_100k_result {MC::Is_Benchmark_Is(is_bench_100k)};
const time_t trial_100k_end {std::clock()};
std::cout << std::setprecision(1) << " 10k samples mu: " << item.first << " sigma: " << item.second << ": ";
print_result(trail_10k_result->value, trail_10k_result->error, trial_10k_start, trial_10k_end);
std::cout << std::setprecision(1) << "100k samples mu: " << item.first << " sigma: " << item.second << ": ";
print_result(trail_100k_result->value, trail_100k_result->error, trial_100k_start, trial_100k_end);
}
std::cout << "Begin IS OTM European Binary:" << std::endl;
// S_0, rate, sigma, maturity, strike, paths, steps, lower, upper
const MC::OtmData otm_data {100, 0.05, 0.2, 1, 0, 1000000, 100, 150, 160};
const std::unique_ptr<MC::Result> otm_explicit_result {MC::Is_Otm_Explicit(otm_data)};
std::cout << "Explicit solution: " << otm_explicit_result->value << std::endl;
const time_t otm_plain_start {std::clock()};
const std::unique_ptr<MC::Result> otm_plain_result {MC::Is_Otm_Plain(otm_data)};
const time_t otm_plain_end {std::clock()};
std::cout << "Plain MC: "; print_result(otm_plain_result->value, otm_plain_result->error, otm_plain_start, otm_plain_end);
const time_t otm_is_start {std::clock()};
const std::unique_ptr<MC::Result> otm_is_result {MC::Is_Otm_Is(otm_data)};
const time_t otm_is_end {std::clock()};
std::cout << " IS MC: "; print_result(otm_is_result->value, otm_is_result->error, otm_is_start, otm_is_end);
return 0;
}