log_prob_propto.hpp
Go to the documentation of this file.
1 #ifndef STAN_MODEL_LOG_PROB_PROPTO_HPP
2 #define STAN_MODEL_LOG_PROB_PROPTO_HPP
3 
4 #include <stan/math/rev/mat.hpp>
5 #include <iostream>
6 #include <vector>
7 
8 namespace stan {
9  namespace model {
10 
11  /**
12  * Helper function to calculate log probability for
13  * <code>double</code> scalars up to a proportion.
14  *
15  * This implementation wraps the <code>double</code> values in
16  * <code>stan::math::var</code> and calls the model's
17  * <code>log_prob()</code> function with <code>propto=true</code>
18  * and the specified parameter for applying the Jacobian
19  * adjustment for transformed parameters.
20  *
21  * @tparam propto True if calculation is up to proportion
22  * (double-only terms dropped).
23  * @tparam jacobian_adjust_transform True if the log absolute
24  * Jacobian determinant of inverse parameter transforms is added to
25  * the log probability.
26  * @tparam M Class of model.
27  * @param[in] model Model.
28  * @param[in] params_r Real-valued parameters.
29  * @param[in] params_i Integer-valued parameters.
30  * @param[in,out] msgs
31  */
32  template <bool jacobian_adjust_transform, class M>
33  double log_prob_propto(const M& model,
34  std::vector<double>& params_r,
35  std::vector<int>& params_i,
36  std::ostream* msgs = 0) {
37  using stan::math::var;
38  using std::vector;
39  vector<var> ad_params_r;
40  ad_params_r.reserve(model.num_params_r());
41  for (size_t i = 0; i < model.num_params_r(); ++i)
42  ad_params_r.push_back(params_r[i]);
43  try {
44  double lp
45  = model.template log_prob<true, jacobian_adjust_transform>
46  (ad_params_r, params_i, msgs).val();
48  return lp;
49  } catch (std::exception &ex) {
51  throw;
52  }
53  }
54 
55  /**
56  * Helper function to calculate log probability for
57  * <code>double</code> scalars up to a proportion.
58  *
59  * This implementation wraps the <code>double</code> values in
60  * <code>stan::math::var</code> and calls the model's
61  * <code>log_prob()</code> function with <code>propto=true</code>
62  * and the specified parameter for applying the Jacobian
63  * adjustment for transformed parameters.
64  *
65  * @tparam propto True if calculation is up to proportion
66  * (double-only terms dropped).
67  * @tparam jacobian_adjust_transform True if the log absolute
68  * Jacobian determinant of inverse parameter transforms is added to
69  * the log probability.
70  * @tparam M Class of model.
71  * @param[in] model Model.
72  * @param[in] params_r Real-valued parameters.
73  * @param[in,out] msgs
74  */
75  template <bool jacobian_adjust_transform, class M>
76  double log_prob_propto(const M& model,
77  Eigen::VectorXd& params_r,
78  std::ostream* msgs = 0) {
79  using stan::math::var;
80  using std::vector;
81  vector<int> params_i(0);
82 
83  double lp;
84  try {
85  vector<var> ad_params_r;
86  ad_params_r.reserve(model.num_params_r());
87  for (size_t i = 0; i < model.num_params_r(); ++i)
88  ad_params_r.push_back(params_r(i));
89  lp
90  = model
91  .template log_prob<true,
92  jacobian_adjust_transform>(ad_params_r, params_i,
93  msgs)
94  .val();
95  } catch (std::exception &ex) {
97  throw;
98  }
100  return lp;
101  }
102 
103  }
104 }
105 #endif
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
static void recover_memory()
const XML_Char XML_Content * model
Definition: expat.h:151
double log_prob_propto(const M &model, std::vector< double > &params_r, std::vector< int > &params_i, std::ostream *msgs=0)