-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqintintegrator.cpp
182 lines (167 loc) · 5.22 KB
/
qintintegrator.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
#include <qintintegrator.h>
#include <utils.h>
#include <vector>
#include <cmath>
#include <limits>
Integrator::Status QintIntegrator::integrate(
Integrand &f,
const Hypercube &h,
Index n, real, real,
EstErr &ee)
{
checkDimension(h, f);
if (n == 0)
{
ee.set(0.0, 0.0);
return ERROR;
}
ps->setCube(&h);
n = ps->getOptimalNumber(n, h);
ps->enableRandomize();
if (randCount == 0) return ERROR;
int m = n / randCount;
if (m < std::pow(2, this->sParam))
{
ee.setNoErr(std::numeric_limits<double>::quiet_NaN());
return ERROR;
}
std::vector<Statistic<>> stats;
stats.reserve(randCount);
std::default_random_engine e(globalSeed);
std::vector<t_sequence> interceptedSequences;
interceptedSequences.reserve(randCount);
std::vector<std::vector<real>> interceptedValues;
interceptedValues.reserve(randCount);
auto iif = dynamic_cast<InterceptableIntegrand*>(&f);
repeat(randCount, [&]
{
Statistic<> s;
Point point(h.getDimension());
int seed = e();
ps->randomize(seed);
if (iif)
{
iif->eraseIntercepted();
iif->reserveIntercepted(m);
}
ps->integrate(point, f, m, s);
if (iif)
{
interceptedSequences.push_back(iif->getInterceptedPoints());
interceptedValues.push_back(iif->getInterceptedValues());
}
stats.push_back(s);
});
std::vector<double> estimates;
estimates.reserve(randCount);
std::vector<double> variances;
variances.reserve(randCount);
std::vector<double> mixed_vals;
mixed_vals.reserve(n);
for (const auto x : stats) estimates.push_back(x.getMean() * h.getVolume());
double qintEst = sum(estimates) / randCount;
CubicShapeIndexer indexer(sParam);
std::vector<std::vector<int>> indexes;
indexes.reserve(randCount);
unsigned i=0;
for (const auto &x : interceptedSequences)
{
auto currentIndex = indexer.CreateIndex(x);
variances.push_back(estimateQintVariance(interceptedValues[i], currentIndex));
mixed_vals.insert(mixed_vals.end(), interceptedValues[i].begin(), interceptedValues[i].end());
++i;
}
double rqmcStdError = std::sqrt(var(estimates) / randCount);
if (randCount == 1) rqmcStdError = -1;
double mcStdError = std::sqrt(var(mixed_vals) / n);
double qintStdError = std::sqrt(sum(variances) / randCount / randCount);
switch (varOption)
{
case 1:
ee.set(qintEst, qintStdError);
break;
case 2:
ee.set(qintEst, mcStdError);
break;
case 3:
ee.set(qintEst, rqmcStdError);
break;
default:
break;
}
return MAX_EVAL_REACHED;
}
std::vector<int> CubicShapeIndexer::CreateIndex(const t_sequence& sequence)
{
std::vector<int> res;
res.reserve(sequence.size());
for (const auto v : sequence)
{
int d = v.size();
std::vector<int> partTimes;
partTimes.reserve(d);
std::vector<int> binaryIndex;
binaryIndex.reserve(d);
int index = 0;
auto dv = std::div(sParam, d);
int a = dv.quot;
int b = dv.rem;
for (int i = 0; i < d; i++)
{
(i < b) ? partTimes.push_back(a + 1) : partTimes.push_back(a);
binaryIndex.push_back(std::floor(v[i] * std::pow(2, partTimes[i])));
index += binaryIndex[i] * std::pow(2, sParam - sum(partTimes));
}
res.push_back(index);
}
return res;
}
//double QintIntegrator::estimateQintVariance(std::vector<double> &values, std::vector<int> &index)
//{
// if (values.size() != index.size()) throw ("Sequence and index sizes do not match!");
// double totalVar = var(values);
// double alphaTerm = 0;
// std::vector<double> alphas(std::pow(2, sParam));
// std::vector<unsigned> alphaCounters(std::pow(2, sParam));
// for (unsigned i=0; i<values.size(); i++)
// {
// alphas[index[i]] += values[i];
// ++alphaCounters[index[i]];
// }
// for (unsigned j=0; j<alphas.size(); j++)
// {
// alphas[j] = alphas[j] / alphaCounters[j] / std::pow(2, sParam);
// }
// for (unsigned i=0; i<alphas.size(); i++)
// {
// for (unsigned j=i; j<alphas.size(); j++)
// {
// alphaTerm += (alphas[i] - alphas[j]) * (alphas[i] - alphas[j]);
// }
// }
// return (totalVar - alphaTerm) / values.size();
//}
double QintIntegrator::estimateQintVariance(std::vector<double> &values, std::vector<int> &index)
{
unsigned m = std::pow(2, sParam);
unsigned n = values.size() / m;
if (values.size() != index.size()) throw ("Sequence and index sizes do not match!");
std::vector<double> alphas(m);
std::vector<unsigned> alphaCounters(m);
for (unsigned i = 0; i < n * m; i++)
{
alphas[index[i]] += values[i];
++alphaCounters[index[i]];
}
for (unsigned j = 0; j < m; j++)
{
if (n == 1)
{
alphas[j] = alphas[j] / alphaCounters[j] / m;
} else
{
alphas[j] = alphas[j] / alphaCounters[j] / m * std::sqrt(n / (n - 1));
}
}
return sumsq(values) / (m * n) / (m * n) - sumsq(alphas) / n;
}