Stan  1.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
dualaverage.hpp
Go to the documentation of this file.
1 #ifndef __STAN__MCMC__DUALAVERAGE_H__
2 #define __STAN__MCMC__DUALAVERAGE_H__
3 
4 #include <cmath>
5 #include <cstddef>
6 
7 #include <vector>
8 
9 #include <iostream>
10 
11 namespace stan {
12 
13  namespace mcmc {
14 
28  class DualAverage {
29  protected:
30  std::vector<double> _gbar, _xbar, _x0, _lastx;
31  int _k;
32  double _gamma;
33 
34  public:
42  DualAverage(double gamma, const std::vector<double>& x0)
43  : _gbar(x0.size(), 0), _xbar(x0.size(), 0), _x0(x0), _lastx(x0),
44  _k(0), _gamma(gamma){
45  }
46 
53  void update(const std::vector<double>& g, std::vector<double>& xk) {
54  _k++;
55  xk.resize(_gbar.size());
56  double avgeta = 1.0 / (_k + 10);
57  double xbar_avgeta = pow(_k, -0.75);
58  double muk = 0.5 * sqrt(_k) / _gamma;
59  for (size_t i = 0; i < _gbar.size(); ++i) {
60  _gbar[i] = avgeta * g[i] + (1 - avgeta) * _gbar[i];
61  xk[i] = _x0[i] - muk * _gbar[i];
62  // fprintf(stderr, "DUALAVERAGE update %d: g = %f, gbar = %f, lastx = %f",
63  // _k, g[0], _gbar[0], _lastx[0]);
64  _lastx[i] = xk[i];
65  _xbar[i] = xbar_avgeta * xk[i] + (1 - xbar_avgeta) * _xbar[i];
66  }
67  // fprintf(stderr, ", xk = %f\n", xk[0]);
68  }
69 
75  inline void setx0(const std::vector<double>& x0) {
76  _x0.assign(x0.begin(), x0.end());
77  }
78 
86  inline void xbar(std::vector<double>& xbar) {
87  xbar.assign(_xbar.begin(), _xbar.end());
88  }
94  inline void gbar(std::vector<double>& gbar) {
95  gbar.assign(_gbar.begin(), _gbar.end());
96  }
102  inline void xk(std::vector<double>& xk) {
103  xk.assign(_lastx.begin(), _lastx.end());
104  }
111  inline void x0(std::vector<double>& x0) {
112  x0.assign(_x0.begin(), _x0.end());
113  }
119  inline int k() { return _k; }
125  inline double gamma() { return _gamma; }
126  };
127 
128  }
129 }
130 
131 #endif

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