Stan  1.0
probability, sampling & optimization
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
An abstract base class for adaptive samplers.
void unset_output_stream()
Unset the stream into which errors are written to 0 so that output messages are ignored.
virtual void get_sampler_param_names(std::vector< std::string > &names)
Get any sampler-specific parameter namess.
virtual void write_sampler_param_names(std::ostream &o)
Write out any sampler-specific parameter names for output.
bool adapting()
Return whether or not parameter adaptation is on.
int n_adapt_steps()
Return how many iterations parameter adaptation has happened for.
int n_steps()
Return the number of iterations for this sampler.
void update_mean_stat(double avg_eta, double adapt_stat)
Updates the mean statistic given the specified adaptation statistic and weighting.
virtual void set_params(const std::vector< double > &x, const std::vector< int > &z)=0
Set the model real and integer parameters to the specified values.
virtual ~adaptive_sampler()
Destructor.
adaptive_sampler(bool adapt, std::ostream *error_msgs=0, std::ostream *output_msgs=0)
Constructs an adaptive sampler with specified adaptation status.
virtual void adapt_on()
Turn on parameter adaptation.
virtual sample next_impl()=0
Returns the next sample from this sampler.
void set_output_stream(std::ostream &output_msgs)
Set the stream into which output will be written as the sampler runs.
void nfevals_plus_eq(int n)
Add the specified number of evaluations to the number of function evaluations.
sample next()
Returns the next sample from this sampler.
void set_mean_stat(double v)
Sets the mean statistic to the specified value.
virtual void get_sampler_params(std::vector< double > &values)
Get any sampler-specific parameters.
unsigned int nfevals()
Returns the number of times that the (possibly unnormalized) log probability function has been evalua...
virtual void find_reasonable_parameters()
Find a reasonable initial setting for the adaptable parameters.
void unset_error_stream()
Unset the stream into which errors are written to 0 so that error messages are ignored.
virtual void adapt_off()
Turn off parameter adaption.
virtual void write_sampler_params(std::ostream &o)
Write out any sampler-specific parameters for output.
virtual void get_parameters(std::vector< double > &params)
Sets the specified parameter vector to the sequence of tunable parameters for this sampler.
void set_error_stream(std::ostream &error_msgs)
Set the stream into which errors will be written as the sampler runs.
double mean_stat()
Returns the value of the statistic we are trying to coerce.
virtual void write_adaptation_params(std::ostream &o)
Use this method to write the adaptation parameters into the output.
Representation of a MCMC sample.
Definition: sampler.hpp:16
Probability, optimization and sampling library.
Definition: agrad.cpp:6

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