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.
-
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.
-
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.
-
-
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
-
abstract
-
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.
-
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.
-
-
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.
-
initialize
(problem: pypesto.problem.Problem, x0: Union[numpy.ndarray, List[numpy.ndarray]])[source]¶ Initialize all samplers.
-
-
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.
-
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
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.
-
-
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