Rstats

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

Statistics, Linear Algebra, Information Measures, Cholesky Matrix Decomposition, Mahalanobis Distance, Multidimensional Data Analysis, Machine Learning and more ...

Usage

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

Use in your source files any of the following structs, when needed:

rust use rstats::{RE,Mstats,MinMax,F64,Med};

and any of the following rstats defined traits:

rust use rstats::{Stats,Vecg,Vecu8,MutVecg,VecVec,VecVecg};

The latest (nightly) version is always available in the github repository rstats. Sometimes it may be a little ahead of the crates.io release versions.

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:

bash 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. Several branches of mathematics: statistics, linear algebra, information theory and set theory are combined in this one consistent package, based on the fact that they all operate on the same objects: Vecs of data items.

RStats begins with basic statistical measures and vector algebra, which provide self-contained basic tools for the machine learning (ML) multidimensional (nd) algorithms but can also be used in their own right. Non analytical statistics is used, whereby 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.

Our treatment of multidimensional sets of points (nd 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 (nd), 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 even much slower to compute than our gm.

Specifically, all such 1d measures are sensitive to the choice of axis and thus are affected by their 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 methods gmedian and its weighted version wgmedian, in traits vecvec and vecvecg respectively.

Terminology

Including some new definitions for sets of nd points, i.e. n points in d dimensional space

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:

In other words, the traits and their methods operate on arguments of their required categories. In classical statistical terminology, the main categories correspond to the number of 'random variables'.

The vectors' end types (for the actual data) are mostly generic: usually some numeric type. There are also some traits specialised for input end type u8 and some that take mutable self. End type f64 is most commonly used for the results.

Errors

RStats crate produces custom errors RError:

rust pub enum RError<T> where T:Sized+Debug { /// Insufficient data NoDataError(T), /// Wrong kind/size of data DataError(T), /// Invalid result, such as prevented division by zero ArithError(T), /// Other error converted to RError OtherError(T) } Each of its enum variants also carries a generic payload T. Most commonly this will be simply a &'static str message giving more specific information, e.g.:

rust return Err(RError::ArithError("cholesky needs a positive definite matrix"));

There is a type alias shortening return declarations to, e.g.: Result<Vec<f64>,RE>, where

rust pub type RE = RError<&'static str>;

More error checking will be added in later versions, where it makes sense.

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. (Since these traits are implemented over the pre-existing Rust Vec type).

Struct

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. These methods are checked and will report RError(s), such as an empty input. This means you have to apply ? to their results to pass the errors up, or explicitly match them to take recovery actions.

Included in this trait are:

Note that fast implementation of 1d medians is as of version 1.1.0 in crate medians.

Trait Vecg

Generic vector algebra operations between two slices &[T], &[U] of any length (dimensionality). It may be necessary to invoke some using the 'turbofish' ::<type> syntax to indicate the type U of the supplied argument, e.g.:
datavec.as_slice().methodname::<f64>(arg)
This is because Rust is currently incapable of inferring the type ('the inference bug').

The simpler methods of this trait are sometimes unchecked (for speed), so some caution with data is advisable.

Trait MutVecg

A select few of the Stats and Vecg methods (e.g. mutable vector addition, subtraction and multiplication) are reimplemented under this trait, so that they can mutate self in-place. This is more efficient and convenient in some circumstances, such as in vector iterative methods.

Trait Vecu8

Some vector algebra as above that can be more efficient when the end type happens to be u8 (bytes). These methods have u8 appended to their names to avoid confusion with Vecg methods. These specific algorithms are different to their generic equivalents in Vecg.

Trait VecVec

Relationships between n vectors (in d dimensions). This general data domain is denoted here as (nd). It is in nd where the main original contribution of this library lies. True geometric median (gm) is found by fast and stable iteration, using improved Weiszfeld's algorithm gmedian. This algorithm solves Weiszfeld's convergence and stability problems in the neighbourhoods of existing set points.

Warning: 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 weighted geometric medians (where each point has its own weight). Matrices multiplications.

Appendix: Recent Releases