pub struct CusumDetector {
pub k: f64,
pub h: f64,
pub s_hi: f64,
pub s_lo: f64,
pub target_mean: f64,
pub target_std: f64,
}Expand description
CUSUM (Cumulative Sum) sequential change-point detector.
Detects a sustained shift in the mean of a metric stream. The algorithm maintains two accumulators:
S_hidetects upward mean shifts (metric increasing).S_lodetects downward mean shifts (metric decreasing).
§Parameters
k— allowable slack, typically0.5 * σ * shift_size_in_sigmas.h— decision threshold (number of standard deviations of the cumulative sum before an alarm is raised). A common choice is4.0 – 5.0.
§Example
use trustformers_debug::regression::statistical::{CusumDetector, ChangeDirection};
let mut cusum = CusumDetector::new(0.0, 1.0, 0.5, 4.0);
// feed values well above the target mean — should eventually alert
let mut alerted = false;
for _ in 0..20 {
if let Some(a) = cusum.update(3.0) {
assert_eq!(a.direction, ChangeDirection::Up);
alerted = true;
break;
}
}
assert!(alerted, "CUSUM should have detected upward shift");Fields§
§k: f64Allowable slack (reference value).
h: f64Decision threshold — alarm when S_hi > h or S_lo > h.
s_hi: f64Upper cumulative sum.
s_lo: f64Lower cumulative sum.
target_mean: f64Target (in-control) process mean.
target_std: f64Target (in-control) process standard deviation.
Implementations§
Source§impl CusumDetector
impl CusumDetector
Sourcepub fn new(target_mean: f64, target_std: f64, k: f64, h: f64) -> Self
pub fn new(target_mean: f64, target_std: f64, k: f64, h: f64) -> Self
Creates a new CUSUM detector.
§Arguments
target_mean— in-control mean.target_std— in-control standard deviation (used to normalise inputs).k— slack parameter (0.5 is a common default for detecting 1σ shifts).h— threshold (4.0–5.0 gives low false-alarm rates for Gaussian inputs).
Sourcepub fn update(&mut self, value: f64) -> Option<CusumAlert>
pub fn update(&mut self, value: f64) -> Option<CusumAlert>
Incorporates a new observation and returns an alert if a change point is detected.
The observation is first standardised as z = (value − target_mean) / target_std
(unless target_std == 0, in which case the raw deviation is used).
After firing, the triggering accumulator is reset to zero so detection
can resume. Callers that wish to track sustained changes should call
reset manually instead.
Auto Trait Implementations§
impl Freeze for CusumDetector
impl RefUnwindSafe for CusumDetector
impl Send for CusumDetector
impl Sync for CusumDetector
impl Unpin for CusumDetector
impl UnsafeUnpin for CusumDetector
impl UnwindSafe for CusumDetector
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.