Debian Codegraph @

contrib/ms-sbvar/switch_dw/switching/dw_metropolis_theta_prior.c

raw
1 /*
2 * Copyright (C) 1996-2011 Daniel Waggoner and Tao Zha
3 *
4 * This free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * It is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * If you did not received a copy of the GNU General Public License
15 * with this software, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "dw_metropolis_theta.h"
19 #include "dw_rand.h"
20 #include "dw_matrix_rand.h"
21 #include "dw_matrix_array.h"
22 #include "dw_std.h"
23
24 #include <stdlib.h>
25 #include <memory.h>
26 #include <math.h>
27 #include <time.h>
28
29
30 /*
31 Assumes:
32 model: pointer to valid TStateModel structure with model->theta a TMixture
33 structure.
34 submodel : a submodel of model->theta
35 logposterior_old : log posterior value of current parameters
36
37 Results:
38 A metropolis draw of theta is stored in submodel.
39
40 Returns:
41 The log posterior value of current draw.
42 */
43 PRECISION draw_metropolis_theta_prior(TStateModel *model, PRECISION logposterior_old)
44 {
45 // TStateModel specific calls
46 TMetropolis_theta *metropolis=model->metropolis_theta;
47
48 int i, k, n=metropolis->n_parameters;
49 TVector theta, theta_old;
50 PRECISION logposterior;
51
52 theta_old=CreateVector(n);
53 theta=CreateVector(n);
54
55 // TStateModel specific calls
56 ConvertThetaToFreeParameters(model,pElementV(theta_old));
57
58 EquateVector(theta,theta_old);
59
60 for (k=i=0; i < metropolis->n_blocks; i++)
61 {
62 for ( ; k < metropolis->block_offsets[i+1]; k++)
63 if (metropolis->direction[k])
64 UpdateV(1.0,theta,dw_gaussian_rnd()*ElementV(metropolis->scale,k),metropolis->direction[k]);
65 else
66 ElementV(theta,k)+=dw_gaussian_rnd()*ElementV(metropolis->scale,k);
67
68 // TStateModel specific calls
69 ConvertFreeParametersToTheta(model,pElementV(theta));
70 logposterior=LogPrior(model);
71
72 if (log(dw_uniform_rnd()) <= logposterior - logposterior_old)
73 {
74 metropolis->jumps[i]++;
75 if (i < metropolis->n_blocks-1)
76 {
77 EquateVector(theta_old,theta);
78 logposterior_old=logposterior;
79 }
80 }
81 else
82 if (i < metropolis->n_blocks-1)
83 EquateVector(theta,theta_old);
84 else
85 {
86 // TStateModel specific calls
87 ConvertFreeParametersToTheta(model,pElementV(theta_old));
88 }
89
90 metropolis->draws[i]++;
91 }
92
93 FreeVector(theta);
94 FreeVector(theta_old);
95
96 return logposterior_old;
97 }
98
99 /*
100 Assumes:
101 model: pointer to valid TStateModel structure with model->theta a TMixture
102 structure.
103
104 Results:
105 A metropolis draw of all parameters are stored in model.
106 */
107 void draw_prior_all_metropolis(TStateModel *model)
108 {
109 // Draw transition matrix parameters and compute log prior
110 DrawTransitionMatrixParametersFromPrior(model);
111
112 // Draw theta
113 draw_metropolis_theta_prior(model,LogPrior(model));
114 }
115
116 /* /\* */
117 /* Assumes: */
118 /* model: pointer to valid TStateModel structure. */
119
120 /* Results: */
121 /* A metropolis draw of theta is stored in model */
122 /* *\/ */
123 /* void Draw_metropolis_theta_prior(TStateModel *model) */
124 /* { */
125 /* // TStateModel specific calls */
126 /* TMetropolis_theta *metropolis=model->metropolis_theta; */
127
128 /* int i, k, n=metropolis->n_parameters; */
129 /* TVector theta, theta_old; */
130 /* PRECISION logposterior, logposterior_old; */
131
132 /* theta_old=CreateVector(n); */
133 /* theta=CreateVector(n); */
134
135 /* // TStateModel specific calls */
136 /* logposterior_old=LogPrior(model); */
137 /* ConvertThetaToFreeParameters(model,pElementV(theta_old)); */
138
139 /* EquateVector(theta,theta_old); */
140
141 /* for (k=i=0; i < metropolis->n_blocks; i++) */
142 /* { */
143 /* for ( ; k < metropolis->block_offsets[i+1]; k++) */
144 /* if (metropolis->direction[k]) */
145 /* UpdateV(1.0,theta,dw_gaussian_rnd()*ElementV(metropolis->scale,k),metropolis->direction[k]); */
146 /* else */
147 /* ElementV(theta,k)+=dw_gaussian_rnd()*ElementV(metropolis->scale,k); */
148
149 /* // TStateModel specific calls */
150 /* ConvertFreeParametersToTheta(model,pElementV(theta)); */
151 /* logposterior=LogPrior(model); */
152
153 /* if (log(dw_uniform_rnd()) <= logposterior - logposterior_old) */
154 /* { */
155 /* metropolis->jumps[i]++; */
156 /* if (i < metropolis->n_blocks-1) */
157 /* { */
158 /* EquateVector(theta_old,theta); */
159 /* logposterior_old=logposterior; */
160 /* } */
161 /* } */
162 /* else */
163 /* if (i < metropolis->n_blocks-1) */
164 /* EquateVector(theta,theta_old); */
165 /* else */
166 /* // TStateModel specific calls */
167 /* ConvertFreeParametersToTheta(model,pElementV(theta_old)); */
168
169 /* metropolis->draws[i]++; */
170 /* } */
171
172 /* FreeVector(theta); */
173 /* FreeVector(theta_old); */
174 /* } */
175
176 /* /\* */
177 /* Assumes: */
178 /* model: pointer to valid TStateModel structure. */
179
180 /* Results: */
181 /* A metropolis draw of theta */
182 /* *\/ */
183 /* void Draw_metropolis_theta_prior_single_block(int block, TStateModel *model) */
184 /* { */
185 /* // TStateModel specific calls */
186 /* TMetropolis_theta *metropolis=model->metropolis_theta; */
187
188 /* int k, n=metropolis->n_parameters; */
189 /* TVector theta, theta_old; */
190 /* PRECISION logposterior, logposterior_old; */
191
192 /* theta_old=CreateVector(n); */
193 /* theta=CreateVector(n); */
194
195 /* // TStateModel specific calls */
196 /* logposterior_old=LogPrior(model); */
197 /* ConvertThetaToFreeParameters(model,pElementV(theta_old)); */
198
199 /* EquateVector(theta,theta_old); */
200
201 /* for (k=metropolis->block_offsets[block]; k < metropolis->block_offsets[block+1]; k++) */
202 /* if (metropolis->direction[k]) */
203 /* UpdateV(1.0,theta,dw_gaussian_rnd()*ElementV(metropolis->scale,k),metropolis->direction[k]); */
204 /* else */
205 /* ElementV(theta,k)+=dw_gaussian_rnd()*ElementV(metropolis->scale,k); */
206
207 /* // TStateModel specific calls */
208 /* ConvertFreeParametersToTheta(model,pElementV(theta)); */
209 /* logposterior=LogPrior(model); */
210
211 /* if (log(dw_uniform_rnd()) <= logposterior - logposterior_old) */
212 /* metropolis->jumps[block]++; */
213 /* else */
214 /* // TStateModel specific calls */
215 /* ConvertFreeParametersToTheta(model,pElementV(theta_old)); */
216
217 /* metropolis->draws[block]++; */
218
219 /* FreeVector(theta); */
220 /* FreeVector(theta_old); */
221 /* } */
222 /*******************************************************************************/
223 /*******************************************************************************/
224 /*******************************************************************************/
225
226 void AdaptiveScale_metropolis_theta_prior(TStateModel *model, PRECISION mid, int period, int max_period, int verbose)
227 {
228 TMetropolis_theta *metropolis=model->metropolis_theta;
229 PRECISION new_scale, old_scale, rescale, logposterior;
230 TAdaptive **adaptive;
231 int done=0, i, k, begin_time=(int)time((time_t*)NULL), count=0, check=period;
232
233 adaptive=(TAdaptive**)dw_CreateArray_pointer(metropolis->n_blocks,dw_free);
234 for (k=metropolis->n_blocks-1; k >= 0; k--)
235 InitializeAdaptive(period,k,adaptive[k]=CreateAdaptive(mid),metropolis);
236
237 if (verbose)
238 printf("Beginning adaptive burn in -- maximum period = %d.\n",max_period);
239
240 logposterior=LogPrior(model);
241 done=0;
242 while (!done)
243 {
244 count++;
245
246 draw_prior_all_metropolis(model);
247
248 if (count == check)
249 {
250 done=1;
251
252 if (verbose)
253 printf("\n%d iterations completed - elapsed time: %d seconds\n",count,(int)time((time_t*)NULL) - begin_time);
254
255 for (k=metropolis->n_blocks-1; k >= 0; k--)
256 {
257 old_scale=adaptive[k]->scale;
258 new_scale=RecomputeScale(metropolis->jumps[k],metropolis->draws[k],adaptive[k]);
259 if (new_scale != old_scale)
260 for (rescale=new_scale/old_scale, i=metropolis->block_offsets[k]; i < metropolis->block_offsets[k+1]; i++)
261 ElementV(metropolis->scale,i)*=rescale;
262 if (adaptive[k]->period <= max_period) done=0;
263 if (verbose) PrintJumpingInfo(k,adaptive[k],metropolis);
264 }
265
266 check+=period;
267 }
268 }
269
270 for (i=k=0; k < metropolis->n_blocks; k++)
271 if (adaptive[k]->scale != adaptive[k]->best_scale)
272 for (rescale=adaptive[k]->best_scale/adaptive[k]->scale; i < metropolis->block_offsets[k+1]; i++)
273 ElementV(metropolis->scale,i)*=rescale;
274
275 dw_FreeArray(adaptive);
276 }
277
278 /* void AdaptiveScale_metropolis_theta_prior_single_block(int block, TStateModel *model, PRECISION mid, int period, int max_period, int verbose) */
279 /* { */
280 /* TMetropolis_theta *metropolis=model->metropolis_theta; */
281 /* TAdaptive *adaptive; */
282 /* int done=0, i, begin_time=(int)time((time_t*)NULL), count=0, check=period; */
283 /* PRECISION new_scale, old_scale, rescale; */
284
285 /* if ((block < 0) || (block >= metropolis->n_blocks)) */
286 /* { */
287 /* if (verbose) printf("AdaptiveScale_metropolis_theta_singleblock(): Invalid value for block\n"); */
288 /* return; */
289 /* } */
290
291 /* InitializeAdaptive(period,block,adaptive=CreateAdaptive(mid),metropolis); */
292
293 /* if (verbose) */
294 /* printf("Beginning adaptive burn in for block %d -- maximum period = %d.\n",block,max_period); */
295
296 /* while (!done) */
297 /* { */
298 /* count++; */
299
300 /* DrawTransitionMatrixParametersFromPrior(model); */
301 /* Draw_metropolis_theta_prior_single_block(block,model); */
302
303 /* if (count == check) */
304 /* { */
305 /* if (verbose) */
306 /* printf("\n%d iterations completed - elapsed time: %d seconds\n",count,(int)time((time_t*)NULL) - begin_time); */
307
308 /* old_scale=adaptive->scale; */
309 /* new_scale=RecomputeScale(metropolis->jumps[block],metropolis->draws[block],adaptive); */
310 /* if (new_scale != old_scale) */
311 /* for (rescale=new_scale/old_scale, i=metropolis->block_offsets[block]; i < metropolis->block_offsets[block+1]; i++) */
312 /* ElementV(metropolis->scale,i)*=rescale; */
313 /* if (adaptive->period > max_period) done=1; */
314 /* if (verbose) PrintJumpingInfo(block,adaptive,metropolis); */
315
316 /* check+=period; */
317 /* } */
318 /* } */
319
320 /* if (adaptive->scale != adaptive->best_scale) */
321 /* for (rescale=adaptive->best_scale/adaptive->scale, i=metropolis->block_offsets[block]; */
322 /* i < metropolis->block_offsets[block+1]; i++) */
323 /* ElementV(metropolis->scale,i)*=rescale; */
324
325 /* dw_free(adaptive); */
326 /* } */
327
328 /*******************************************************************************/
329 /*******************************************************************************/
330 /*******************************************************************************/
331
332 /*
333 Assumes
334 model : pointer to valid TStateModel structure
335 center : target value for acceptance ration
336 peroid : initial length of sub-runs to calibrate scales
337 max_period : maximum length of sub-runs to calibrate scales
338 verbose : non-zero for output to stdout
339
340 Results
341 The values of model->metropolis_scale are calibrated to obtain the
342 desired acceptance ratio.
343 */
344 void Calibrate_metropolis_theta_prior(TStateModel *model, PRECISION center, int period, int max_period, int verbose)
345 {
346 int n_theta=NumberFreeParametersTheta(model), n_q=NumberFreeParametersQ(model);
347 TVector initial_theta=(TVector)NULL, initial_q=(TVector)NULL;
348
349 // Save initial parameters
350 if (n_theta) ConvertThetaToFreeParameters(model,pElementV(initial_theta=CreateVector(n_theta)));
351 if (n_q) ConvertQToFreeParameters(model,pElementV(initial_q=CreateVector(n_q)));
352
353 // Calibrate
354 AdaptiveScale_metropolis_theta_prior(model,center,period,max_period,verbose);
355
356 // Reset initial parameters
357 if (n_theta)
358 {
359 ConvertFreeParametersToTheta(model,pElementV(initial_theta));
360 FreeVector(initial_theta);
361 }
362 if (n_q)
363 {
364 ConvertFreeParametersToQ(model,pElementV(initial_q));
365 FreeVector(initial_q);
366 }
367 }
368
369 /* /\* */
370 /* Assumes */
371 /* idx : 0 <= idx < NumberFreeParametersTheta(model) */
372 /* model : pointer to valid TStateModel structure */
373 /* center : target acceptance rate */
374 /* peroid : initial length of sub-runs to calibrate scale for direction idx */
375 /* max_period : maximum length of sub-runs to calibrate scale for direction idx */
376 /* verbose : non-zero for output to stdout */
377
378 /* Returns */
379 /* The total number of draws made. A single draw consists of a draw of the */
380 /* states, a draw of the transition matrix parameters, and a draw of theta. The */
381 /* draw of theta is only in the direction idx. */
382
383 /* Results */
384 /* The value of ElementV(model->metropolis_scale_theta,idx) is calibrated to */
385 /* obtain the desired acceptance ratio. */
386 /* *\/ */
387 /* int Calibrate_metropolis_theta_prior_single_direction(int idx, TStateModel *model, PRECISION center, int period, int max_period, int verbose) */
388 /* { */
389 /* TMetropolis_theta *metropolis=model->metropolis_theta; */
390 /* int *dims, n_draws, n_blocks, n_theta=NumberFreeParametersTheta(model), n_q=NumberFreeParametersQ(model); */
391 /* TVector initial_theta=(TVector)NULL, initial_q=(TVector)NULL; */
392
393 /* // save initial parameters */
394 /* if (n_theta) ConvertThetaToFreeParameters(model,pElementV(initial_theta=CreateVector(n_theta))); */
395 /* if (n_q) ConvertQToFreeParameters(model,pElementV(initial_q=CreateVector(n_q))); */
396
397 /* // Save old blocks */
398 /* n_blocks=metropolis->n_blocks; */
399 /* dims=(int*)dw_malloc(n_blocks*sizeof(int)); */
400 /* memcpy(dims,metropolis->block_dims,n_blocks*sizeof(int)); */
401
402 /* // Setup blocks */
403 /* Setup_metropolis_theta_blocks_full(metropolis); */
404
405 /* // Calibrate */
406 /* AdaptiveScale_metropolis_theta_prior_single_block(idx,model,center,period,max_period,verbose); */
407 /* n_draws=metropolis->draws[idx]; */
408
409 /* // Reset starting value */
410 /* if (n_theta) */
411 /* { */
412 /* ConvertFreeParametersToTheta(model,pElementV(initial_theta)); */
413 /* FreeVector(initial_theta); */
414 /* } */
415 /* if (n_q) */
416 /* { */
417 /* ConvertFreeParametersToQ(model,pElementV(initial_q)); */
418 /* FreeVector(initial_q); */
419 /* } */
420
421 /* // Reset blocks */
422 /* Setup_metropolis_theta_blocks(metropolis,n_blocks,dims); */
423 /* dw_free(dims); */
424
425 /* return n_draws; */
426 /* } */
427
428 /* /\* */
429 /* Assumes */
430 /* model : pointer to valid TStateModel structure */
431 /* center_s : target acceptance rate for each direction invidually */
432 /* peroid_s : initial length of sub-runs to calibrate scale for each direction individually */
433 /* max_period_s : maximum length of sub-runs to calibrate scale for each direction individually */
434 /* center_a : target acceptance rate for each block */
435 /* peroid_a : initial length of sub-runs to calibrate scales for each block */
436 /* max_period_a : maximum length of sub-runs to calibrate scales for each block */
437 /* verbose : non-zero for output to stdout */
438
439 /* Returns */
440 /* The total number of theta draws made. */
441
442 /* Results */
443 /* The values of model->metropolis_scale_theta are calibrated to obtain the */
444 /* desired acceptance ratio. */
445 /* *\/ */
446 /* int Calibrate_metropolis_theta_prior_two_pass(TStateModel *model, PRECISION center_s, int period_s, int max_period_s, */
447 /* PRECISION center_a, int period_a, int max_period_a, int verbose) */
448 /* { */
449 /* TMetropolis_theta *metropolis=model->metropolis_theta; */
450 /* int *dims, n_draws=0, n_blocks, i, n_theta=NumberFreeParametersTheta(model), n_q=NumberFreeParametersQ(model); */
451 /* TVector initial_theta=(TVector)NULL, initial_q=(TVector)NULL; */
452
453 /* // save initial value */
454 /* if (n_theta) ConvertThetaToFreeParameters(model,pElementV(initial_theta=CreateVector(n_theta))); */
455 /* if (n_q) ConvertQToFreeParameters(model,pElementV(initial_q=CreateVector(n_q))); */
456
457 /* // Individual direction run */
458 /* if (period_s > 0) */
459 /* { */
460 /* // Save old blocks */
461 /* n_blocks=metropolis->n_blocks; */
462 /* memcpy(dims=(int*)dw_malloc(n_blocks*sizeof(int)),metropolis->block_dims,n_blocks*sizeof(int)); */
463
464 /* // Setup blocks */
465 /* Setup_metropolis_theta_blocks_full(metropolis); */
466
467 /* // Calibrate */
468 /* for (i=0; i < n_theta; i++) */
469 /* { */
470 /* AdaptiveScale_metropolis_theta_prior_single_block(i,model,center_s,period_s,max_period_s,verbose); */
471 /* n_draws+=metropolis->draws[i]; */
472 /* if (n_theta) ConvertFreeParametersToTheta(model,pElementV(initial_theta)); */
473 /* if (n_q) ConvertFreeParametersToQ(model,pElementV(initial_q)); */
474 /* } */
475
476 /* // Reset blocks */
477 /* Setup_metropolis_theta_blocks(metropolis,n_blocks,dims); */
478 /* dw_free(dims); */
479 /* } */
480 /* else */
481 /* ResetCounts_metropolis_theta(metropolis); */
482
483 /* // Final run */
484 /* if (period_a > 0) */
485 /* { */
486 /* AdaptiveScale_metropolis_theta(model,center_a,period_a,max_period_a,verbose); */
487 /* for (i=0; i < metropolis->n_blocks; i++) n_draws+=metropolis->draws[i]; */
488 /* if (n_theta) ConvertFreeParametersToTheta(model,pElementV(initial_theta)); */
489 /* if (n_q) ConvertFreeParametersToQ(model,pElementV(initial_q)); */
490 /* } */
491
492 /* if (n_theta) FreeVector(initial_theta); */
493 /* if (n_q) FreeVector(initial_q); */
494
495 /* return n_draws; */
496 /* } */