Stan  2.14.0
probability, sampling & optimization
var_adaptation.hpp
Go to the documentation of this file.
1 #ifndef STAN_MCMC_VAR_ADAPTATION_HPP
2 #define STAN_MCMC_VAR_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 var_adaptation(int n)
15  : windowed_adaptation("variance"), estimator_(n) {}
16 
17  bool learn_variance(Eigen::VectorXd& var, const Eigen::VectorXd& q) {
18  if (adaptation_window())
19  estimator_.add_sample(q);
20 
21  if (end_adaptation_window()) {
23 
24  estimator_.sample_variance(var);
25 
26  double n = static_cast<double>(estimator_.num_samples());
27  var = (n / (n + 5.0)) * var
28  + 1e-3 * (5.0 / (n + 5.0)) * Eigen::VectorXd::Ones(var.size());
29 
30  estimator_.restart();
31 
33  return true;
34  }
35 
37  return false;
38  }
39 
40  protected:
41  stan::math::welford_var_estimator estimator_;
42  };
43 
44  } // mcmc
45 
46 } // stan
47 #endif
Probability, optimization and sampling library.
stan::math::welford_var_estimator estimator_
bool learn_variance(Eigen::VectorXd &var, const Eigen::VectorXd &q)

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