-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpsis.py
351 lines (292 loc) · 9.75 KB
/
psis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
"""Pareto smoothed importance sampling (PSIS)
This module implements Pareto smoothed importance sampling (PSIS) and PSIS
leave-one-out 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 paramaters 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 (2016). Practical
Bayesian model evaluation using leave-one-out cross-validation
and WAIC. Statistics and Computing, doi:10.1007/s11222-016-9696-4.
Aki Vehtari, Andrew Gelman and Jonah Gabry (2016). Pareto
smoothed importance sampling. arXiv preprint arXiv:1507.02646v4.
"""
# Copyright (c) 2015 Aki Vehtari, Tuomas Sivula
# Original Matlab version by Aki Vehtari. Translation to Python
# by Tuomas Sivula.
# This software is distributed under the GNU General Public
# License (version 3 or later); please refer to the file
# License.txt, included with the software, for details.
from __future__ import division # For Python 2 compatibility
import numpy as np
def 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`. If tail index k>0.5, variance of the raw estimate does
not exist and if tail index k>1 the mean of the raw estimate does not exist
and the PSIS estimate is likely to have large variation and some bias.
Parameters
----------
log_lik : ndarray
Array of size n x m containing n posterior samples of the log likelihood
terms p(y_i|\theta^s).
Additional keyword arguments are passed to the psislw() function (see the
corresponding documentation).
Returns
-------
loo : scalar
sum of the leave-one-out log predictive densities
loos : ndarray
individual leave-one-out log predictive density terms
ks : ndarray
estimated Pareto tail indeces
"""
# ensure overwrite flag in passed arguments
kwargs['overwrite_lw'] = True
# log raw weights from log_lik
lw = -log_lik
# compute Pareto smoothed log weights given raw log weights
lw, ks = psislw(lw, **kwargs)
# compute
lw += log_lik
loos = sumlogs(lw, axis=0)
loo = loos.sum()
return loo, loos, ks
def psislw(lw, wcpp=20, wtrunc=3/4, overwrite_lw=False):
"""Pareto smoothed importance sampling (PSIS).
Parameters
----------
lw : ndarray
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.
wcpp : number
Percentage of samples used for GPD fit estimate (default is 20).
wtrunc : float
Positive parameter for truncating very large weights to n^wtrunc.
Providing False or 0 disables truncation. Default values is 3/4.
overwrite_lw : bool, optional
If True, the input array `lw` is smoothed in-place. By default, a new
array is allocated.
Returns
-------
lw_out : ndarray
smoothed log weights
kss : ndarray
Pareto tail indices
"""
if lw.ndim == 2:
n, m = lw.shape
elif lw.ndim == 1:
n = len(lw)
m = 1
else:
raise ValueError("Argument `lw` must be 1 or 2 dimensional.")
if n <= 1:
raise ValueError("More than one log-weight needed.")
if overwrite_lw:
# in-place operation
lw_out = lw
else:
# allocate new array for output
lw_out = np.copy(lw, order='K')
# allocate output array for kss
kss = np.empty(m)
# precalculate constants
cutoffmin = np.log(np.finfo(float).tiny)
logn = np.log(n)
# loop over sets of log weights
for i, x in enumerate(lw_out.T if lw_out.ndim == 2 else lw_out[None,:]):
# improve numerical accuracy
x -= np.max(x)
# divide log weights into body and right tail
xcutoff = max(
np.percentile(x, 100 - wcpp),
cutoffmin
)
expxcutoff = np.exp(xcutoff)
tailinds, = np.where(x > xcutoff)
x2 = x[tailinds]
n2 = len(x2)
if n2 <= 4:
# not enough tail samples for gpdfitnew
k = np.inf
else:
# order of tail samples
x2si = np.argsort(x2)
# fit generalized Pareto distribution to the right tail samples
np.exp(x2, out=x2)
x2 -= expxcutoff
k, sigma = gpdfitnew(x2, sort=x2si)
# compute ordered statistic for the fit
sti = np.arange(0.5, n2)
sti /= n2
qq = gpinv(sti, k, sigma)
qq += expxcutoff
np.log(qq, out=qq)
# place the smoothed tail into the output array
x[tailinds[x2si]] = qq
if wtrunc > 0:
# truncate too large weights
lwtrunc = wtrunc * logn - logn + sumlogs(x)
x[x > lwtrunc] = lwtrunc
# renormalize weights
x -= sumlogs(x)
# store tail index k
kss[i] = k
# If the provided input array is one dimensional, return kss as scalar.
if lw_out.ndim == 1:
kss = kss[0]
return lw_out, kss
def gpdfitnew(x, sort=True, sort_in_place=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
----------
x : ndarray
One dimensional data array
sort : bool 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_place : bool, optional
If `sort` is True and `sort_in_place` is True, the array is sorted
in-place (False by default).
Returns
-------
k, sigma : float
estimated parameter values
Notes
-----
This function returns a negative of Zhang and Stephens's k, because it is
more common parameterisation.
"""
if x.ndim != 1 or len(x) <= 1:
raise ValueError("Invalid input array.")
# check if x should be sorted
if sort is True:
if sort_in_place:
x.sort()
xsorted = True
else:
sort = np.argsort(x)
xsorted = False
elif sort is False:
xsorted = True
else:
xsorted = False
n = len(x)
m = 80 + int(np.sqrt(n))
bs = np.arange(1, m + 1, dtype=float)
bs -= 0.5
np.divide(m, bs, out=bs)
np.sqrt(bs, out=bs)
np.subtract(1, bs, out=bs)
if xsorted:
bs /= 3 * x[int(n/4 + 0.5) - 1]
bs += 1 / x[-1]
else:
bs /= 3 * x[sort[int(n/4 + 0.5) - 1]]
bs += 1 / x[sort[-1]]
ks = np.negative(bs)
temp = ks[:,None] * x
np.log1p(temp, out=temp)
np.mean(temp, axis=1, out=ks)
L = bs / ks
np.negative(L, out=L)
np.log(L, out=L)
L -= ks
L -= 1
L *= n
temp = L - L[:,None]
np.exp(temp, out=temp)
w = np.sum(temp, axis=1)
np.divide(1, w, out=w)
# remove negligible weights
dii = w >= 10 * np.finfo(float).eps
if not np.all(dii):
w = w[dii]
bs = bs[dii]
# normalise w
w /= w.sum()
# posterior mean for b
b = np.sum(bs * w)
# Estimate for k, note that we return a negative of Zhang and
# Stephens's k, because it is more common parameterisation.
temp = (-b) * x
np.log1p(temp, out=temp)
k = np.mean(temp)
# estimate for sigma
sigma = -k / b
return k, sigma
def gpinv(p, k, sigma):
"""Inverse Generalised Pareto distribution function."""
x = np.empty(p.shape)
x.fill(np.nan)
if sigma <= 0:
return x
ok = (p > 0) & (p < 1)
if np.all(ok):
if np.abs(k) < np.finfo(float).eps:
np.negative(p, out=x)
np.log1p(x, out=x)
np.negative(x, out=x)
else:
np.negative(p, out=x)
np.log1p(x, out=x)
x *= -k
np.expm1(x, out=x)
x /= k
x *= sigma
else:
if np.abs(k) < np.finfo(float).eps:
# x[ok] = - np.log1p(-p[ok])
temp = p[ok]
np.negative(temp, out=temp)
np.log1p(temp, out=temp)
np.negative(temp, out=temp)
x[ok] = temp
else:
# x[ok] = np.expm1(-k * np.log1p(-p[ok])) / k
temp = p[ok]
np.negative(temp, out=temp)
np.log1p(temp, out=temp)
temp *= -k
np.expm1(temp, out=temp)
temp /= k
x[ok] = temp
x *= sigma
x[p == 0] = 0
if k >= 0:
x[p == 1] = np.inf
else:
x[p == 1] = -sigma / k
return x
def 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.
"""
maxx = x.max(axis=axis, keepdims=True)
xnorm = x - maxx
np.exp(xnorm, out=xnorm)
out = np.sum(xnorm, axis=axis, out=out)
if isinstance(out, np.ndarray):
np.log(out, out=out)
else:
out = np.log(out)
out += np.squeeze(maxx)
return out