Stan  2.10.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/fun/Eigen.hpp>
6 #include <stan/math/prim/mat/fun/welford_covar_estimator.hpp>
7 #include <vector>
8 
9 namespace stan {
10 
11  namespace mcmc {
12 
14  public:
15  explicit covar_adaptation(int n)
16  : windowed_adaptation("covariance"), estimator_(n) {}
17 
18  bool learn_covariance(Eigen::MatrixXd& covar, const Eigen::VectorXd& q) {
19  if (adaptation_window())
20  estimator_.add_sample(q);
21 
22  if (end_adaptation_window()) {
24 
25  estimator_.sample_covariance(covar);
26 
27  double n = static_cast<double>(estimator_.num_samples());
28  covar = (n / (n + 5.0)) * covar
29  + 1e-3 * (5.0 / (n + 5.0))
30  * Eigen::MatrixXd::Identity(covar.rows(), covar.cols());
31 
32  estimator_.restart();
33 
35  return true;
36  }
37 
39  return false;
40  }
41 
42  protected:
43  stan::math::welford_covar_estimator estimator_;
44  };
45 
46  } // mcmc
47 
48 } // stan
49 
50 #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.