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 { 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 +})