Rust Statistics and Vector Algebra Library

Overview

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 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 (mean vector).

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 quicker 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. They are computed here by the novel methods smedian and gmedian and their weighted versions wsmedian and wgmedian.

Implementation

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

Documentation

To see more detailed comments, plus some examples, see the source.

Structs and auxiliary functions

  • struct Med to hold median and quartiles

  • struct MStats to hold mean and standard deviation

  • struct MinMax reexported from crate indxvec to hold min and max values of a vector and their indices. It is returned by function indxvec::merge::minmax.

  • auxiliary functions: i64tof64, tof64, here, wsum, wi, wv, printvv, genvec, genvecu8.

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

Included in this trait are:

  • means (arithmetic, geometric and harmonic),
  • standard deviations,
  • linearly weighted means (useful for time dependent data analysis),
  • median and quartiles,
  • autocorrelation, entropy
  • linear transformation to [0,1],
  • other measures and vector algebra operators

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):

  • Vector additions, subtractions and products (scalar, kronecker, outer),
  • Other relationships and measures,
  • Pearson's, Spearman's and Kendall's correlations.

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

Trait Vecf64

A handful of primitive methods as in Vecg but operating on 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).
  • Frequency count of bytes by their values (Histogram or Probability Density Function).
  • Entropy measures in units of e (using natural logarithms).

Trait VecVec

Relationships between n vectors (in d dimensions). 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 a secant method. These measures solve Weiszfeld's convergence problems in the vicinity of existing set points.

  • sums of distances, eccentricity (radius) measure,
  • centroid, medoid, outliers, true geometric median,
  • transformation to zero (geometric) median data,
  • trend between two sets of multidimensional vectors,
  • covariance and comediance matrices (weighted and unweighted).

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

  • Centroid\Centre\Mean is the (generally non member) point that minimises the sum of squares of distances to all member points. Thus it is susceptible to outliers. Specifically, it is the n-dimensional arithmetic mean. By drawing physical analogy with gravity, it is sometimes called 'the centre of mass'. Centroid can also sometimes mean the member of the set which is the nearest to the Centre. Here we follow the common (if somewhat confusing) usage: Centroid = Centre = Arithmetic Mean.

  • Quasi\Marginal Median is the point minimising sums of distances separately in each dimension (its coordinates are 1-d medians along each axis). It is a mistaken concept which we do not use here.

  • Tukey Median is the point maximising Tukey's Depth, which is the minimum number of (outlying) points found in a hemisphere in any direction. Potentially useful concept but not yet implemented here, as its advantages over gm are not clear.

  • Medoid is the member of the set with the least sum of distances to all other members.

  • Outlier is the member of the set with the greatest sum of distances to all other members.

  • Median or the true geometric median (gm), is the point (generally non member), which minimises the sum of distances to all members. This is the one we want. It is much less susceptible to outliers than centroid. In addition, unlike quasi median, gm is rotation independent.

  • Zero median vector is obtained by subtracting the geometric median. This is a proposed alternative to the commonly used zero mean vector, obtained by subtracting the centroid.

  • Comediance is similar to covariance, except zero median vectors are used to compute it, instead of zero mean vectors.

Appendix II: Recent Releases

  • Version 1.0.2 Updated the dependency indxvec to version 1. A few minor changes to this document.

  • Version 1.0.1 Minor change: sortedeccs and wsortedeccs now take gm as an argument for more efficient repeated use. Vecvec test improved.

  • Version 1.0.0 Rstats reaches stability (of sorts)! Some code simplifications: smedian and wsmedian are now just slightly more accurate than gmedian and wgmedian respectively, otherwise their performance is very similar. Sometimes a bit slower, at other times, especially on 'difficult' data, they can be a bit faster.

  • Version 0.9.5 Improved smedian. Also added a weighted version of it: wsmedian.

  • Version 0.9.4 Organisation improvements. Added trait Vecf64 and moved into it relevant methods from Vecg. Added a few functions to MutVecf64 trait. Simplified gmedian.

  • Version 0.9.3 Added hwmeanstd - harmonic weighted mean and standard deviation. Tidied up readme badges and some tests. Simplified random number generation. Weights for the weighted means are now ascending (more intuitive).

  • Version 0.9.2 Fixed some tests broken by moved functions. Added harmonic standard deviation, harmonic centroid and more tests.

  • Version 0.9.1 Made the auxiliary functions more visible by moving them to lib.rs (the top level of the crate).

  • Version 0.9.0 Added kron and outer products to Vecg trait.

You might also like...
Peakrs Dataframe is a library and framework facilitates the extraction, transformation, and loading (ETL) of data.

Peakrs Dataframe Peakrs Dataframe is a library and framework facilitates the extraction, transformation, and loading (ETL) of data. Its first applicat

