petitRADTRANS.retrieval.psis

Pareto smoothed importance sampling (PSIS)

This module implements Pareto smoothed importance sampling (PSIS) and PSIS leave-one-out (LOO) cross-validation for Python (Numpy).

Included functions

psisloo

Pareto smoothed importance sampling leave-one-out log predictive densities.

psislw

Pareto smoothed importance sampling.

gpdfitnew

Estimate the parameters for the Generalized Pareto Distribution (GPD).

gpinv

Inverse generalised Pareto distribution function.

sumlogs

Sum of vector where numbers are represented by their logarithms.

References

Aki Vehtari, Andrew Gelman and Jonah Gabry (2017). Practical Bayesian model evaluation using leave-one-out cross-validation and WAIC. Statistics and Computing, 27(5):1413–1432. doi:10.1007/s11222-016-9696-4. https://arxiv.org/abs/1507.04544

Aki Vehtari, Andrew Gelman and Jonah Gabry (2017). Pareto smoothed importance sampling. https://arxiv.org/abs/arXiv:1507.02646v5

3-Clause BSD License

Copyright 2017 Aki Vehtari, Tuomas Sivula

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Module Contents

Functions

psisloo(log_lik, **kwargs)

PSIS leave-one-out log predictive densities.

psislw(lw[, r_eff, overwrite_lw])

Pareto smoothed importance sampling (PSIS).

gpdfitnew(x[, sort, sort_in_place, return_quadrature])

Estimate the paramaters for the Generalized Pareto Distribution (GPD)

gpinv(p, k, sigma)

Inverse Generalised Pareto distribution function.

sumlogs(x[, axis, out])

Sum of vector where numbers are represented by their logarithms.

petitRADTRANS.retrieval.psis.psisloo(log_lik, **kwargs)

PSIS leave-one-out log predictive densities.

Computes the log predictive densities given posterior samples of the log likelihood terms \(p(y_i|\theta^s)\) in input parameter log_lik. Returns a sum of the leave-one-out log predictive densities loo, individual leave-one-out log predictive density terms loos and an estimate of Pareto tail indeces ks. The estimates are unreliable if tail index k > 0.7 (see more in the references listed in the module docstring).

Additional keyword arguments are passed to the psislw() function (see the corresponding documentation).

Parameters

log_likndarray

Array of size n x m containing n posterior samples of the log likelihood terms \(p(y_i|\theta^s)\).

Returns

looscalar

sum of the leave-one-out log predictive densities

loosndarray

individual leave-one-out log predictive density terms

ksndarray

estimated Pareto tail indeces

petitRADTRANS.retrieval.psis.psislw(lw, r_eff=1.0, overwrite_lw=False)

Pareto smoothed importance sampling (PSIS).

Parameters

lwndarray

Array of size n x m containing m sets of n log weights. It is also possible to provide one dimensional array of length n.

r_effscalar, optional

relative MCMC efficiency N_eff / N

overwrite_lwbool, optional

If True, the input array lw is smoothed in-place, assuming the array is F-contiguous. By default, a new array is allocated.

Returns

lw_outndarray

smoothed log weights

kssndarray

Pareto tail indices

petitRADTRANS.retrieval.psis.gpdfitnew(x, sort=True, sort_in_place=False, return_quadrature=False)

Estimate the paramaters for the Generalized Pareto Distribution (GPD)

Returns empirical Bayes estimate for the parameters of the two-parameter generalized Parato distribution given the data.

Parameters

xndarray

One dimensional data array

sortbool or ndarray, optional

If known in advance, one can provide an array of indices that would sort the input array x. If the input array is already sorted, provide False. If True (default behaviour), the array is sorted internally.

sort_in_placebool, optional

If sort is True and sort_in_place is True, the array is sorted in-place (False by default).

return_quadraturebool, optional

If True, quadrature points and weight ks and w of the marginal posterior distribution of k are also calculated and returned. False by default.

Returns

k, sigmafloat

estimated parameter values

ks, wndarray

Quadrature points and weights of the marginal posterior distribution of k. Returned only if return_quadrature is True.

Notes

This function returns a negative of Zhang and Stephens’s k, because it is more common parameterisation.

petitRADTRANS.retrieval.psis.gpinv(p, k, sigma)

Inverse Generalised Pareto distribution function.

petitRADTRANS.retrieval.psis.sumlogs(x, axis=None, out=None)

Sum of vector where numbers are represented by their logarithms.

Calculates np.log(np.sum(np.exp(x), axis=axis)) in such a fashion that it works even when elements have large magnitude.