-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcmclib_docs_de.html
281 lines (206 loc) · 9.45 KB
/
mcmclib_docs_de.html
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
272
273
274
275
276
277
278
279
280
281
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="MCMCLib: a C++ MCMC library.">
<meta name="author" content="Keith O'Hara">
<meta name="keywords" content="mcmc, MCMC, Metropolis Hastings, RWMH, Differential Evolution, Metropolis-adjusted Langevin algorithm, MALA, Hamiltonian Monte Carlo, HMC, C++, C++11, Cpp, NYU, New York University, Econometrics, Research" />
<link rel="shortcut icon" type="image/x-icon" href="siteicon.ico">
<title>MCMC: DE</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/modern-business.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- Additional Settings -->
<link href="css/yuxuan_settings.css" rel="stylesheet">
<!-- Syntax Highlighter -->
<script type="text/javascript" src="js/syntaxhighlighter.js"></script>
<link type="text/css" rel="stylesheet" href="css/swift_theme.css">
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-93902857-1', 'auto');
ga('send', 'pageview');
</script>
<!-- MathJax -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script type="text/javascript" async
src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_CHTML">
</script>
<script async defer src="https://buttons.github.io/buttons.js"></script>
<script src="js/jquery.js"></script>
<script>
$(function(){
$("#mynavbar").load("navbar.html")
$("#mcmchead").load("mcmclib_header.html")
$("#myfooter").load("footer.html")
});
</script>
</head>
<style>
pre {
display: inline-block;
}
</style>
<body>
<!-- Navigation -->
<div id="mynavbar"></div>
<!-- Page Content -->
<div class="container">
<!-- Page Heading/Breadcrumbs -->
<div id="mcmchead"></div>
<br>
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<a data-toggle="collapse" href="#collapse1"><h4><strong style="font-size: 120%;">MCMC: Differential Evolution</strong></h4></a>
</div>
<div id="collapse1" class="panel-collapse collapse">
<div class="panel-body">
<a href="#definition">Definition</a> <br>
<a href="#details">Details</a> <br>
<a href="#examples">Examples</a>
</div>
</div>
</div>
</div>
</div>
<!-- -->
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<p>Differential Evolution (DE) is a MCMC variant of the well-known stochastic genetic search algorithm used for global optimization.</p>
<hr style="height:2px;border-width:0;background-color:black">
<h3 style="text-align: left;" id="definition"><strong style="font-size: 100%;">Definition and Syntax</strong></h3>
<pre class="brush: cpp;">
bool de(const arma::vec& initial_vals, arma::cube& draws_out, std::function<double (const arma::vec& vals_inp, void* target_data)> target_log_kernel, void* target_data);
bool de(const arma::vec& initial_vals, arma::cube& draws_out, std::function<double (const arma::vec& vals_inp, void* target_data)> target_log_kernel, void* target_data, algo_settings_t& settings);
</pre>
<p><strong>Function arguments:</strong></p>
<ul>
<li><code>initial_vals</code> a column vector of initial values.</li>
<li><code>draws_out</code> a three-dimensional array containing posterior draws.</li>
<li><code>target_log_kernel</code> the target log-posterior kernel function, taking two arguments:
<ul>
<li><code>vals_inp</code> a vector of input values; and</li>
<!-- <li><code>grad_out</code> an empty vector, as DE does not require the gradient to be known/exist; and</li> -->
<li><code>target_data</code> additional parameters passed to the function.</li>
</ul>
<li><code>target_data</code> additional parameters passed to the posterior kernel.</li>
<li><code>settings</code> parameters controlling the MCMC routine; see below.</li>
</ul>
<p><strong>MCMC control parameters:</strong></p>
<ul>
<li><code>bool vals_bound</code> whether the search space is bounded. If true, then</li>
<ul>
<li><code>arma::vec lower_bounds</code> this defines the lower bounds.</li>
<li><code>arma::vec upper_bounds</code> this defines the upper bounds.</li>
</ul>
<li><code>int de_n_pop</code> population size of each generation.</li>
<li><code>int de_n_gen</code> number of vectors to generate.</li>
<li><code>int de_n_burnin</code> number of burnin generations.</li>
<!-- <li><code>double de_par_F</code> the mutation parameter $F$ in the details section below.</li> -->
<!-- <li><code>double de_par_CR</code> the crossover parameter $CR$ in the details section below.</li> -->
<li><code>double de_par_b</code> the uniform sampling parameter $b$ in the details section below.</li>
<li><code>arma::vec de_initial_lb</code> lower bounds on the initial population; defaults to <code>initial_vals</code> $- \ 0.5$.</li>
<li><code>arma::vec de_initial_ub</code> upper bounds on the initial population; defaults to <code>initial_vals</code> $+ \ 0.5$.</li>
</ul>
<hr style="height:2px;border-width:0;background-color:black">
<h3 style="text-align: left;" id="details"><strong style="font-size: 100%;">Details</strong></h3>
<p>See Cajo J. F. Ter Braak (2006).</p>
<hr>
<h4> Basic Differential Evolution MCMC (DE-MCMC)</h4>
<p>Let $\boldsymbol{\theta}^{(i)}$ denote a $N_p \times d$-dimensional array of values at stage $i$ of the algorithm. The basic DE-MCMC algorithm is comprised of two steps.</p>
<ul>
<li>Set $\gamma = 2.38 / \sqrt{2 d}$.</li>
<li><strong>The Mutation Step.</strong> For random and unique indices $a,b$:</li>
$$\theta^* = \theta^{(i)} + \gamma \times (\boldsymbol{\theta}^{(i)}(a,:) - \boldsymbol{\theta}^{(i)}(b,:))) + U$$
where $U \sim \text{Unif}[-b,b]$ and $b$ is determined by <code>de_par_b</code>.
<!-- <li><strong>The Crossover Step.</strong> For a random integer $r_k \in \{1, \ldots, d\}$,</li> -->
<!-- $$X_c^* (j,k) = \begin{cases} X^*(j,k) & \text{ if } u_k \leq CR \text{ or } k = r_k \\ X_i (j,k) & \text{ else } \end{cases}$$ -->
<li><strong>The Update Step.</strong> Let
$$\alpha = \min \left\{ 1, K(\theta^{(*)} | X) / K(\theta^{(i)} | X) \right\}$$
where $K$ is the posterior kernel. Then
$$\theta^{(i+1)} = \begin{cases} \theta^* & \text{ if } Z < \alpha \\ \theta^{(i)} & \text{ else } \end{cases}$$
where $Z \sim \text{Unif}(0,1)$.</li>
</ul>
<hr style="height:2px;border-width:0;background-color:black">
<h3 style="text-align: left;" id="examples"><strong style="font-size: 100%;">Examples</strong></h3>
<p>Normal likelihood with normal prior.</p>
<br>
<pre class="brush: cpp;">
#include "mcmc.hpp"
struct norm_data {
double sigma;
arma::vec x;
double mu_0;
double sigma_0;
};
double ll_dens(const arma::vec& vals_inp, void* ll_data)
{
const double mu = vals_inp(0);
const double pi = arma::datum::pi;
norm_data* dta = reinterpret_cast<norm_data*>(ll_data);
const double sigma = dta->sigma;
const arma::vec x = dta->x;
const int n_vals = x.n_rows;
//
const double ret = - ((double) n_vals) * (0.5*std::log(2*pi) + std::log(sigma)) - arma::accu( arma::pow(x - mu,2) / (2*sigma*sigma) );
//
return ret;
}
double log_pr_dens(const arma::vec& vals_inp, void* ll_data)
{
norm_data* dta = reinterpret_cast< norm_data* >(ll_data);
const double mu_0 = dta->mu_0;
const double sigma_0 = dta->sigma_0;
const double pi = arma::datum::pi;
const double x = vals_inp(0);
const double ret = - 0.5*std::log(2*pi) - std::log(sigma_0) - std::pow(x - mu_0,2) / (2*sigma_0*sigma_0);
return ret;
}
double log_target_dens(const arma::vec& vals_inp, void* ll_data)
{
return ll_dens(vals_inp,ll_data) + log_pr_dens(vals_inp,ll_data);
}
int main()
{
const int n_data = 100;
const double mu = 2.0;
norm_data dta;
dta.sigma = 1.0;
dta.mu_0 = 1.0;
dta.sigma_0 = 2.0;
arma::vec x_dta = mu + arma::randn(n_data,1);
dta.x = x_dta;
//
mcmc::algo_settings_t settings;
arma::vec initial_val(1);
initial_val(0) = 1.0;
arma::cube draws_out;
mcmc::de(initial_val,draws_out,log_target_dens,&dta,settings);
arma::cout << "draws:\n" << draws_out.slice(100).rows(0,9) << arma::endl;
std::cout << "acceptance rate = " << settings.de_accept_rate << arma::endl;
return 0;
}
</pre>
</div>
</div>
</div>
<div id="myfooter"></div>
<!-- jQuery -->
<!--<script src="js/jquery.js"></script>-->
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>