-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMC.cpp
75 lines (59 loc) · 1.4 KB
/
MC.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
#include "MC.h"
using namespace std;
using namespace arma;
MC::MC(function<double()> generation,
double precision,
function<double()> batch): m_generation(generation),
m_precision(precision),
m_batch(batch) {
}
int MC::get_number_simulation() {
int const N(1000);
double mean(0.0), variance(0.0), gen;
for (int i = 0; i < N; ++i) {
gen = m_generation();
mean += gen;
variance += gen*gen;
}
mean /= N;
variance = variance/N - N/(N-1)*mean*mean;
int number_simulation;
number_simulation = 8*variance/m_precision/m_precision;
if(number_simulation==0) {
number_simulation = 1;
}
return number_simulation;
}
double MC::compute() {
int number_simulation;
number_simulation = get_number_simulation();
double sum(0.0);
for (int i = 0; i < number_simulation; ++i) {
sum += m_generation();
}
return sum/number_simulation;
}
double MC::get_lambda() {
int const N(1000);
double var(0.0), cov(0.0), gen1, gen2;
for (int i = 0; i < N; ++i) {
gen1 = m_generation();
gen2 = m_batch();
var += (gen1-gen2)*(gen1-gen2);
cov += gen1*(gen1-gen2);
}
var /= N;
cov /= N;
return cov/var;
}
double MC::batch() {
int number_simulation;
double lambda;
number_simulation = get_number_simulation();
lambda = get_lambda();
double sum(0.0);
for (int i = 0; i < number_simulation; ++i) {
sum += (1-lambda)*m_generation()+lambda*m_batch();
}
return sum/number_simulation;
}