Skip to main content

Anomaly

Struct Anomaly 

Source
pub struct Anomaly<const N: usize> { /* private fields */ }
Expand description

Flags a reading that lies far from the recent norm.

“Tell me when something is off” needs a baseline: a reading is suspicious only relative to what is usual. An Anomaly keeps a rolling window of recent readings and flags one that sits more than a chosen number of standard deviations from their mean - the three-sigma rule, the standard z-score test, with the threshold left to the caller (3.0 is the usual choice). It is dependency-free: instead of taking a square root it compares the squared deviation against the squared threshold, which is the same test.

With a perfectly flat baseline the spread is zero, so any change at all reads as anomalous; real sensor noise gives a non-zero baseline, where this is not an issue.

§Examples

use pamoja_kit::Anomaly;

let mut watch = Anomaly::<8>::new(3.0);
// Establish a steady baseline.
for reading in [10.0, 10.2, 9.8, 10.1, 9.9, 10.0, 10.2, 9.8] {
    watch.check(reading);
}
assert!(!watch.check(10.1)); // close to the norm: fine
assert!(watch.check(20.0)); // a far jump: flagged

Implementations§

Source§

impl<const N: usize> Anomaly<N>

Source

pub fn new(sigmas: f32) -> Self

Creates a detector that flags readings beyond sigmas standard deviations.

§Arguments
  • sigmas - the threshold in standard deviations; 3.0 is the common three-sigma rule. Its magnitude is used.
§Returns

A detector with an empty history.

Source

pub fn check(&mut self, reading: f32) -> bool

Tests a reading against the recent norm, then folds it into the history.

The reading is judged against the window of earlier readings, so the value being tested does not inflate its own baseline. Until at least two readings have been seen there is no spread to judge against, so nothing is flagged.

§Arguments
  • reading - the latest reading.
§Returns

true if reading lies more than the configured standard deviations from the mean of the recent window.

Source

pub fn len(&self) -> usize

Returns the number of readings in the baseline window so far.

Source

pub fn is_empty(&self) -> bool

Returns true if no readings have been recorded yet.

Trait Implementations§

Source§

impl<const N: usize> Clone for Anomaly<N>

Source§

fn clone(&self) -> Anomaly<N>

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<const N: usize> Copy for Anomaly<N>

Source§

impl<const N: usize> Debug for Anomaly<N>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<const N: usize> Freeze for Anomaly<N>
where Window<N>: Freeze,

§

impl<const N: usize> RefUnwindSafe for Anomaly<N>

§

impl<const N: usize> Send for Anomaly<N>
where Window<N>: Send,

§

impl<const N: usize> Sync for Anomaly<N>
where Window<N>: Sync,

§

impl<const N: usize> Unpin for Anomaly<N>
where Window<N>: Unpin,

§

impl<const N: usize> UnsafeUnpin for Anomaly<N>
where Window<N>: UnsafeUnpin,

§

impl<const N: usize> UnwindSafe for Anomaly<N>
where Window<N>: UnwindSafe,

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, 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.