![]() |
Stan
1.0
probability, sampling & optimization
|
An abstract base class for adaptive samplers. More...
#include <adaptive_sampler.hpp>
Public Member Functions | |
| adaptive_sampler (bool adapt, std::ostream *error_msgs=0, std::ostream *output_msgs=0) | |
| Constructs an adaptive sampler with specified adaptation status. More... | |
| virtual | ~adaptive_sampler () |
| Destructor. More... | |
| void | set_error_stream (std::ostream &error_msgs) |
| Set the stream into which errors will be written as the sampler runs. More... | |
| void | unset_error_stream () |
| Unset the stream into which errors are written to 0 so that error messages are ignored. More... | |
| void | set_output_stream (std::ostream &output_msgs) |
| Set the stream into which output will be written as the sampler runs. More... | |
| void | unset_output_stream () |
| Unset the stream into which errors are written to 0 so that output messages are ignored. More... | |
| 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. More... | |
| sample | next () |
| Returns the next sample from this sampler. More... | |
| virtual sample | next_impl ()=0 |
| Returns the next sample from this sampler. More... | |
| virtual void | find_reasonable_parameters () |
| Find a reasonable initial setting for the adaptable parameters. More... | |
| virtual void | get_parameters (std::vector< double > ¶ms) |
| Sets the specified parameter vector to the sequence of tunable parameters for this sampler. More... | |
| double | mean_stat () |
| Returns the value of the statistic we are trying to coerce. More... | |
| void | set_mean_stat (double v) |
| Sets the mean statistic to the specified value. More... | |
| void | update_mean_stat (double avg_eta, double adapt_stat) |
| Updates the mean statistic given the specified adaptation statistic and weighting. More... | |
| unsigned int | nfevals () |
| Returns the number of times that the (possibly unnormalized) log probability function has been evaluated by this sampler. More... | |
| void | nfevals_plus_eq (int n) |
| Add the specified number of evaluations to the number of function evaluations. More... | |
| int | n_steps () |
| Return the number of iterations for this sampler. More... | |
| int | n_adapt_steps () |
| Return how many iterations parameter adaptation has happened for. More... | |
| virtual void | adapt_on () |
| Turn on parameter adaptation. More... | |
| virtual void | adapt_off () |
| Turn off parameter adaption. More... | |
| bool | adapting () |
| Return whether or not parameter adaptation is on. More... | |
| virtual void | write_sampler_params (std::ostream &o) |
| Write out any sampler-specific parameters for output. More... | |
| virtual void | write_adaptation_params (std::ostream &o) |
| Use this method to write the adaptation parameters into the output. More... | |
| virtual void | write_sampler_param_names (std::ostream &o) |
| Write out any sampler-specific parameter names for output. More... | |
| virtual void | get_sampler_param_names (std::vector< std::string > &names) |
| Get any sampler-specific parameter namess. More... | |
| virtual void | get_sampler_params (std::vector< double > &values) |
| Get any sampler-specific parameters. More... | |
Protected Attributes | |
| bool | _adapt |
| unsigned int | _n_steps |
| int | _n_adapt_steps |
| unsigned int | _nfevals |
| double | _mean_stat |
| std::ostream * | _error_msgs |
| std::ostream * | _output_msgs |
An abstract base class for adaptive samplers.
The adaptive_sampler abstract base class maintains the adaptation status, number of steps, as well as recording function evals and the mean statistic being optimized.
Definition at line 19 of file adaptive_sampler.hpp.
|
inline |
Constructs an adaptive sampler with specified adaptation status.
| adapt | Initial adaptation status. |
| error_msgs | Pointer to output stream for error messages. |
Definition at line 40 of file adaptive_sampler.hpp.
|
inlinevirtual |
Destructor.
Definition at line 55 of file adaptive_sampler.hpp.
|
inlinevirtual |
Turn off parameter adaption.
Reimplemented in stan::mcmc::hmc_base< BaseRNG >, stan::mcmc::hmc_base< boost::mt19937 >, and stan::mcmc::adaptive_cdhmc.
Definition at line 243 of file adaptive_sampler.hpp.
|
inlinevirtual |
Turn on parameter adaptation.
Definition at line 236 of file adaptive_sampler.hpp.
|
inline |
Return whether or not parameter adaptation is on.
Definition at line 252 of file adaptive_sampler.hpp.
|
inlinevirtual |
Find a reasonable initial setting for the adaptable parameters.
May not be applicable/implemented for all samplers.
The default implementation is a no-op.
Reimplemented in stan::mcmc::hmc_base< BaseRNG >, stan::mcmc::hmc_base< boost::mt19937 >, and stan::mcmc::adaptive_cdhmc.
Definition at line 140 of file adaptive_sampler.hpp.
|
inlinevirtual |
Sets the specified parameter vector to the sequence of tunable parameters for this sampler.
The default implementation sets the specified vector to empty.
| [out] | params | Where to store the returned parameters. |
Reimplemented in stan::mcmc::hmc_base< BaseRNG >, stan::mcmc::hmc_base< boost::mt19937 >, and stan::mcmc::adaptive_cdhmc.
Definition at line 151 of file adaptive_sampler.hpp.
|
inlinevirtual |
Get any sampler-specific parameter namess.
| [out] | names | Output vector to which param names are written. |
Reimplemented in stan::mcmc::nuts_diag< BaseRNG >, stan::mcmc::nuts< BaseRNG >, and stan::mcmc::adaptive_hmc< BaseRNG >.
Definition at line 310 of file adaptive_sampler.hpp.
|
inlinevirtual |
Get any sampler-specific parameters.
| [out] | values | Output vector to which params are written. All values are casted to type double. This function should match get_sampler_param_names. |
Reimplemented in stan::mcmc::nuts_diag< BaseRNG >, stan::mcmc::nuts< BaseRNG >, and stan::mcmc::adaptive_hmc< BaseRNG >.
Definition at line 321 of file adaptive_sampler.hpp.
|
inline |
Returns the value of the statistic we are trying to coerce.
For example, if we are trying to set the average acceptance probability of HMC to 0.651 then this method will return the realized acceptance probability averaged across all samples so far.
Definition at line 164 of file adaptive_sampler.hpp.
|
inline |
Return how many iterations parameter adaptation has happened for.
Definition at line 229 of file adaptive_sampler.hpp.
|
inline |
Return the number of iterations for this sampler.
Definition at line 220 of file adaptive_sampler.hpp.
|
inline |
Returns the next sample from this sampler.
This method increments the count of steps with and without adaptation and calls the virtual next_impl() to produce a result.
Definition at line 117 of file adaptive_sampler.hpp.
|
pure virtual |
Returns the next sample from this sampler.
This pure virtual method must be implemented by concrete subclasses to produce the next sample.
Implemented in stan::mcmc::nuts_diag< BaseRNG >, stan::mcmc::nuts< BaseRNG >, stan::mcmc::hmc, stan::mcmc::adaptive_hmc< BaseRNG >, and stan::mcmc::adaptive_cdhmc.
|
inline |
Returns the number of times that the (possibly unnormalized) log probability function has been evaluated by this sampler.
This is a useful alternative to wall time in evaluating the relative performance of different algorithms. However, it is up to the sampler implementation to be sure to actually keep track of this.
Definition at line 201 of file adaptive_sampler.hpp.
|
inline |
Add the specified number of evaluations to the number of function evaluations.
| n | Number of evaluations to add. |
Definition at line 211 of file adaptive_sampler.hpp.
|
inline |
Set the stream into which errors will be written as the sampler runs.
| error_msgs | Stream to which error messages are written. |
Definition at line 64 of file adaptive_sampler.hpp.
|
inline |
Sets the mean statistic to the specified value.
| v | New value of mean statistic. |
Definition at line 173 of file adaptive_sampler.hpp.
|
inline |
Set the stream into which output will be written as the sampler runs.
| error_msgs | Stream to which output messages are written. |
Definition at line 83 of file adaptive_sampler.hpp.
|
pure virtual |
Set the model real and integer parameters to the specified values.
This method will typically be used to set the parameters by the client of this class after initialization.
| x | Real parameters. |
| z | Integer parameters. |
Implemented in stan::mcmc::hmc_base< BaseRNG >, stan::mcmc::hmc_base< boost::mt19937 >, stan::mcmc::hmc, and stan::mcmc::adaptive_cdhmc.
|
inline |
Unset the stream into which errors are written to 0 so that error messages are ignored.
Definition at line 72 of file adaptive_sampler.hpp.
|
inline |
Unset the stream into which errors are written to 0 so that output messages are ignored.
Definition at line 91 of file adaptive_sampler.hpp.
|
inline |
Updates the mean statistic given the specified adaptation statistic and weighting.
The behavior is equivalent to doing
mean_stat += avg_eta * adapt_stat + (1 - avg_eta) * mean_stat.
| avg_eta | Weight of adaptation statistic. |
| adapt_stat | Adaptation statistic. |
Definition at line 186 of file adaptive_sampler.hpp.
|
inlinevirtual |
Use this method to write the adaptation parameters into the output.
These should be written as one or more comment lines, each starting with a pound (#) character.
| o | Output stream to which adaptation information is written. |
Reimplemented in stan::mcmc::nuts_diag< BaseRNG >.
Definition at line 283 of file adaptive_sampler.hpp.
|
inlinevirtual |
Write out any sampler-specific parameter names for output.
This method must match write_sampler_params() in terms of number of parameters written.
Params should be writte starting with a comma, then the first parameter, then a comma, then the second parameter, ending on the final parameter.
The base class implementation is a no-op.
| [out] | o | Output stream to which param names are written. |
Reimplemented in stan::mcmc::nuts_diag< BaseRNG >, stan::mcmc::nuts< BaseRNG >, and stan::mcmc::adaptive_hmc< BaseRNG >.
Definition at line 301 of file adaptive_sampler.hpp.
|
inlinevirtual |
Write out any sampler-specific parameters for output.
This method must match write_sampler_param_names() in terms of number of parameters written.
Params should be writte starting with a comma, then the first parameter, then a comma, then the second parameter, ending on the final parameter.
The base class implementation is a no-op.
| [out] | o | Output stream to which params are written. |
Reimplemented in stan::mcmc::nuts_diag< BaseRNG >, stan::mcmc::nuts< BaseRNG >, and stan::mcmc::adaptive_hmc< BaseRNG >.
Definition at line 271 of file adaptive_sampler.hpp.
|
protected |
Definition at line 22 of file adaptive_sampler.hpp.
|
protected |
Definition at line 27 of file adaptive_sampler.hpp.
|
protected |
Definition at line 26 of file adaptive_sampler.hpp.
|
protected |
Definition at line 24 of file adaptive_sampler.hpp.
|
protected |
Definition at line 23 of file adaptive_sampler.hpp.
|
protected |
Definition at line 25 of file adaptive_sampler.hpp.
|
protected |
Definition at line 28 of file adaptive_sampler.hpp.