R/ac_reference.R

Defines functions recurrent_ensemble recurrent_incremental adapt_reset adapt_incremental adapt_retrain adapt_static

Documented in adapt_incremental adapt_reset adapt_retrain adapt_static recurrent_ensemble recurrent_incremental

# =============================================================================
# Reference methods: named *derivations* of the drift-adaptation experiment line
# (which activity variants), instantiated with the published defaults. Two
# families:
#   - adapt_*      : general concept-drift baselines (single model, no concept
#                    reuse) -- degenerate derivations over concept_pool_inactive().
#   - recurrent_*  : the recurrent-concept method (concept management), i.e. the
#                    REMIND method, over concept_pool().
# Each constructor fixes the derivation and exposes the instantiation parameters
# (method, train_size and tuning) as arguments. Change the tuning to taste; change
# the *derivation* by composing adp_recurrent() directly.
# =============================================================================

# ---- General baselines (adapt_*) --------------------------------------------

#' @title Baseline: static model
#' @description Drift baseline that trains a model once and never adapts (lower bound). A degenerate
#'  [adp_recurrent()]: no drift reaction ([dfr_inactive()]), no concept reuse ([concept_pool_inactive()]),
#'  frozen model ([train_static()]).
#' @param method Classifier (e.g. `cla_nb`, `torch_cla_mlp`).
#' @param train_size Rows needed to train and the sliding-window size.
#' @param memory Memory prototype (default [memory_undersampling()]).
#' @param warmup_size Minimum rows to train (default 100).
#' @param th Decision threshold; `NULL` (default) learns the F1-optimal threshold.
#' @return An `adp_recurrent` object.
#' @export
adapt_static <- function(method, train_size, memory = memory_undersampling(),
                         warmup_size = 100L, th = NULL) {
  adp_recurrent(method, dfr_inactive(), concept_pool_inactive(), memory,
                trainer = train_static(), train_size = train_size, warmup_size = warmup_size,
                th = th, rare_policy = rare_drop(), record_policy = rec_always())
}

#' @title Baseline: periodic retraining
#' @description Drift baseline that retrains a single model from scratch on its sliding window at every
#'  batch ([train_retrain()]), with no drift reaction and no concept reuse.
#' @inheritParams adapt_static
#' @return An `adp_recurrent` object.
#' @export
adapt_retrain <- function(method, train_size, memory = memory_undersampling(),
                          warmup_size = 100L, th = NULL) {
  adp_recurrent(method, dfr_inactive(), concept_pool_inactive(), memory,
                trainer = train_retrain(), train_size = train_size, warmup_size = warmup_size,
                th = th, rare_policy = rare_drop(), record_policy = rec_always())
}

#' @title Baseline: incremental model
#' @description Drift baseline that updates a single model incrementally with each batch
#'  ([train_incremental()]), with no drift reaction and no concept reuse (online learning).
#' @inheritParams adapt_static
#' @param epochs Epochs of each incremental update (default 5).
#' @return An `adp_recurrent` object.
#' @export
adapt_incremental <- function(method, train_size, epochs = 5L, memory = memory_undersampling(),
                              warmup_size = 100L, th = NULL) {
  adp_recurrent(method, dfr_inactive(), concept_pool_inactive(), memory,
                trainer = train_incremental(epochs), train_size = train_size,
                warmup_size = warmup_size, th = th, rare_policy = rare_drop(),
                record_policy = rec_always())
}

#' @title Baseline: detect-and-reset
#' @description Drift baseline that trains a fresh model and freezes it, and starts a new one whenever
#'  the detector flags a drift (detect-and-reset). No concept reuse ([concept_pool_inactive()]).
#' @inheritParams adapt_static
#' @param detector Drift detector that triggers the reset (default [dfr_ddm()]).
#' @return An `adp_recurrent` object.
#' @export
adapt_reset <- function(method, train_size, detector = dfr_ddm(), memory = memory_undersampling(),
                        warmup_size = 100L, th = NULL) {
  adp_recurrent(method, detector, concept_pool_inactive(), memory,
                trainer = train_static(), train_size = train_size, warmup_size = warmup_size,
                th = th, rare_policy = rare_drop(), record_policy = rec_always())
}


