Struct Mode

Source
pub struct Mode<T: Float + ToBytes> { /* private fields */ }
Expand description

Mode computation struct

This struct stores data to allow efficient computation of the mode. This struct takes linear space. It implements FromIterator to allow collection.

§Examples

let mut mode = inc_stats::Mode::new();
for &num in &[2.0, 4.0, 8.0] {
    mode.add(num);
}
assert_eq!(3, mode.count());
let mode: inc_stats::Mode<f64> = [2.0, 4.0, 8.0].iter().collect();
assert_eq!(3, mode.count());

Implementations§

Source§

impl<T: Float + ToBytes> Mode<T>

Source

pub fn new() -> Self

Create a new Mode object with no data

Source

pub fn add(&mut self, rval: impl DerefCopy<Output = T>)

Add a data point

Source

pub fn count(&self) -> usize

Get the number of data points

§Examples
let num: inc_stats::Mode<_> = [1.0, 2.0, std::f64::NAN].iter().collect();
assert_eq!(3, num.count());
Source

pub fn count_distinct(&self) -> usize

Count the number of distinct values

Distinctness for floating points is very finicy. Values that may print the same may not be same underlying value. Computations that yield the same value in “real” math may not yield the same value in floating point math.

This ignores nans

§Examples
let num: inc_stats::Mode<_> = [1.0, 2.0, 2.0, std::f64::NAN].iter().collect();
assert_eq!(2, num.count_distinct());
Source

pub fn count_distinct_nan(&self) -> usize

Count the number of distinct values

This treats all NaNs as different

§Examples
let num: inc_stats::Mode<_> = [1.0, std::f64::NAN, std::f64::NAN].iter().collect();
assert_eq!(3, num.count_distinct_nan());

Treat all nans the same

let num: inc_stats::Mode<_> = [1.0, std::f64::NAN, std::f64::NAN].iter().collect();
assert_eq!(2, std::cmp::min(num.count_distinct() + 1, num.count_distinct_nan()));
Source

pub fn modes(&self) -> impl Iterator<Item = T> + '_

Return an iterator of all of the modes

Multiple modes are retruned in the order they became a mode. NaNs are ignored.

This iterator has read only reference to the mode data structure that must be dropped to continue modifying the mode.

Constant time.

§Examples
let mut mode = inc_stats::Mode::new();
{
    let mut it = mode.modes();
    assert!(it.next().is_none());
}

mode.add(5.0);
{
    let mut it = mode.modes();
    assert_eq!(Some(5.0), it.next());
    assert!(it.next().is_none());
}

mode.add(3.0);
{
    let mut it = mode.modes();
    assert_eq!(Some(5.0), it.next());
    assert_eq!(Some(3.0), it.next());
    assert!(it.next().is_none());
}

mode.add(3.0);
{
    let mut it = mode.modes();
    assert_eq!(Some(3.0), it.next());
    assert!(it.next().is_none());
}
Source

pub fn modes_nan(&self) -> impl Iterator<Item = T> + '_

Return an iterator of all of the modes

This iterator will include NaN if present as a mode. NaN will always be returned last

Constant time.

§Examples
let mode: inc_stats::Mode<_> = [std::f64::NAN, 5.0].iter().collect();
let mut it = mode.modes_nan();
assert_eq!(Some(5.0), it.next());
assert!(it.next().unwrap().is_nan());
assert!(it.next().is_none());
Source

pub fn mode(&self) -> Option<T>

Return the current mode

If multiple modes exist, this returns the first element that reached the largest count. NaNs are ignored when computing the mode.

Constant time.

§Examples
let mode: inc_stats::Mode<_> = [2.0, 4.0, std::f64::NAN, 4.0].iter().collect();
assert_eq!(4.0, mode.mode().unwrap());
let mode = inc_stats::Mode::<f64>::new();
assert!(mode.mode().is_none());
Source

pub fn mode_nan(&self) -> Option<T>

Return the current mode

If multiple modes exist, this returns the first element that reached the largest count that wasn’t NaN. NaN will be returned only if it is the unique mode.

Constant time.

§Examples
let mode: inc_stats::Mode<_> = [2.0, 4.0, std::f64::NAN, std::f64::NAN].iter().collect();
assert!(mode.mode_nan().unwrap().is_nan());
Source

pub fn mode_count(&self) -> usize

Return the number of times the mode occurred

Constant time.

§Examples
let mode: inc_stats::Mode<_> = [2.0, 4.0, std::f64::NAN, 4.0].iter().collect();
assert_eq!(2, mode.mode_count());
Source

pub fn mode_count_nan(&self) -> usize

Return the number of times the mode occurred

Counts NaNs as a possible mode.

Constant time.

§Examples
let mode: inc_stats::Mode<_> = [2.0, 4.0, std::f64::NAN, std::f64::NAN].iter().collect();
assert_eq!(2, mode.mode_count_nan());

Trait Implementations§

Source§

impl<T: Debug + Float + ToBytes> Debug for Mode<T>

Source§

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

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

impl<T: Float + ToBytes, V: DerefCopy<Output = T>> FromIterator<V> for Mode<T>

Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = V>,

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Mode<T>

§

impl<T> RefUnwindSafe for Mode<T>
where T: RefUnwindSafe,

§

impl<T> Send for Mode<T>
where T: Send,

§

impl<T> Sync for Mode<T>
where T: Sync,

§

impl<T> Unpin for Mode<T>
where T: Unpin,

§

impl<T> UnwindSafe for Mode<T>
where T: 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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.