1 #ifndef __STAN__MCMC__UTIL_HPP__
2 #define __STAN__MCMC__UTIL_HPP__
8 #include <boost/random/uniform_01.hpp>
9 #include <boost/random/mersenne_twister.hpp>
10 #include <boost/exception/diagnostic_information.hpp>
11 #include <boost/exception_ptr.hpp>
21 const std::domain_error&
e) {
22 if (!error_msgs)
return;
23 *error_msgs << std::endl
24 <<
"Informational Message: The parameter state is about to be Metropolis"
25 <<
" rejected due to the following underlying, non-fatal (really)"
26 <<
" issue (and please ignore that what comes next might say 'error'): "
29 <<
"If the problem persists across multiple draws, you might have"
30 <<
" a problem with an initial state or a gradient somewhere."
32 <<
" If the problem does not persist, the resulting samples will still"
33 <<
" be drawn from the posterior."
60 std::vector<double>& x, std::vector<double>& m,
61 std::vector<double>& g,
double epsilon,
62 std::ostream* error_msgs = 0,
63 std::ostream* output_msgs = 0) {
69 }
catch (std::domain_error
e) {
71 logp = -std::numeric_limits<double>::infinity();
82 const std::vector<double>& step_sizes,
83 std::vector<double>& x, std::vector<double>& m,
84 std::vector<double>& g,
double epsilon,
85 std::ostream* error_msgs = 0,
86 std::ostream* output_msgs = 0) {
87 for (
size_t i = 0; i < m.size(); i++)
88 m[i] += 0.5 * epsilon * step_sizes[i] * g[i];
89 for (
size_t i = 0; i < x.size(); i++)
90 x[i] += epsilon * step_sizes[i] * m[i];
94 }
catch (std::domain_error
e) {
96 logp = -std::numeric_limits<double>::infinity();
98 for (
size_t i = 0; i < m.size(); i++)
99 m[i] += 0.5 * epsilon * step_sizes[i] * g[i];
106 const Eigen::MatrixXd& _cov_L,
107 std::vector<double>& x, std::vector<double>& m,
108 std::vector<double>& g,
double epsilon,
109 std::ostream* error_msgs = 0,
110 std::ostream* output_msgs = 0) {
111 Eigen::Map<Eigen::VectorXd> x_mat(&x[0],x.size());
112 Eigen::Map<Eigen::VectorXd> m_mat(&m[0],m.size());
113 Eigen::Map<Eigen::VectorXd> g_mat(&g[0],g.size());
114 m_mat += (0.5 *
epsilon) * (_cov_L.transpose().triangularView<Eigen::Upper>() * g_mat);
115 x_mat += epsilon * (_cov_L.triangularView<Eigen::Lower>() * m_mat);
119 }
catch (std::domain_error
e) {
121 logp = -std::numeric_limits<double>::infinity();
123 m_mat += (0.5 *
epsilon) * (_cov_L.transpose().triangularView<Eigen::Upper>() * g_mat);
129 Eigen::MatrixXd& cov_L)
131 std::fstream cov_stream(cov_file.c_str());
132 for(
int i = 0; i < cov_L.rows(); i++){
133 for(
int j=0; j< cov_L.cols(); j++){
134 cov_stream >> cov_L(i,j);
140 cov_L = cov_L.selfadjointView<Eigen::Upper>().llt().matrixL();
148 boost::uniform_01<boost::mt19937&>& rand_uniform_01) {
151 for (
size_t k = 0; k < probs.size(); ++k)
152 probs[k] =
exp(probs[k] - mx);
157 double sample_0_sum =
std::max(rand_uniform_01() * sum_probs, sum_probs);
159 double cum_unnorm_prob = probs[0];
160 while (cum_unnorm_prob < sample_0_sum)
161 cum_unnorm_prob += probs[++k];