Percentiles

Struct Percentiles 

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

Data percentile struct

This struct stores data to allow efficient computation of percentiles. This struct takes linear space. It implements FromIterator to allow collection. This collection ignores NaNs.

The structure is designed for efficient computation of percentiles when data is added and then percentiles are computed. Adding data is constant time, querying percentiles is linear time, with some caching to make it faster for computing several percentiles. If you were going to query percentiles while adding data, then you probably want to use a different data structure.

§Examples

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

Implementations§

Source§

impl<T: Float + FromPrimitive> Percentiles<T>

Source

pub fn new() -> Self

Create a new Percentiles 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

Source

pub fn percentiles<P, I>( &self, percentiles: I, ) -> Result<Option<Vec<T>>, StatsError>
where P: DerefCopy<Output = f64>, I: IntoIterator<Item = P>,

Get a number of percentiles

This takes linear time in the number of added data points, and log linear in the number of percentiles. This will be marginally more efficient than calling percentile repeatedly in a bad order.

§Examples:
let percs: inc_stats::Percentiles<f64> = [1.0, 3.0, 7.0].iter().collect();
let quarts = percs.percentiles(&[0.75, 0.25, 0.5]).unwrap().unwrap();
assert!((5.0 - quarts[0]).abs() < 1.0e-6);
assert!((2.0 - quarts[1]).abs() < 1.0e-6);
assert!((3.0 - quarts[2]).abs() < 1.0e-6);
Source

pub fn percentile( &self, percentile: impl DerefCopy<Output = f64>, ) -> Result<Option<T>, StatsError>

Get a percentile

Linear time.

§Examples:
let percs: inc_stats::Percentiles<f64> = [1.0, 5.0].iter().collect();
let quart = percs.percentile(0.25).unwrap().unwrap();
assert!((2.0 - quart).abs() < 1.0e-6);
Source

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

Get the median

Linear time.

§Examples:
let percs: inc_stats::Percentiles<f64> = [1.0, 5.0, 100.0].iter().collect();
let med = percs.median().unwrap();
assert_eq!(5.0, med);

Trait Implementations§

Source§

impl<T: Debug + Float + FromPrimitive> Debug for Percentiles<T>

Source§

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

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

impl<T: Float + FromPrimitive> Default for Percentiles<T>

Source§

fn default() -> Self

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

impl<T: Float + FromPrimitive, V: DerefCopy<Output = T>> FromIterator<V> for Percentiles<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 Percentiles<T>

§

impl<T> !RefUnwindSafe for Percentiles<T>

§

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

§

impl<T> !Sync for Percentiles<T>

§

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

§

impl<T> UnwindSafe for Percentiles<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.