statrs/
lib.rs

1//! This crate aims to be a functional port of the Math.NET Numerics
2//! Distribution package and in doing so providing the Rust numerical computing
3//! community with a robust, well-tested statistical distribution package. This
4//! crate also ports over some of the special statistical functions from
5//! Math.NET in so far as they are used in the computation of distribution
6//! values. This crate depends on the `rand` crate to provide RNG.
7//!
8//! # Sampling
9//! The common use case is to set up the distributions and sample from them which depends on the `Rand` crate for random number generation.
10#![cfg_attr(feature = "rand", doc = "```")]
11#![cfg_attr(not(feature = "rand"), doc = "```ignore")]
12//! use statrs::distribution::Exp;
13//! use rand::distributions::Distribution;
14//! let mut r = rand::rngs::OsRng;
15//! let n = Exp::new(0.5).unwrap();
16//! print!("{}", n.sample(&mut r));
17//! ```
18//!
19//! # Introspecting distributions
20//! Statrs also comes with a number of useful utility traits for more detailed introspection of distributions.
21//! ```
22//! use statrs::distribution::{Exp, Continuous, ContinuousCDF}; // `cdf` and `pdf`
23//! use statrs::statistics::Distribution; // statistical moments and entropy
24//!
25//! let n = Exp::new(1.0).unwrap();
26//! assert_eq!(n.mean(), Some(1.0));
27//! assert_eq!(n.variance(), Some(1.0));
28//! assert_eq!(n.entropy(), Some(1.0));
29//! assert_eq!(n.skewness(), Some(2.0));
30//! assert_eq!(n.cdf(1.0), 0.6321205588285576784045);
31//! assert_eq!(n.pdf(1.0), 0.3678794411714423215955);
32//! ```
33//!
34//! # Utility functions
35//! as well as utility functions including `erf`, `gamma`, `ln_gamma`, `beta`, etc.
36//!
37//! ```
38//! use statrs::distribution::FisherSnedecor;
39//! use statrs::statistics::Distribution;
40//!
41//! let n = FisherSnedecor::new(1.0, 1.0).unwrap();
42//! assert!(n.variance().is_none());
43//! ```
44//! ## Distributions implemented
45//! Statrs comes with a number of commonly used distributions including Normal, Gamma, Student's T, Exponential, Weibull, etc. view all implemented in `distributions` module.
46
47#![crate_type = "lib"]
48#![crate_name = "statrs"]
49#![allow(clippy::excessive_precision)]
50#![allow(clippy::many_single_char_names)]
51#![forbid(unsafe_code)]
52#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
53#![cfg_attr(docsrs, feature(doc_cfg))]
54
55#[macro_use]
56extern crate approx;
57
58#[macro_export]
59macro_rules! assert_almost_eq {
60    ($a:expr, $b:expr, $prec:expr $(,)?) => {
61        if !$crate::prec::almost_eq($a, $b, $prec) {
62            panic!(
63                "assertion failed: `abs(left - right) < {:e}`, (left: `{}`, right: `{}`)",
64                $prec, $a, $b
65            );
66        }
67    };
68}
69
70pub mod consts;
71#[macro_use]
72pub mod distribution;
73pub mod euclid;
74pub mod function;
75pub mod generate;
76pub mod prec;
77pub mod statistics;
78pub mod stats_tests;