Stan  1.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
adaptive_sampler.hpp
Go to the documentation of this file.
1 #ifndef __STAN__MCMC__ADAPTIVE_SAMPLER_HPP__
2 #define __STAN__MCMC__ADAPTIVE_SAMPLER_HPP__
3 
4 #include <stan/mcmc/sampler.hpp>
5 #include <iostream>
6 
7 namespace stan {
8 
9  namespace mcmc {
10 
20 
21  protected:
22  bool _adapt;
23  unsigned int _n_steps;
25  unsigned int _nfevals;
26  double _mean_stat;
27  std::ostream* _error_msgs;
28  std::ostream* _output_msgs;
29 
30  public:
31 
40  adaptive_sampler(bool adapt,
41  std::ostream* error_msgs = 0,
42  std::ostream* output_msgs = 0)
43  : _adapt(adapt),
44  _n_steps(0),
45  _n_adapt_steps(0),
46  _nfevals(0),
47  _mean_stat(0),
48  _error_msgs(error_msgs),
49  _output_msgs(output_msgs) {
50  }
51 
55  virtual ~adaptive_sampler() {
56  }
57 
64  void set_error_stream(std::ostream& error_msgs) {
65  _error_msgs = &error_msgs;
66  }
67 
73  _error_msgs = 0;
74  }
75 
76 
83  void set_output_stream(std::ostream& output_msgs) {
84  _output_msgs = &output_msgs;
85  }
86 
92  _output_msgs = 0;
93  }
94 
95 
106  virtual void set_params(const std::vector<double>& x,
107  const std::vector<int>& z) = 0;
108 
118  ++_n_steps;
119  if (adapting())
120  ++_n_adapt_steps;
121  return next_impl();
122  }
123 
131  virtual sample next_impl() = 0;
132 
140  virtual void find_reasonable_parameters() {
141  };
142 
151  virtual void get_parameters(std::vector<double>& params) {
152  params.resize(0);
153  }
154 
164  inline double mean_stat() {
165  return _mean_stat;
166  }
167 
173  inline void set_mean_stat(double v) {
174  _mean_stat = v;
175  }
176 
186  inline void update_mean_stat(double avg_eta,
187  double adapt_stat) {
188  _mean_stat = avg_eta * adapt_stat + (1 - avg_eta) * _mean_stat;
189  }
190 
201  inline unsigned int nfevals() {
202  return _nfevals;
203  }
204 
211  inline void nfevals_plus_eq(int n) {
212  _nfevals += n;
213  }
214 
220  inline int n_steps() {
221  return _n_steps;
222  }
223 
229  inline int n_adapt_steps() {
230  return _n_adapt_steps;
231  }
232 
236  virtual void adapt_on() {
237  _adapt = true;
238  }
239 
243  virtual void adapt_off() {
244  _adapt = false;
245  }
246 
252  inline bool adapting() {
253  return _adapt;
254  }
255 
271  virtual void write_sampler_params(std::ostream& o) {
272  }
273 
283  virtual void write_adaptation_params(std::ostream& o) {
284  }
285 
301  virtual void write_sampler_param_names(std::ostream& o) {
302  }
303 
310  virtual void get_sampler_param_names(std::vector<std::string>& names) {
311  }
312 
321  virtual void get_sampler_params(std::vector<double>& values) {
322  }
323 
324  };
325 
326  }
327 
328 }
329 
330 #endif

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