Trait medians::Median

source ·
pub trait Median<'a, T> {
    // Required methods
    fn qmedian_by(
        self,
        c: &mut impl FnMut(&T, &T) -> Ordering,
        q: impl Fn(&T) -> f64
    ) -> Result<f64, Me>;
    fn uqmedian(self, q: impl Fn(&T) -> u64) -> Result<f64, Me>;
    fn median_by(
        self,
        c: &mut impl FnMut(&T, &T) -> Ordering
    ) -> Result<Medians<'a, T>, Me>;
    fn zeroed(
        self,
        centre: f64,
        quantify: impl Fn(&T) -> f64
    ) -> Result<Vec<f64>, Me>;
    fn med_correlation(
        self,
        v: Self,
        c: &mut impl FnMut(&T, &T) -> Ordering,
        q: impl Fn(&T) -> f64
    ) -> Result<f64, Me>;
    fn mad(self, centre: f64, quantify: impl Fn(&T) -> f64) -> f64;
}
Expand description

Fast 1D generic medians, plus related methods

Required Methods§

source

fn qmedian_by( self, c: &mut impl FnMut(&T, &T) -> Ordering, q: impl Fn(&T) -> f64 ) -> Result<f64, Me>

Median by comparison c, at the end quantified to a single f64 by q

source

fn uqmedian(self, q: impl Fn(&T) -> u64) -> Result<f64, Me>

Median of types quantifiable to u64 by q, at the end converted to a single f64.
For data that is already u64, use function medianu64

source

fn median_by( self, c: &mut impl FnMut(&T, &T) -> Ordering ) -> Result<Medians<'a, T>, Me>

Median by comparison c, returns odd/even result

source

fn zeroed( self, centre: f64, quantify: impl Fn(&T) -> f64 ) -> Result<Vec<f64>, Me>

Zero mean/median data, produced by subtracting the centre

source

fn med_correlation( self, v: Self, c: &mut impl FnMut(&T, &T) -> Ordering, q: impl Fn(&T) -> f64 ) -> Result<f64, Me>

Median correlation = cosine of an angle between two zero median Vecs

source

fn mad(self, centre: f64, quantify: impl Fn(&T) -> f64) -> f64

Median of absolute differences (MAD).

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<'a, T> Median<'a, T> for &'a [T]

Medians of &[T]

source§

fn qmedian_by( self, c: &mut impl FnMut(&T, &T) -> Ordering, q: impl Fn(&T) -> f64 ) -> Result<f64, Me>

Median of &[T] by comparison c, quantified to a single f64 by q. When T is a primitive type directly convertible to f64, pass in as f64 for q. When f64:From is implemented, pass in |x| x.into() for q. When T is Ord, use |a,b| a.cmp(b) as the comparator closure. In all other cases, use custom closures c and q. When T is not quantifiable at all, use the ultimate median_by method.

source§

fn uqmedian(self, q: impl Fn(&T) -> u64) -> Result<f64, Me>

Median of &[T], quantifiable to u64’s by q. Returns a single f64. When T is a primitive type directly convertible to u64, use as u64 as q. When u64:From is implemented, use |x| x.into() as q. In all other cases, use custom quantification closure q. When T is not quantifiable at all, use the ultimate median_by method.

source§

fn median_by( self, c: &mut impl FnMut(&T, &T) -> Ordering ) -> Result<Medians<'a, T>, Me>

Median(s) of unquantifiable type by general comparison closure

source§

fn zeroed(self, centre: f64, q: impl Fn(&T) -> f64) -> Result<Vec<f64>, Me>

Zero mean/median data produced by subtracting the centre

source§

fn med_correlation( self, v: Self, c: &mut impl FnMut(&T, &T) -> Ordering, q: impl Fn(&T) -> f64 ) -> Result<f64, Me>

We define median based correlation as cosine of an angle between two zero median vectors (analogously to Pearson’s zero mean vectors)

§Example
use medians::{Medianf64,Median};
use core::convert::identity;
use core::cmp::Ordering::*;
let v1 = vec![1_f64,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.,13.,14.];
let v2 = vec![14_f64,1.,13.,2.,12.,3.,11.,4.,10.,5.,9.,6.,8.,7.];
assert_eq!(v1.medf_correlation(&v2).unwrap(),-0.1076923076923077);
assert_eq!(v1.med_correlation(&v2,&mut |a,b| a.total_cmp(b),|&a| identity(a)).unwrap(),-0.1076923076923077);
source§

fn mad(self, centre: f64, q: impl Fn(&T) -> f64) -> f64

Data dispersion estimator MAD (Median of Absolute Differences). MAD is more stable than standard deviation and more general than quartiles. When argument centre is the median, it is the most stable measure of data dispersion.

Implementors§