Stan  2.14.0
probability, sampling & optimization
covar_adaptation.hpp
Go to the documentation of this file.
1 #ifndef STAN_MCMC_COVAR_ADAPTATION_HPP
2 #define STAN_MCMC_COVAR_ADAPTATION_HPP
3 
4 #include <stan/math/prim/mat.hpp>
6 #include <vector>
7 
8 namespace stan {
9 
10  namespace mcmc {
11 
13  public:
14  explicit covar_adaptation(int n)
15  : windowed_adaptation("covariance"), estimator_(n) {}
16 
17  bool learn_covariance(Eigen::MatrixXd& covar, const Eigen::VectorXd& q) {
18  if (adaptation_window())
19  estimator_.add_sample(q);
20 
21  if (end_adaptation_window()) {
23 
24  estimator_.sample_covariance(covar);
25 
26  double n = static_cast<double>(estimator_.num_samples());
27  covar = (n / (n + 5.0)) * covar
28  + 1e-3 * (5.0 / (n + 5.0))
29  * Eigen::MatrixXd::Identity(covar.rows(), covar.cols());
30 
31  estimator_.restart();
32 
34  return true;
35  }
36 
38  return false;
39  }
40 
41  protected:
42  stan::math::welford_covar_estimator estimator_;
43  };
44 
45  } // mcmc
46 
47 } // stan
48 
49 #endif
Probability, optimization and sampling library.
stan::math::welford_covar_estimator estimator_
bool learn_covariance(Eigen::MatrixXd &covar, const Eigen::VectorXd &q)

     [ Stan Home Page ] © 2011–2016, Stan Development Team.