Rstats

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

Usage

Insert in your Cargo.toml file under [dependencies] rstats = "^0.9" and in your source file(s) use rstats:: followed by any of these functions and traits that you need: {functions, Stats, MutStats, Vecu8, Vecg, MutVecg, VecVec, VecVecg };

Introduction

Rstats is primarily about characterising multidimensional sets of points, with applications to Machine Learning and Big Data Analysis. It 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 is constructed from the first principles. Some original concepts, not found elsewhere, are introduced and implemented here. Specifically, the new multidimensional (geometric) median algorithm. Also, the comediance matrix as a replacement for the covariance matrix. It is obtained simply by supplying covar with the geometric median instead of the usual centroid.

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

Most 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 easier to compute.

Specifically, all such 1-d measures are sensitive to the choice of axis (are affected by rotation).

In contrast, our methods, based on the true geometric median (gm), computed here by novel gmedian and wgmedian, are axis (rotation) independent.

Implementation

The main constituent parts of Rstats are Rust traits grouping together methods applicable to a single vector of numbers (Stats), two vectors (Vecg), or n vectors (VecVec and VecVecg). End type f64 is most commonly used for the results, whereas the inputs to the generic methods can be vectors (or slices) of any numeric end types.

Documentation

To see more detailed comments, plus some examples in the implementation files, scroll to the bottom of the trait and unclick [+] to the left of the implementations of the trait. To see the tests, consult tests/tests.rs. The tests also serve as simple usage examples.

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

Structs and auxiliary functions

Traits

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 similar on their results.

Included in this trait are:

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.

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.

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.

Vecu8

VecVec

Relationships between n vectors (nD). This is the original contribution of this library. True geometric median is found by fast and stable iteration, using improved Weiszfeld's algorithm boosted by multidimensional secant method.

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

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