rust-average/src/lib.rs

120 lines
4.1 KiB
Rust
Raw Normal View History

2017-05-29 00:39:33 +02:00
//! This crate provides estimators for statistics on a sequence of numbers. The
//! typical workflow looks like this:
//!
//! 1. If necessary, build your custom estimator using [`concatenate`] or
//! [`define_moments`].
2017-06-21 19:34:51 +02:00
//! 2. Initialize the estimator of your choice with `new()`.
//! 3. Add some subset (called "sample") of the sequence of numbers (called
2017-05-29 00:39:33 +02:00
//! "population") for which you want to estimate the statistic, using `add()`
2017-05-19 17:53:54 +02:00
//! or `collect()`.
2017-06-21 19:34:51 +02:00
//! 4. Calculate the statistic with `mean()` or similar.
2017-05-19 17:53:54 +02:00
//!
//! You can run several estimators in parallel and merge them into one with
//! `merge()`.
//!
2017-05-24 19:45:58 +02:00
//! Everything is calculated iteratively in a single pass using constant memory,
//! so the sequence of numbers can be an iterator. The used algorithms try to
//! avoid numerical instabilities.
2018-07-06 10:52:19 +02:00
//!
2017-12-18 18:14:24 +01:00
//! If you want [Serde](https://github.com/serde-rs/serde) support,
2018-07-27 16:53:27 +02:00
//! include `"serde1"` in your list of features.
2017-12-18 18:14:24 +01:00
//!
2017-12-20 14:55:09 +01:00
//! Note that deserializing does not currently check for all invalid inputs.
//! For example, if you deserialize a corrupted [`Variance`] it may return
2017-12-18 18:14:24 +01:00
//! a negative value for variance, even though that is mathematically impossible.
2017-12-20 14:55:09 +01:00
//! In a future minor release some of these checks may be added.
2017-05-24 19:45:58 +02:00
//!
2017-05-29 00:39:33 +02:00
//!
2017-05-29 00:58:25 +02:00
//! ### Example
//!
//! ```
//! use average::{MeanWithError, Estimate};
2017-05-29 00:58:25 +02:00
//!
//! let mut a: MeanWithError = (1..6).map(f64::from).collect();
2017-05-29 00:58:25 +02:00
//! a.add(42.);
//! println!("The mean is {} ± {}.", a.mean(), a.error());
//! ```
//!
//!
2017-05-29 00:39:33 +02:00
//! ## Estimators
//!
//! * Mean ([`Mean`]) and its error ([`MeanWithError`]).
//! * Weighted mean ([`WeightedMean`]) and its error
//! ([`WeightedMeanWithError`]).
//! * Variance ([`Variance`]), skewness ([`Skewness`]) and kurtosis
//! ([`Kurtosis`]).
//! * Arbitrary higher moments ([`define_moments`]).
2017-05-29 00:39:33 +02:00
//! * Quantiles ([`Quantile`]).
//! * Minimum ([`Min`]) and maximum ([`Max`]).
//!
2018-07-06 10:52:19 +02:00
//!
2017-05-29 00:39:33 +02:00
//! ## Estimating several statistics at once
//!
//! The estimators are designed to have minimal state. The recommended way to
//! calculate several of them at once is to create a struct with all the
//! estimators you need. You can then implement `add` for your struct by
//! forwarding to the underlying estimators. Everything is inlined, so there
//! should be no overhead.
//!
//! You can avoid the boilerplate code by using the [`concatenate`] macro.
2017-05-29 00:39:33 +02:00
//!
//! Note that calculating moments requires calculating the lower moments, so you
//! only need to include the highest moment in your struct.
//!
2017-05-22 18:47:46 +02:00
//!
2018-07-06 10:52:19 +02:00
//! ## Calculating histograms
//!
//! The [`define_histogram`] macro can be used to define a histogram struct that
//! uses constant memory. See [`Histogram10`] (defined using
//! `define_histogram!(..., 10)`) and the extension trait [`Histogram`]
//! for the methods available to the generated struct.
2018-07-06 10:52:19 +02:00
//!
//!
//! [`Mean`]: ./struct.Mean.html
//! [`MeanWithError`]: ./type.MeanWithError.html
//! [`WeightedMean`]: ./struct.WeightedMean.html
//! [`WeightedMeanWithError`]: ./struct.WeightedMeanWithError.html
//! [`Variance`]: ./struct.Variance.html
//! [`Skewness`]: ./struct.Skewness.html
//! [`Kurtosis`]: ./struct.Kurtosis.html
//! [`Quantile`]: ./struct.Quantile.html
//! [`Min`]: ./struct.Min.html
//! [`Max`]: ./struct.Max.html
//! [`concatenate`]: ./macro.concatenate.html
//! [`define_moments`]: ./macro.define_moments.html
2018-07-06 10:52:19 +02:00
//! [`define_histogram`]: ./macro.define_histogram.html
//! [`Histogram10`]: ./struct.Histogram10.html
//! [`Histogram`]: ./trait.Histogram.html
2017-05-19 17:53:54 +02:00
#![cfg_attr(feature = "cargo-clippy", allow(float_cmp))]
2017-05-29 00:58:25 +02:00
#![no_std]
2017-04-26 20:43:00 +02:00
2019-07-08 16:04:59 +02:00
#[cfg(feature = "serde1")]
2017-12-18 04:11:11 +01:00
extern crate serde;
#[cfg(feature = "serde1")]
#[macro_use] extern crate serde_derive;
#[cfg(feature = "serde1")]
#[macro_use] extern crate serde_big_array;
2019-07-08 16:04:59 +02:00
2017-04-26 20:24:20 +02:00
2017-05-05 17:42:21 +02:00
#[macro_use] mod macros;
#[macro_use] mod moments;
2017-05-28 21:13:47 +02:00
mod weighted_mean;
2017-05-24 17:29:20 +02:00
mod minmax;
2017-05-25 15:29:21 +02:00
mod quantile;
2017-06-23 17:43:42 +02:00
mod traits;
#[macro_use] mod histogram;
2017-04-26 20:24:20 +02:00
2019-07-08 16:04:19 +02:00
pub use crate::moments::{Mean, Variance, Skewness, Kurtosis, MeanWithError};
pub use crate::weighted_mean::{WeightedMean, WeightedMeanWithError};
pub use crate::minmax::{Min, Max};
pub use crate::quantile::Quantile;
pub use crate::traits::{Estimate, Merge, Histogram};
define_histogram!(hist, 10);
2019-07-08 16:04:19 +02:00
pub use crate::hist::Histogram as Histogram10;
define_moments!(Moments4, 4);