Skip to content

Commit

Permalink
Improved robustness and documentation for lookout()
Browse files Browse the repository at this point in the history
  • Loading branch information
robjhyndman committed Jun 12, 2024
1 parent f251e17 commit 29247c0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Encoding: UTF-8
LazyData: true
LazyDataCompression: xz
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.0
RoxygenNote: 7.3.1
Depends:
R (>= 4.1.0)
Suggests:
Expand Down
16 changes: 12 additions & 4 deletions R/lookout.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#' @title Lookout probabilities
#' @description Compute leave-one-out log score probabilities using a
#' Generalized Pareto distribution. These give the probability of each observation
#' being an anomaly.
#' being from the same distribution as the majority of observations. A low probability
#' indicates a likely anomaly.
#' @details This function can work with several object types.
#' If `object` is not `NULL`, then the object is passed to \code{\link{density_scores}}
#' to compute density scores (and possibly LOO density scores). Otherwise,
Expand Down Expand Up @@ -59,12 +60,19 @@ lookout <- function(
loo_scores <- density_scores(object, loo = TRUE) |> suppressWarnings()
}
}
threshold <- stats::quantile(density_scores, prob = threshold_probability, type = 8)
if (sum(density_scores > threshold) == 0L) {
threshold <- stats::quantile(density_scores,
prob = threshold_probability,
type = 8, na.rm = TRUE
)
if (sum(density_scores > threshold, na.rm = TRUE) == 0L) {
warning("No scores above threshold.")
return(rep(1, length(density_scores)))
}
gpd <- evd::fpot(density_scores, threshold = threshold, std.err = FALSE)$estimate
finite <- density_scores < Inf
if (any(!finite, na.rm = TRUE)) {
warning("Infinite density scores will be ignored in GPD.")
}
gpd <- evd::fpot(density_scores[finite], threshold = threshold, std.err = FALSE)$estimate
evd::pgpd(
loo_scores,
loc = threshold,
Expand Down
3 changes: 2 additions & 1 deletion man/lookout.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 29247c0

Please sign in to comment.