generate_transitions.hpp
Go to the documentation of this file.
1 #ifndef STAN_SERVICES_UTIL_GENERATE_TRANSITIONS_HPP
2 #define STAN_SERVICES_UTIL_GENERATE_TRANSITIONS_HPP
3 
8 #include <string>
9 
10 namespace stan {
11  namespace services {
12  namespace util {
13 
14  /**
15  * Generates MCMC transitions.
16  *
17  * @tparam Model model class
18  * @tparam RNG random number generator class
19  * @param[in,out] sampler MCMC sampler used to generate transitions
20  * @param[in] num_iterations number of MCMC transitions
21  * @param[in] start starting iteration number used for printing messages
22  * @param[in] finish end iteration number used for printing messages
23  * @param[in] num_thin when save is true, a draw will be written to the
24  * mcmc_writer every num_thin iterations
25  * @param[in] refresh number of iterations to print a message. If
26  * refresh is zero, iteration number messages will not be printed
27  * @param[in] save if save is true, the transitions will be written
28  * to the mcmc_writer. If false, transitions will not be written
29  * @param[in] warmup indicates whether these transitions are warmup. Used
30  * for printing iteration number messages
31  * @param[in,out] mcmc_writer writer to handle mcmc otuput
32  * @param[in,out] init_s starts as the initial unconstrained parameter
33  * values. When the function completes, this will have the final
34  * iteration's unconstrained parameter values
35  * @param[in] model model
36  * @param[in,out] base_rng random number generator
37  * @param[in,out] callback interrupt callback called once an iteration
38  * @param[in,out] logger logger for messages
39  */
40  template <class Model, class RNG>
42  int num_iterations, int start,
43  int finish, int num_thin, int refresh,
44  bool save, bool warmup,
46  stan::mcmc::sample& init_s,
47  Model& model, RNG& base_rng,
48  callbacks::interrupt& callback,
49  callbacks::logger& logger) {
50  for (int m = 0; m < num_iterations; ++m) {
51  callback();
52 
53  if (refresh > 0
54  && (start + m + 1 == finish
55  || m == 0
56  || (m + 1) % refresh == 0)) {
57  int it_print_width
58  = std::ceil(std::log10(static_cast<double>(finish)));
59  std::stringstream message;
60  message << "Iteration: ";
61  message << std::setw(it_print_width) << m + 1 + start
62  << " / " << finish;
63  message << " [" << std::setw(3)
64  << static_cast<int>( (100.0 * (start + m + 1)) / finish )
65  << "%] ";
66  message << (warmup ? " (Warmup)" : " (Sampling)");
67 
68  logger.info(message);
69  }
70 
71  init_s = sampler.transition(init_s, logger);
72 
73  if (save && ((m % num_thin) == 0)) {
74  mcmc_writer.write_sample_params(base_rng, init_s, sampler, model);
75  mcmc_writer.write_diagnostic_params(init_s, sampler);
76  }
77  }
78  }
79 
80  }
81  }
82 }
83 
84 #endif
Filter events based on their run/event numbers.
virtual sample transition(sample &init_sample, callbacks::logger &logger)=0
void write_sample_params(RNG &rng, stan::mcmc::sample &sample, stan::mcmc::base_mcmc &sampler, Model &model)
void write_diagnostic_params(stan::mcmc::sample &sample, stan::mcmc::base_mcmc &sampler)
rosenbrock_model_namespace::rosenbrock_model Model
void save(bool IsData, bool Bad)
Definition: cellShifts.C:453
virtual void info(const std::string &message)
Definition: logger.hpp:47
T log10(T number)
Definition: d0nt_math.hpp:120
const XML_Char XML_Content * model
Definition: expat.h:151
void generate_transitions(stan::mcmc::base_mcmc &sampler, int num_iterations, int start, int finish, int num_thin, int refresh, bool save, bool warmup, util::mcmc_writer &mcmc_writer, stan::mcmc::sample &init_s, Model &model, RNG &base_rng, callbacks::interrupt &callback, callbacks::logger &logger)
void refresh()
Definition: show_event.C:21
fvar< T > ceil(const fvar< T > &x)
Definition: ceil.hpp:11