Samplers

Base Classes

class desilike.samplers.base.StaticSampler(likelihood, rng=None, directory=None)[source]

Class defining common functions used by static samplers.

Initialize the sampler.

Parameters:
  • likelihood (BaseLikelihood) – Likelihood to sample.

  • rng (numpy.random.Generator, int or None, optional) – Random number generator. Default is None.

  • directory (str, Path, or None, optional) – Save samples to this folder. Default is None.

abstract get_samples(**kwargs)[source]

Abstract method to get the samples to be evaluated.

Parameters:

**kwargs – Extra keyword arguments.

Returns:

Samples in parameter space to evaluate.

Return type:

numpy.ndarray of shape (n_samples, n_dim)

run(**kwargs)[source]

Run the sampler.

Parameters:

**kwargs – Keyword arguments passed to the get_samples method.

Returns:

samples – Posterior samples.

Return type:

desilike.Samples

class desilike.samplers.base.PopulationSampler(likelihood, rng=None, directory=None)[source]

Class defining common functions used by population samplers.

Initialize the sampler.

Parameters:
  • likelihood (BaseLikelihood) – Likelihood to sample.

  • rng (numpy.random.Generator, int or None, optional) – Random number generator. Default is None.

  • directory (str, Path, or None, optional) – Save samples to this folder. Default is None.

run(**kwargs)[source]

Run the sampler.

Parameters:

**kwargs – Keyword arguments passed to the run function of the sampler.

Returns:

samples – Posterior samples.

Return type:

desilike.Samples

class desilike.samplers.base.MarkovChainSampler(likelihood, n_chains=1, chains=None, rng=None, directory=None)[source]

Class defining common functions used by Markov chain samplers.

Initialize the sampler.

Parameters:
  • likelihood (BaseLikelihood) – Likelihood to sample.

  • n_chains (int, optional) – Number of independent chains. Default is 1.

  • chains (list of desilike.samples.Chain or None, optional) – If given on rank 0, continue the chains. In that case, we will ignore what was read from disk. Default is None.

  • rng (numpy.random.Generator, int, or None, optional) – Random number generator. Default is None.

  • directory (str, Path, or None, optional) – Save samples to this location. Default is None.

Raises:

ValueError – If burn_in is a float and larger than unity.

run(burn_in=0.2, min_steps=0, max_steps=None, adaptation_steps=None, check_every=300, checks_passed=2, gelman_rubin=1.1, ess=None, concatenate=True, save_every=300, max_init_attempts=100)[source]

Run the sampler.

Parameters:
  • burn_in (float or int, optional) – Fraction of samples to remove from each chain. If an integer, number of iterations(steps) to remove. Default is 0.2.

  • min_steps (int, optional) – Minimum number of steps to run. Default is 0.

  • max_steps (int or None, optional) – Maximum number of steps to run. If None, no limit is applied. Default is None.

  • adaptation_steps (int, optional) – Number of learning steps for samplers that can learn effective hyperparameters online. These samplers include Metropolis-Hastings MCMC, HMC, NUTS, and MCLMC. If None, use the sampler-specific default value. Default is None.

  • check_every (int, optional) – After how many steps convergence is checked. Default is 300.

  • checks_passed (int, optional) – Threshold for the number of successive successful convergence checks. If fulfilled (and the minimum number of iterations is reached), the sampling will stop. Default is 2.

  • gelman_rubin (float or None) – Used to asses convergence. If given, the maximum value of the Gelman-Rubin statistic. Default is 1.1.

  • ess (float or None) – Used to asses convergence. If given, the minimum effective sample size per chain. The effective sample size is the number of chain elements divided by the autocorrelation time. Default is None.

  • concatenate (bool, optional) – Whether to concatenate individual chains into one chain. Default is True.

  • save_every (int, optional) – After how many steps results are saved. Default is 300.

  • max_init_attempts (int, optional) – Maximum number of attempts to initialize each chain. Default is 100.

