rxmc.param_sampling.BatchedAdaptiveMetropolisSampler#
- class rxmc.param_sampling.BatchedAdaptiveMetropolisSampler(params: list[Parameter], prior, starting_location: ndarray, initial_proposal_cov: ndarray, epsilon_fraction: float = 1e-06)[source]#
Bases:
SamplerMetropolis sampler that updates the proposal covariance after each batch.
After every completed batch — burn-in batches included — the proposal covariance is replaced by the empirical covariance of that batch, scaled by
2.38² / ndim. Burn-in only affects whether the samples are recorded, so the proposal adapts during burn-in as is standard.- 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.
initial_proposal_cov (np.ndarray, shape (ndim, ndim)) – Initial proposal covariance matrix.
epsilon_fraction (float, optional) – Small regularisation fraction added to the empirical covariance diagonal. Defaults to
1e-6.
- __init__(params: list[Parameter], prior, starting_location: ndarray, initial_proposal_cov: ndarray, 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 sampler for one batch, then adapt the proposal.
- sample(n_steps: int, starting_location: ndarray, rng: Generator, log_posterior: Callable[[ndarray], float], burn: bool = False)[source]#
Run the sampler for one batch, then adapt the proposal.
Overrides
Sampler.sample()to replace the proposal covariance with the current batch’s empirical covariance after every batch, burn-in or not.proposalandproposal_covalways reflect the proposal the next batch will use.- 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 the samples (they are not recorded); the covariance update still happens. Defaults toFalse.