-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegrator.h
271 lines (214 loc) · 7.5 KB
/
integrator.h
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//
// Created by 王泽远 on 2022/12/29.
//
#ifndef INTEGRATOR_H
#define INTEGRATOR_H
#include <cmath>
#include <functional>
#include <random>
#include <numeric>
#include <utility>
using my_float = double;
using integrand = std::function<my_float (my_float)>;
template<typename T>
using domain = std::pair<T,T>;
using integral_info = std::pair<integrand,domain<my_float>>;
integral_info make_integral_info(const integrand& ig,my_float a, my_float b){
return std::make_pair(ig,std::make_pair(a,b));
}
/*
* Trapezoid integrator
* Caution: fp swamping issue
*/
my_float integrator(const integral_info& int_info,const int steps){
const auto & dom = int_info.second;
const auto & f = int_info.first;
auto interval = dom.second-dom.first;
auto dx = interval/steps;
my_float res = 0;
my_float a1;
my_float a2 = f(dom.first);
for(int i = 1;i<=steps;++i){
a1 = a2;
a2 = f(dom.first+dx*i);
res += (a1+a2)*dx*0.5;
}
return res;
}
class sampler{
public:
virtual void set_domain(my_float a,my_float b) = 0;
virtual my_float next_sample() = 0;
virtual my_float evaluate(my_float sample) const = 0;
};
using sample_info = std::pair<sampler&,const int>;
sample_info make_sample_info(sampler& _sampler, const int N){
return std::make_pair(std::ref(_sampler),N);
}
/*
* Most naive monte carlo integrator
* A special case of MIS MC integrator(with w(x) == 1)
*/
my_float naive_monte_carlo_integrator(const integral_info & int_info,sample_info s_info){
const auto N = s_info.second;
auto & _sampler = s_info.first;
_sampler.set_domain(int_info.second.first,int_info.second.second);
const auto & f = int_info.first;
my_float result{};
int i = 1;
for(;i<=N;++i){
auto x = _sampler.next_sample();
auto pdf = _sampler.evaluate(x);
result += (f(x)/pdf - result )/my_float(i);
}
return result;
}
/*
* Monte carlo integrator with Multiple Importance Sampling
*/
my_float mis_monte_carlo_integrator(const integral_info & int_info,std::vector<sample_info>& sample_infos){
my_float result{};
for(auto & si : sample_infos){
si.first.set_domain(int_info.second.first,int_info.second.second);
}
const auto & f = int_info.first;
for(auto & si : sample_infos){
const auto Ni = si.second;
const auto inv_float_Ni = 1.0/static_cast<my_float>(Ni);
auto& sampler_i = si.first;
const auto wi = [&sample_infos,Ni,&sampler_i](my_float sample)->my_float {
//balance heuristic
my_float sum{};
for(auto & sj : sample_infos){
sum += sj.second * sj.first.evaluate(sample);
}
return sampler_i.evaluate(sample)*Ni/sum;
};
for(int j =0; j < Ni; ++j){
auto xij = sampler_i.next_sample();
auto pdf_i = sampler_i.evaluate(xij);
result += f(xij)/pdf_i * inv_float_Ni * wi(xij);
}
}
return result;
}
/*
* Sample Importance Resampling
*/
struct sir_sampler{
private:
std::uniform_real_distribution<my_float> dice;
std::mt19937 gen;
std::vector<my_float> _proposal_set;
std::vector<my_float> _prefix_sum;
my_float correct_factor;
public:
sir_sampler(sampler& proposal_sampler,const std::function<my_float(my_float)>& target_distribution,const int M){
static std::random_device rd; // Will be used to obtain a seed for the random number engine
gen = std::mt19937(rd());
dice = std::uniform_real_distribution<my_float>(0,1);
const auto inv_float_M = 1.0/static_cast<my_float>(M);
std::vector<my_float> weight_set{};
_proposal_set.reserve(M);
weight_set.reserve(M);
//generate M proposal samples, and calculate weights for each.
for(int j = 0; j<M; ++j){
auto xj = proposal_sampler.next_sample();
_proposal_set.emplace_back(xj);
auto p_hat = target_distribution(xj);
auto p = proposal_sampler.evaluate(xj);
auto w = (p_hat/p)*inv_float_M;
weight_set.emplace_back(w);
}
correct_factor = std::accumulate(weight_set.begin(),weight_set.end(),0.0);
_prefix_sum.clear();
std::inclusive_scan(weight_set.cbegin(),weight_set.cend(),std::back_inserter(_prefix_sum),std::plus<my_float>{});
auto inv_total_sum = 1.0/_prefix_sum.back();
std::for_each(_prefix_sum.begin(),_prefix_sum.end(),[inv_total_sum](auto & s){s *= inv_total_sum;});
}
my_float next_sample(){
auto rand = dice(gen);
//use binary search to draw a new weighted sample
auto lower = std::lower_bound(_prefix_sum.begin(),_prefix_sum.end(),rand);
return _proposal_set[std::distance(_prefix_sum.begin(),lower)];
}
my_float get_correct_factor() const{
return correct_factor;
}
};
/*
* Weighted Reservoir Sampling
*/
struct reservoir{
my_float current_sample{};
my_float total_weight{};
};
struct wrs_sampler{
private:
std::uniform_real_distribution<my_float> dice;
std::mt19937 gen;
reservoir _reservoir{};
std::function<my_float(my_float)> _target_distribution;
std::shared_ptr<sampler> _proposal_sampler;
int M = 0;
public:
wrs_sampler(std::shared_ptr<sampler> proposal_sampler,const std::function<my_float(my_float)>& target_distribution){
static std::random_device rd; // Will be used to obtain a seed for the random number engine
gen = std::mt19937(rd());
dice = std::uniform_real_distribution<my_float>(0,1);
_target_distribution = target_distribution;
_proposal_sampler = std::move(proposal_sampler);
}
my_float next_sample(){
auto x = _proposal_sampler->next_sample();
auto p = _proposal_sampler->evaluate(x);
auto p_hat = _target_distribution(x);
auto w = p_hat/p;
if(M == 0){
_reservoir.current_sample = x;
}else{
auto threshold = w/(w+_reservoir.total_weight);
auto rand = dice(gen);
if(rand < threshold){
_reservoir.current_sample = x;
}
}
_reservoir.total_weight += w;
M++;
return _reservoir.current_sample;
}
my_float get_correct_factor() const{
return _reservoir.total_weight/M;
}
};
/*
* Monte carlo integrator with Resampled Importance Sampling
*/
my_float ris_monte_carlo_integrator(const integral_info & int_info,sampler& proposal_sampler,const std::function<my_float(my_float)>& target_distribution,const int N){
proposal_sampler.set_domain(int_info.second.first,int_info.second.second);
sir_sampler sir{proposal_sampler,target_distribution,1000};
const auto & f = int_info.first;
my_float result{};
for(int i = 1; i<=N; ++i){
auto x = sir.next_sample();
result += (f(x)/target_distribution(x) - result)/my_float(i);
}
result *= sir.get_correct_factor();
return result;
}
/*
* Monte carlo integrator with Weighted Reservoir Sampling
* Has side effect on WR sampler.
*/
my_float wrs_monte_carlo_integrator(const integral_info & int_info,wrs_sampler& wrs,const std::function<my_float(my_float)>& target_distribution,const int N){
const auto inv_float_N = 1.0/static_cast<my_float>(N);
const auto & f = int_info.first;
my_float result{};
for(int i = 0; i<N; ++i){
auto x = wrs.next_sample();
result += f(x)/target_distribution(x) * inv_float_N;
}
result *= wrs.get_correct_factor();
return result;
}
#endif //INTEGRATOR_H