Returns:

samples – Posterior chains.

Return type:

desilike.Samples or list of desilike.Samples

Static Samplers

class desilike.samplers.GridSampler(likelihood, rng=None, directory=None)[source]

A simple grid sampler.

Initialize the sampler.

Parameters:
  • likelihood (BaseLikelihood) – Likelihood to sample.

  • rng (numpy.random.Generator, int or None, optional) – Random number generator. Default is None.

  • directory (str, Path, or None, optional) – Save samples to this folder. Default is None.

get_samples(grid=11)[source]

Get samples on the grid.

Parameters:

grid (dict, int, or numpy.ndarray, optional) – A dictionary giving either the grid size or the grid itself. If providing a number, the parameter is evenly within the parameter limits. If only a single value is provided instead of a dictionary, it is applied to all parameters. Default is 11.

Returns:

Grid to be evaluated.

Return type:

numpy.ndarray of shape (n_samples, n_dim)

class desilike.samplers.ImportanceSampler(likelihood, rng=None, directory=None)[source]

An importance sampler.

This class can be used to transform samples from one posterior to another. Alternatively, it can also be used to combine likelihoods from two experiments.

Initialize the sampler.

Parameters:
  • likelihood (BaseLikelihood) – Likelihood to sample.

  • rng (numpy.random.Generator, int or None, optional) – Random number generator. Default is None.

  • directory (str, Path, or None, optional) – Save samples to this folder. Default is None.

get_samples(samples=None)[source]

Get samples on the grid.

Parameters:

chain (desilike.Samples, optional) – Input chain that defines the samples.

Returns:

Grid to be evaluated.

Return type:

numpy.ndarray of shape (n_samples, n_dim)

run(samples, resample=True)[source]

Reweight a sample using importance sampling.

Parameters:
  • samples (desilike.samples.Chain) – Input samples with a corresponding posterior.

  • resample (bool, optional) – If True, the weights for the chain will be multiplied by the ratio of the new and old posterior. Effectively, the new chain will sample the new posterior (if it previously sampled the old). If False, the weights will be multiplied by the the new likelihood. This can be useful when combining observations. Default is True.

Return type:

desilike.Samples

class desilike.samplers.QMCSampler(likelihood, rng=None, directory=None)[source]

Sampler based on Quasi Monte-Carlo (QMC) sequences.

In addition to sequences in scipy.qmc, this module also implements Kronecker sequences.

References

Initialize the sampler.

Parameters:
  • likelihood (BaseLikelihood) – Likelihood to sample.

  • rng (numpy.random.Generator, int or None, optional) – Random number generator. Default is None.

  • directory (str, Path, or None, optional) – Save samples to this folder. Default is None.

get_samples(size=1000, engine='kronecker', **kwargs)[source]

Get samples from a QMC sequence.

Parameters:
  • size (dict or int, optional) – Size of the sequence. Default is 1000.

  • engine (str, optional) – Engine to use. Choices are ‘sobol’, ‘halton’, ‘lhs’, ‘kronecker’. Default is ‘kronecker’.

  • **kwargs – Extra keyword arguments passed to the engine.

Returns:

Grid to be evaluated.

Return type:

numpy.ndarray of shape (n_samples, n_dim)

Population Samplers

class desilike.samplers.DynestySampler(likelihood, dynamic=True, rng=None, directory=None, **kwargs)[source]

Wrapper for dynesty nested samplers.

References

Initialize the dynesty sampler.

Parameters:
  • likelihood (BaseLikelihood) – Likelihood to sample.

  • dynamic (boolean, optional) – If True, use dynesty.DynamicPopulationSampler instead of dynesty.PopulationSampler. Default is True.

  • rng (numpy.random.Generator, int or None, optional) – Random number generator. Default is None.

  • directory (str, Path, optional) – Save samples to this location. Default is None.

  • **kwargs – Extra keyword arguments passed to dynesty during initialization.

