Skip to content

Commit 624a399

Browse files
author
Milo Banks
authored
Update Core (#83)
* [pypa] * [core] update, fix #82 * Revert "[pypa]" This reverts commit 3ec1841.
1 parent f7770e8 commit 624a399

File tree

10 files changed

+361
-277
lines changed

10 files changed

+361
-277
lines changed

include/epiworld/agent-meat-virus-sampling.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace sampler {
2020
* @return Virus<TSeq>* of the selected virus. If none selected (or none
2121
* available,) returns a nullptr;
2222
*/
23-
template<typename TSeq>
23+
template<typename TSeq = EPI_DEFAULT_TSEQ>
2424
inline std::function<void(Agent<TSeq>*,Model<TSeq>*)> make_update_susceptible(
2525
std::vector< epiworld_fast_uint > exclude = {}
2626
)
@@ -179,7 +179,7 @@ inline std::function<void(Agent<TSeq>*,Model<TSeq>*)> make_update_susceptible(
179179
* @return Virus<TSeq>* of the selected virus. If none selected (or none
180180
* available,) returns a nullptr;
181181
*/
182-
template<typename TSeq = int>
182+
template<typename TSeq = EPI_DEFAULT_TSEQ>
183183
inline std::function<Virus<TSeq>*(Agent<TSeq>*,Model<TSeq>*)> make_sample_virus_neighbors(
184184
std::vector< epiworld_fast_uint > exclude = {}
185185
)
@@ -347,7 +347,7 @@ inline std::function<Virus<TSeq>*(Agent<TSeq>*,Model<TSeq>*)> make_sample_virus_
347347
* @return Virus<TSeq>* of the selected virus. If none selected (or none
348348
* available,) returns a nullptr;
349349
*/
350-
template<typename TSeq = int>
350+
template<typename TSeq = EPI_DEFAULT_TSEQ>
351351
inline Virus<TSeq> * sample_virus_single(Agent<TSeq> * p, Model<TSeq> * m)
352352
{
353353

include/epiworld/epiworld.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
/* Versioning */
2020
#define EPIWORLD_VERSION_MAJOR 0
21-
#define EPIWORLD_VERSION_MINOR 4
21+
#define EPIWORLD_VERSION_MINOR 6
2222
#define EPIWORLD_VERSION_PATCH 0
2323

2424
static const int epiworld_version_major = EPIWORLD_VERSION_MAJOR;
@@ -88,4 +88,4 @@ namespace epiworld {
8888

8989
}
9090

91-
#endif
91+
#endif

include/epiworld/math/lfmcmc/lfmcmc-bones.hpp

Lines changed: 101 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ using LFMCMCKernelFun = std::function<epiworld_double(const std::vector< epiworl
2323

2424
/**
2525
* @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.
2828
* @param m LFMCMC model.
2929
* @tparam TData
3030
*/
3131
template<typename TData>
3232
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,
3535
LFMCMC<TData>* m
3636
);
3737

@@ -60,32 +60,32 @@ inline LFMCMCProposalFun<TData> make_proposal_norm_reflective(
6060
* Proposals are made within a radious 1 of the current
6161
* state of the parameters.
6262
*
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
6565
* @tparam TData
6666
* @param m LFMCMC model.
6767
*/
6868
template<typename TData>
6969
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,
7272
LFMCMC<TData>* m
7373
);
7474

7575
/**
7676
* @brief Uses the uniform kernel with euclidean distance
7777
*
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
8181
* @param epsilon Epsilon parameter
82-
* @param m LFMCMC model.
82+
* @param m LFMCMC model
8383
* @return epiworld_double
8484
*/
8585
template<typename TData>
8686
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,
8989
epiworld_double epsilon,
9090
LFMCMC<TData>* m
9191
);
@@ -94,14 +94,17 @@ inline epiworld_double kernel_fun_uniform(
9494
* @brief Gaussian kernel
9595
*
9696
* @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
99102
* @return epiworld_double
100103
*/
101104
template<typename TData>
102105
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,
105108
epiworld_double epsilon,
106109
LFMCMC<TData>* m
107110
);
@@ -116,7 +119,7 @@ class LFMCMC {
116119
private:
117120

118121
// Random number sampling
119-
std::mt19937 * engine = nullptr;
122+
std::shared_ptr< std::mt19937 > m_engine = nullptr;
120123

121124
std::shared_ptr< std::uniform_real_distribution<> > runifd =
122125
std::make_shared< std::uniform_real_distribution<> >(0.0, 1.0);
@@ -128,86 +131,98 @@ class LFMCMC {
128131
std::make_shared< std::gamma_distribution<> >();
129132

130133
// 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;
137136

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;
139141

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;
143143

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
145149

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
150151

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
154157

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
157161

158162
// 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>;
163167

164168
// 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;
167171

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;
170174

175+
// Timing
171176
// std::chrono::milliseconds
172-
std::chrono::duration<epiworld_double,std::micro> time_elapsed =
177+
std::chrono::duration<epiworld_double,std::micro> m_elapsed_time =
173178
std::chrono::duration<epiworld_double,std::micro>::zero();
174179

175-
inline void get_elapsed(
180+
inline void get_elapsed_time(
176181
std::string unit,
177182
epiworld_double * last_elapsed,
178183
std::string * unit_abbr,
179184
bool print
180-
);
185+
) const;
181186

182187
void chrono_start();
183188
void chrono_end();
189+
190+
// Progress
191+
bool verbose = true;
192+
Progress progress_bar;
184193

185194
public:
186195

187196
void run(
188-
std::vector< epiworld_double > param_init,
197+
std::vector< epiworld_double > params_init_,
189198
size_t n_samples_,
190-
epiworld_double epsilon_
199+
epiworld_double epsilon_,
200+
int seed = -1
191201
);
192202

193203
LFMCMC() {};
194-
LFMCMC(TData & observed_data_) : observed_data(&observed_data_) {};
204+
LFMCMC(const TData & observed_data_) : m_observed_data(observed_data_) {};
195205
~LFMCMC() {};
196206

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+
198210
void set_proposal_fun(LFMCMCProposalFun<TData> fun);
199211
void set_simulation_fun(LFMCMCSimFun<TData> fun);
200212
void set_summary_fun(LFMCMCSummaryFun<TData> fun);
201213
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);
202217

203218
/**
204219
* @name Random number generation
205220
*
206221
* @param eng
207222
*/
208223
///@{
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();
211226
void seed(epiworld_fast_uint s);
212227
void set_rand_gamma(epiworld_double alpha, epiworld_double beta);
213228
epiworld_double runif();
@@ -219,28 +234,38 @@ class LFMCMC {
219234
///@}
220235

221236
// 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;};
239261

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();
242264

243-
void print() ;
265+
// Printing
266+
LFMCMC<TData> & verbose_off();
267+
LFMCMC<TData> & verbose_on();
268+
void print(size_t burnin = 0u) const;
244269

245270
};
246271

0 commit comments

Comments
 (0)