Skip to content

Drift Detection & Descriptors

The stride.drift subpackage provides sequential drift monitoring algorithms, error stream tracking, and post-detection characterization descriptors.


Overview

Streaming drift detection in STRIDE wraps River sequential detectors to monitor binary classification error streams \(e_t \in \{0, 1\}\). When an alarm is triggered, the descriptor reconstructs the transition onset, duration, and error trajectory.

from stride.drift import BinaryErrorDriftDescriptor, DriftDescription

Key Components

Class / Structure Description
BinaryErrorDriftDescriptor Wraps River drift detectors (e.g. DDM, EDDM, ADWIN) and applies CUSUM, threshold, or gradient lookback to estimate drift onset and duration.
DualADWIN Dual-threshold ADWIN wrapper running coupled estimators for simultaneous early warning (\(\delta_{\text{warn}}\)) and confirmed drift (\(\delta_{\text{drift}}\)).
DriftDescription Dataclass storing reconstructed drift event parameters (detected_at, drift_start_index, drift_end_index, duration, error_rate_at_start, error_rate_at_peak, severity).

API Specification

stride.drift

Drift detection methods, sequential error descriptors, and drift event tracking.

BinaryErrorDriftDescriptor

BinaryErrorDriftDescriptor(
    warning_grace_period=3,
    rate_calculation_sample_size=100,
    ddm=DDM(),
    lookback_method="cusum",
    lookforward_method="peak",
    degradation_only=True,
)

Track and characterize concept drifts identified by sequential error detectors.

Wraps River detectors (such as DDM and EDDM) to continuously monitor the binary error stream of an online model (\(e_t \in \{0, 1\}\)). When a warning or drift signal is triggered, it applies change-point lookback algorithms (CUSUM, threshold, gradient) to estimate the actual onset timestamp and lookforward methods to locate the drift peak.

Attributes:

Name Type Description
warning_grace_period

Allowed consecutive non-warning steps before warning chain resets.

rate_calculation_sample_size

Number of samples used to calculate local error rates.

ddm

Underlying River binary drift and warning detector instance.

lookback_method

Change-point algorithm used to locate onset ("cusum", "threshold", "gradient", "none").

lookforward_method

Algorithm used to estimate stabilization ("peak", "recovery", "none").

error_history list[int | float]

Window of recent error signals.

complete_error_history list[int | float]

Full recorded error stream sequence.

drift_detected

Flag indicating whether drift occurred at current timestep.

last_detected_drift DriftDescription | None

Most recent DriftDescription object, or None.

current_index

Current stream iteration index.

degradation_only

Whether to restrict detections to error degradation rather than improvement.

Initialize the binary error drift descriptor.

Parameters:

Name Type Description Default
warning_grace_period int

Allowed non-warning steps within a warning chain.

3
rate_calculation_sample_size int

Window size for moving error rate averages.

100
ddm Any

River drift detector instance with warning_detected and drift_detected.

DDM()
lookback_method str

Method for onset estimation ("cusum", "threshold", "gradient", "none").

'cusum'
lookforward_method str

Method for end/peak estimation ("peak", "recovery", "none").

'peak'
degradation_only bool

Whether to restrict detections to error degradation rather than improvement.

True
Source code in src/stride/drift/binary_descriptor.py
def __init__(
    self,
    warning_grace_period: int = 3,
    rate_calculation_sample_size: int = 100,
    ddm: Any = river_drift.binary.DDM(),
    lookback_method: str = "cusum",
    lookforward_method: str = "peak",
    degradation_only: bool = True,
) -> None:
    """Initialize the binary error drift descriptor.

    Args:
        warning_grace_period: Allowed non-warning steps within a warning chain.
        rate_calculation_sample_size: Window size for moving error rate averages.
        ddm: River drift detector instance with `warning_detected` and `drift_detected`.
        lookback_method: Method for onset estimation ("cusum", "threshold", "gradient", "none").
        lookforward_method: Method for end/peak estimation ("peak", "recovery", "none").
        degradation_only: Whether to restrict detections to error degradation rather than improvement.
    """
    self.warning_grace_period = warning_grace_period
    self.rate_calculation_sample_size = rate_calculation_sample_size
    self.ddm = ddm
    self.lookback_method = lookback_method
    self.lookforward_method = lookforward_method
    self.degradation_only = degradation_only

    self.warning_grace_period_left = warning_grace_period
    self.error_history: list[int | float] = []
    self.complete_error_history: list[int | float] = []
    self.previous_was_warning = False
    self.assume_warning = False
    self.last_detected_drift: DriftDescription | None = None
    self.drift_detected = False
    self.current_index = 0

