R/ac_policy.R

Defines functions record_limits.rec_stable record_limits.rec_always record_limits rec_stable rec_always record_policy rare_dropped.rare_drop rare_dropped.rare_keep rare_dropped rare_drop rare_keep rare_policy

Documented in rare_drop rare_dropped rare_keep rare_policy rec_always record_limits record_policy rec_stable

# =============================================================================
# Optional activities of the experiment line (AEL optionality): each is a small
# strategy family whose neutral element suppresses the activity (the batch passes
# through unchanged). A new behaviour on either axis is a new class here, plugged
# in, with no change to the backbone. The single uniform `state -> state` identity
# is part of the (ii) evolution; here each family carries its own neutral element.
# =============================================================================

#' @title Rare-batch admission
#' @description Base class of the batch-admission activity (scope: [drift_workflow()]): what the line
#'  does with a labeled batch that has too few positives (< 1% of its rows) to drive adaptation. The
#'  variants are [rare_keep()] (neutral: the batch is not dropped) and [rare_drop()] (the batch is
#'  discarded). In both, a rare batch that arrives before any model is trained is only postponed, since
#'  no model can be trained on it yet. New admission rules plug in by subclassing and implementing
#'  [rare_dropped()].
#' @return A `rare_policy` object.
#' @import daltoolbox
#' @export
rare_policy <- function() {
  obj <- dal_base()
  class(obj) <- append("rare_policy", class(obj))
  obj
}

#' @title Keep rare batches (neutral)
#' @description Admission activity that never drops a rare batch: it enters the data windows and only
#'  its drift adaptation is postponed. The neutral element of batch admission.
#' @return A `rare_keep` object.
#' @export
rare_keep <- function() {
  obj <- rare_policy()
  class(obj) <- append("rare_keep", class(obj))
  obj
}

#' @title Drop rare batches
#' @description Admission activity that discards a rare batch entirely (it is set aside and does not
#'  enter the data windows).
#' @return A `rare_drop` object.
#' @export
rare_drop <- function() {
  obj <- rare_policy()
  class(obj) <- append("rare_drop", class(obj))
  obj
}

#' @title Is a batch dropped by the admission policy?
#' @description Generic of the batch-admission activity: decide whether the current batch is discarded.
#' @param policy A [rare_policy()] object.
#' @param usable `TRUE` when the batch has enough positives (>= 1%).
#' @return `TRUE` when the batch is dropped.
#' @export
rare_dropped <- function(policy, usable) {
  UseMethod("rare_dropped")
}

#' @export
rare_dropped.rare_keep <- function(policy, usable) FALSE

#' @export
rare_dropped.rare_drop <- function(policy, usable) !usable


#' @title Learned-limit recording
#' @description Base class of the recording activity (scope: concept): which labeled batches enter the
#'  learned limits of the active concept (see [learned_limit()]). The variants are [rec_always()]
#'  (neutral: every batch that reaches the concept pool records) and [rec_stable()] (a batch flagged by
#'  a real, non-passive detector does not record). New recording rules plug in by subclassing and
#'  implementing [record_limits()].
#' @return A `record_policy` object.
#' @import daltoolbox
#' @export
record_policy <- function() {
  obj <- dal_base()
  class(obj) <- append("record_policy", class(obj))
  obj
}

#' @title Record on every batch (neutral)
#' @description Recording activity that lets every batch reaching the concept pool enter the learned
#'  limits, whatever the detector reported. The neutral element of limit recording.
#' @return A `rec_always` object.
#' @export
rec_always <- function() {
  obj <- record_policy()
  class(obj) <- append("rec_always", class(obj))
  obj
}

#' @title Record only on stable batches
#' @description Recording activity that keeps a batch out of the learned limits when a real
#'  (non-passive) detector flagged a drift on it; passive detectors never block recording.
#' @return A `rec_stable` object.
#' @export
rec_stable <- function() {
  obj <- record_policy()
  class(obj) <- append("rec_stable", class(obj))
  obj
}

#' @title Does the current batch record into the learned limits?
#' @description Generic of the recording activity: decide whether the current batch's statistics enter
#'  the learned limits of the active concept.
#' @param policy A [record_policy()] object.
#' @param drifted `TRUE` when the detector flagged a drift on this batch.
#' @param drift_method The drift detector (its family decides whether a drift blocks recording).
#' @return `TRUE` when the batch records.
#' @export
record_limits <- function(policy, drifted, drift_method) {
  UseMethod("record_limits")
}

#' @export
record_limits.rec_always <- function(policy, drifted, drift_method) TRUE

#' @export
record_limits.rec_stable <- function(policy, drifted, drift_method) {
  !(drifted && !inherits(drift_method, "dfr_passive"))
}

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.