Sampling¶
Draw samples from the distribution, with support for various samplers.
-
class
pypesto.sample.
AdaptiveMetropolisSampler
(options: Dict = None)¶ Bases:
pypesto.sample.metropolis.MetropolisSampler
Metropolis-Hastings sampler with adaptive proposal covariance.
-
__init__
(options: Dict = None)¶ Initialize self. See help(type(self)) for accurate signature.
-
classmethod
default_options
()¶ Convenience method to set/get default options.
- Returns
Default sampler options.
- Return type
default_options
-
initialize
(problem: pypesto.problem.Problem, x0: numpy.ndarray)¶ 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: Sequence[float] = None, n_chains: int = None, options: Dict = None)¶ Bases:
pypesto.sample.parallel_tempering.ParallelTemperingSampler
Parallel tempering sampler with adaptive temperature adaptation.
-
adjust_betas
(i_sample: int, swapped: Sequence[bool])¶ Update temperatures as in Vousden2016.
-
classmethod
default_options
() → Dict¶ Convenience method to set/get default options.
- Returns
Default sampler options.
- Return type
default_options
-
-
class
pypesto.sample.
InternalSampler
(options: Dict = None)¶ 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¶ 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)¶ 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
set_last_sample
(sample: pypesto.sample.sampler.InternalSample)¶ Set the last sample in the chain to the passed value.
- Parameters
sample – The sample that will replace the last sample in the chain.
-
abstract
-
class
pypesto.sample.
McmcPtResult
(trace_x: numpy.ndarray, trace_neglogpost: numpy.ndarray, trace_neglogprior: numpy.ndarray, betas: Iterable[float], burn_in: int = None, time: float = 0.0, auto_correlation: float = None, effective_sample_size: float = None, message: str = None)¶ 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: int = None, time: float = 0.0, auto_correlation: float = None, effective_sample_size: float = None, message: str = None)¶ Initialize self. See help(type(self)) for accurate signature.
-
class
pypesto.sample.
MetropolisSampler
(options: Dict = None)¶ Bases:
pypesto.sample.sampler.InternalSampler
Simple Metropolis-Hastings sampler with fixed proposal variance.
-
__init__
(options: Dict = None)¶ Initialize self. See help(type(self)) for accurate signature.
-
classmethod
default_options
()¶ Convenience method to set/get default options.
- Returns
Default sampler options.
- Return type
default_options
-
get_last_sample
() → pypesto.sample.sampler.InternalSample¶ 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.sample.result.McmcPtResult¶ Get the generated samples.
-
initialize
(problem: pypesto.problem.Problem, x0: numpy.ndarray)¶ 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)¶ 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.
-
sample
(n_samples: int, beta: float = 1.0)¶ Perform sampling.
- Parameters
n_samples – Number of samples to generate.
beta – Inverse of the temperature to which the system is elevated.
-
set_last_sample
(sample: pypesto.sample.sampler.InternalSample)¶ 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: Sequence[float] = None, n_chains: int = None, options: Dict = None)¶ Bases:
pypesto.sample.sampler.Sampler
Simple parallel tempering sampler.
-
__init__
(internal_sampler: pypesto.sample.sampler.InternalSampler, betas: Sequence[float] = None, n_chains: int = None, options: Dict = None)¶ Initialize self. See help(type(self)) for accurate signature.
-
adjust_betas
(i_sample: int, swapped: Sequence[bool])¶ Adjust temperature values. Default: Do nothing.
-
classmethod
default_options
() → Dict¶ Convenience method to set/get default options.
- Returns
Default sampler options.
- Return type
default_options
-
get_samples
() → pypesto.sample.result.McmcPtResult¶ Concatenate all chains.
-
initialize
(problem: pypesto.problem.Problem, x0: Union[numpy.ndarray, List[numpy.ndarray]])¶ 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)¶ Perform sampling.
- Parameters
n_samples – Number of samples to generate.
beta – Inverse of the temperature to which the system is elevated.
-
swap_samples
() → Sequence[bool]¶ Swap samples as in Vousden2016.
-
-
class
pypesto.sample.
Pymc3Sampler
(step_function=None, **kwargs)¶ 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)¶ Initialize self. See help(type(self)) for accurate signature.
-
get_samples
() → pypesto.sample.result.McmcPtResult¶ Get the generated samples.
-
initialize
(problem: pypesto.problem.Problem, x0: numpy.ndarray)¶ 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)¶ 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)¶ Convenience method to translate options and fill in defaults.
- Parameters
options – Options configuring the sampler.
-
class
pypesto.sample.
Sampler
(options: Dict = None)¶ 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: Dict = None)¶ Initialize self. See help(type(self)) for accurate signature.
-
classmethod
default_options
() → Dict¶ Convenience method to set/get default options.
- Returns
Default sampler options.
- Return type
default_options
-
abstract
get_samples
() → pypesto.sample.result.McmcPtResult¶ Get the generated samples.
-
abstract
initialize
(problem: pypesto.problem.Problem, x0: Union[numpy.ndarray, List[numpy.ndarray]])¶ 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)¶ 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)¶ Convenience method to translate options and fill in defaults.
- Parameters
options – Options configuring the sampler.
-
-
pypesto.sample.
auto_correlation
(result: pypesto.result.Result) → float¶ 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.
effective_sample_size
(result: pypesto.result.Result) → float¶ 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¶ Calculates the burn-in of MCMC chains.
- Parameters
result – The pyPESTO result object with filled sample result.
zscore – The Geweke test threshold. Default 2.
- 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: pypesto.sample.sampler.Sampler = None, x0: Union[numpy.ndarray, List[numpy.ndarray]] = None, result: pypesto.result.Result = None) → pypesto.result.Result¶ 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