Sampler#
- class pocomc.Sampler(prior: callable, likelihood: callable, n_dim: int = None, n_ess: int = 1000, n_active: int = 250, likelihood_args: list = None, likelihood_kwargs: dict = None, vectorize: bool = False, pool=None, flow=None, train_config: dict = None, precondition: bool = True, n_prior: int = None, sample: str = None, max_steps: int = None, patience: int = None, ess_threshold: int = None, output_dir: str = None, output_label: str = None, random_state: int = None)#
Preconditioned Monte Carlo class.
- Parameters:
prior (callable) – Class implementing the prior distribution.
likelihood (callable) – Function returning the log likelihood of a set of parameters.
n_dim (int) – The total number of parameters/dimensions (Optional as it can be infered from the prior class).
n_ess (int) – The effective sample size maintained during the run (default is
n_ess=1000
).n_active (int) – The number of active particles (default is
n_active=250
). It must be smaller thann_ess
.likelihood_args (list) – Extra arguments to be passed to likelihood (default is
likelihood_args=None
).likelihood_kwargs (dict) – Extra arguments to be passed to likelihood (default is
likelihood_kwargs=None
).vectorize (bool) – If True, vectorize
likelihood
calculation (default isvectorize=False
).pool (pool) – Provided
MPI
ormultiprocessing
pool for parallelisation (default ispool=None
).flow (
torch.nn.Module
orNone
) – Normalizing flow (default isNone
). The default is a Masked Autoregressive Flow (MAF) with 6 blocks of 3x64 layers and residual connections.train_config (dict or
None
) – Configuration for training the normalizing flow (default istrain_config=None
). Options include a dictionary with the following keys:"validation_split"
,"epochs"
,"batch_size"
,"patience"
,"learning_rate"
,"annealing"
,"gaussian_scale"
,"laplace_scale"
,"noise"
,"shuffle"
,"clip_grad_norm"
,"verbose"
.precondition (bool) – If True, use preconditioned MCMC (default is
precondition=True
). If False, use standard MCMC without normalizing flow.n_prior (int) – Number of prior samples to draw (default is
n_prior=2*(n_ess//n_active)*n_active
).sample (
str
) – Type of MCMC sampler to use (default issample="pcn"
). Options are"pcn"
(Preconditioned Crank-Nicolson) or"rwm"
(Random-Walk Metropolis).max_steps (int) – Maximum number of MCMC steps (default is
max_steps=5*n_dim
).patience (int) – Number of steps for early stopping of MCMC (default is
patience=None
). Ifpatience=None
, MCMC terminates automatically.ess_threshold (int) – Effective sample size threshold for resampling (default is
ess_threshold=4*n_dim
).output_dir (
str
orNone
) – Output directory for storing the state files of the sampler. Default isNone
which creates astates
directory. Output files can be used to resume a run.output_label (
str
orNone
) – Label used in state files. Defaullt isNone
which corresponds to"pmc"
. The saved states are named as"{output_dir}/{output_label}_{i}.state"
wherei
is the iteration index. Output files can be used to resume a run.random_state (int or
None
) – Initial random seed.
- evidence()#
Return the log evidence estimate and error.
- load_state(path: str | Path)#
Load state of sampler from file.
- Parameters:
path (
Union[str, Path]
) – Path from which to load state.
- posterior(resample=False, trim_importance_weights=True, return_logw=False, ess_trim=0.99, bins_trim=1000)#
Return posterior samples.
- Parameters:
resample (bool) – If True, resample particles (default is
resample=False
).trim_importance_weights (bool) – If True, trim importance weights (default is
trim_importance_weights=True
).return_logw (bool) – If True, return log importance weights (default is
return_logw=False
).ess_trim (float) – Effective sample size threshold for trimming (default is
ess_trim=0.99
).bins_trim (int) – Number of bins for trimming (default is
bins_trim=1_000
).
- Returns:
samples (
np.ndarray
) – Samples from the posterior.weights (
np.ndarray
) – Importance weights.logl (
np.ndarray
) – Log likelihoods.logp (
np.ndarray
) – Log priors.
- property results#
Return results.
- Returns:
results – Dictionary containing the results.
- Return type:
dict
- run(n_total: int = 5000, n_evidence: int = 5000, progress: bool = True, resume_state_path: str | Path = None, save_every: int = None)#
Run Preconditioned Monte Carlo.
- Parameters:
n_total (int) – The total number of effectively independent samples to be collected (default is
n_total=5000
).n_evidence (int) – The number of importance samples used to estimate the evidence (default is
n_evidence=5000
). Ifn_evidence=0
, the evidence is not estimated using importance sampling and the SMC estimate is used instead. Ifpreconditioned=False
, the evidence is estimated using SMC andn_evidence
is ignored.progress (bool) – If True, print progress bar (default is
progress=True
).resume_state_path (
Union[str, Path]
) – Path of state file used to resume a run. Default isNone
in which case the sampler does not load any previously saved states. An example of using this option to resume or continue a run is e.g.resume_state_path = "states/pmc_1.state"
.save_every (
int
orNone
) – Argument which determines how often (i.e. every how many iterations)pocoMC
saves state files to theoutput_dir
directory. Default isNone
in which case no state files are stored during the run.
- save_state(path: str | Path)#
Save current state of sampler to file.
- Parameters:
path (
Union[str, Path]
) – Path to save state.