describe_drift

describe_drift()

Return the DriftDescription object for the most recent drift event.

Returns:

Type Description
DriftDescription | None

Description object containing duration, onset, and error rates, or None.

Source code in src/stride/drift/binary_descriptor.py
def describe_drift(self) -> DriftDescription | None:
    """Return the `DriftDescription` object for the most recent drift event.

    Returns:
        Description object containing duration, onset, and error rates, or `None`.
    """
    return self.last_detected_drift

find_drift_end_peak

find_drift_end_peak(detection_idx, lookforward_window=200)

Estimate peak error rate timestamp following drift alarm.

Parameters:

Name Type Description Default
detection_idx int

Stream index where drift alarm was raised.

required
lookforward_window int

Maximum subsequent samples evaluated.

200

Returns:

Type Description
int

Sample index corresponding to maximum error rate window.

Source code in src/stride/drift/binary_descriptor.py
def find_drift_end_peak(self, detection_idx: int, lookforward_window: int = 200) -> int:
    """Estimate peak error rate timestamp following drift alarm.

    Args:
        detection_idx: Stream index where drift alarm was raised.
        lookforward_window: Maximum subsequent samples evaluated.

    Returns:
        Sample index corresponding to maximum error rate window.
    """
    max_idx = len(self.complete_error_history)
    end_idx = min(detection_idx + lookforward_window, max_idx)

    if detection_idx >= max_idx:
        return detection_idx

    window_size = self.rate_calculation_sample_size
    current_idx = detection_idx

    if current_idx + window_size <= max_idx:
        current_window = self.complete_error_history[current_idx : current_idx + window_size]
        previous_error_rate = float(np.mean(current_window))
    else:
        return detection_idx

    best_idx = detection_idx
    step_size = window_size
    current_idx += step_size

    while current_idx + window_size <= end_idx:
        window = self.complete_error_history[current_idx : current_idx + window_size]
        current_error_rate = float(np.mean(window))

        if current_error_rate < previous_error_rate:
            return best_idx

        best_idx = current_idx
        previous_error_rate = current_error_rate
        current_idx += step_size

    return best_idx

find_drift_end_recovery

find_drift_end_recovery(
    detection_idx, lookforward_window=200
)

Estimate stabilization point where error rates return toward baseline.

Parameters:

Name Type Description Default
detection_idx int

Stream index where drift alarm was raised.

required
lookforward_window int

Maximum subsequent samples evaluated.

200

Returns:

Type Description
int

Sample index representing error recovery point.

Source code in src/stride/drift/binary_descriptor.py
def find_drift_end_recovery(self, detection_idx: int, lookforward_window: int = 200) -> int:
    """Estimate stabilization point where error rates return toward baseline.

    Args:
        detection_idx: Stream index where drift alarm was raised.
        lookforward_window: Maximum subsequent samples evaluated.

    Returns:
        Sample index representing error recovery point.
    """
    max_idx = len(self.complete_error_history)
    end_idx = min(detection_idx + lookforward_window, max_idx)

    if detection_idx >= max_idx or end_idx - detection_idx < 20:
        return detection_idx

    window_size = min(20, end_idx - detection_idx)
    baseline_window = self.complete_error_history[detection_idx : detection_idx + window_size]
    baseline_high = float(np.mean(baseline_window))

    cusum = 0.0
    threshold = 1.5

    for i in range(detection_idx + window_size, end_idx):
        cusum = max(0.0, cusum + (baseline_high - self.complete_error_history[i] - 0.1))
        if cusum > threshold:
            return i

    return end_idx - 1

find_drift_start_cusum

find_drift_start_cusum(detection_idx, lookback_window=300)

Estimate drift onset using CUSUM change-point detection.

Parameters:

Name Type Description Default
detection_idx int

Stream index where drift alarm was raised.

required
lookback_window int

Maximum history window examined prior to detection.

300

Returns:

Type Description
int

Estimated sample index representing drift onset.

