swh:1:snp:d80eec3f654c152adbdd6e641362bcb340d39fe2
Raw File
Tip revision: b1e1ec6e7274271545927903a176788696cd0eb2 authored by Daniel Lee on 27 July 2016, 20:17:21 UTC
Merge pull request #1984 from stan-dev/release/v2.11.0
Tip revision: b1e1ec6
var_adaptation.hpp
#ifndef STAN_MCMC_VAR_ADAPTATION_HPP
#define STAN_MCMC_VAR_ADAPTATION_HPP

#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/mcmc/windowed_adaptation.hpp>
#include <stan/math/prim/mat/fun/welford_var_estimator.hpp>
#include <vector>

namespace stan {

  namespace mcmc {

    class var_adaptation: public windowed_adaptation {
    public:
      explicit var_adaptation(int n)
        : windowed_adaptation("variance"), estimator_(n) {}

      bool learn_variance(Eigen::VectorXd& var, const Eigen::VectorXd& q) {
        if (adaptation_window())
          estimator_.add_sample(q);

        if (end_adaptation_window()) {
          compute_next_window();

          estimator_.sample_variance(var);

          double n = static_cast<double>(estimator_.num_samples());
          var = (n / (n + 5.0)) * var
                + 1e-3 * (5.0 / (n + 5.0)) * Eigen::VectorXd::Ones(var.size());

          estimator_.restart();

          ++adapt_window_counter_;
          return true;
        }

        ++adapt_window_counter_;
        return false;
      }

    protected:
      stan::math::welford_var_estimator estimator_;
    };

  }  // mcmc

}  // stan
#endif
back to top