standalone_gqs.hpp
Go to the documentation of this file.
1 #ifndef STAN_SERVICES_SAMPLE_STANDALONE_GQS_HPP
2 #define STAN_SERVICES_SAMPLE_STANDALONE_GQS_HPP
3 
10 #include <string>
11 #include <vector>
12 
13 namespace stan {
14  namespace services {
15 
16  /**
17  * Return the number of constrained parameters for the specified
18  * model.
19  *
20  * @tparam Model type of model
21  * @param[in] model model to query
22  * @return number of constrained parameters for the model
23  */
24  template <class Model>
26  std::vector<std::string> param_names;
27  static const bool include_tparams = false;
28  static const bool include_gqs = false;
29  model.constrained_param_names(param_names, include_tparams,
30  include_gqs);
31  return param_names.size();
32  }
33 
34  /**
35  * Given a set of draws from a fitted model, generate corresponding
36  * quantities of interest. Data written to callback writer.
37  * Return code indicates success or type of error.
38  *
39  * @tparam Model model class
40  * @param[in] model instantiated model
41  * @param[in] draws sequence of draws of unconstrained parameters
42  * @param[in] seed seed to use for randomization
43  * @param[in, out] interrupt called every iteration
44  * @param[in, out] logger logger to which to write warning and error messages
45  * @param[in, out] sample_writer writer to which draws are written
46  * @return error code
47  */
48  template <class Model>
50  const std::vector<std::vector<double> >& draws,
51  unsigned int seed,
52  callbacks::interrupt& interrupt,
53  callbacks::logger& logger,
54  callbacks::writer& sample_writer) {
55  if (draws.empty()) {
56  logger.error("Empty set of draws from fitted model.");
57  return error_codes::DATAERR;
58  }
59 
60  int num_params = num_constrained_params(model);
61  std::vector<std::string> gq_names;
62  static const bool include_tparams = false;
63  static const bool include_gqs = true;
64  model.constrained_param_names(gq_names, include_tparams, include_gqs);
65  if (!(static_cast<size_t>(num_params) < gq_names.size())) {
66  logger.error("Model doesn't generate any quantities of interest.");
67  return error_codes::CONFIG;
68  }
69 
70  util::gq_writer writer(sample_writer, logger, num_params);
71  boost::ecuyer1988 rng = util::create_rng(seed, 1);
72  writer.write_gq_names(model);
73 
74  std::stringstream msg;
75  for (const std::vector<double>& draw : draws) {
76  if (static_cast<size_t>(num_params) != draw.size()) {
77  msg << "Wrong number of params in draws from fitted model. ";
78  msg << "Expecting " << num_params << " columns, ";
79  msg << "found " << draws[0].size() << " columns.";
80  std::string msgstr = msg.str();
81  logger.error(msgstr);
82  return error_codes::DATAERR;
83  }
84  interrupt(); // call out to interrupt and fail
85  writer.write_gq_values(model, rng, draw);
86  }
87  return error_codes::OK;
88  }
89 
90  }
91 }
92 #endif
rosenbrock_model_namespace::rosenbrock_model Model
unsigned int seed
Definition: runWimpSim.h:102
void write_gq_values(const Model &model, RNG &rng, const std::vector< double > &draw)
Definition: gq_writer.hpp:72
virtual void error(const std::string &message)
Definition: logger.hpp:75
int standalone_generate(const Model &model, const std::vector< std::vector< double > > &draws, unsigned int seed, callbacks::interrupt &interrupt, callbacks::logger &logger, callbacks::writer &sample_writer)
int num_constrained_params(const Model &model)
const XML_Char XML_Content * model
Definition: expat.h:151
boost::ecuyer1988 create_rng(unsigned int seed, unsigned int chain)
Definition: create_rng.hpp:25
void write_gq_names(const Model &model)
Definition: gq_writer.hpp:49
enum BeamMode string