rxmc.param_sampling.Sampler#
- class rxmc.param_sampling.Sampler(params: list[Parameter], prior, starting_location: ndarray, sampling_algorithm, args: tuple = None, kwargs: dict = None)[source]#
Bases:
objectBase class wrapping a sampling algorithm with chain recording.
- 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.
sampling_algorithm (callable) – Function implementing the sampling algorithm. Must have the signature
f(x0, bounds, n_steps, log_posterior, rng, *args, **kwargs)and return(chain, logp_chain, n_accepted).args (tuple, optional) – Extra positional arguments passed to sampling_algorithm.
kwargs (dict, optional) – Extra keyword arguments passed to sampling_algorithm.
- __init__(params: list[Parameter], prior, starting_location: ndarray, sampling_algorithm, args: tuple = None, kwargs: dict = None)[source]#
Methods
__init__(params, prior, starting_location, ...)Acceptance fraction for each completed batch.
Acceptance fraction of the most recent batch.
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 sampling algorithm for one batch.
- record_batch(n_steps: int, n_accepted: int, chain: ndarray, logp_chain: ndarray)[source]#
Append a completed batch to the running chain.
- Parameters:
n_steps (int) – Number of steps in the batch.
n_accepted (int) – Number of accepted proposals in the batch.
chain (np.ndarray, shape (n_steps, ndim)) – Sampled parameter vectors.
logp_chain (np.ndarray, shape (n_steps,)) – Log posterior values for the batch.
- sample(n_steps: int, starting_location: ndarray, rng: Generator, log_posterior: Callable[[ndarray], float], burn: bool = False)[source]#
Run the sampling algorithm for one batch.
Updates
self.stateto the last sample; records the batch unless burn isTrue.- Parameters:
n_steps (int) – Number of steps to run.
starting_location (np.ndarray, shape (ndim,)) – Starting parameter vector for this batch.
rng (np.random.Generator) – Random number generator.
log_posterior (callable) – Function
f(x) -> floatreturning the log posterior atx.burn (bool, optional) – If
True, discard samples (burn-in); onlyself.stateis updated. Defaults toFalse.
- most_recent_batch_acceptance_fraction() float[source]#
Acceptance fraction of the most recent batch.
- Returns:
float – Fraction of proposals accepted in the last batch, or
0.0if no batches have been run.