Skip to content

Commit b16435a

Browse files
committed
Reverted tail logic/warning handling
1 parent 8850e14 commit b16435a

1 file changed

Lines changed: 24 additions & 14 deletions

File tree

R/psis.R

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -212,24 +212,34 @@ do_psis_i <- function(log_ratios_i, tail_len_i, ...) {
212212
S <- length(log_ratios_i)
213213
# shift log ratios for safer exponentation
214214
lw_i <- log_ratios_i - max(log_ratios_i)
215+
khat <- Inf
216+
smooth_tail <- TRUE
215217

216-
if (length(unique(utils::tail(sort(log_ratios_i), tail_len_i))) == 1) {
217-
warning(
218-
"Can't fit generalized Pareto distribution ",
219-
"because all tail values are the same.",
220-
call. = FALSE
221-
)
218+
if (
219+
enough_tail_samples(tail_len_i)
220+
) {
221+
lw_tail <- utils::tail(sort.int(lw_i), tail_len_i)
222+
if (abs(max(lw_tail) - min(lw_tail)) < .Machine$double.eps / 100) {
223+
warning(
224+
"Can't fit generalized Pareto distribution ",
225+
"because all tail values are the same.",
226+
call. = FALSE
227+
)
228+
smooth_tail <- FALSE
229+
}
222230
}
223231

224-
smoothed <- suppressWarnings(posterior::ps_tail(
225-
x = lw_i,
226-
ndraws_tail = tail_len_i,
227-
tail = "right",
228-
are_log_weights = TRUE
229-
))
232+
if (smooth_tail) {
233+
smoothed <- suppressWarnings(posterior::ps_tail(
234+
x = lw_i,
235+
ndraws_tail = tail_len_i,
236+
tail = "right",
237+
are_log_weights = TRUE
238+
))
230239

231-
lw_i <- smoothed$x
232-
khat <- smoothed$k
240+
lw_i <- smoothed$x
241+
khat <- smoothed$k
242+
}
233243

234244
# truncate at max of raw wts (i.e., 0 since max has been subtracted)
235245
lw_i[lw_i > 0] <- 0

0 commit comments

Comments
 (0)