@@ -23,15 +23,15 @@ using LFMCMCKernelFun = std::function<epiworld_double(const std::vector< epiworl
23
23
24
24
/* *
25
25
* @brief Proposal function
26
- * @param params_now Vector where to save the new parameters.
27
- * @param params_prev Vector of reference parameters.
26
+ * @param new_params Vector where to save the new parameters.
27
+ * @param old_params Vector of reference parameters.
28
28
* @param m LFMCMC model.
29
29
* @tparam TData
30
30
*/
31
31
template <typename TData>
32
32
inline void proposal_fun_normal (
33
- std::vector< epiworld_double >& params_now ,
34
- const std::vector< epiworld_double >& params_prev ,
33
+ std::vector< epiworld_double >& new_params ,
34
+ const std::vector< epiworld_double >& old_params ,
35
35
LFMCMC<TData>* m
36
36
);
37
37
@@ -60,32 +60,32 @@ inline LFMCMCProposalFun<TData> make_proposal_norm_reflective(
60
60
* Proposals are made within a radious 1 of the current
61
61
* state of the parameters.
62
62
*
63
- * @param params_now Where to write the new parameters
64
- * @param params_prev Reference parameters
63
+ * @param new_params Where to write the new parameters
64
+ * @param old_params Reference parameters
65
65
* @tparam TData
66
66
* @param m LFMCMC model.
67
67
*/
68
68
template <typename TData>
69
69
inline void proposal_fun_unif (
70
- std::vector< epiworld_double >& params_now ,
71
- const std::vector< epiworld_double >& params_prev ,
70
+ std::vector< epiworld_double >& new_params ,
71
+ const std::vector< epiworld_double >& old_params ,
72
72
LFMCMC<TData>* m
73
73
);
74
74
75
75
/* *
76
76
* @brief Uses the uniform kernel with euclidean distance
77
77
*
78
- * @param stats_now Vector of current statistics based on
79
- * simulated data.
80
- * @param stats_obs Vector of observed statistics
78
+ * @param simulated_stats Vector of statistics based on
79
+ * simulated data
80
+ * @param observed_stats Vector of observed statistics
81
81
* @param epsilon Epsilon parameter
82
- * @param m LFMCMC model.
82
+ * @param m LFMCMC model
83
83
* @return epiworld_double
84
84
*/
85
85
template <typename TData>
86
86
inline epiworld_double kernel_fun_uniform (
87
- const std::vector< epiworld_double >& stats_now ,
88
- const std::vector< epiworld_double >& stats_obs ,
87
+ const std::vector< epiworld_double >& simulated_stats ,
88
+ const std::vector< epiworld_double >& observed_stats ,
89
89
epiworld_double epsilon,
90
90
LFMCMC<TData>* m
91
91
);
@@ -94,14 +94,17 @@ inline epiworld_double kernel_fun_uniform(
94
94
* @brief Gaussian kernel
95
95
*
96
96
* @tparam TData
97
- * @param epsilon
98
- * @param m
97
+ * @param simulated_stats Vector of statistics based on
98
+ * simulated data
99
+ * @param observed_stats Vector of observed statistics
100
+ * @param epsilon Epsilon parameter
101
+ * @param m LFMCMC model
99
102
* @return epiworld_double
100
103
*/
101
104
template <typename TData>
102
105
inline epiworld_double kernel_fun_gaussian (
103
- const std::vector< epiworld_double >& stats_now ,
104
- const std::vector< epiworld_double >& stats_obs ,
106
+ const std::vector< epiworld_double >& simulated_stats ,
107
+ const std::vector< epiworld_double >& observed_stats ,
105
108
epiworld_double epsilon,
106
109
LFMCMC<TData>* m
107
110
);
@@ -116,7 +119,7 @@ class LFMCMC {
116
119
private:
117
120
118
121
// Random number sampling
119
- std::mt19937 * engine = nullptr ;
122
+ std::shared_ptr< std:: mt19937 > m_engine = nullptr ;
120
123
121
124
std::shared_ptr< std::uniform_real_distribution<> > runifd =
122
125
std::make_shared< std::uniform_real_distribution<> >(0.0 , 1.0 );
@@ -128,86 +131,98 @@ class LFMCMC {
128
131
std::make_shared< std::gamma_distribution<> >();
129
132
130
133
// Process data
131
- TData * observed_data;
132
-
133
- // Information about the size of the problem
134
- size_t n_samples;
135
- size_t n_statistics;
136
- size_t n_parameters;
134
+ TData m_observed_data;
135
+ std::vector< TData > * m_simulated_data = nullptr ;
137
136
138
- epiworld_double epsilon;
137
+ // Information about the size of the process
138
+ size_t m_n_samples;
139
+ size_t m_n_stats;
140
+ size_t m_n_params;
139
141
140
- std::vector< epiworld_double > params_now;
141
- std::vector< epiworld_double > params_prev;
142
- std::vector< epiworld_double > params_init;
142
+ epiworld_double m_epsilon;
143
143
144
- std::vector< epiworld_double > observed_stats; // /< Observed statistics
144
+ std::vector< epiworld_double > m_initial_params; // /< Initial parameters
145
+ std::vector< epiworld_double > m_current_proposed_params; // /< Proposed parameters for the next sample
146
+ std::vector< epiworld_double > m_current_accepted_params; // /< Most recently accepted parameters (current state of MCMC)
147
+ std::vector< epiworld_double > m_current_proposed_stats; // /< Statistics from simulation run with proposed parameters
148
+ std::vector< epiworld_double > m_current_accepted_stats; // /< Statistics from simulation run with most recently accepted params
145
149
146
- std::vector< epiworld_double > sampled_params; // /< Sampled Parameters
147
- std::vector< epiworld_double > sampled_stats; // /< Sampled statistics
148
- std::vector< epiworld_double > sampled_stats_prob; // /< Sampled statistics
149
- std::vector< bool > sampled_accepted; // /< Indicator of accepted statistics
150
+ std::vector< epiworld_double > m_observed_stats; // /< Observed statistics
150
151
151
- std::vector< epiworld_double > accepted_params; // /< Posterior distribution (accepted samples)
152
- std::vector< epiworld_double > accepted_stats; // /< Posterior distribution (accepted samples)
153
- std::vector< epiworld_double > accepted_params_prob; // /< Posterior probability
152
+ std::vector< epiworld_double > m_all_sample_params; // /< Parameter samples
153
+ std::vector< epiworld_double > m_all_sample_stats; // /< Statistic samples
154
+ std::vector< bool > m_all_sample_acceptance; // /< Indicator if sample was accepted
155
+ std::vector< epiworld_double > m_all_sample_drawn_prob; // /< Drawn probabilities (runif()) for each sample
156
+ std::vector< epiworld_double > m_all_sample_kernel_scores; // /< Kernel scores for each sample
154
157
155
- std::vector< epiworld_double > drawn_prob; // /< Drawn probabilities (runif())
156
- std::vector< TData > * sampled_data = nullptr ;
158
+ std::vector< epiworld_double > m_all_accepted_params; // /< Posterior distribution of parameters from accepted samples
159
+ std::vector< epiworld_double > m_all_accepted_stats; // /< Posterior distribution of statistics from accepted samples
160
+ std::vector< epiworld_double > m_all_accepted_kernel_scores; // /< Kernel scores for each accepted sample
157
161
158
162
// Functions
159
- LFMCMCSimFun<TData> simulation_fun ;
160
- LFMCMCSummaryFun<TData> summary_fun ;
161
- LFMCMCProposalFun<TData> proposal_fun = proposal_fun_normal<TData>;
162
- LFMCMCKernelFun<TData> kernel_fun = kernel_fun_uniform<TData>;
163
+ LFMCMCSimFun<TData> m_simulation_fun ;
164
+ LFMCMCSummaryFun<TData> m_summary_fun ;
165
+ LFMCMCProposalFun<TData> m_proposal_fun = proposal_fun_normal<TData>;
166
+ LFMCMCKernelFun<TData> m_kernel_fun = kernel_fun_uniform<TData>;
163
167
164
168
// Misc
165
- std::vector< std::string > names_parameters ;
166
- std::vector< std::string > names_statistics ;
169
+ std::vector< std::string > m_param_names ;
170
+ std::vector< std::string > m_stat_names ;
167
171
168
- std::chrono::time_point<std::chrono::steady_clock> time_start ;
169
- std::chrono::time_point<std::chrono::steady_clock> time_end ;
172
+ std::chrono::time_point<std::chrono::steady_clock> m_start_time ;
173
+ std::chrono::time_point<std::chrono::steady_clock> m_end_time ;
170
174
175
+ // Timing
171
176
// std::chrono::milliseconds
172
- std::chrono::duration<epiworld_double,std::micro> time_elapsed =
177
+ std::chrono::duration<epiworld_double,std::micro> m_elapsed_time =
173
178
std::chrono::duration<epiworld_double,std::micro>::zero();
174
179
175
- inline void get_elapsed (
180
+ inline void get_elapsed_time (
176
181
std::string unit,
177
182
epiworld_double * last_elapsed,
178
183
std::string * unit_abbr,
179
184
bool print
180
- );
185
+ ) const ;
181
186
182
187
void chrono_start ();
183
188
void chrono_end ();
189
+
190
+ // Progress
191
+ bool verbose = true ;
192
+ Progress progress_bar;
184
193
185
194
public:
186
195
187
196
void run (
188
- std::vector< epiworld_double > param_init ,
197
+ std::vector< epiworld_double > params_init_ ,
189
198
size_t n_samples_,
190
- epiworld_double epsilon_
199
+ epiworld_double epsilon_,
200
+ int seed = -1
191
201
);
192
202
193
203
LFMCMC () {};
194
- LFMCMC (TData & observed_data_) : observed_data(& observed_data_) {};
204
+ LFMCMC (const TData & observed_data_) : m_observed_data( observed_data_) {};
195
205
~LFMCMC () {};
196
206
197
- void set_observed_data (TData & observed_data_) {observed_data = &observed_data_;};
207
+ // Setting LFMCMC variables
208
+ void set_observed_data (const TData & observed_data_) {m_observed_data = observed_data_;};
209
+
198
210
void set_proposal_fun (LFMCMCProposalFun<TData> fun);
199
211
void set_simulation_fun (LFMCMCSimFun<TData> fun);
200
212
void set_summary_fun (LFMCMCSummaryFun<TData> fun);
201
213
void set_kernel_fun (LFMCMCKernelFun<TData> fun);
214
+
215
+ void set_params_names (std::vector< std::string > names);
216
+ void set_stats_names (std::vector< std::string > names);
202
217
203
218
/* *
204
219
* @name Random number generation
205
220
*
206
221
* @param eng
207
222
*/
208
223
// /@{
209
- void set_rand_engine (std::mt19937 & eng);
210
- std::mt19937 & get_rand_endgine ();
224
+ void set_rand_engine (std::shared_ptr< std:: mt19937 > & eng);
225
+ std::shared_ptr< std:: mt19937 > & get_rand_endgine ();
211
226
void seed (epiworld_fast_uint s);
212
227
void set_rand_gamma (epiworld_double alpha, epiworld_double beta);
213
228
epiworld_double runif ();
@@ -219,28 +234,38 @@ class LFMCMC {
219
234
// /@}
220
235
221
236
// Accessing parameters of the function
222
- size_t get_n_samples () const {return n_samples;};
223
- size_t get_n_statistics () const {return n_statistics;};
224
- size_t get_n_parameters () const {return n_parameters;};
225
- epiworld_double get_epsilon () const {return epsilon;};
226
-
227
- const std::vector< epiworld_double > & get_params_now () {return params_now;};
228
- const std::vector< epiworld_double > & get_params_prev () {return params_prev;};
229
- const std::vector< epiworld_double > & get_params_init () {return params_init;};
230
- const std::vector< epiworld_double > & get_statistics_obs () {return observed_stats;};
231
- const std::vector< epiworld_double > & get_statistics_hist () {return sampled_stats;};
232
- const std::vector< bool > & get_statistics_accepted () {return sampled_accepted;};
233
- const std::vector< epiworld_double > & get_posterior_lf_prob () {return accepted_params_prob;};
234
- const std::vector< epiworld_double > & get_drawn_prob () {return drawn_prob;};
235
- std::vector< TData > * get_sampled_data () {return sampled_data;};
236
-
237
- void set_par_names (std::vector< std::string > names);
238
- void set_stats_names (std::vector< std::string > names);
237
+ size_t get_n_samples () const {return m_n_samples;};
238
+ size_t get_n_stats () const {return m_n_stats;};
239
+ size_t get_n_params () const {return m_n_params;};
240
+ epiworld_double get_epsilon () const {return m_epsilon;};
241
+
242
+ const std::vector< epiworld_double > & get_initial_params () const {return m_initial_params;};
243
+ const std::vector< epiworld_double > & get_current_proposed_params () const {return m_current_proposed_params;};
244
+ const std::vector< epiworld_double > & get_current_accepted_params () const {return m_current_accepted_params;};
245
+ const std::vector< epiworld_double > & get_current_proposed_stats () const {return m_current_proposed_stats;};
246
+ const std::vector< epiworld_double > & get_current_accepted_stats () const {return m_current_accepted_stats;};
247
+
248
+ const std::vector< epiworld_double > & get_observed_stats () const {return m_observed_stats;};
249
+
250
+ const std::vector< epiworld_double > & get_all_sample_params () const {return m_all_sample_params;};
251
+ const std::vector< epiworld_double > & get_all_sample_stats () const {return m_all_sample_stats;};
252
+ const std::vector< bool > & get_all_sample_acceptance () const {return m_all_sample_acceptance;};
253
+ const std::vector< epiworld_double > & get_all_sample_drawn_prob () const {return m_all_sample_drawn_prob;};
254
+ const std::vector< epiworld_double > & get_all_sample_kernel_scores () const {return m_all_sample_kernel_scores;};
255
+
256
+ const std::vector< epiworld_double > & get_all_accepted_params () const {return m_all_accepted_params;};
257
+ const std::vector< epiworld_double > & get_all_accepted_stats () const {return m_all_accepted_stats;};
258
+ const std::vector< epiworld_double > & get_all_accepted_kernel_scores () const {return m_all_accepted_kernel_scores;};
259
+
260
+ std::vector< TData > * get_simulated_data () const {return m_simulated_data;};
239
261
240
- std::vector< epiworld_double > get_params_mean ();
241
- std::vector< epiworld_double > get_stats_mean ();
262
+ std::vector< epiworld_double > get_mean_params ();
263
+ std::vector< epiworld_double > get_mean_stats ();
242
264
243
- void print () ;
265
+ // Printing
266
+ LFMCMC<TData> & verbose_off ();
267
+ LFMCMC<TData> & verbose_on ();
268
+ void print (size_t burnin = 0u ) const ;
244
269
245
270
};
246
271
0 commit comments