Rstats

GitHub last commit crates.io crates.io docs.rs

Usage

Insert rstats = "^1" in the Cargo.toml file, under [dependencies].

Use in source files any of the following structs, as needed:
use rstats::{MinMax,Med,Mstats};
and any of the following helper functions:
use rstats::{i64tof64,tof64,here,wi,wv,wsum,printvv,genvec,genvecu8};
and any of the following traits:
use rstats::{Stats,MutStats,Vecu8,Vecf64,Vecg,MutVecg,VecVec,VecVecg};

It is highly recommended to read and run tests/tests.rs, which shows examples of usage.

To run all the tests, use single thread in order to produce the results in the right order:
cargo test --release -- --test-threads=1 --nocapture --color always

Introduction

Rstats is primarily about characterising multidimensional sets of points, with applications to Machine Learning and Big Data Analysis. It uses non analytical statistics, where the 'random variables' are replaced by vectors of real data. Probabilities densities and other parameters are always obtained from the data, not from some assumed distributions.

This crate begins with basic statistical measures and vector algebra, which provide self-contained tools for the multidimensional algorithms but can also be used in their own right.

Our treatment of multidimensional sets of points (vectors) is constructed from the first principles. Some original concepts, not found elsewhere, are introduced and implemented here:

Zero median vectors are generally preferable to the commonly used zero mean vectors.

In n dimensions, many authors 'cheat' by using quasi medians (1-d medians along each axis). Quasi medians are a poor start to stable characterisation of multidimensional data. In a highly dimensional space, they are not even any faster to compute.

Specifically, all 1-d measures are sensitive to the choice of axis and thus are affected by rotation.

In contrast, analyses based on the true geometric median (gm) are axis (rotation) independent. Also, they are more stable, as medians have a 50% breakdown point (the maximum possible). They are computed here by gmedian and its weighted version wgmedian.

Implementation

The main constituent parts of Rstats are its traits. The selection of traits (to import) is primarily determined by the types of objects to be handled. These are mostly vectors of arbitrary length (dimensionality). The main traits are implementing methods applicable to a single vector (of numbers) - Stats, methods (of vector algebra) for two vectors - Vecg, methods for n vectors - VecVec, and methods for n vectors with another generic argument - VecVecg.

In other words, the traits and their methods operate on arguments of their required categories. In classical statistical parlance, the main categories correspond to the number of 'random variables'. However, the vectors' end types (for the actual data) are mostly generic: usually some numeric type. There are also some traits specialised for input end types f64 and u8 and some that take mutable self. End type f64 is most commonly used for the results.

Documentation

For more detailed comments, plus some examples, see the source. You may have to unclick the 'implementations on foreign types' somewhere near the bottom of the page in the rust docs to get to it.

Structs and auxiliary functions

Trait Stats

One dimensional statistical measures implemented for all numeric end types.

Its methods operate on one slice of generic data and take no arguments. For example, s.amean() returns the arithmetic mean of the data in slice s. Some of these methods are checked and will report all kinds of errors, such as an empty input. This means you have to call .unwrap() or something better on their results.

Included in this trait are:

Trait MutStats

A few of the Stats methods are reimplemented under this trait (only for f64), so that they mutate self in-place. This is more efficient and convenient in some circumstances, such as in vector iterative methods.

Trait Vecg

Vector algebra operations between two slices &[T], &[U] of any length (dimensionality):

This trait is unchecked (for speed), so some caution with data is advisable.

Trait Vecf64

A handful of methods from Vecg, specialised to an argument of known end type f64 or &[f64].

Traits MutVecg & MutVecf64

Mutable vector addition, subtraction and multiplication.
Mutate self in-place. This is for efficiency and convenience. Specifically, in vector iterative methods.

MutVecf64 is to be used in preference, when the end type of self is known to be f64. Beware that these methods work by side-effect and do not return anything, so they can not be functionally chained.

Trait Vecu8

Some vector algebra as above that can be more efficient when the end type happens to be u8 (bytes). They have u8 appended to their names to avoid confusion with Vecg methods.

Trait VecVec

Relationships between n vectors (in d dimensions). This is the main original contribution of this library. True geometric median is found by fast and stable iteration, using improved Weiszfeld's algorithm gmedian, optionally boosted by a secant method smedian. These algorithms both solve Weiszfeld's convergence and stability problems in the neighbourhood of existing set points.

Trait VecVec is entirely unchecked, so check your data upfront.

Trait VecVecg

Methods which take an additional generic vector argument, such as a vector of weights for computing the weighted geometric medians.

Appendix I: Terminology (and some new definitions) for sets of nD points

Appendix II: Recent Releases