A library that allows for the arbitrary inspection and manipulation of the memory and code of a process on a Linux system.
A library that allows for the arbitrary inspection and manipulation of the memory and code of a process on a Linux system.

raminspect raminspect is a crate that allows for the inspection and manipulation of the memory and code of a running process on a Linux system. It pro

Rust library for ANSI terminal colours and styles (bold, underline)

rust-ansi-term This is a library for controlling colours and formatting, such as red bold text or blue underlined text, on ANSI terminals. View the Ru

Cross-platform Rust library for coloring and formatting terminal output
Cross-platform Rust library for coloring and formatting terminal output

Coloring terminal output Documentation term-painter is a cross-platform (i.e. also non-ANSI terminals) Rust library for coloring and formatting termin

A Rust curses library, supports Unix platforms and Windows

pancurses pancurses is a curses library for Rust that supports both Linux and Windows by abstracting away the backend that it uses (ncurses-rs and pdc

Pure-Rust rewrite of the Linux fontconfig library (no system dependencies) - using ttf-parser and allsorts

rust-fontconfig Pure-Rust rewrite of the Linux fontconfig library (no system dependencies) - using allsorts as a font parser in order to parse .woff,

Rust library to convert RGB 24-bit colors into ANSI 256 (8-bit) color codes with zero dependencies and at compile-time.
Rust library to convert RGB 24-bit colors into ANSI 256 (8-bit) color codes with zero dependencies and at compile-time.

rgb2ansi256 rgb2ansi256 is a small Rust library to convert RGB 24-bit colors into ANSI 256 (8-bit) color codes with zero dependencies and const fn. Th

Vari (Väri) is a Rust library for formatting strings with colors and cosmetic stuff to the terminal.
Vari (Väri) is a Rust library for formatting strings with colors and cosmetic stuff to the terminal.

Vari Vari (Väri) is a Rust library for formatting strings with colors and cosmetic stuff to the terminal. Like Rich library for Python. Väri means "co

Rust library crate providing utility functions for diff and patch of slices

This crate provides the Change enum as an abstraction for diff::Result, lcs_diff::DiffResult, and wu_diff::DiffResult; the diff_changes(), diff_diff()

Owner
null
🛡️ Terminal-based, real-time traffic monitoring and statistics for your AdGuard Home instance

AdGuardian-Term Terminal-based, real-time traffic monitoring and statistics for your AdGuard Home instance About AdGuardian Terminal Eddition - Keep a

Alicia Sykes 629 Jun 14, 2023
Tokei is a program that displays statistics about your code.

Tokei is a program that displays statistics about your code. Tokei will show the number of files, total lines within those files and code, comments, and blanks grouped by language.

null 7.5k Jan 1, 2023
A utility for exporting administrative/moderation statistics from your Lemmy instance's PostgreSQL database to InfluxDB!

Lemmy (Stats) Data Exporter About This Project This project aims to act as a bridge between Lemmy's PostgreSQL database and InfluxDB, primarily to tra

Russell 3 Jul 5, 2023
A concurrent, append-only vector

The vector provided by this crate suports concurrent get and push operations. Reads are always lock-free, as are writes except when resizing is required.

Ibraheem Ahmed 18 Nov 27, 2022
Rust Imaging Library's Python binding: A performant and high-level image processing library for Python written in Rust

ril-py Rust Imaging Library for Python: Python bindings for ril, a performant and high-level image processing library written in Rust. What's this? Th

Cryptex 13 Dec 6, 2022
Tooling and library for generation, validation and verification of supply chain metadata documents and frameworks

Spector Spector is both tooling and a library for the generation, validation and verification of supply chain metadata documents and frameworks. Many

Kusari 13 May 4, 2023
This library provides a convenient derive macro for the standard library's std::error::Error trait.

derive(Error) This library provides a convenient derive macro for the standard library's std::error::Error trait. [dependencies] therror = "1.0" Compi

Sebastian Thiel 5 Oct 23, 2023
A small Rust library that let's you get position and size of the active window on Windows and MacOS

active-win-pos-rs A small Rust library that let's you get position and size of the active window on Windows and MacOS Build % git clone https://github

Dmitry Malkov 21 Jan 6, 2023
Ember is a minimalistic Rust library for creating 2D graphics, games, and interactive visualizations with ease and simplicity.

Ember Ember is a simple and fun 2D rendering library for Rust, allowing you to quickly create graphics and interactive applications with ease. It uses

null 8 May 4, 2023
Utility library for some Lenovo IdeaPad laptops. Supports IdeaPad Intel and AMD Models (15IIL05 and 15ARE05)

ideapad A Rust utility library for some Lenovo IdeaPad specific functionality. A Fair Warning This crate calls raw ACPI methods, which on the best cas

ALinuxPerson 2 Aug 31, 2022