Sample

Draw samples from the distribution, with support for various samplers.

class pypesto.sample.AdaptiveMetropolisSampler(options: Optional[Dict] = None)[source]

Bases: pypesto.sample.metropolis.MetropolisSampler

Metropolis-Hastings sampler with adaptive proposal covariance.

__init__(options: Optional[Dict] = None)[source]

Initialize self. See help(type(self)) for accurate signature.

classmethod default_options()[source]

Return the default options for the sampler.

initialize(problem: pypesto.problem.Problem, x0: numpy.ndarray)[source]

Initialize the sampler.

class pypesto.sample.AdaptiveParallelTemperingSampler(internal_sampler: pypesto.sample.sampler.InternalSampler, betas: Optional[Sequence[float]] = None, n_chains: Optional[int] = None, options: Optional[Dict] = None)[source]

Bases: pypesto.sample.parallel_tempering.ParallelTemperingSampler

Parallel tempering sampler with adaptive temperature adaptation.

adjust_betas(i_sample: int, swapped: Sequence[bool])[source]

Update temperatures as in Vousden2016.

classmethod default_options()Dict[source]

Get default options for sampler.

class pypesto.sample.EmceeSampler(nwalkers: int = 1, sampler_args: Optional[dict] = None, run_args: Optional[dict] = None)[source]

Bases: pypesto.sample.sampler.Sampler

Use emcee for sampling.

Wrapper around https://emcee.readthedocs.io/en/stable/, see there for details.

__init__(nwalkers: int = 1, sampler_args: Optional[dict] = None, run_args: Optional[dict] = None)[source]

Initialize sampler.

Parameters
  • nwalkers (The number of walkers in the ensemble.) –

  • sampler_args – Further keyword arguments that are passed on to emcee.EnsembleSampler.__init__.

  • run_args – Further keyword arguments that are passed on to emcee.EnsembleSampler.run_mcmc.

get_samples()pypesto.result.sample.McmcPtResult[source]

Get the samples into the fitting pypesto format.

initialize(problem: pypesto.problem.Problem, x0: Union[numpy.ndarray, List[numpy.ndarray]])None[source]

Initialize the sampler.

sample(n_samples: int, beta: float = 1.0)None[source]

Return the most recent sample state.

class pypesto.sample.InternalSampler(options: Optional[Dict] = None)[source]

Bases: pypesto.sample.sampler.Sampler

Sampler to be used inside a parallel tempering sampler.

The last sample can be obtained via get_last_sample and set via set_last_sample.

abstract get_last_sample()pypesto.sample.sampler.InternalSample[source]

Get the last sample in the chain.

Returns

The last sample in the chain in the exchange format.

Return type

internal_sample

make_internal(temper_lpost: bool)[source]

Allow the inner samplers to be used as inner samplers.

Can be called by parallel tempering samplers during initialization. Default: Do nothing.

Parameters

temper_lpost – Whether to temperate the posterior or only the likelihood.

abstract set_last_sample(sample: pypesto.sample.sampler.InternalSample)[source]

Set the last sample in the chain to the passed value.

Parameters

sample – The sample that will replace the last sample in the chain.

class pypesto.sample.MetropolisSampler(options: Optional[Dict] = None)[source]

Bases: pypesto.sample.sampler.InternalSampler

Simple Metropolis-Hastings sampler with fixed proposal variance.

__init__(options: Optional[Dict] = None)[source]

Initialize self. See help(type(self)) for accurate signature.

classmethod default_options()[source]

Return the default options for the sampler.

get_last_sample()pypesto.sample.sampler.InternalSample[source]

Get the last sample in the chain.

Returns

The last sample in the chain in the exchange format.

Return type

internal_sample

get_samples()pypesto.result.sample.McmcPtResult[source]

Get the samples into the fitting pypesto format.

initialize(problem: pypesto.problem.Problem, x0: numpy.ndarray)[source]

Initialize the sampler.

make_internal(temper_lpost: bool)[source]

Allow the inner samplers to be used as inner samplers.

Can be called by parallel tempering samplers during initialization. Default: Do nothing.

Parameters

temper_lpost – Whether to temperate the posterior or only the likelihood.

sample(n_samples: int, beta: float = 1.0)[source]

Load last recorded particle.

set_last_sample(sample: pypesto.sample.sampler.InternalSample)[source]

Set the last sample in the chain to the passed value.

Parameters

sample – The sample that will replace the last sample in the chain.

class pypesto.sample.ParallelTemperingSampler(internal_sampler: pypesto.sample.sampler.InternalSampler, betas: Optional[Sequence[float]] = None, n_chains: Optional[int] = None, options: Optional[Dict] = None)[source]

Bases: pypesto.sample.sampler.Sampler

Simple parallel tempering sampler.

__init__(internal_sampler: pypesto.sample.sampler.InternalSampler, betas: Optional[Sequence[float]] = None, n_chains: Optional[int] = None, options: Optional[Dict] = None)[source]

Initialize self. See help(type(self)) for accurate signature.

adjust_betas(i_sample: int, swapped: Sequence[bool])[source]

Adjust temperature values. Default: Do nothing.

classmethod default_options()Dict[source]

Return the default options for the sampler.

get_samples()pypesto.result.sample.McmcPtResult[source]

Concatenate all chains.

initialize(problem: pypesto.problem.Problem, x0: Union[numpy.ndarray, List[numpy.ndarray]])[source]

Initialize all samplers.

sample(n_samples: int, beta: float = 1.0)[source]

