-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcmclib_docs_aees.html
More file actions
304 lines (225 loc) · 10.1 KB
/
mcmclib_docs_aees.html
File metadata and controls
304 lines (225 loc) · 10.1 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<!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: AEES</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-5">
<div class="panel panel-default">
<div class="panel-heading">
<a data-toggle="collapse" href="#collapse1"><h4><strong style="font-size: 120%;">MCMC: Adaptive Equi-Energy Sampler</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>MCMC via adaptive parallel tempering.</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 aees(const arma::vec& initial_vals, arma::mat& draws_out, std::function<double (const arma::vec& vals_inp, void* target_data)> target_log_kernel, void* target_data);
bool aees(const arma::vec& initial_vals, arma::mat& 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 two-dimensional array containing the 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>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 aees_n_initial_draws</code> number of initial draws.</li>
<li><code>int aees_n_draws</code> number of posterior draws to keep.</li>
<li><code>int aees_n_burnin</code> number of burnin draws.</li>
<li><code>int aees_n_rings</code> number of energy rings.</li>
<li><code>double aees_prob_par</code> probability parameter controlling the frequency of equi-energy moves versus local Metropolis-Hastings moves. Larger value means more equi-energy moves.</li>
<li><code>arma::vec aees_temper_vec</code> $K$-dimensional vector of temperature values.</li>
<li><code>double rwmh_par_scale</code> scaling parameter for the proposal covariance matrix <code>rwmh_cov_mat</code>.</li>
<li><code>arma::mat rwmh_cov_mat</code> covariance matrix of the random walk proposals.</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 Kou, Zhou, Wong (2006) for the standard equi-energy sampler, and Schreck, Fort, Moulines (2013) for the adaptive version presented here.</p>
<hr>
<p>Let $\theta_k^{(i)}$ denote a $d$-dimensional vector of stored values at stage $i$ of the algorithm, drawn from target distribution $\pi_k$, where $k \in \{ 0, 1, \ldots, K \}$. We will use the following notation to define a tempered target distribution:</p>
$$\pi_k (\theta) \propto \exp( - H(\theta | X) / T_k)$$
<p>where $T_0 = 1$ and $H$ is the energy function (the negative of the log-posterior kernel function). $\pi$ without a subscript refers to the intended target distribution: $\pi_0$.</p>
<br>
<p>The algorithm is as follows</p>
<ul>
<li>Sample $\theta_K^{(i+1)} \sim \pi_K$ using Metropolis-Hastings</li>
<li><strong>for</strong> $k \in \{ K-1, K-2, \ldots, 0 \}$ <strong>do</strong>: <br> if $i > (K-k) \times ($<code>aees_n_initial_draws</code> $+$ <code>aees_n_burnin</code>$)$</li>
<ul>
<li>sample $z \sim U(0,1)$</li>
<li>if $z > $ <code>aees_prob_par</code></li>
<ul>
<strong>local move</strong>
<li>sample $\theta_k^{(i+1)} \sim \pi_k$ using Metropolis-Hastings</li>
</ul>
<li>else</li>
<ul>
<strong>equi-energy move</strong>
<li>construct <code>aees_n_rings</code>-evenly spaced energy rings using draws previous draws from $\pi_{k+1}$, $\{ \theta_{k+1}^{(0)}, \ldots, \theta_{k+1}^{(i)} \}$</li>
<li>choose $\theta_k^{(*)}$with uniform probability from the closest energy ring based on $\pi(\theta_k^{(i)})$</li>
<li>Let
$$\alpha = \min \left\{ 1, \dfrac{\pi_{k}(\theta_k^{(*)})}{\pi_{k+1}(\theta_k^{(*)})} \dfrac{\pi_{k+1}(\theta_k^{(i)})}{\pi_{k}(\theta_k^{(i)})} \right\}$$
Then
$$\theta_k^{(i+1)} = \begin{cases} \theta_k^{(*)} & \text{ if } Z < \alpha \\ \theta_k^{(i)} & \text{ else } \end{cases}$$
where $Z \sim \text{Unif}(0,1)$.</li>
</ul>
</ul>
</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>Gaussian mixture model.</p>
<br>
<pre class="brush: cpp;">
#include "mcmc.hpp"
double
gaussian_mixture(const arma::vec& X_vec_inp, const arma::vec& weights, const arma::mat& mu, const arma::vec& sig_sq)
{
const double pi = arma::datum::pi;
const int n_vals = X_vec_inp.n_elem;
const int n_mix = weights.n_elem;
//
double dens_val = 0;
for (int i=0; i < n_mix; i++) {
double dist_val = arma::accu(arma::pow(X_vec_inp - mu.col(i),2));
dens_val += weights(i) * std::exp(-0.5*dist_val/sig_sq(i)) / std::pow(2.0*pi*sig_sq(i), static_cast<double>(n_vals)/2.0);
}
//
return log(dens_val);
}
struct mixture_data {
arma::mat mu;
arma::vec sig_sq;
arma::vec weights;
};
double
target_log_kernel(const arma::vec& vals_inp, void* target_data)
{
mixture_data* dta = reinterpret_cast<mixture_data*>(target_data);
return gaussian_mixture(vals_inp, dta->weights, dta->mu, dta->sig_sq);
}
int main()
{
const int n_vals = 2;
arma::vec T_vec(2);
T_vec(0) = 60.0;
T_vec(1) = 9.0;
const int n_mix = 2;
//
arma::mat mu = arma::ones(n_vals,n_mix) + 1.0;
mu.col(0) *= -1.0;
arma::vec weights(n_mix);
weights(0) = 0.5;
weights(1) = 0.5;
arma::vec sig_sq = arma::ones(n_mix,1)*0.1;
mixture_data dta;
dta.mu = mu;
dta.sig_sq = sig_sq;
dta.weights = weights;
//
mcmc::algo_settings_t settings;
settings.aees_n_draws = 20000;
settings.aees_n_burnin = 1000;
settings.aees_n_initial_draws = 1000;
settings.aees_prob_par = 0.05;
settings.aees_temper_vec = T_vec;
settings.aees_n_rings = 11;
settings.rwmh_par_scale = 1.0;
settings.rwmh_cov_mat = 0.35*arma::eye(n_vals,n_vals);
//
arma::mat draws_out;
mcmc::aees(mu.col(0),draws_out,target_log_kernel,&dta,settings);
//
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>