Raises:

ValueError – If directory is not None but dynamic is Flalse.

class desilike.samplers.NautilusSampler(likelihood, rng=None, directory=None, **kwargs)[source]

Wrapper for nautilus importance nested sampling.

References

Initialize the nautilus sampler.

Parameters:
  • likelihood (BaseLikelihood) – Likelihood to sample.

  • rng (numpy.random.Generator, int or None, optional) – Random number generator. Default is None.

  • directory (str, Path, optional) – Save samples to this location. Default is None.

  • **kwargs – Extra keyword arguments passed to nautilus during initialization.

class desilike.samplers.PocoMCSampler(likelihood, rng=None, directory=None, **kwargs)[source]

Class for the pocoMC preconditioned Monte Carlo sampling.

References

Initialize the PocoMC sampler.

Parameters:
  • likelihood (BaseLikelihood) – Likelihood to sample.

  • rng (numpy.random.Generator, int or None, optional) – Random number generator. Default is None.

  • directory (str, Path, optional) – Save samples to this location. Default is None.

  • **kwargs – Extra keyword arguments passed to pocoMC during initialization.

Markov Chain Samplers

class desilike.samplers.EmceeSampler(likelihood, n_walkers=10, chains=None, rng=None, directory=None, **kwargs)[source]

Wrapper for the affine-invariant ensemble sampler emcee.

References

Initialize the emcee sampler.

Parameters:
  • likelihood (BaseLikelihood) – Likelihood to sample.

  • n_walkers (int, optional) – Number of walkers. Note that each walker produces a chain but different chains are not strictly independent. Default is 10.

  • chains (list of desilike.samples.Chain, optional) – If given, continue the chains. In that case, we will ignore what was read from disk. Default is None.

  • rng (numpy.random.Generator, int, or None, optional) – Random number generator. Default is None.

  • directory (str, Path, or None, optional) – Save samples to this location. Default is None.

  • **kwargs – Extra keyword arguments passed to emcee during initialization.

class desilike.samplers.HMCSampler(likelihood, n_chains=1, chains=None, step_size=0.001, inverse_mass_matrix=None, num_integration_steps=60, rng=None, directory=None, **kwargs)[source]

Wrapper for Hamiltonian Monte-Carlo (HMC).

Initialize the HMC sampler.

Parameters:
  • likelihood (BaseLikelihood) – Likelihood to sample.

  • n_chains (int, optional) – Number of independent chains. Default is 1.

  • chains (list of desilike.samples.Chain or None, optional) – If given on rank 0, continue the chains. In that case, we will ignore what was read from disk. Default is None.

  • step_size (float, optional) – Size of the integration step. Default is 1e-3.

  • inverse_mass_matrix (numpy.ndarray, optional) – The value to use for the inverse mass matrix when drawing a value for the momentum and computing the kinetic energy. If one-dimensional, a diagonal mass matrix is assumed. If None, a unity matrix is used. Default is None.

  • num_integration_steps (int, optional) – Number of times we run the symplectic integrator to build the trajectory. Default is 60.

  • rng (numpy.random.RandomState, int, or None, optional) – Random number generator for seeding. If None, no seed is used. Default is None.

  • directory (str, Path, or None, optional) – Save samples to this location. Default is None.

  • **kwargs – Extra keyword arguments passed to blackjax.hmc during initialization.

class desilike.samplers.MCLMCSampler(likelihood, n_chains=1, chains=None, L=1.0, step_size=0.1, rng=None, directory=None, **kwargs)[source]

Wrapper for the Microcanonical Langevin Monte Carlo (MCLMC) sampler.

References

Initialize the Microcanonical Langevin Monte Carlo (MCLMC) sampler.

