Skip to main content

SpectralResidual

Struct SpectralResidual 

Source
pub struct SpectralResidual {
    pub averaging_window: usize,
    pub judgement_window: usize,
    pub threshold: f64,
    pub min_zscore: f64,
    pub sensitivity: f64,
    pub batch_size: Option<usize>,
}
Expand description

Spectral residual anomaly scorer.

Built with SpectralResidual::new (the defaults of Ren et al. 2019) and adjusted with the with_* methods; run with SpectralResidual::analyze.

Fields§

§averaging_window: usize

Width of the moving average applied to the log amplitude spectrum (q in Ren et al.; default 3).

§judgement_window: usize

How many preceding saliency values a point is compared against (z; default 40).

§threshold: f64

Score above which a point is an anomaly (τ; default 3).

§min_zscore: f64

Minimum |value − mean| / std of the point against the judgement window before it, for it to be an anomaly (default 1.5), so that saliency alone does not flag noise on a flat series. A window with no spread passes any departure from its level.

§sensitivity: f64

Coverage of the band around the expected value, in percent (default 70): its half-width is the normal quantile for that two-sided coverage times the robust spread of the non-anomalous residuals.

§batch_size: Option<usize>

Process the series in consecutive batches of this many points instead of at once (default None). Long series with slow drift are scored against their local context this way; the last batch absorbs a short remainder.

Implementations§

Source§

impl SpectralResidual

Source

pub fn new() -> Self

The defaults of Ren et al. (2019): q = 3, z = 40, τ = 3, z-score gate 1.5, 70% band, no batching.

Source

pub fn with_averaging_window(self, q: usize) -> Self

Sets the spectral averaging window (≥ 1).

Source

pub fn with_judgement_window(self, z: usize) -> Self

Sets the judgement window (≥ 1).

Source

pub fn with_threshold(self, threshold: f64) -> Self

Sets the anomaly threshold on the score (> 0).

Source

pub fn with_min_zscore(self, min_zscore: f64) -> Self

Sets the z-score gate (≥ 0; 0 disables it).

Source

pub fn with_sensitivity(self, sensitivity: f64) -> Self

Sets the band coverage in percent (0 < s < 100).

Source

pub fn with_batch_size(self, batch_size: Option<usize>) -> Self

Sets the batch size (≥ MIN_OBSERVATIONS), or None for one batch.

Source

pub fn analyze( &self, series: &[f64], ) -> Result<Vec<SrPoint>, SpectralResidualError>

Scores every point of the series.

The result has one SrPoint per input point, in order.

§Errors

Returns the one condition that was not met — the option that is out of its domain, the shortfall in observations, or the position of the first non-finite value — rather than a refusal a caller has to match against the whole rulebook to interpret.

§Examples
use u_analytics::detection::{SpectralResidual, SpectralResidualError};

let mut series: Vec<f64> = (0..60).map(|t| (t as f64 * 0.3).sin()).collect();
series[40] += 4.0; // a spike
let points = SpectralResidual::new().analyze(&series).unwrap();
assert!(points[40].is_anomaly);
assert!(points[40].score > points[20].score);

// A refusal names the option it is about, the value and its domain.
let refused = SpectralResidual::new().with_threshold(0.0).analyze(&series);
assert_eq!(
    refused,
    Err(SpectralResidualError::OptionOutOfRange {
        option: "threshold",
        requirement: "a finite number > 0",
        got: 0.0,
        min: None,
        exclusive_min: Some(0.0),
        max: None,
        exclusive_max: None,
    })
);

Trait Implementations§

Source§

impl Clone for SpectralResidual

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SpectralResidual

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SpectralResidual

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for SpectralResidual

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for SpectralResidual

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.