Nothing
#' @title Selector
#' @description Base class of the selectors, the concept pool's `model_selector`: on drift, a selector
#' chooses, among the stored concepts, the one that fits the current batch (or none, so a new concept
#' is created). A selector accepts a stored concept when a statistic between the batch and the concept
#' is within a limit (see [learned_limit()]): either fixed, or the `tol_quantile` quantile of the
#' statistics recorded on the batches that belonged to the concept (see `update_concept()` and
#' `concept_pool()`). Choosing the slot to *replace* when the pool is full is a different role, the
#' [evictor()].
#' @return A `selector` object.
#' @import daltoolbox
#' @export
selector <- function() {
obj <- dal_base()
class(obj) <- append("selector", class(obj))
obj
}
#' @title Select a concept
#' @description Generic called by the concept pool, implemented by each selector with its own evidence:
#' among the stored concepts, choose the one that fits the current batch.
#' @param obj A `selector` object.
#' @param concept_list The stored concepts.
#' @param valid_indices Indices of `concept_list` eligible in this call.
#' @param X Features of the current batch.
#' @param y Labels of the current batch, `NULL` when no label is available yet (the `check`, before
#' the prediction). Selectors that use the feature distribution alone ignore it.
#' @return `list(cl = concept_list, sc = slot)`, with `sc = 0` when no stored concept fits.
#' @export
select_model <- function(obj, concept_list, valid_indices, X, y) {
UseMethod("select_model")
}
#' @title Evictor
#' @description Base class of the evictors, the concept pool's `model_deleter`: when every slot is
#' used and a new concept must be stored, an evictor chooses the slot to replace.
#' @return An `evictor` object.
#' @import daltoolbox
#' @export
evictor <- function() {
obj <- dal_base()
class(obj) <- append("evictor", class(obj))
obj
}
#' @title Evict a concept
#' @description Generic called by the concept pool to choose the slot to replace, implemented by each
#' evictor.
#' @param obj An `evictor` object.
#' @param concept_list The stored concepts.
#' @return The index of the slot to be replaced.
#' @export
evict <- function(obj, concept_list) {
UseMethod("evict")
}
# =============================================================================
# Learned limit — a selector's acceptance limit (shared by all selectors)
# =============================================================================
#' @title Learned acceptance limit
#' @description The acceptance limit a selector compares its statistic against. A `fixed` number
#' overrides everything; otherwise the limit is learned as the `quantile` of the statistics a
#' concept recorded on the batches that belonged to it (pooling across all stored concepts while a
#' concept has fewer than `min_history` of its own values). Each selector composes one; the
#' per-concept statistics are kept in `concept$history[[key]]`, shared storage that the cross-concept
#' pooling needs.
#' @param fixed A fixed limit (a number), or `NULL` to learn it from the history.
#' @param quantile Quantile of the learned limit.
#' @param key History key under which a concept stores this selector's statistics.
#' @param min_history Own values a concept needs before it stops pooling the other concepts' values.
#' @return A `learned_limit` object.
#' @export
learned_limit <- function(fixed = NULL, quantile = 0.8, key, min_history = 5) {
structure(list(fixed = fixed, quantile = quantile, key = key, min_history = min_history),
class = "learned_limit")
}
# Appends a statistic to a concept's history for this limit (NA is ignored). Returns the concept.
limit_record <- function(ll, concept, value) {
if (length(value) == 1 && !is.na(value))
concept$history[[ll$key]] <- c(concept$history[[ll$key]], value)
concept
}
# Sets a concept's whole history vector for this limit (used by the block-based initialization).
limit_init <- function(ll, concept, values) {
values <- values[!is.na(values)]
if (length(values) > 0) concept$history[[ll$key]] <- values
concept
}
# Has concept `concept` recorded no statistic for this limit yet?
limit_empty <- function(ll, concept) length(concept$history[[ll$key]]) == 0
# Limit of concept i: `fixed` when given; otherwise the quantile of its own history (pooling all
# stored concepts while it has fewer than `min_history` values). Without any history the limit is 0:
# there is no evidence that the batch belongs to any concept.
limit_value <- function(ll, concept_list, i) {
if (!is.null(ll$fixed)) return(ll$fixed)
h <- concept_list[[i]]$history[[ll$key]]
if (length(h) < ll$min_history) h <- unlist(lapply(concept_list, function(cn) cn$history[[ll$key]]))
if (length(h) == 0) return(0)
as.numeric(stats::quantile(h, ll$quantile))
}
#' @title Inactive selector
#' @description Never changes the concept: always reactivates the concept in slot 1 (the first and
#' only one stored).
#' @return A `slct_inactive` object.
#' @export
slct_inactive <- function() {
obj <- selector()
class(obj) <- append("slct_inactive", class(obj))
obj
}
#' @export
select_model.slct_inactive <- function(obj, concept_list, valid_indices, X, y) {
list(cl = concept_list, sc = 1)
}
# =============================================================================
# Concept ranker: how the check (at predict time) picks a concept among the
# candidates it scored. A variation point of the line (no neutral element: both
# criteria are genuine choices). Scope: concept/selection.
# =============================================================================
#' @title Concept ranker
#' @description Base class of the concept rankers: how the `check` of [adp_recurrent()] picks, at
#' predict time, the concept to answer among the candidates it scored by PSI against their memory. The
#' variants are [rank_ratio()] (smallest PSI/limit) and [rank_psi()] (smallest PSI). New criteria plug
#' in by subclassing and implementing [rank_criterion()].
#' @return A `concept_ranker` object.
#' @import daltoolbox
#' @export
concept_ranker <- function() {
obj <- dal_base()
class(obj) <- append("concept_ranker", class(obj))
obj
}
#' @title Rank by PSI/limit ratio
#' @description Ranker that picks the concept with the smallest ratio between its PSI and its limit.
#' @return A `rank_ratio` object.
#' @export
rank_ratio <- function() {
obj <- concept_ranker()
class(obj) <- append("rank_ratio", class(obj))
obj
}
#' @title Rank by PSI
#' @description Ranker that picks the concept with the smallest PSI, ignoring its limit.
#' @return A `rank_psi` object.
#' @export
rank_psi <- function() {
obj <- concept_ranker()
class(obj) <- append("rank_psi", class(obj))
obj
}
#' @title Criterion to minimize when the check picks a concept
#' @description Generic of the concept ranker: given, for each candidate, its PSI and its PSI/limit
#' ratio, return the vector the check minimizes to choose the answering concept.
#' @param ranker A [concept_ranker()] object.
#' @param psi Numeric vector: the PSI of each candidate.
#' @param ratio Numeric vector: the PSI/limit ratio of each candidate.
#' @return The numeric vector to minimize.
#' @export
rank_criterion <- function(ranker, psi, ratio) {
UseMethod("rank_criterion")
}
#' @export
rank_criterion.rank_ratio <- function(ranker, psi, ratio) ratio
#' @export
rank_criterion.rank_psi <- function(ranker, psi, ratio) psi
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.