R/slct_redundant.R

Defines functions evict.evt_redundant evt_redundant

Documented in evt_redundant

#' @title Redundant evictor
#' @description Used as the concept pool's `model_deleter`: finds the pair of stored concepts whose
#'  memories are the most similar (smallest PSI) and returns the slot of the less used of the two, so
#'  the repository keeps one representative of each regime. Concepts active for fewer than `min_qty`
#'  batches are protected. With fewer than two eligible concepts, behaves as [evt_least_used()].
#' @param numerical_cols Names of the numerical features (see [slct_psi()]).
#' @param n_tiles Number of quantile bins of the numerical PSI.
#' @param min_qty Minimum number of active batches for a concept to be eligible for removal.
#' @return A `evt_redundant` object.
#' @export
evt_redundant <- function(numerical_cols = NULL, n_tiles = 10, min_qty = 5) {
  obj <- evictor()
  obj$numerical_cols <- numerical_cols
  obj$n_tiles <- n_tiles
  obj$min_qty <- min_qty
  class(obj) <- append("evt_redundant", class(obj))
  obj
}

#' @export
evict.evt_redundant <- function(obj, concept_list) {
  qtys <- vapply(concept_list, function(cn) cn$qty, numeric(1))
  elig <- which(qtys >= obj$min_qty & vapply(concept_list, function(cn) NROW(cn$memory$X) > 0, logical(1)))

  if (length(elig) < 2) return(evict(evt_least_used(), concept_list))
  
  best_psi <- Inf
  pair <- NULL
  for (a in seq_len(length(elig) - 1)) {
    for (b in (a + 1):length(elig)) {
      i <- elig[a]; j <- elig[b]
      p <- slct_psi_mean(concept_list[[i]]$memory$X, concept_list[[j]]$memory$X, obj$numerical_cols, obj$n_tiles)
      if (!is.na(p) && p < best_psi) {
        best_psi <- p
        pair <- c(i, j)
      }
    }
  }
  
  if (is.null(pair)) return(evict(evt_least_used(), concept_list))
  pair[which.min(qtys[pair])]
}

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.