Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Imports:
checkmate,
matrixStats (>= 0.52),
parallel,
posterior (>= 1.7.0),
posterior (>= 1.8.0),
rlang (>= 1.0.6),
stats
Suggests:
Expand Down
44 changes: 7 additions & 37 deletions R/psis.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,18 @@ psis_apply <- function(x, item, fun = c("[[", "attr"), fun_val = numeric(1)) {
do_psis_i <- function(log_ratios_i, tail_len_i, ...) {
S <- length(log_ratios_i)
# shift log ratios for safer exponentation
lw_i <- log_ratios_i - max(log_ratios_i)
max_log_ratio <- max(log_ratios_i)
lw_i <- log_ratios_i - max_log_ratio
khat <- Inf

if (enough_tail_samples(tail_len_i)) {
# only the tail and the cutoff need sorting: a partial sort finds the
# cutoff value, and the values at or above it are usually few
cut_pos <- S - tail_len_i
# largest value not in the tail
cut_val <- sort.int(lw_i, partial = cut_pos)[cut_pos]
top <- which(lw_i >= cut_val)
top <- top[order(lw_i[top])]
top <- top[order(lw_i[top], method = "radix")]
tail_ids <- top[seq(length(top) - tail_len_i + 1, length(top))]
lw_tail <- lw_i[tail_ids]
if (abs(max(lw_tail) - min(lw_tail)) < .Machine$double.eps / 100) {
Expand All @@ -230,52 +232,20 @@ do_psis_i <- function(log_ratios_i, tail_len_i, ...) {
class = "loo_warning_pareto_tail_constant"
)
} else {
cutoff <- cut_val # largest value not in the tail
smoothed <- psis_smooth_tail(lw_tail, cutoff)
khat <- smoothed$k
smoothed <- posterior::ps_tail_sorted_log_ratios(lw_tail, cut_val)
khat <- if (is.na(smoothed$k)) Inf else smoothed$k
lw_i[tail_ids] <- smoothed$tail
}
}

# truncate at max of raw wts (i.e., 0 since max has been subtracted)
lw_i[lw_i > 0] <- 0
# shift log weights back so that the smallest log weights remain unchanged
lw_i <- lw_i + max(log_ratios_i)
lw_i <- lw_i + max_log_ratio

list(log_weights = lw_i, pareto_k = khat)
}

#' PSIS tail smoothing for a single vector
#'
#' @noRd
#' @param x Vector of tail elements already sorted in ascending order.
#' @return A named list containing:
#' * `tail`: vector same size as `x` containing the logs of the
#' order statistics of the generalized pareto distribution.
#' * `k`: scalar shape parameter estimate.
#'
psis_smooth_tail <- function(x, cutoff) {
len <- length(x)
exp_cutoff <- exp(cutoff)

# save time not sorting since x already sorted
fit <- posterior::gpdfit(exp_x_minus_exp_y(x, cutoff), sort_x = FALSE)
k <- fit$k
sigma <- fit$sigma
if (is.na(k)) {
k <- Inf
}
if (is.finite(k)) {
p <- (seq_len(len) - 0.5) / len
qq <- posterior::qgeneralized_pareto(p, 0, sigma, k) + exp_cutoff
tail <- log(qq)
} else {
tail <- x
}
list(tail = tail, k = k)
}


#' Calculate tail lengths to use for fitting the GPD
#'
#' The number of weights (i.e., tail length) used to fit the generalized Pareto
Expand Down
18 changes: 10 additions & 8 deletions tests/testthat/test_psis.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ test_that("do_psis_i throws warning if all tail values the same", {
expect_equal(val$pareto_k, Inf)
})

test_that("psis_smooth_tail returns original tail values if k is infinite", {
xx <- c(4, 1, 4, 4, 4, 4, 4, 2, 3, 4, 4)
expect_warning(
val <- do_psis_i(xx, tail_len_i = 6),
"all tail values are the same"
)
expect_equal(val$pareto_k, Inf)
})

test_that("psis handles negative infinite log ratios", {
log_ratios <- c(rep(-Inf, 90), seq(-9, 0, length.out = 10))

Expand All @@ -171,21 +180,14 @@ test_that("exp_x_minus_exp_y is stable for nearby values", {
)
})

test_that("psis_smooth_tail returns original tail values if k is infinite", {
xx <- log(c(2, 2, 2, 2, 3, 4, 5, 6))
val <- suppressWarnings(psis_smooth_tail(xx, 0))
expect_equal(val$tail, xx)
expect_equal(val$k, Inf)
})

test_that("do_psis_i selects the same tail as a full sort, also with ties", {
# reference: the tail from a full sort, as in loo 2.10.1
do_psis_i_full_sort <- function(log_ratios_i, tail_len_i) {
S <- length(log_ratios_i)
lw_i <- log_ratios_i - max(log_ratios_i)
ord <- sort.int(lw_i, index.return = TRUE)
tail_ids <- seq(S - tail_len_i + 1, S)
smoothed <- psis_smooth_tail(ord$x[tail_ids], ord$x[min(tail_ids) - 1])
smoothed <- posterior::ps_tail_sorted_log_ratios(ord$x[tail_ids], ord$x[min(tail_ids) - 1])
lw_i[ord$ix[tail_ids]] <- smoothed$tail
lw_i[lw_i > 0] <- 0
list(log_weights = lw_i + max(log_ratios_i), pareto_k = smoothed$k)
Expand Down
Loading