pub struct StatRegressionDetector {
pub metric_name: String,
pub baseline: StatBaselineStats,
pub config: StatRegressionConfig,
pub detection_history: Vec<StatRegressionEvent>,
}Expand description
Statistical regression detector for a single scalar training metric.
Uses a fixed StatBaselineStats as the reference distribution and emits
StatRegressionEvents whenever a new observation deviates significantly
from that baseline.
§Example
use trustformers_debug::regression::statistical::{
StatBaselineStats, StatRegressionConfig, StatRegressionDetector, StatRegressionDirection,
};
let samples = [1.0f64, 1.1, 0.9, 1.05, 0.95];
let baseline = StatRegressionDetector::build_baseline(&samples);
let config = StatRegressionConfig {
z_score_threshold: 2.0,
relative_threshold: 0.15,
min_samples_for_detection: 2,
direction: StatRegressionDirection::Either,
};
let mut detector = StatRegressionDetector::new("loss", baseline, config);
// Far-out value should trigger a regression
let event = detector.check_point(10, 5.0);
assert!(event.is_some());Fields§
§metric_name: String§baseline: StatBaselineStats§config: StatRegressionConfig§detection_history: Vec<StatRegressionEvent>Implementations§
Source§impl StatRegressionDetector
impl StatRegressionDetector
Sourcepub fn new(
metric_name: &str,
baseline: StatBaselineStats,
config: StatRegressionConfig,
) -> Self
pub fn new( metric_name: &str, baseline: StatBaselineStats, config: StatRegressionConfig, ) -> Self
Creates a new detector for the named metric.
Sourcepub fn z_score(&self, value: f64) -> f64
pub fn z_score(&self, value: f64) -> f64
Computes the z-score of a new observation relative to the baseline.
Sourcepub fn relative_change(&self, value: f64) -> f64
pub fn relative_change(&self, value: f64) -> f64
Computes the relative change (value − mean) / mean.
Sourcepub fn check_point(
&mut self,
step: u64,
value: f64,
) -> Option<StatRegressionEvent>
pub fn check_point( &mut self, step: u64, value: f64, ) -> Option<StatRegressionEvent>
Evaluates a new data point and returns a StatRegressionEvent if a
regression is detected, or None otherwise.
A regression is detected when:
|z_score| > config.z_score_thresholdand|relative_change| > config.relative_thresholdand- The direction constraint is satisfied.
Detection only fires once baseline.sample_count >= min_samples_for_detection.
Sourcepub fn recent_events(&self, n: usize) -> &[StatRegressionEvent]
pub fn recent_events(&self, n: usize) -> &[StatRegressionEvent]
Returns the last n events in detection history (oldest first).
Sourcepub fn build_baseline(samples: &[f64]) -> StatBaselineStats
pub fn build_baseline(samples: &[f64]) -> StatBaselineStats
Builds a StatBaselineStats from a slice of samples using Welford’s
one-pass algorithm for numerically stable variance computation.
Returns a baseline with sample_count = 0 and all zeros when samples
is empty.
Auto Trait Implementations§
impl Freeze for StatRegressionDetector
impl RefUnwindSafe for StatRegressionDetector
impl Send for StatRegressionDetector
impl Sync for StatRegressionDetector
impl Unpin for StatRegressionDetector
impl UnsafeUnpin for StatRegressionDetector
impl UnwindSafe for StatRegressionDetector
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.