lenstronomy.Sampling.Samplers package

Submodules

lenstronomy.Sampling.Samplers.base_nested_sampler module

class NestedSampler(likelihood_module, prior_type, prior_means, prior_sigmas, width_scale, sigma_scale)[source]

Bases: object

Base class for nested samplers.

__init__(likelihood_module, prior_type, prior_means, prior_sigmas, width_scale, sigma_scale)[source]
Parameters:
  • likelihood_module – likelihood_module like in likelihood.py (should be callable)

  • prior_type – ‘uniform’ of ‘gaussian’, for converting the unit hypercube to param cube

  • prior_means – if prior_type is ‘gaussian’, mean for each param

  • prior_sigmas – if prior_type is ‘gaussian’, std dev for each param

  • width_scale – scale the widths of the parameters space by this factor

  • sigma_scale – if prior_type is ‘gaussian’, scale the gaussian sigma by this factor

prior(u, *args)[source]

Compute the mapping between the unit cube and parameter cube.

Parameters:

u – unit hypercube, sampled by the algorithm

Returns:

hypercube in parameter space

log_likelihood(p, *args)[source]

Compute the log-likelihood given list of parameters.

Parameters:

x – parameter values

Returns:

log-likelihood (from the likelihood module)

run(kwargs_run)[source]

Run the nested sampling algorithm.

lenstronomy.Sampling.Samplers.cobaya_sampler module

class CobayaSampler(likelihood_class, mean_start, sigma_start)[source]

Bases: object

__init__(likelihood_class, mean_start, sigma_start)[source]

Wrapper for pure Metropolis–Hastings MCMC sampling with Cobaya.

If you use this sampler, you must cite the following works:

Lewis & Bridle, https://arxiv.org/abs/astro-ph/0205436

Lewis, https://arxiv.org/abs/1304.4473

Torrado & Lewis, https://arxiv.org/abs/2005.05290

For more information about Cobaya, see https://cobaya.readthedocs.io/en/latest/index.html

Parameters:
  • likelihood_class – Likelihood() instance

  • mean_start – initial point for parameters are drawn from Gaussians with these means

  • sigma_start – initial point for parameters are drawn from Gaussians with these standard deviations

run(**kwargs)[source]
Parameters:

kwargs – dictionary of keyword arguments for Cobaya. kwargs that can be passed are: ‘proposal_widths’ (standard deviation of the Gaussian from which initial point is drawn, list or dict), ‘latex’ (list of LaTeX lables for params), ‘path’ (where products will be saved, string), ‘force_overwrite’ (whether or not to overwite previous products with the same name, bool) and ‘mpi’ (to run in MPI mode, bool). Furthermore, all the cobaya-native kwargs for the mcmc sampler listed in the docs are available: https://cobaya.readthedocs.io/en/latest/sampler_mcmc.html#options-and-defaults except ‘drag’ and ‘blocking’, since there is no obvious parameter speed hierarchy in a strong lensing likelihood. If none of these kwargs are passed, the default values/settings will be used.

lenstronomy.Sampling.Samplers.dynesty_sampler module

class DynestySampler(likelihood_module, prior_type='uniform', prior_means=None, prior_sigmas=None, width_scale=1, sigma_scale=1, bound='multi', sample='auto', use_mpi=False, use_pool=None)[source]

Bases: NestedSampler

Wrapper for dynamical nested sampling algorithm Dynesty by J. Speagle.

paper : https://arxiv.org/abs/1904.02180 doc : https://dynesty.readthedocs.io/

__init__(likelihood_module, prior_type='uniform', prior_means=None, prior_sigmas=None, width_scale=1, sigma_scale=1, bound='multi', sample='auto', use_mpi=False, use_pool=None)[source]
Parameters:
  • likelihood_module – likelihood_module like in likelihood.py (should be callable)

  • prior_type – ‘uniform’ of ‘gaussian’, for converting the unit hypercube to param cube

  • prior_means – if prior_type is ‘gaussian’, mean for each param

  • prior_sigmas – if prior_type is ‘gaussian’, std dev for each param

  • width_scale – scale the widths of the parameters space by this factor

  • sigma_scale – if prior_type is ‘gaussian’, scale the gaussian sigma by this factor

  • bound – specific to Dynesty, see https://dynesty.readthedocs.io

  • sample – specific to Dynesty, see https://dynesty.readthedocs.io

  • use_mpi – Use MPI computing if True

  • use_pool – specific to Dynesty, see https://dynesty.readthedocs.io

run(kwargs_run)[source]

Run the Dynesty nested sampler.

see https://dynesty.readthedocs.io for content of kwargs_run

Parameters:

kwargs_run – kwargs directly passed to DynamicNestedSampler.run_nested

Returns:

samples, means, logZ, logZ_err, logL, results

lenstronomy.Sampling.Samplers.multinest_sampler module

