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