# ---- Recurrent-concept method (recurrent_*) ---------------------------------

#' @title Recurrent-concept method, incremental (REMIND-I)
#' @description The recurrent-concept method with incremental training of the active concept
#'  ([train_incremental()]): the drift detector triggers the concept pool, which stores and reuses
#'  concepts across recurring regimes via a PSI selector with a learned limit. Reproduces REMIND-I with
#'  the published defaults; a named derivation of [adp_recurrent()].
#' @param method Classifier used by every concept (e.g. `torch_cla_mlp`).
#' @param train_size Rows needed to train a concept and the sliding-window size.
#' @param numerical_cols Numeric columns the PSI is computed on (`NULL`: all).
#' @param detector Drift detector (default [dfr_passive()]).
#' @param n_slots Concept pool size (default 20).
#' @param n_tiles PSI bins (default 5).
#' @param max_psi Fixed PSI limit; `NULL` (default) learns it from the concept history.
#' @param tol_quantile Quantile of the learned PSI limit (default 0.8).
#' @param epochs Epochs of each incremental update (default 5).
#' @param memory Memory prototype (default [memory_undersampling()]).
#' @param warmup_size Minimum rows to train, run the detector and apply the check (default 100).
#' @param th Decision threshold; `NULL` (default) learns the F1-optimal threshold.
#' @param check `NULL` (default) disables the predict-time check; a [slct_psi()] enables it.
#' @return An `adp_recurrent` object.
#' @export
recurrent_incremental <- function(method, train_size, numerical_cols = NULL,
                                  detector = dfr_passive(), n_slots = 20L, n_tiles = 5L,
                                  max_psi = NULL, tol_quantile = 0.8, epochs = 5L,
                                  memory = memory_undersampling(), warmup_size = 100L,
                                  th = NULL, check = NULL) {
  adp_recurrent(method, detector,
                concept_pool(slct_psi(max_psi = max_psi, tol_quantile = tol_quantile,
                                      numerical_cols = numerical_cols, n_tiles = n_tiles),
                             evt_least_used(), n_slots = n_slots,
                             history_order = "before", history_init = "blocks"),
                memory, trainer = train_incremental(epochs), train_size = train_size,
                warmup_size = warmup_size, th = th, check = check,
                rare_policy = rare_drop(), record_policy = rec_always(), check_rank = rank_ratio())
}

#' @title Recurrent-concept method, ensemble (REMIND-E)
#' @description The recurrent-concept method with frozen concepts ([train_static()]): the pool keeps an
#'  ensemble of concepts, reusing (never updating) them across recurring regimes. Reproduces REMIND-E
#'  with the published defaults; a named derivation of [adp_recurrent()].
#' @inheritParams recurrent_incremental
#' @return An `adp_recurrent` object.
#' @export
recurrent_ensemble <- function(method, train_size, numerical_cols = NULL,
                               detector = dfr_passive(), n_slots = 20L, n_tiles = 5L,
                               max_psi = NULL, tol_quantile = 0.8,
                               memory = memory_undersampling(), warmup_size = 100L,
                               th = NULL, check = NULL) {
  adp_recurrent(method, detector,
                concept_pool(slct_psi(max_psi = max_psi, tol_quantile = tol_quantile,
                                      numerical_cols = numerical_cols, n_tiles = n_tiles),
                             evt_least_used(), n_slots = n_slots,
                             history_order = "before", history_init = "blocks"),
                memory, trainer = train_static(), train_size = train_size,
                warmup_size = warmup_size, th = th, check = check,
                rare_policy = rare_drop(), record_policy = rec_always(), check_rank = rank_ratio())
}

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.