class MultiNestSampler(likelihood_module, prior_type='uniform', prior_means=None, prior_sigmas=None, width_scale=1, sigma_scale=1, output_dir=None, output_basename='-', remove_output_dir=False, use_mpi=False)[source]

Bases: NestedSampler

Wrapper for nested sampling algorithm MultInest by F.

Feroz & M. Hobson papers : arXiv:0704.3704, arXiv:0809.3437, arXiv:1306.2144 pymultinest doc : https://johannesbuchner.github.io/PyMultiNest/pymultinest.html

__init__(likelihood_module, prior_type='uniform', prior_means=None, prior_sigmas=None, width_scale=1, sigma_scale=1, output_dir=None, output_basename='-', remove_output_dir=False, use_mpi=False)[source]
Parameters:
  • likelihood_module – likelihood_module like in likelihood.py (should be callable)

  • prior_type – ‘uniform’ of ‘gaussian’, for converting the unit hypercube to param cube

  • prior_means – if prior_type is ‘gaussian’, mean for each param

  • prior_sigmas – if prior_type is ‘gaussian’, std dev for each param

  • width_scale – scale the widths of the parameters space by this factor

  • sigma_scale – if prior_type is ‘gaussian’, scale the gaussian sigma by this factor

  • output_dir – name of the folder that will contain output files

  • output_basename – prefix for output files

  • remove_output_dir – remove the output_dir folder after completion

  • use_mpi – flag directly passed to MultInest sampler (NOT TESTED)

run(kwargs_run)[source]

Run the MultiNest nested sampler.

see https://johannesbuchner.github.io/PyMultiNest/pymultinest.html for content of kwargs_run

Parameters:

kwargs_run – kwargs directly passed to pymultinest.run

Returns:

samples, means, logZ, logZ_err, logL, stats

lenstronomy.Sampling.Samplers.nautilus module

class Nautilus(likelihood_module)[source]

Bases: object

__init__(likelihood_module)[source]

Importance nested sampling with Nautilus [1]

References:

param likelihood_module:

Likelihood() instance

nautilus_sampling(prior_type='uniform', mpi=False, thread_count=1, verbose=True, one_step=False, **kwargs_nautilus)[source]
Parameters:
  • prior_type – string; prior type. Currently only ‘uniform’ supported (in addition to Prior class in Likelihood module)

  • mpi – MPI option (currently not supported)

  • thread_count – integer; multi-threading option (currently not supported)

  • verbose – verbose statements of Nautilus

  • one_step – boolean, if True, only runs one iteration of filling the sampler and re-training. This is meant for test purposes of the sampler to operate with little computational effort

  • kwargs_nautilus – additional keyword arguments for Nautilus

Returns:

points, log_w, log_l, log_z

likelihood(args)[source]

Log likelihood.

Parameters:

args – ctype

Returns:

log likelihood

lenstronomy.Sampling.Samplers.nautilus_sampler module

class NautilusSampler(likelihood_module, prior_type='uniform', prior_means=None, prior_sigmas=None, width_scale=1, sigma_scale=1, mpi=False, **kwargs)[source]

Bases: NestedSampler

Wrapper for the nautilus sampler by Johannes U. Lange.

paper : https://arxiv.org/abs/2306.16923 doc : https://nautilus-sampler.readthedocs.io

__init__(likelihood_module, prior_type='uniform', prior_means=None, prior_sigmas=None, width_scale=1, sigma_scale=1, mpi=False, **kwargs)[source]
Parameters:
  • likelihood_module – likelihood_module like in likelihood.py (should be callable)

  • prior_type – ‘uniform’ of ‘gaussian’, for converting the unit hypercube to param cube

  • prior_means – if prior_type is ‘gaussian’, mean for each param

  • prior_sigmas – if prior_type is ‘gaussian’, std dev for each param

  • width_scale – scale the widths of the parameters space by this factor

  • sigma_scale – if prior_type is ‘gaussian’, scale the gaussian sigma by this factor

  • mpi – Use MPI computing if True

  • kwargs – kwargs directly passed to Sampler

run(**kwargs)[source]

Run the nautilus nested sampler.

see https://nautilus-sampler.readthedocs.io for content of kwargs

Parameters:

kwargs – kwargs directly passed to Sampler.run

Returns:

samples, means, log_Z, log_Z_err, log_L, results

lenstronomy.Sampling.Samplers.polychord_sampler module

class DyPolyChordSampler(likelihood_module, prior_type='uniform', prior_means=None, prior_sigmas=None, width_scale=1, sigma_scale=1, output_dir=None, output_basename='-', resume_dyn_run=False, polychord_settings=None, remove_output_dir=False, use_mpi=False)[source]

Bases: NestedSampler

Wrapper for dynamical nested sampling algorithm DyPolyChord by E. Higson, M. Hobson, W. Handley, A. Lasenby.

