-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
integral_approximation2.cpp
218 lines (189 loc) · 7.18 KB
/
integral_approximation2.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
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
/**
* @file
* @brief [Monte Carlo
* Integration](https://en.wikipedia.org/wiki/Monte_Carlo_integration)
*
* @details
* In mathematics, Monte Carlo integration is a technique for numerical
* integration using random numbers. It is a particular Monte Carlo method that
* numerically computes a definite integral. While other algorithms usually
* evaluate the integrand at a regular grid, Monte Carlo randomly chooses points
* at which the integrand is evaluated. This method is particularly useful for
* higher-dimensional integrals.
*
* This implementation supports arbitrary pdfs.
* These pdfs are sampled using the [Metropolis-Hastings
* algorithm](https://en.wikipedia.org/wiki/Metropolis–Hastings_algorithm). This
* can be swapped out by every other sampling techniques for example the inverse
* method. Metropolis-Hastings was chosen because it is the most general and can
* also be extended for a higher dimensional sampling space.
*
* @author [Domenic Zingsheim](https://github.com/DerAndereDomenic)
*/
#define _USE_MATH_DEFINES /// for M_PI on windows
#include <cmath> /// for math functions
#include <cstdint> /// for fixed size data types
#include <ctime> /// for time to initialize rng
#include <functional> /// for function pointers
#include <iostream> /// for std::cout
#include <random> /// for random number generation
#include <vector> /// for std::vector
/**
* @namespace math
* @brief Math algorithms
*/
namespace math {
/**
* @namespace monte_carlo
* @brief Functions for the [Monte Carlo
* Integration](https://en.wikipedia.org/wiki/Monte_Carlo_integration)
* implementation
*/
namespace monte_carlo {
using Function = std::function<double(
double&)>; /// short-hand for std::functions used in this implementation
/**
* @brief Generate samples according to some pdf
* @details This function uses Metropolis-Hastings to generate random numbers.
* It generates a sequence of random numbers by using a markov chain. Therefore,
* we need to define a start_point and the number of samples we want to
* generate. Because the first samples generated by the markov chain may not be
* distributed according to the given pdf, one can specify how many samples
* should be discarded before storing samples.
* @param start_point The starting point of the markov chain
* @param pdf The pdf to sample
* @param num_samples The number of samples to generate
* @param discard How many samples should be discarded at the start
* @returns A vector of size num_samples with samples distributed according to
* the pdf
*/
std::vector<double> generate_samples(const double& start_point,
const Function& pdf,
const uint32_t& num_samples,
const uint32_t& discard = 100000) {
std::vector<double> samples;
samples.reserve(num_samples);
double x_t = start_point;
std::default_random_engine generator;
std::uniform_real_distribution<double> uniform(0.0, 1.0);
std::normal_distribution<double> normal(0.0, 1.0);
generator.seed(time(nullptr));
for (uint32_t t = 0; t < num_samples + discard; ++t) {
// Generate a new proposal according to some mutation strategy.
// This is arbitrary and can be swapped.
double x_dash = normal(generator) + x_t;
double acceptance_probability = std::min(pdf(x_dash) / pdf(x_t), 1.0);
double u = uniform(generator);
// Accept "new state" according to the acceptance_probability
if (u <= acceptance_probability) {
x_t = x_dash;
}
if (t >= discard) {
samples.push_back(x_t);
}
}
return samples;
}
/**
* @brief Compute an approximation of an integral using Monte Carlo integration
* @details The integration domain [a,b] is given by the pdf.
* The pdf has to fulfill the following conditions:
* 1) for all x \in [a,b] : p(x) > 0
* 2) for all x \not\in [a,b] : p(x) = 0
* 3) \int_a^b p(x) dx = 1
* @param start_point The start point of the Markov Chain (see generate_samples)
* @param function The function to integrate
* @param pdf The pdf to sample
* @param num_samples The number of samples used to approximate the integral
* @returns The approximation of the integral according to 1/N \sum_{i}^N f(x_i)
* / p(x_i)
*/
double integral_monte_carlo(const double& start_point, const Function& function,
const Function& pdf,
const uint32_t& num_samples = 1000000) {
double integral = 0.0;
std::vector<double> samples =
generate_samples(start_point, pdf, num_samples);
for (double sample : samples) {
integral += function(sample) / pdf(sample);
}
return integral / static_cast<double>(samples.size());
}
} // namespace monte_carlo
} // namespace math
/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
std::cout << "Disclaimer: Because this is a randomized algorithm,"
<< std::endl;
std::cout
<< "it may happen that singular samples deviate from the true result."
<< std::endl
<< std::endl;
;
math::monte_carlo::Function f;
math::monte_carlo::Function pdf;
double integral = 0;
double lower_bound = 0, upper_bound = 0;
/* \int_{-2}^{2} -x^2 + 4 dx */
f = [&](double& x) { return -x * x + 4.0; };
lower_bound = -2.0;
upper_bound = 2.0;
pdf = [&](double& x) {
if (x >= lower_bound && x <= -1.0) {
return 0.1;
}
if (x <= upper_bound && x >= 1.0) {
return 0.1;
}
if (x > -1.0 && x < 1.0) {
return 0.4;
}
return 0.0;
};
integral = math::monte_carlo::integral_monte_carlo(
(upper_bound - lower_bound) / 2.0, f, pdf);
std::cout << "This number should be close to 10.666666: " << integral
<< std::endl;
/* \int_{0}^{1} e^x dx */
f = [&](double& x) { return std::exp(x); };
lower_bound = 0.0;
upper_bound = 1.0;
pdf = [&](double& x) {
if (x >= lower_bound && x <= 0.2) {
return 0.1;
}
if (x > 0.2 && x <= 0.4) {
return 0.4;
}
if (x > 0.4 && x < upper_bound) {
return 1.5;
}
return 0.0;
};
integral = math::monte_carlo::integral_monte_carlo(
(upper_bound - lower_bound) / 2.0, f, pdf);
std::cout << "This number should be close to 1.7182818: " << integral
<< std::endl;
/* \int_{-\infty}^{\infty} sinc(x) dx, sinc(x) = sin(pi * x) / (pi * x)
This is a difficult integral because of its infinite domain.
Therefore, it may deviate largely from the expected result.
*/
f = [&](double& x) { return std::sin(M_PI * x) / (M_PI * x); };
pdf = [&](double& x) {
return 1.0 / std::sqrt(2.0 * M_PI) * std::exp(-x * x / 2.0);
};
integral = math::monte_carlo::integral_monte_carlo(0.0, f, pdf, 10000000);
std::cout << "This number should be close to 1.0: " << integral
<< std::endl;
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // run self-test implementations
return 0;
}