1 #ifndef __STAN__MCMC__DUALAVERAGE_H__ 2 #define __STAN__MCMC__DUALAVERAGE_H__ 43 : _gbar(x0.size(), 0), _xbar(x0.size(), 0), _x0(x0), _lastx(x0),
53 void update(
const std::vector<double>& g, std::vector<double>&
xk) {
55 xk.resize(_gbar.size());
56 double avgeta = 1.0 / (_k + 10);
57 double xbar_avgeta =
pow(_k, -0.75);
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];
65 _xbar[i] = xbar_avgeta * xk[i] + (1 - xbar_avgeta) * _xbar[i];
75 inline void setx0(
const std::vector<double>&
x0) {
76 _x0.assign(x0.begin(), x0.end());
87 xbar.assign(_xbar.begin(), _xbar.end());
95 gbar.assign(_gbar.begin(), _gbar.end());
102 inline void xk(std::vector<double>&
xk) {
103 xk.assign(_lastx.begin(), _lastx.end());
111 inline void x0(std::vector<double>&
x0) {
112 x0.assign(_x0.begin(), _x0.end());
119 inline int k() {
return _k; }
void xk(std::vector< double > &xk)
Get the current iterate.
int k()
Return how many iterations we've run for.
var sqrt(const var &a)
Return the square root of the specified variable (cmath).
Probability, optimization and sampling library.
void setx0(const std::vector< double > &x0)
Set the point towards which each iterate is shrunk.
Implements Nesterov's dual average algorithm.
void x0(std::vector< double > &x0)
Get the point towards which we're shrinking the iterates.
DualAverage(double gamma, const std::vector< double > &x0)
Constructor.
void gbar(std::vector< double > &gbar)
Get the average of all previous gradients.
var pow(const var &base, const var &exponent)
Return the base raised to the power of the exponent (cmath).
std::vector< double > _x0
std::vector< double > _xbar
std::vector< double > _lastx
void update(const std::vector< double > &g, std::vector< double > &xk)
Produces the next iterate xk given the current gradient g.
std::vector< double > _gbar
double gamma()
Return the regularization parameter gamma.
void xbar(std::vector< double > &xbar)
Get the exponentially weighted moving average of all previous iterates.