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]¶ Convenience method to set/get default options.
- Returns
Default sampler options.
- Return type
default_options
-
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.
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]¶ - 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
.
-
initialize
(problem: pypesto.problem.Problem, x0: Union[numpy.ndarray, List[numpy.ndarray]]) → None[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.
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]¶ This function can be called by parallel tempering samplers during initialization to allow the inner samplers to adjust to them being used as inner samplers. Default: Do nothing.
- Parameters
temper_lpost – Whether to temperate the posterior or only the likelihood.
-
abstract
-
class
pypesto.sample.
McmcPtResult
(trace_x: numpy.ndarray, trace_neglogpost: numpy.ndarray, trace_neglogprior: numpy.ndarray, betas: Iterable[float], burn_in: Optional[int] = None, time: float = 0.0, auto_correlation: Optional[float] = None, effective_sample_size: Optional[float] = None, message: Optional[str] = None)[source]¶ Bases:
dict
The result of a sampler run using Markov-chain Monte Carlo, and optionally parallel tempering.
Can be used like a dict.
- Parameters
trace_x ([n_chain, n_iter, n_par]) – Parameters.
trace_neglogpost ([n_chain, n_iter]) – Negative log posterior values.
trace_neglogprior ([n_chain, n_iter]) – Negative log prior values.
betas ([n_chain]) – The associated inverse temperatures.
burn_in ([n_chain]) – The burn in index.
time ([n_chain]) – The computation time.
auto_correlation ([n_chain]) – The estimated chain autcorrelation.
effective_sample_size ([n_chain]) – The estimated effective sample size.
message (str) – Textual comment on the profile result.
Here –
denotes the number of chains (n_chain) –
the number of (n_iter) –
(i.e. (iterations) –
chain length) (the) –
n_par the number of parameters. (and) –
-
__init__
(trace_x: numpy.ndarray, trace_neglogpost: numpy.ndarray, trace_neglogprior: numpy.ndarray, betas: Iterable[float], burn_in: Optional[int] = None, time: float = 0.0, auto_correlation: Optional[float] = None, effective_sample_size: Optional[float] = None, message: Optional[str] = None)[source]¶ Initialize self. See help(type(self)) for accurate signature.
-
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]¶ Convenience method to set/get default options.
- Returns
Default sampler options.
- Return type
default_options
-
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
-
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.
-
make_internal
(temper_lpost: bool)[source]¶ This function can be called by parallel tempering samplers during initialization to allow the inner samplers to adjust to them being used as inner samplers. Default: Do nothing.
- Parameters
temper_lpost – Whether to temperate the posterior or only the likelihood.
-
-
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]¶ Convenience method to set/get default options.
- Returns
Default sampler options.
- Return type
default_options
-
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.
-
-
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.
-
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]¶ Convenience method to 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) → float[source]¶ Calculates 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, 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) → 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, zscore: float = 2.0) → int[source]¶ Calculates 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] = None) → pypesto.result.Result[source]¶ This is the main function to 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.
- Returns
A result with filled in sample_options part.
- Return type
result