TDigest

Struct TDigest 

Source
pub struct TDigest { /* private fields */ }
Expand description

The main struct representing a t-digest.

The TDigest struct allows efficient computation of quantiles and supports merging from other t-digests.

§Examples

use tdigests::TDigest;

let values = vec![1.0, 2.0, 3.0];
let digest = TDigest::from_values(values);

let q = digest.estimate_quantile(0.5);
assert_eq!(q, 2.0);

Implementations§

Source§

impl TDigest

Source

pub fn from_values(values: Vec<f64>) -> Self

Creates a t-digest from a list of values.

§Panics

Panics if the input values are empty or if all values are NaN.

§Examples
use tdigests::TDigest;

let values = vec![3.0, 1.0, 2.0];
let digest = TDigest::from_values(values);
Source

pub fn from_centroids(centroids: Vec<Centroid>) -> Self

Creates a t-digest from existing centroids.

This method is useful when working in distributed systems where centroids are generated on partitions of data and need to be merged.

§Panics

Panics if the input centroids is empty or if all centroids have either zero weight or NaN means.

§Examples
use tdigests::Centroid;
use tdigests::TDigest;

let centroids = vec![
    Centroid::new(10.0, 1.0),
    Centroid::new(20.0, 2.0),
    Centroid::new(30.0, 1.0),
];
let digest = TDigest::from_centroids(centroids);
Source

pub fn centroids(&self) -> &[Centroid]

Returns the internal centroids.

§Examples
use tdigests::TDigest;

let values = vec![1.0, 2.0, 3.0];
let digest = TDigest::from_values(values);

let centroids = digest.centroids();
assert_eq!(centroids.len(), 3);
Source

pub fn merge(&self, other: &TDigest) -> TDigest

Merges another t-digest into this one.

§Examples
use tdigests::TDigest;

let values1 = vec![1.0, 2.0, 3.0];
let digest1 = TDigest::from_values(values1);

let values2 = vec![4.0, 5.0, 6.0];
let digest2 = TDigest::from_values(values2);

let merged_digest = digest1.merge(&digest2);
Source

pub fn compress(&mut self, max_centroids: usize)

Compresses the centroids to reduce their number while maintaining accuracy.

This method should be called after adding a large number of points to reduce the memory footprint and improve estimation performance.

§Examples
use tdigests::TDigest;

let values = (0..1000).map(|i| i as f64).collect::<Vec<_>>();
let mut digest = TDigest::from_values(values);

// Compress to have at most 100 centroids
digest.compress(100);

assert!(digest.centroids().len() <= 100);
Source

pub fn estimate_quantile(&self, q: f64) -> f64

Estimates the quantile for a given cumulative probability q.

§Examples
use tdigests::TDigest;

let values = (0..=100).map(|i| i as f64).collect::<Vec<_>>();
let digest = TDigest::from_values(values);
let median = digest.estimate_quantile(0.5);
assert_eq!(median, 50.0);
Source

pub fn estimate_rank(&self, x: f64) -> f64

Compute the relative rank of a given value x, specified as a percentage ranging from 0.0 to 1.0.

Returns a value between 0.0 and 1.0 representing the probability that a random variable is less than or equal to x.

§Examples
use tdigests::TDigest;

let values = (0..=100).map(|i| i as f64).collect::<Vec<_>>();
let digest = TDigest::from_values(values);
let rank = digest.estimate_rank(50.0);
assert_eq!(rank, 0.5);

Trait Implementations§

Source§

impl Clone for TDigest

Source§

fn clone(&self) -> TDigest

Returns a duplicate of the value. Read more
1.0.0§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for TDigest

Source§

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

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

impl PartialEq for TDigest

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for TDigest

Auto Trait Implementations§

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

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

§

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
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

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

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

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

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.