Stan  1.0
probability, sampling & optimization
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
Implements Nesterov's dual average algorithm.
Definition: dualaverage.hpp:28
double gamma()
Return the regularization parameter gamma.
void x0(std::vector< double > &x0)
Get the point towards which we're shrinking the iterates.
std::vector< double > _gbar
Definition: dualaverage.hpp:30
std::vector< double > _xbar
Definition: dualaverage.hpp:30
int k()
Return how many iterations we've run for.
DualAverage(double gamma, const std::vector< double > &x0)
Constructor.
Definition: dualaverage.hpp:42
std::vector< double > _lastx
Definition: dualaverage.hpp:30
void setx0(const std::vector< double > &x0)
Set the point towards which each iterate is shrunk.
Definition: dualaverage.hpp:75
void update(const std::vector< double > &g, std::vector< double > &xk)
Produces the next iterate xk given the current gradient g.
Definition: dualaverage.hpp:53
void xbar(std::vector< double > &xbar)
Get the exponentially weighted moving average of all previous iterates.
Definition: dualaverage.hpp:86
void xk(std::vector< double > &xk)
Get the current iterate.
void gbar(std::vector< double > &gbar)
Get the average of all previous gradients.
Definition: dualaverage.hpp:94
std::vector< double > _x0
Definition: dualaverage.hpp:30
var sqrt(const var &a)
Return the square root of the specified variable (cmath).
Definition: agrad.hpp:1761
var pow(const var &base, const var &exponent)
Return the base raised to the power of the exponent (cmath).
Definition: agrad.hpp:1778
Probability, optimization and sampling library.
Definition: agrad.cpp:6

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