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];
The prob_grad class represents densities with fixed numbers of discrete and scalar parameters and the...
virtual double grad_log_prob(std::vector< double > ¶ms_r, std::vector< int > ¶ms_i, std::vector< double > &gradient, std::ostream *output_stream=0)=0
var exp(const var &a)
Return the exponentiation of the specified variable (cmath).
double epsilon()
Return minimum positive number representable.
T sum(const std::vector< T > &xs)
Return the sum of the values in the specified standard vector.
double e()
Return the base of the natural logarithm.
double max(std::vector< double > &x)
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
void scaled_add(std::vector< double > &x, std::vector< double > &y, double lambda)
double leapfrog(stan::model::prob_grad &model, std::vector< int > z, std::vector< double > &x, std::vector< double > &m, std::vector< double > &g, double epsilon, std::ostream *error_msgs=0, std::ostream *output_msgs=0)
Computes the log probability for a single leapfrog step in Hamiltonian Monte Carlo.
int sample_unnorm_log(std::vector< double > probs, boost::uniform_01< boost::mt19937 & > &rand_uniform_01)
void write_error_msgs(std::ostream *error_msgs, const std::domain_error &e)
double rescaled_leapfrog(stan::model::prob_grad &model, std::vector< int > z, const std::vector< double > &step_sizes, std::vector< double > &x, std::vector< double > &m, std::vector< double > &g, double epsilon, std::ostream *error_msgs=0, std::ostream *output_msgs=0)
Probability, optimization and sampling library.