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: object

Base 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, ...)

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 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.state to the last sample; records the batch unless burn is True.

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) -> float returning the log posterior at x.

  • burn (bool, optional) – If True, discard samples (burn-in); only self.state is updated. Defaults to False.

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.0 if no batches have been run.

batch_acceptance_fractions() ndarray[source]#

Acceptance fraction for each completed batch.

Returns:

np.ndarray – Per-batch acceptance fractions, or [0.0] if no batches run.

overall_acceptance_fraction() float[source]#

Overall acceptance fraction across all completed batches.

Returns:

float – Total accepted / total proposed, or 0.0 if no batches run.