Parameters:
  • likelihood (BaseLikelihood) – Likelihood to sample.

  • n_chains (int, optional) – Number of independent chains. Default is 1.

  • chains (list of desilike.samples.Chain or None, optional) – If given on rank 0, continue the chains. In that case, we will ignore what was read from disk. Default is None.

  • L (float, optional) – Momentum decoherence scale. Default is 1.

  • step_size (float, optional) – The value to use for the step size in the integrator. Default is 0.1.

  • rng (numpy.random.RandomState or int, optional) – Random number generator. Default is None.

  • directory (str, Path, optional) – Save samples to this location. Default is None.

  • **kwargs – Extra keyword arguments passed to blackjax.mclmc during initialization.

class desilike.samplers.MetropolisHastingsSampler(likelihood, n_chains=1, cov=None, f_fast=1, f_drag=0, fast=None, chains=None, rng=None, directory=None)[source]

A Metropolis-Hastings sampler with fast-slow decomposition.

Note that this is a from-scratch reimplementation of this algorithm.

References

Initialize the Metropolis-Hastings sampler.

Parameters:
  • likelihood (BaseLikelihood) – Likelihood to sample.

  • n_chains (int, optional) – Number of independent chains. Default is 1.

  • cov (numpy.ndarray or None, optional) – Covariance matrix estimate used to whiten parameter space. If None, the sampler will use each parameter’s proposal scale.

  • f_fast (int, optional) – Oversampling factor of fast parameters. The default is 1 which implies not oversampling.

  • f_drag (int, optional) – Factor for dragging of fast parameters. The default is 0, i.e., no dragging.

  • fast (list of str or None, optional) – List of dimensions that are considered fast. Default is None.

  • chains (list of desilike.samples.Chain, optional) – If given, continue the chains. In that case, we will ignore what was read from disk. Default is None.

  • rng (numpy.random.Generator, int, or None, optional) – Random number generator. Default is None.

  • directory (str, Path, or None, optional) – Save samples to this location. Default is None.

class desilike.samplers.NoUTurnSampler(likelihood, n_chains=1, chains=None, step_size=0.001, inverse_mass_matrix=None, rng=None, directory=None, **kwargs)[source]

Wrapper for No-U-Turn Sampler (NUTS).

References

pdf>`_

Initialize the No-U-Turn Sampler.

Parameters:
  • likelihood (BaseLikelihood) – Likelihood to sample.

  • n_chains (int, optional) – Number of independent chains. Default is 1.

  • chains (list of desilike.samples.Chain or None, optional) – If given on rank 0, continue the chains. In that case, we will ignore what was read from disk. Default is None.

  • step_size (float, optional) – Size of the integration step. Default is 1e-3.

  • inverse_mass_matrix (numpy.ndarray, optional) – The value to use for the inverse mass matrix when drawing a value for the momentum and computing the kinetic energy. If one-dimensional, a diagonal mass matrix is assumed. If None, a unity matrix is used. Default is None.

  • rng (numpy.random.RandomState, int, or None, optional) – Random number generator for seeding. If None, no seed is used. Default is None.

  • directory (str, Path, or None, optional) – Save samples to this location. Default is None.

  • **kwargs – Extra keyword arguments passed to blackjax.nuts during initialization.

class desilike.samplers.ZeusSampler(likelihood, n_walkers=10, chains=None, rng=None, directory=None, **kwargs)[source]

Wrapper for the ensemble slice sampler zeus.

References

Initialize the zeus sampler.

Parameters:
  • likelihood (BaseLikelihood) – Likelihood to sample.

  • n_walkers (int, optional) – Number of walkers. Note that each walker produces a chain but different chains are not strictly independent. Default is 10.

  • chains (list of desilike.samples.Chain, optional) – If given, continue the chains. In that case, we will ignore what was read from disk. Default is None.

  • rng (numpy.random.Generator, int, or None, optional) – Random number generator. Default is None.

  • directory (str, Path, or None, optional) – Save samples to this location. Default is None.

  • **kwargs – Extra keyword arguments passed to zeus during initialization.