rxmc.param_sampling.AdaptiveMetropolisSampler#
- class rxmc.param_sampling.AdaptiveMetropolisSampler(params: list[Parameter], prior, starting_location: ndarray, adapt_start: int = 100, window_size: int = 1000, epsilon_fraction: float = 1e-06)[source]#
Bases:
SamplerMetropolis sampler that adapts the proposal covariance with a sliding window.
The proposal covariance is estimated from the last window_size samples after adapt_start steps have been collected.
- Parameters:
params (list of Parameter) – Parameters to sample.
prior (object) – Prior distribution with a callable
logpdf(x)method.starting_location (np.ndarray, shape (ndim,)) – Initial parameter vector.
adapt_start (int, optional) – Step at which adaptation begins. Defaults to
100.window_size (int, optional) – Number of past samples used for covariance estimation. Defaults to
1000.epsilon_fraction (float, optional) – Small regularisation term for the proposal covariance. Defaults to
1e-6.
- __init__(params: list[Parameter], prior, starting_location: ndarray, adapt_start: int = 100, window_size: int = 1000, epsilon_fraction: float = 1e-06)[source]#
Methods
__init__(params, prior, starting_location[, ...])batch_acceptance_fractions()Acceptance fraction for each completed batch.
most_recent_batch_acceptance_fraction()Acceptance fraction of the most recent batch.
overall_acceptance_fraction()Overall acceptance fraction across all completed batches.
record_batch(n_steps, n_accepted, chain, ...)Append a completed batch to the running chain.
sample(n_steps, starting_location, rng, ...)Run the adaptive sampler for one batch, passing history to the algorithm.
- sample(n_steps: int, starting_location: ndarray, rng: Generator, log_posterior: Callable[[ndarray], float], burn: bool = False)[source]#
Run the adaptive sampler for one batch, passing history to the algorithm.
Overrides
Sampler.sample()to forward the accumulated chain so the adaptive algorithm can estimate the covariance from all past samples.- Parameters:
n_steps (int) – Number of steps to run.
starting_location (np.ndarray, shape (ndim,)) – Starting parameter vector.
rng (np.random.Generator) – Random number generator.
log_posterior (callable) – Function
f(x) -> floatreturning the log posterior.burn (bool, optional) – If
True, discard samples. Defaults toFalse.