Debian Codegraph @

contrib/ms-sbvar/switch_dw/switching/dw_metropolis_block.h

raw
1 /*
2 * Copyright (C) 1996-2012 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 #ifndef __dw_metropolis_block__
19 #define __dw_metropolis_block__
20
21 #include "dw_matrix.h"
22 #include "dw_switch.h"
23
24 #ifdef __cplusplus
25 extern "C"
26 {
27 #endif
28
29 /*******************************************************************************/
30 /**************************** TMetropolis Structure ****************************/
31 /*******************************************************************************/
32 typedef struct TMetropolis_block_tag
33 {
34 //=== Metropolis parameters ===
35 int n_parameters;
36 int n_blocks;
37 int *block_dims;
38 int *block_offsets;
39 TVector scale;
40 TVector *direction;
41
42 //=== Simulation status fields ===
43 int *jumps;
44 int *draws;
45
46 //=== Simulation functions ===
47 void *structure;
48 void (*pGetParameters)(void*, PRECISION*);
49 void (*pPutParameters)(void*, PRECISION*);
50 PRECISION (*pDensity)(void*);
51
52 //=== Other parameters ===
53 int n_other_parameters;
54 void (*pGetOtherParameters)(void*, PRECISION*);
55 void (*pPutOtherParameters)(void*, PRECISION*);
56 PRECISION (*pDrawGibbs)(PRECISION, void*);
57
58 } TMetropolis_block;
59
60 TMetropolis_block* Create_metropolis_block(int n_parameters, void (*Get)(void*, PRECISION*), void (*Put)(void*, PRECISION*),PRECISION (*Density)(void*),PRECISION (*Gibbs)(PRECISION, void*));
61 void Free_metropolis_block(TMetropolis_block *metropolis);
62
63 void Setup_metropolis(TMetropolis_block *metropolis, TVector scale, TVector *directions);
64 void Setup_metropolis_diagonal(TMetropolis_block *metropolis);
65 void Setup_metropolis_hessian(TMetropolis_block *metropolis, TMatrix hessian);
66 void Setup_metropolis_variance(TMetropolis_block *metropolis, TMatrix variance);
67
68 void Setup_metropolis_blocks(TMetropolis_block *metropolis, int n_blocks, int *dims);
69 void Setup_metropolis_full(TMetropolis_block *metropolis);
70 void Setup_metropolis_single(TMetropolis_block *metropolis);
71
72 void ResetCounts_metropolis_block(TMetropolis_block *metropolis);
73
74 PRECISION draw_metropolis_in_gibbs(PRECISION logdensity, TMetropolis_block *metropolis);
75 PRECISION metropolis_draw_all_blocks(PRECISION logdensity, TMetropolis_block *metropolis);
76 PRECISION metropolis_draw_single_block(PRECISION logdensity, int block, TMetropolis_block *metropolis);
77
78 void Calibrate_metropolis_theta(TStateModel *model, PRECISION center, int period, int max_period, int verbose);
79 int Calibrate_metropolis_theta_single_direction(int idx, TStateModel *model, PRECISION center, int period, int max_period, int verbose);
80 int Calibrate_metropolis_theta_two_pass(TStateModel *model, PRECISION center_s, int period_s, int max_period_s, PRECISION center_a, int period_a, int max_period_a, int verbose);
81
82
83 /*******************************************************************************/
84 /****************************** Adaptive Scaling *******************************/
85 /*******************************************************************************/
86 typedef struct
87 {
88 int period;
89 int begin_draws;
90 int end_draws;
91 int begin_jumps;
92 PRECISION previous_ratio;
93 PRECISION scale;
94 PRECISION best_scale;
95 PRECISION low_scale;
96 PRECISION low_jump_ratio;
97 PRECISION high_scale;
98 PRECISION high_jump_ratio;
99
100 PRECISION mid;
101 PRECISION log_mid;
102 PRECISION lower_bound;
103 PRECISION upper_bound;
104
105 } TAdaptive;
106 TAdaptive* CreateAdaptive(PRECISION mid);
107 void SetAdaptiveCenter(PRECISION mid, TAdaptive *adaptive);
108 void InitializeAdaptive(int period, int block, TAdaptive *adaptive, TMetropolis_block *metropolis);
109 PRECISION RecomputeScale(int end_jumps, int end_draws, TAdaptive *adaptive);
110 void PrintJumpingInfo(int b, TAdaptive *adaptive, TMetropolis_block *metropolis);
111 void AdaptiveScale_metropolis_theta(TStateModel *model, PRECISION mid, int period, int max_period, int verbose);
112 void AdaptiveScale_metropolis_theta_single_block(int block, TStateModel *model, PRECISION mid, int period, int max_period, int verbose);
113
114 #ifdef __cplusplus
115 }
116 #endif
117
118 #endif