From cc09cfda47680a803a21e87d9ed64d3ff645b316 Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Fri, 26 Jun 2026 23:23:06 +0300 Subject: [PATCH 1/2] fix returned k when both tails give NA --- R/pareto_smooth.R | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/R/pareto_smooth.R b/R/pareto_smooth.R index 7de50fa9..b82accec 100644 --- a/R/pareto_smooth.R +++ b/R/pareto_smooth.R @@ -345,7 +345,11 @@ pareto_smooth.default <- function(x, ) right_k <- smoothed$k - k <- max(left_k, right_k, na.rm = TRUE) + if (is.na(left_k) && is.na(right_k)) { + k <- NA + } else { + k <- max(left_k, right_k, na.rm = TRUE) + } x <- smoothed$x } else { From f32efd9306954011988552096730366963b58ae2 Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Fri, 26 Jun 2026 23:44:13 +0300 Subject: [PATCH 2/2] test that output is NA if both tails are constant --- tests/testthat/test-pareto_smooth.R | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-pareto_smooth.R b/tests/testthat/test-pareto_smooth.R index b4203097..d9dc10d1 100644 --- a/tests/testthat/test-pareto_smooth.R +++ b/tests/testthat/test-pareto_smooth.R @@ -13,6 +13,18 @@ test_that("pareto_khat handles constant tail correctly", { }) +test_that("pareto_khat handles both tails being constant correctly", { + + # left and right tails are constant, so khat should be NA in all cases + x <- c(rep(-100, 10), sort(rnorm(100)), rep(100, 10)) + + expect_true(is.na(pareto_khat(x, tail = "left", ndraws_tail = 10))) + expect_true(is.na(pareto_khat(x, tail = "right", ndraws_tail = 10))) + expect_true(is.na(pareto_khat(x, tail = "both", ndraws_tail = 10))) + +}) + + test_that("pareto_smooth handles non-constant x with constant tail", { # x is non-constant but the right tail is constant @@ -281,4 +293,4 @@ test_that("check ps_min_ss behavior special cases", { expect_true(is.infinite(ps_min_ss(2))) # k < 1 expect_equal(ps_min_ss(0.5), 10^(1 / (1 - max(0, 0.5)))) -}) \ No newline at end of file +})