rxmc.adaptive_metropolis.adaptive_metropolis#
- rxmc.adaptive_metropolis.adaptive_metropolis(x0: ndarray, bounds: ndarray, n_steps: int, log_posterior: Callable[[ndarray], float], rng: Generator, adapt_start: int = 1000, window_size: int = 1000, epsilon_fraction: float = 1e-06, previous_chain: ndarray = None) Tuple[ndarray, ndarray, int][source]#
Adaptive Metropolis algorithm with sliding-window covariance adaptation.
Before adapt_start steps the proposal is a diagonal Gaussian scaled to 1 % of
|x0|. After adapt_start steps the proposal covariance is estimated from the last window_size samples and scaled by2.38² / ndim(the Gelman–Roberts–Gilks optimal scale).- Parameters:
x0 (np.ndarray, shape (ndim,)) – Initial parameter vector.
bounds (np.ndarray, shape (ndim, 2)) – Parameter bounds; each row is
[lower, upper]. Proposals outside these bounds are rejected outright.n_steps (int) – Number of MCMC steps to generate.
log_posterior (callable) – Function
f(x) -> floatreturning the log posterior atx.rng (np.random.Generator) – Random number generator for reproducibility.
adapt_start (int, optional) – Step index at which covariance adaptation begins. Ignored when previous_chain is supplied. Defaults to
1000.window_size (int, optional) – Number of past samples used for covariance estimation. Defaults to
1000.epsilon_fraction (float, optional) – Fraction of the mean diagonal element added to the covariance for numerical stability. Defaults to
1e-6.previous_chain (np.ndarray, shape (m, ndim), optional) – Chain from a prior run to continue from. When provided the new samples are appended and adaptation uses all available history.
- Returns:
chain (np.ndarray, shape (n_steps, ndim)) – Newly generated samples (does not include previous_chain).
logp_chain (np.ndarray, shape (n_steps,)) – Log posterior values for the new samples.
accepted (int) – Number of accepted proposals in this run.