1 #ifndef __STAN__MCMC__UTIL_HPP__
2 #define __STAN__MCMC__UTIL_HPP__
7 #include <boost/random/uniform_01.hpp>
8 #include <boost/random/mersenne_twister.hpp>
9 #include <boost/exception/diagnostic_information.hpp>
10 #include <boost/exception_ptr.hpp>
20 const std::domain_error&
e) {
21 if (!error_msgs)
return;
22 *error_msgs << std::endl
23 <<
"Informational Message: The parameter state is about to be Metropolis"
24 <<
" rejected due to the following underlying, non-fatal (really)"
25 <<
" issue (and please ignore that what comes next might say 'error'): "
28 <<
"If the problem persists across multiple draws, you might have"
29 <<
" a problem with an initial state or a gradient somewhere."
31 <<
" If the problem does not persist, the resulting samples will still"
32 <<
" be drawn from the posterior."
59 std::vector<double>& x, std::vector<double>& m,
60 std::vector<double>& g,
double epsilon,
61 std::ostream* error_msgs = 0,
62 std::ostream* output_msgs = 0) {
68 }
catch (std::domain_error
e) {
70 logp = -std::numeric_limits<double>::infinity();
81 const std::vector<double>& step_sizes,
82 std::vector<double>& x, std::vector<double>& m,
83 std::vector<double>& g,
double epsilon,
84 std::ostream* error_msgs = 0,
85 std::ostream* output_msgs = 0) {
86 for (
size_t i = 0; i < m.size(); i++)
87 m[i] += 0.5 * epsilon * step_sizes[i] * g[i];
88 for (
size_t i = 0; i < x.size(); i++)
89 x[i] += epsilon * step_sizes[i] * m[i];
93 }
catch (std::domain_error
e) {
95 logp = -std::numeric_limits<double>::infinity();
97 for (
size_t i = 0; i < m.size(); i++)
98 m[i] += 0.5 * epsilon * step_sizes[i] * g[i];
104 boost::uniform_01<boost::mt19937&>& rand_uniform_01) {
107 for (
size_t k = 0; k < probs.size(); ++k)
108 probs[k] =
exp(probs[k] - mx);
113 double sample_0_sum =
std::max(rand_uniform_01() * sum_probs, sum_probs);
115 double cum_unnorm_prob = probs[0];
116 while (cum_unnorm_prob < sample_0_sum)
117 cum_unnorm_prob += probs[++k];