papers : arXiv:1704.03459, arXiv:1804.06406 doc : https://dypolychord.readthedocs.io

__init__(likelihood_module, prior_type='uniform', prior_means=None, prior_sigmas=None, width_scale=1, sigma_scale=1, output_dir=None, output_basename='-', resume_dyn_run=False, polychord_settings=None, remove_output_dir=False, use_mpi=False)[source]
Parameters:
  • likelihood_module – likelihood_module like in likelihood.py (should be callable)

  • prior_type – ‘uniform’ of ‘gaussian’, for converting the unit hypercube to param cube

  • prior_means – if prior_type is ‘gaussian’, mean for each param

  • prior_sigmas – if prior_type is ‘gaussian’, std dev for each param

  • width_scale – scale the widths of the parameters space by this factor

  • sigma_scale – if prior_type is ‘gaussian’, scale the gaussian sigma by this factor

  • output_dir – name of the folder that will contain output files

  • output_basename – prefix for output files

  • resume_dyn_run – if True, previous resume files will not be deleted so that previous run can be resumed

  • polychord_settings – settings dictionary to send to pypolychord. Check dypolychord documentation for details.

  • remove_output_dir – remove the output_dir folder after completion

  • use_mpi – Use MPI computing if True

run(dynamic_goal, kwargs_run)[source]

Run the DyPolyChord dynamical nested sampler.

see https://dypolychord.readthedocs.io for content of kwargs_run

Parameters:
  • dynamic_goal – 0 for evidence computation, 1 for posterior computation

  • kwargs_run – kwargs directly passed to dyPolyChord.run_dypolychord

Returns:

samples, means, logZ, logZ_err, logL, ns_run

log_likelihood(args)[source]

Compute the log-likelihood given list of parameters.

Parameters:

args – parameter values

Returns:

log-likelihood (from the likelihood module)

lenstronomy.Sampling.Samplers.pso module

Created on Sep 30, 2013 modified on March 3-7, 2020.

@authors: J. Akeret, S. Birrer, A. Shajib

class ParticleSwarmOptimizer(func, low, high, particle_count=25, pool=None, args=None, kwargs=None)[source]

Bases: object

Optimizer using a swarm of particles.

Parameters:
  • func – A function that takes a vector in the parameter space as input and returns the natural logarithm of the posterior probability for that position.

  • low – array of the lower bound of the parameter space

  • high – array of the upper bound of the parameter space

  • particle_count – the number of particles to use.

  • pool – (optional) An alternative method of using the parallelized algorithm. If provided, the value of threads is ignored and the object provided by pool is used for all parallelization. It can be any object with a map method that follows the same calling sequence as the built-in map function.

__init__(func, low, high, particle_count=25, pool=None, args=None, kwargs=None)[source]
Parameters:
  • func (python definition) – function to call to return log likelihood

  • low (numpy array) – lower bound of the parameters

  • high (numpy array) – upper bound of the parameters

  • particle_count (int) – number of particles in each iteration of the PSO

  • pool (None or MPI pool) – MPI pool for mapping different processes

  • args (list) – positional arguments to send to func. The function will be called as func(x, *args, **kwargs).

  • kwargs (dict) – keyword arguments to send to func. The function will be called as func(x, *args, **kwargs)

set_global_best(position, velocity, fitness)[source]

Set the global best particle.

Parameters:
  • position (list or ndarray) – position of the new global best

  • velocity (list or ndarray) – velocity of the new global best

  • fitness (float) – fitness of the new global best

Returns:

None

Return type:

sample(max_iter=1000, c1=1.193, c2=1.193, p=0.7, m=0.001, n=0.01, early_stop_tolerance=None, verbose=True)[source]

Launches the PSO. Yields the complete swarm per iteration.

Parameters:
  • max_iter – maximum iterations

  • c1 – cognitive weight

  • c2 – social weight

  • p – stop criterion, percentage of particles to use

  • m – stop criterion, difference between mean fitness and global best

  • n – stop criterion, difference between norm of the particle vector and norm of the global best

  • early_stop_tolerance – will terminate at the given value (should be specified as a chi^2)

  • verbose (boolean) – prints when it stopped

optimize(max_iter=1000, verbose=True, c1=1.193, c2=1.193, p=0.7, m=0.001, n=0.01, early_stop_tolerance=None)[source]

Run the optimization and return a full list of optimization outputs.

Parameters:
  • max_iter – maximum iterations

  • verbose – if True, print a message every 10 iterations

  • c1 – cognitive weight

  • c2 – social weight

  • p – stop criterion, percentage of particles to use

  • m – stop criterion, difference between mean fitness and global best

  • n – stop criterion, difference between norm of the particle vector and norm of the global best

  • early_stop_tolerance – will terminate at the given value (should be specified as a chi^2)

is_master()[source]

Check if the current processor is the master.

Returns:

Return type:

Module contents