Sampling¶
sampling
¶
Main Sampler that handles the algorithm, output and storage of samples.
Classes:
| Name | Description |
|---|---|
SamplerRunSettings |
Settings for a sampler run |
Sampler |
Main sampler that runs the MCMC loop |
ls_mcmc.sampling.SamplerRunSettings
dataclass
¶
Settings for running the MCMC sampler.
Attributes:
| Name | Type | Description |
|---|---|---|
num_samples |
int
|
Number of samples to generate. |
initial_state |
np.ndarray
|
Initial state for the sampler. |
print_interval |
int
|
Interval at which outputs should be printed or saved to a file. |
store_interval |
int
|
Interval at which sample values should be stored. |
ls_mcmc.sampling._SamplerCheckpoint
dataclass
¶
Checkpoint data for resuming MCMC sampling.
Attributes:
| Name | Type | Description |
|---|---|---|
iteration |
int
|
Current iteration number. |
current_state |
np.ndarray[tuple[int], np.dtype[np.floating]]
|
Current state of the Markov chain. |
rng_state |
dict
|
State of the random number generator. |
run_settings |
SamplerRunSettings
|
Original run settings. |
outputs_state |
Iterable[output.MCMCOutput]
|
Serialized state of output objects. |
ls_mcmc.sampling.Sampler
¶
MCMC sampler that runs a given algorithm and manages outputs, logging, and storage.
Methods:
| Name | Description |
|---|---|
resume_from_checkpoint |
Resume sampling from a saved checkpoint |
run |
Run the main MCMC loop |
__init__
¶
__init__(algorithm: algorithms.MCMCAlgorithm, sample_storage: storage.MCMCStorage = None, outputs: Iterable[output.MCMCOutput] | None = None, logger: logging.MCMCLogger | None = None, seed: int = 0) -> None
Initializes the Sampler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
algorithm
|
algorithms.MCMCAlgorithm
|
The MCMC algorithm to use. |
required |
sample_storage
|
storage.MCMCStorage
|
Storage for samples (e.g. disk). |
None
|
outputs
|
Iterable[output.MCMCOutput]
|
Outputs to compute during sampling for logging. |
None
|
logger
|
logging.MCMCLogger
|
Logger for progress and diagnostics. |
None
|
seed
|
int
|
Random seed for reproducibility. |
0
|
resume_from_checkpoint
classmethod
¶
resume_from_checkpoint(algorithm: algorithms.MCMCAlgorithm, sample_storage: storage.MCMCStorage, logger: logging.MCMCLogger, checkpoint_path: Path | None = None) -> Sampler
Resume sampling from a saved checkpoint.
run
¶
run(run_settings: SamplerRunSettings, iteration: int = 0) -> tuple[storage.MCMCStorage, Iterable[output.MCMCOutput]]
Run the MCMC sampler for the specified settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run_settings
|
SamplerRunSettings
|
Settings for the sampler run. |
required |
iteration
|
int
|
The iteration number at which the sampler starts. Only relevant for restarting a chain. |
0
|
Returns:
| Type | Description |
|---|---|
tuple[storage.MCMCStorage, Iterable[output.MCMCOutput]]
|
tuple[storage.MCMCStorage, Iterable[output.MCMCOutput]]: The sample storage and the outputs after sampling. |
_run_utilities
¶
_run_utilities(it: int, state: np.ndarray[tuple[int], np.dtype[np.floating]], accepted: bool) -> None
Update outputs, store samples, and log progress for the current iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
it
|
int
|
Current iteration number. |
required |
state
|
np.ndarray
|
Current state of the chain. |
required |
accepted
|
bool
|
Whether the proposed state was accepted. |
required |