RWMH#
- class hmclab.Samplers.RWMH(seed: Optional[int] = None)[source]#
Bases:
hmclab.Samplers._AbstractSampler
- __init__(seed: Optional[int] = None) None #
Constructor that sets the random number generator for the sampler. Pass a seed to make a Markov chain reproducible (given that all settings are re-used.)
- Parameters
seed (int) – Description of parameter seed.
Methods
Constructor that sets the random number generator for the sampler.
autotune
get_diagnostics
load_results
plot_acceptance_rate
plot_stepsizes
Print Jupyter widget from _repr_html_() to stdout.
Sampling using the Metropolis-Hastings algorithm.
Attributes
A NumPy ndarray containing all past acceptance rates.
accepted_proposals
An integer representing the amount of accepted proposals.
amount_of_writes
A positive integer representing the amount of times the sampler has written to disk.
A boolean indicating if the stepsize is tuned towards a specific acceptance rate using diminishing adaptive parameters.
current_model
A NumPy array containing the model at the current state of the Markov chain.
current_proposal
An integer representing the current proposal index (before thinning) of the Markov chain.
current_proposal_after_thinning
An integer representing the current proposal index after thinning of the Markov chain.
current_x
A float containing \(\chi = -\log\left( p\\right)\) (i.e.
diagnostic_mode
Boolean describing whether functions need to be profiled for performance.
dimensions
An integer representing the dimensionality in which the MCMC sampler works.
disable_progressbar
A bool describing whether or not to disable the TQDM progressbar
distribution
The _AbstractDistribution-derived object on which the sampler works.
end_time
Time (and date) at which sampling was terminated / finished.
exchange_interval
Integer describing how many samples lie between the attempted swap of states between samplers.
exchange_schedule
A pre-communicated exchange schedule determining when and which samplers try to exchange states.
functions_to_diagnose
Array of functions to be profiled, typically contains functions only from the abstract base class.
A float tuning the rate of decrease at which a new step size is adapted, according to rate = (iteration_number) ** (-learning_rate).
main_thread_keyboard_interrupt
max_time
A float representing the maximum time in seconds that sampling is allowed to take before it is automatically terminated.
Minimal step length which is chosen if timestep becomes zero or negative during autotuning.
name
The name of the sampler
online_thinning
An integer representing the interval between stored samples.
parallel
A boolean describing if this sampler works in an array of multiple samplers.
pipe_matrix
A matrix of two-way communicators between samplers.
progressbar_refresh_rate
A float representing how many seconds lies between an update of the progress bar statistics (acceptance rate etc.).
proposals
An integer representing the amount of requested proposals for a run.
proposed_model
A NumPy array containing the model at the proposed state of the Markov chain.
proposed_x
A float containing \(\chi = -\log\left( p\\right)\) (i.e.
ram_buffer
A NumPy ndarray containing the samples that are as of yet not written to disk.
ram_buffer_size
A positive integer indicating the size of the RAM buffer in amount of samples.
rng
sampler_index
An integer describing the index of the parallel sampler in the array of samplers.
sampler_specific_functions_to_diagnose
Array of sampler specific functions to be profiled.
samples_hdf5_dataset
A string containing the HDF5 group of the hdf5 file to which samples will be stored.
samples_hdf5_filehandle
A HDF5 file handle of the hdf5 file to which samples will be stored.
samples_hdf5_filename
A string containing the path+filename of the hdf5 file to which samples will be stored.
start_time
Time (and date) at which sampling was started.
A parameter describing the standard deviation of a multivariate normal (MVN) used as the proposal distribution for Random Walk Metropolis-Hastings.
A NumPy ndarray containing all past step lengths for the RWMH algorithm.
A float representing the optimal acceptance rate used for autotuning, if autotuning is True.
times_started
An integer representing how often this sampler object has started sampling.
- stepsize: Union[float, numpy.ndarray] = 1.0#
A parameter describing the standard deviation of a multivariate normal (MVN) used as the proposal distribution for Random Walk Metropolis-Hastings. Using a _numpy.ndarray column vector (shape dimensions × 1) will give every dimensions a unique step length. Correlations in the MVN are not yet implemented. Has a strong influence on acceptance rate. An essential tuning parameter.
- autotuning: bool = False#
A boolean indicating if the stepsize is tuned towards a specific acceptance rate using diminishing adaptive parameters.
- learning_rate: float = 0.75#
A float tuning the rate of decrease at which a new step size is adapted, according to rate = (iteration_number) ** (-learning_rate). Needs to be larger than 0.5 and equal to or smaller than 1.0.
- target_acceptance_rate: float = 0.65#
A float representing the optimal acceptance rate used for autotuning, if autotuning is True.
- acceptance_rates: numpy.ndarray = None#
A NumPy ndarray containing all past acceptance rates. Collected if autotuning is True.
- stepsizes: numpy.ndarray = None#
A NumPy ndarray containing all past step lengths for the RWMH algorithm. Collected if autotuning is True.
- minimal_stepsize: float = 1e-18#
Minimal step length which is chosen if timestep becomes zero or negative during autotuning.
- sample(samples_hdf5_filename: str, distribution: hmclab.Distributions.base._AbstractDistribution, stepsize: Union[float, numpy.ndarray] = 1.0, initial_model: Optional[numpy.ndarray] = None, proposals: int = 100, online_thinning: int = 1, diagnostic_mode: bool = False, ram_buffer_size: Optional[int] = None, overwrite_existing_file: bool = False, max_time: Optional[float] = None, autotuning: bool = False, target_acceptance_rate: float = 0.65, learning_rate: float = 0.75, queue=None, disable_progressbar=False)[source]#
Sampling using the Metropolis-Hastings algorithm.
- Parameters
samples_hdf5_filename (str) – String containing the (path+)filename of the HDF5 file to contain the samples. A required parameter.
distribution (_AbstractDistribution) – The distribution to sample. Should be an instance of a subclass of _AbstractDistribution. A required parameter.
stepsize (_Union[float, _numpy.ndarray]) – A parameter describing the standard deviation of a multivariate normal (MVN) used as the proposal distribution for Random Walk Metropolis-Hastings. Using a _numpy.ndarray column vector (shape dimensions × 1) will give every dimensions a unique step length. Correlations in the MVN are not yet implemented. Has a strong influence on acceptance rate. An essential tuning parameter.
initial_model (_numpy) – A NumPy column vector (shape dimensions × 1) containing the starting model of the Markov chain. This model will not be written out as a sample.
proposals (int) – An integer representing the amount of proposals the algorithm should make.
online_thinning (int) – An integer representing the degree of online thinning, i.e. the interval between storing samples.
diagnostic_mode (bool) – A boolean describing if subroutines of sampling should be timed. Useful for finding slow parts of the algorithm. Will add overhead to each function.
ram_buffer_size (int) – An integer representing how many samples should be kept in RAM before writing to storage.
overwrite_existing_file (bool) – A boolean describing whether or not to silently overwrite existing files. Use with caution.
max_time (float) – A float representing the maximum time in seconds that sampling is allowed to take before it is automatically terminated. The value None is used for unlimited time.
autotuning (bool) – A boolean describing whether or not stepsize is automatically adjusted. Uses a diminishing adapting scheme to satisfy detailed balance.
target_acceptance_rate (float) – A float between 0.0 and 1.0 that is the target acceptance rate of autotuning. The algorithm will try to achieve this acceptance rate.
learning_rate (float) – A float larger than 0.5 but smaller than or equal to 1.0, describing how aggressively the stepsize is updated. Lower is more aggresive. Arbitrary keyword arguments.
- Raises
AssertionError – For any unspecified invalid entry.
ValueError – For any invalid value of algorithm settings.
TypeError – For any invalid value of algorithm settings.
- print_results() None #
Print Jupyter widget from _repr_html_() to stdout.