Sample and swap in between samplers.

swap_samples()Sequence[bool][source]

Swap samples as in Vousden2016.

class pypesto.sample.Pymc3Sampler(step_function=None, **kwargs)[source]

Bases: pypesto.sample.sampler.Sampler

Wrapper around Pymc3 samplers.

Parameters
  • step_function – A pymc3 step function, e.g. NUTS, Slice. If not specified, pymc3 determines one automatically (preferable).

  • **kwargs – Options are directly passed on to pymc3.sample.

__init__(step_function=None, **kwargs)[source]

Initialize self. See help(type(self)) for accurate signature.

get_samples()pypesto.result.sample.McmcPtResult[source]

Convert result from Pymc3 to McmcPtResult.

initialize(problem: pypesto.problem.Problem, x0: numpy.ndarray)[source]

Initialize the sampler.

Parameters
  • problem – The problem for which to sample.

  • x0 – Should, but is not required to, be used as initial parameter.

sample(n_samples: int, beta: float = 1.0)[source]

Sample the problem.

Parameters
  • n_samples – Number of samples to be computed.

  • beta – Inverse temperature for the log probability function.

classmethod translate_options(options)[source]

Translate options and fill in defaults.

Parameters

options – Options configuring the sampler.

class pypesto.sample.Sampler(options: Optional[Dict] = None)[source]

Bases: abc.ABC

Sampler base class, not functional on its own.

The sampler maintains an internal chain, which is initialized in initialize, and updated in sample.

__init__(options: Optional[Dict] = None)[source]

Initialize self. See help(type(self)) for accurate signature.

classmethod default_options()Dict[source]

Set/Get default options.

Returns

Default sampler options.

Return type

default_options

abstract get_samples()pypesto.result.sample.McmcPtResult[source]

Get the generated samples.

abstract initialize(problem: pypesto.problem.Problem, x0: Union[numpy.ndarray, List[numpy.ndarray]])[source]

Initialize the sampler.

Parameters
  • problem – The problem for which to sample.

  • x0 – Should, but is not required to, be used as initial parameter.

abstract sample(n_samples: int, beta: float = 1.0)[source]

Perform sampling.

Parameters
  • n_samples – Number of samples to generate.

  • beta – Inverse of the temperature to which the system is elevated.

classmethod translate_options(options)[source]

Translate options and fill in defaults.

Parameters

options – Options configuring the sampler.

pypesto.sample.auto_correlation(result: pypesto.result.result.Result)float[source]

Calculate the autocorrelation of the MCMC chains.

Parameters

result – The pyPESTO result object with filled sample result.

Returns

Estimate of the integrated autocorrelation time of the MCMC chains.

Return type

auto_correlation

pypesto.sample.calculate_ci_mcmc_sample(result: pypesto.result.result.Result, ci_level: float = 0.95, exclude_burn_in: bool = True)Tuple[numpy.ndarray, numpy.ndarray][source]

Calculate parameter credibility intervals based on MCMC samples.

Parameters
  • result – The pyPESTO result object with filled sample result.

  • ci_level – Lower tail probability, defaults to 95% interval.

Returns

Bounds of the MCMC percentile-based confidence interval.

Return type

lb, ub

pypesto.sample.calculate_ci_mcmc_sample_prediction(simulated_values: numpy.ndarray, ci_level: float = 0.95)Tuple[numpy.ndarray, numpy.ndarray][source]

Calculate prediction credibility intervals based on MCMC samples.

Parameters
  • simulated_values – Simulated model states or model observables.

  • ci_level – Lower tail probability, defaults to 95% interval.

Returns

Bounds of the MCMC-based prediction confidence interval.

Return type

lb, ub

pypesto.sample.effective_sample_size(result: pypesto.result.result.Result)float[source]

Calculate the effective sample size of the MCMC chains.

Parameters

result – The pyPESTO result object with filled sample result.

Returns

Estimate of the effective sample size of the MCMC chains.

Return type

ess

pypesto.sample.geweke_test(result: pypesto.result.result.Result, zscore: float = 2.0)int[source]

Calculate the burn-in of MCMC chains.

Parameters
  • result – The pyPESTO result object with filled sample result.

  • zscore – The Geweke test threshold.

Returns

Iteration where the first and the last fraction of the chain do not differ significantly regarding Geweke test -> Burn-In

Return type

burn_in

pypesto.sample.sample(problem: pypesto.problem.Problem, n_samples: int, sampler: Optional[pypesto.sample.sampler.Sampler] = None, x0: Optional[Union[numpy.ndarray, List[numpy.ndarray]]] = None, result: Optional[pypesto.result.result.Result] = None, filename: Optional[str] = 'Auto')pypesto.result.result.Result[source]

Call to do parameter sampling.

Parameters
  • problem – The problem to be solved. If None is provided, a pypesto.AdaptiveMetropolisSampler is used.

  • n_samples – Number of samples to generate.

  • sampler – The sampler to perform the actual sampling.

  • x0 – Initial parameter for the Markov chain. If None, the best parameter found in optimization is used. Note that some samplers require an initial parameter, some may ignore it. x0 can also be a list, to have separate starting points for parallel tempering chains.

  • result – A result to write to. If None provided, one is created from the problem.

  • filename – Name of the hdf5 file, where the result will be saved. Default is “Auto”, in which case it will automatically generate a file named year_month_day_sampling_result.hdf5. Deactivate saving by setting filename to None.

Returns

A result with filled in sample_options part.

Return type

result