Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mcmc_writer.hpp
Go to the documentation of this file.
1 #ifndef STAN__IO__MCMC__WRITER__HPP
2 #define STAN__IO__MCMC__WRITER__HPP
3 
4 #include <ostream>
5 #include <iomanip>
6 #include <string>
7 #include <vector>
8 
10 #include <stan/mcmc/sample.hpp>
11 #include <stan/model/prob_grad.hpp>
12 
14 
15 namespace stan {
16 
17  namespace io {
18 
26  template <class M,
27  class SampleRecorder, class DiagnosticRecorder,
28  class MessageRecorder>
29  class mcmc_writer {
30 
31  private:
32  SampleRecorder& sample_recorder_;
33  DiagnosticRecorder& diagnostic_recorder_;
34  MessageRecorder& message_recorder_;
35 
36  std::ostream* msg_stream_;
37 
38  public:
39 
51  mcmc_writer(SampleRecorder& sample_recorder,
52  DiagnosticRecorder& diagnostic_recorder,
53  MessageRecorder& message_recorder,
54  std::ostream* msg_stream = 0)
55  : sample_recorder_(sample_recorder),
56  diagnostic_recorder_(diagnostic_recorder),
57  message_recorder_(message_recorder),
58  msg_stream_(msg_stream) {
59  }
60 
80  stan::mcmc::base_mcmc* sampler,
81  M& model) {
82  std::vector<std::string> names;
83 
84  sample.get_sample_param_names(names);
85  sampler->get_sampler_param_names(names);
86  model.constrained_param_names(names, true, true);
87 
88  sample_recorder_(names);
89  }
90 
91 
106  template <class RNG>
107  void write_sample_params(RNG& rng,
109  stan::mcmc::base_mcmc& sampler,
110  M& model) {
111  std::vector<double> values;
112 
113  sample.get_sample_params(values);
114  sampler.get_sampler_params(values);
115 
116  Eigen::VectorXd model_values;
117 
118  model.write_array(rng,
119  const_cast<Eigen::VectorXd&>(sample.cont_params()),
120  model_values,
121  true, true,
122  msg_stream_);
123 
124  for (int i = 0; i < model_values.size(); ++i)
125  values.push_back(model_values(i));
126 
127  sample_recorder_(values);
128  }
129 
138  template <class Recorder>
140  Recorder& recorder) {
141  if (!recorder.is_recording())
142  return;
143  std::stringstream stream;
144  sampler->write_sampler_state(&stream);
145 
146  recorder("Adaptation terminated\n" + stream.str());
147  }
148 
149 
158  write_adapt_finish(sampler, sample_recorder_);
159  write_adapt_finish(sampler, diagnostic_recorder_);
160  }
161 
162 
176  stan::mcmc::base_mcmc* sampler,
177  M& model) {
178  std::vector<std::string> names;
179 
180  sample.get_sample_param_names(names);
181  sampler->get_sampler_param_names(names);
182 
183  std::vector<std::string> model_names;
184  model.unconstrained_param_names(model_names, false, false);
185 
186  sampler->get_sampler_diagnostic_names(model_names, names);
187 
188  diagnostic_recorder_(names);
189  }
190 
204  stan::mcmc::base_mcmc* sampler) {
205  std::vector<double> values;
206 
207  sample.get_sample_params(values);
208  sampler->get_sampler_params(values);
209  sampler->get_sampler_diagnostics(values);
210 
211  diagnostic_recorder_(values);
212  }
213 
214 
230  template <class Recorder>
231  void write_timing(double warmDeltaT, double sampleDeltaT,
232  Recorder& recorder) {
233  if (!recorder.is_recording())
234  return;
235 
236  std::string title(" Elapsed Time: ");
237  std::stringstream ss;
238 
239 
240  recorder();
241 
242  ss.str("");
243  ss << title << warmDeltaT << " seconds (Warm-up)";
244  recorder(ss.str());
245 
246  ss.str("");
247  ss << std::string(title.size(), ' ') << sampleDeltaT
248  << " seconds (Sampling)";
249  recorder(ss.str());
250 
251  ss.str("");
252  ss << std::string(title.size(), ' ')
253  << warmDeltaT + sampleDeltaT
254  << " seconds (Total)";
255  recorder(ss.str());
256 
257  recorder();
258  }
259 
260 
267  void write_timing(double warmDeltaT, double sampleDeltaT) {
268  write_timing(warmDeltaT, sampleDeltaT, sample_recorder_);
269  write_timing(warmDeltaT, sampleDeltaT, diagnostic_recorder_);
270  write_timing(warmDeltaT, sampleDeltaT, message_recorder_);
271  }
272 
273  };
274 
275  } //io
276 
277 } // stan
278 
279 #endif
mcmc_writer(SampleRecorder &sample_recorder, DiagnosticRecorder &diagnostic_recorder, MessageRecorder &message_recorder, std::ostream *msg_stream=0)
Constructor.
Definition: mcmc_writer.hpp:51
void write_sample_params(RNG &rng, stan::mcmc::sample &sample, stan::mcmc::base_mcmc &sampler, M &model)
Outputs samples.
void write_sample_names(stan::mcmc::sample &sample, stan::mcmc::base_mcmc *sampler, M &model)
Outputs parameter string names.
Definition: mcmc_writer.hpp:79
virtual void get_sampler_diagnostic_names(std::vector< std::string > &model_names, std::vector< std::string > &names)
Definition: base_mcmc.hpp:35
virtual void get_sampler_params(std::vector< double > &values)
Definition: base_mcmc.hpp:31
void get_sample_params(std::vector< double > &values)
Definition: sample.hpp:55
void write_adapt_finish(stan::mcmc::base_mcmc *sampler, Recorder &recorder)
Internal method.
virtual void get_sampler_diagnostics(std::vector< double > &values)
Definition: base_mcmc.hpp:38
double cont_params(int k) const
Definition: sample.hpp:30
void write_timing(double warmDeltaT, double sampleDeltaT, Recorder &recorder)
Internal method.
virtual void get_sampler_param_names(std::vector< std::string > &names)
Definition: base_mcmc.hpp:29
void write_diagnostic_names(stan::mcmc::sample sample, stan::mcmc::base_mcmc *sampler, M &model)
Print diagnostic names.
void write_adapt_finish(stan::mcmc::base_mcmc *sampler)
Prints additional info to the streams.
void write_timing(double warmDeltaT, double sampleDeltaT)
Print timing information to all streams.
mcmc_writer writes out headers and samples
Definition: mcmc_writer.hpp:29
virtual void write_sampler_state(std::ostream *o)
Definition: base_mcmc.hpp:33
void write_diagnostic_params(stan::mcmc::sample &sample, stan::mcmc::base_mcmc *sampler)
Print diagnostic params to the diagnostic stream.
void get_sample_param_names(std::vector< std::string > &names)
Definition: sample.hpp:50
void sample(stan::mcmc::base_mcmc *sampler, int num_warmup, int num_samples, int num_thin, int refresh, bool save, stan::io::mcmc_writer< Model, SampleRecorder, DiagnosticRecorder, MessageRecorder > &writer, stan::mcmc::sample &init_s, Model &model, RNG &base_rng, const std::string &prefix, const std::string &suffix, std::ostream &o, StartTransitionCallback &callback)
Definition: sample.hpp:13

     [ Stan Home Page ] © 2011–2014, Stan Development Team.