Source code in src/stride/drift/binary_descriptor.py
def find_drift_start_cusum(self, detection_idx: int, lookback_window: int = 300) -> int:
    """Estimate drift onset using CUSUM change-point detection.

    Args:
        detection_idx: Stream index where drift alarm was raised.
        lookback_window: Maximum history window examined prior to detection.

    Returns:
        Estimated sample index representing drift onset.
    """
    if detection_idx < 50:
        return 0

    start_idx = max(0, detection_idx - lookback_window)
    history_segment = self.complete_error_history[start_idx:detection_idx]

    if len(history_segment) < 20:
        return start_idx

    baseline_size = min(50, len(history_segment) // 4)
    baseline_mean = np.mean(history_segment[:baseline_size])

    cusum = 0.0
    threshold = 1.5

    for i in range(baseline_size, len(history_segment)):
        cusum = max(0.0, cusum + (history_segment[i] - baseline_mean - 0.1))
        if cusum > threshold:
            return start_idx + i

    return start_idx + baseline_size

find_drift_start_gradient

find_drift_start_gradient(
    detection_idx, lookback_window=300
)

Estimate drift onset using error rate gradient acceleration.

Parameters:

Name Type Description Default
detection_idx int

Stream index where drift alarm was raised.

required
lookback_window int

Maximum history window examined prior to detection.

300

Returns:

Type Description
int

Estimated sample index representing drift onset.

Source code in src/stride/drift/binary_descriptor.py
def find_drift_start_gradient(self, detection_idx: int, lookback_window: int = 300) -> int:
    """Estimate drift onset using error rate gradient acceleration.

    Args:
        detection_idx: Stream index where drift alarm was raised.
        lookback_window: Maximum history window examined prior to detection.

    Returns:
        Estimated sample index representing drift onset.
    """
    if detection_idx < 50:
        return 0

    window_size = 20
    start_idx = max(0, detection_idx - lookback_window)

    error_rates = []
    for i in range(start_idx, detection_idx - window_size):
        window = self.complete_error_history[i : i + window_size]
        error_rates.append(float(np.mean(window)))

    if len(error_rates) < 2:
        return start_idx

    gradients = np.diff(error_rates)
    gradient_threshold = 0.01
    for i, grad in enumerate(gradients):
        if grad > gradient_threshold:
            return start_idx + i

    return start_idx

find_drift_start_threshold

find_drift_start_threshold(
    detection_idx, lookback_window=300
)

Estimate drift onset by searching for sustained error rate elevation.

Parameters:

Name Type Description Default
detection_idx int

Stream index where drift alarm was raised.

required
lookback_window int

Maximum history window examined prior to detection.

300

Returns:

Type Description
int

Estimated sample index representing drift onset.

Source code in src/stride/drift/binary_descriptor.py
def find_drift_start_threshold(self, detection_idx: int, lookback_window: int = 300) -> int:
    """Estimate drift onset by searching for sustained error rate elevation.

    Args:
        detection_idx: Stream index where drift alarm was raised.
        lookback_window: Maximum history window examined prior to detection.

    Returns:
        Estimated sample index representing drift onset.
    """
    if detection_idx < 50:
        return 0

    window_size = self.rate_calculation_sample_size
    start_idx = max(0, detection_idx - lookback_window)

    baseline_end = min(start_idx + window_size, detection_idx - 50)
    if baseline_end <= start_idx:
        return start_idx

    baseline = self.complete_error_history[start_idx:baseline_end]
    baseline_rate = float(np.mean(baseline))

    increase_threshold = 1.5
    sustained_count = 0
    sustained_threshold = 20

    for i in range(baseline_end, detection_idx):
        if i + window_size > detection_idx:
            window_size_local = detection_idx - i
        else:
            window_size_local = window_size

        if window_size_local < 10:
            continue

        window = self.complete_error_history[i : i + window_size_local]
        current_rate = float(np.mean(window))

        if current_rate > baseline_rate * increase_threshold:
            sustained_count += 1
            if sustained_count >= sustained_threshold:
                return max(start_idx, i - sustained_threshold)
        else:
            sustained_count = 0

    return baseline_end

post_process_drift_ends

post_process_drift_ends(drift_descriptions)

Post-process a collection of detected drifts to compute peak or recovery timestamps.

Parameters:

Name Type Description Default
drift_descriptions list[DriftDescription]

Sequence of DriftDescription objects to update.

required

Returns:

Type Description
list[DriftDescription]

Updated sequence of descriptions with resolved end indices and peak rates.

Source code in src/stride/drift/binary_descriptor.py
def post_process_drift_ends(self, drift_descriptions: list[DriftDescription]) -> list[DriftDescription]:
    """Post-process a collection of detected drifts to compute peak or recovery timestamps.

    Args:
        drift_descriptions: Sequence of `DriftDescription` objects to update.

    Returns:
        Updated sequence of descriptions with resolved end indices and peak rates.
    """
    if self.lookforward_method == "none":
        return drift_descriptions

    for drift_description in drift_descriptions:
        detection_idx = drift_description.detected_at or 0

        if self.lookforward_method == "peak":
            drift_end_idx = self.find_drift_end_peak(detection_idx)
        elif self.lookforward_method == "recovery":
            drift_end_idx = self.find_drift_end_recovery(detection_idx)
        else:
            drift_end_idx = detection_idx

        drift_description.drift_end_index = drift_end_idx

        window_size = self.rate_calculation_sample_size
        end_window_begin = max(0, drift_end_idx - window_size // 2)
        end_window_end = min(drift_end_idx + window_size // 2, len(self.complete_error_history))
        end_window = self.complete_error_history[end_window_begin:end_window_end]
        drift_description.error_rate_at_peak = float(np.mean(end_window)) if len(end_window) > 0 else 0.0

        if drift_description.drift_start_index is not None:
            drift_description.drift_duration = drift_end_idx - drift_description.drift_start_index

    return drift_descriptions

update

update(x)

Update detector state with a binary error signal (\(0\) for correct, \(1\) for error).

Parameters:

Name Type Description Default
x int | float

Binary prediction error indicator.

required
Source code in src/stride/drift/binary_descriptor.py
def update(self, x: int | float) -> None:
    """Update detector state with a binary error signal ($0$ for correct, $1$ for error).

    Args:
        x: Binary prediction error indicator.
    """
    prev_estimation = getattr(self.ddm, "estimation", None)
    prev_warn_est = None
    if hasattr(self.ddm, "adwin_warn") and hasattr(self.ddm.adwin_warn, "estimation"):
        prev_warn_est = self.ddm.adwin_warn.estimation

    self.ddm.update(x)
    self.complete_error_history.append(x)
    self.drift_detected = False

    warning_detected = getattr(self.ddm, "warning_detected", False)
    if self.degradation_only and warning_detected and prev_warn_est is not None:
        if hasattr(self.ddm, "adwin_warn") and self.ddm.adwin_warn.estimation < prev_warn_est:
            warning_detected = False

    if warning_detected:
        self.warning_grace_period_left = self.warning_grace_period
    elif self.previous_was_warning is True:
        self.warning_grace_period_left -= 1

    if self.previous_was_warning and self.warning_grace_period_left > 0:
        self.assume_warning = True
    elif warning_detected:
        self.assume_warning = True
    else:
        self.assume_warning = False

    self.error_history.append(x)

    drift_detected = getattr(self.ddm, "drift_detected", getattr(self.ddm, "change_detected", False))

    # Directional filter: only flag if error rate degraded (worsened)
    if self.degradation_only and drift_detected and prev_estimation is not None:
        curr_estimation = getattr(self.ddm, "estimation", None)
        if curr_estimation is not None and curr_estimation < prev_estimation:
            drift_detected = False

    if not self.assume_warning and not drift_detected:
        self.error_history = self.error_history[-self.rate_calculation_sample_size :]

    if drift_detected:
        detection_idx = self.current_index

        # Determine dynamic lookback window
        if hasattr(self.ddm, "width"):
            lookback_window = int(self.ddm.width)
        else:
            lookback_window = len(self.error_history)

        # Find actual drift start using selected method
        if self.lookback_method == "cusum":
            drift_start_idx = self.find_drift_start_cusum(detection_idx, lookback_window=lookback_window)
        elif self.lookback_method == "threshold":
            drift_start_idx = self.find_drift_start_threshold(detection_idx, lookback_window=lookback_window)
        elif self.lookback_method == "gradient":
            drift_start_idx = self.find_drift_start_gradient(detection_idx, lookback_window=lookback_window)
        else:  # 'none'
            drift_start_idx = max(0, detection_idx - lookback_window)

        # Ensure the found starting point is within valid bounds [detection_idx - lookback_window, detection_idx]
        min_lookback_bound = max(0, detection_idx - lookback_window)
        drift_start_idx = max(min_lookback_bound, min(drift_start_idx, detection_idx))

        window_size = self.rate_calculation_sample_size
        start_window_begin = max(0, drift_start_idx - window_size // 2)
        start_window_end = min(drift_start_idx + window_size // 2, len(self.complete_error_history))
        start_window = self.complete_error_history[start_window_begin:start_window_end]
        error_rate_at_warning = float(np.mean(start_window)) if len(start_window) > 0 else 0.0

        detection_window_begin = max(0, detection_idx - window_size)
        detection_window = self.complete_error_history[detection_window_begin:detection_idx]
        error_rate_at_detection = float(np.mean(detection_window)) if len(detection_window) > 0 else 0.0

        drift_duration = detection_idx - drift_start_idx

        self.last_detected_drift = DriftDescription(
            error_rate_at_detection=error_rate_at_detection,
            error_rate_at_warning=error_rate_at_warning,
            drift_duration=drift_duration,
            drift_start_index=drift_start_idx,
            drift_end_index=detection_idx,
            error_rate_at_peak=error_rate_at_detection,
            detected_at=detection_idx,
        )

        self.drift_detected = True

        self.warning_grace_period_left = self.warning_grace_period
        self.error_history = []
        self.previous_was_warning = False
        self.assume_warning = False

    self.previous_was_warning = self.assume_warning
    self.current_index += 1

DriftDescription dataclass

DriftDescription(
    error_rate_at_warning=None,
    error_rate_at_detection=None,
    drift_duration=None,
    drift_start_index=None,
    drift_end_index=None,
    error_rate_at_peak=None,
    detected_at=None,
)

Represents characteristics of a detected concept drift event.

Attributes:

Name Type Description
error_rate_at_warning float | None

Error rate calculated at initial warning threshold.

error_rate_at_detection float | None

Error rate when drift alarm was triggered.

drift_duration int | None

Total count of stream samples between onset and detection.

drift_start_index int | None

Stream sample index pinpointing drift onset.

drift_end_index int | None

Stream sample index where drift concluded or stabilized.

error_rate_at_peak float | None

Maximum observed error rate across the drift event.

detected_at int | None

Stream sample index where detector emitted drift signal.

DualADWIN

DualADWIN(
    delta_warn=0.05,
    delta_drift=0.002,
    clock=32,
    max_buckets=5,
    min_window_length=5,
    grace_period=10,
)

Dual-threshold ADWIN wrapper providing explicit warning and drift signals.

Maintains two River ADWIN instances internally configured with different confidence bounds (\(\delta_{\text{warn}} > \delta_{\text{drift}}\)). The more sensitive instance (\(\delta_{\text{warn}}\)) triggers early warning alerts to commence lookback caching, while the conservative instance (\(\delta_{\text{drift}}\)) confirms actual concept drift.

Attributes:

Name Type Description
adwin_warn

Highly sensitive ADWIN instance tracking warning state.

adwin_drift

Conservative ADWIN instance tracking confirmed drift state.

Examples:

>>> detector = DualADWIN(delta_warn=0.05, delta_drift=0.002)
>>> detector.update(1.0)
>>> detector.drift_detected
False

Initialize dual-threshold ADWIN detector.

Parameters:

Name Type Description Default
delta_warn float

Confidence value for warning threshold. Higher delta implies higher sensitivity.

0.05
delta_drift float

Confidence value for confirmed drift threshold.

0.002
clock int

Number of incoming samples between subwindow split checks.

32
max_buckets int

Maximum number of buckets maintained at each exponential compression tier.

5
min_window_length int

Minimum subwindow size required before testing for statistical difference.

5
grace_period int

Initial observations processed before drift detection begins.

10
Source code in src/stride/drift/adwin.py
def __init__(
    self,
    delta_warn: float = 0.05,
    delta_drift: float = 0.002,
    clock: int = 32,
    max_buckets: int = 5,
    min_window_length: int = 5,
    grace_period: int = 10,
) -> None:
    """Initialize dual-threshold ADWIN detector.

    Args:
        delta_warn: Confidence value for warning threshold. Higher delta implies higher sensitivity.
        delta_drift: Confidence value for confirmed drift threshold.
        clock: Number of incoming samples between subwindow split checks.
        max_buckets: Maximum number of buckets maintained at each exponential compression tier.
        min_window_length: Minimum subwindow size required before testing for statistical difference.
        grace_period: Initial observations processed before drift detection begins.
    """
    self.adwin_warn = ADWIN(
        delta=delta_warn,
        clock=clock,
        max_buckets=max_buckets,
        min_window_length=min_window_length,
        grace_period=grace_period,
    )
    self.adwin_drift = ADWIN(
        delta=delta_drift,
        clock=clock,
        max_buckets=max_buckets,
        min_window_length=min_window_length,
        grace_period=grace_period,
    )

drift_detected property

drift_detected

Flag indicating whether confirmed drift threshold was exceeded at current step.

estimation property

estimation

Estimated mean error rate of the main drift tracking instance window.

warning_detected property

warning_detected

Flag indicating whether early warning threshold was exceeded at current step.

width property

width

Current window width of the main drift tracking instance.

update

update(x)

Update both warning and drift ADWIN instances with a new observation.

Parameters:

Name Type Description Default
x float | int

Input observation value (e.g., binary classification error \(0\) or \(1\)).

required
Source code in src/stride/drift/adwin.py
def update(self, x: float | int) -> None:
    """Update both warning and drift ADWIN instances with a new observation.

    Args:
        x: Input observation value (e.g., binary classification error $0$ or $1$).
    """
    self.adwin_warn.update(x)
    self.adwin_drift.update(x)