R/slct_psi_then_error.R

Defines functions init_history.slct_psi_then_error update_concept.slct_psi_then_error select_model.slct_psi_then_error slct_psi_then_error

Documented in slct_psi_then_error

#' @title PSI then error selector
#' @description Combines [slct_psi()] and [slct_error_metrics()]: among the concepts accepted by the
#'  PSI stage, in increasing order of PSI, selects the first one that also passes the error stage.
#'  No concept passing both: a new one is created.
#' @param max_psi Fixed PSI limit. `NULL` (default): learned (see [slct_psi()]).
#' @param tolerance Fixed error tolerance. `NULL` (default): learned (see [slct_error_metrics()]).
#' @param tol_quantile Quantile of both learned limits, PSI and error.
#' @param numerical_cols Names of the numerical features (see [slct_psi()]).
#' @param n_tiles Number of quantile bins of the numerical PSI.
#' @param th Classification threshold. `NULL` (default): the threshold of each concept.
#' @param metric Metric object (e.g. `mt_accuracy()`).
#' @param init_blocks Start the error history by blocks (see [slct_error_metrics()]). `FALSE`
#'  (default): only the PSI stage starts by blocks.
#' @param pick Among the concepts accepted by the PSI stage: `"first"` (default)
#'  selects the first one, in increasing order of PSI, that also passes the error stage;
#'  `"min_error"` (used in the experiments) selects, among those passing both stages, the one with the smallest
#'  error on the batch (ties: the smallest PSI).
#' @return A `slct_psi_then_error` object.
#' @export
slct_psi_then_error <- function(max_psi = NULL, tolerance = NULL, tol_quantile = 0.8,
                                numerical_cols = NULL, n_tiles = 10, th = NULL, metric = mt_accuracy(),
                                init_blocks = FALSE, pick = c("first", "min_error")) {
  obj <- selector()
  obj$psi <- slct_psi(max_psi, tol_quantile, numerical_cols, n_tiles)
  obj$error <- slct_error_metrics(tolerance, tol_quantile, th, metric, init_blocks)
  obj$pick <- match.arg(pick)
  class(obj) <- append("slct_psi_then_error", class(obj))
  obj
}

#' @export
select_model.slct_psi_then_error <- function(obj, concept_list, valid_indices, X, y) {
  best <- 0
  best_err <- Inf
  for (i in slct_psi_rank(obj$psi, concept_list, valid_indices, X)) {
    res <- slct_error_values(obj$error, concept_list[[i]], X, y)
    concept_list[[i]] <- res$concept
    if (is.na(res$err_concept)) next
    tol <- limit_value(obj$error$limit, concept_list, i)
    if (res$err_batch <= res$err_concept + tol) {
      if (!identical(obj$pick, "min_error")) return(list(cl = concept_list, sc = i))
      if (res$err_batch < best_err) {
        best <- i
        best_err <- res$err_batch
      }
    }
  }
  list(cl = concept_list, sc = best)
}

#' @export
update_concept.slct_psi_then_error <- function(obj, concept, X, y, ...) {
  concept <- update_concept(obj$psi, concept, X, y, ...)
  update_concept(obj$error, concept, X, y, ...)
}

#' @export
init_history.slct_psi_then_error <- function(obj, concept, block_size, ...) {
  concept <- init_history(obj$psi, concept, block_size)
  init_history(obj$error, concept, block_size, ...)   # no-op unless init_blocks = TRUE
}

Try the heimdall package in your browser

Any scripts or data that you put into this service are public.

heimdall documentation built on Dec. 2, 2026, 5:08 p.m.