compute

Crates.io Documentation

A crate for scientific and statistical computing. For a list of what this crate provides, see FEATURES.md. For more detailed explanations, see the documentation.

To use the latest stable version in your Rust program, add the following to your Cargo.toml file:

rust // Cargo.toml [dependencies] compute = "0.1"

For the latest version, add the following to your Cargo.toml file: rust [dependencies] compute = { git = "https://github.com/al-jshen/compute" }

There are many functions which rely on linear algebra methods. You can either use the provided Rust methods (default), or use BLAS and/or LAPACK. To do so, activate the "blas" and/or the "lapack" feature flags in Cargo.toml:

rust // example with BLAS only compute = {version = "0.1", features = ["blas"]}

Examples

Statistical distributions

```rust use compute::distributions::*;

let beta = Beta::new(2., 2.); let betadata: Vec = b.sample_vec(1000); // vector of 1000 variates

println!("{}", beta.mean()); println!("{}", beta.var()); println!("{}", beta.pdf(0.5)); // probability distribution function

let binom = Binomial::new(4, 0.5);

println!("{}", p.sample()); // sample single value println!("{}", p.pmf(2)); // probability mass function ```

Polynomial Regression and GLMs

```rust use compute::prelude::*;

let x = vec![1., 2., 3., 4.]; let xd = design(&x, x.len()); // make a design matrix let y = vec![3., 5., 7., 9.];

let mut clf = PolynomialRegressor::new(2); // degree 2 (i.e. quadratic) clf.fit(&x, &y); // linear least squares fitting println!("{:?}", clf.coef); // get model coefficients

let ybin = vec![0., 0., 1., 1.]; let mut glm = GLM::new(ExponentialFamily::Bernoulli); // logistic regression glm.setpenalty(1.); // ridge regression glm.fit(&xd, &y, 25).unwrap(); // fit with scoring algorithm (MLE), cap iterations at 25 glm.coef().unwrap() // get estimated coefficients glm.coef().coefcovariancematrix().unwrap() // get covariance matrix for estimated coefficients ```

Time series models

```rust use compute::timeseries::*;

let x = vec![-2.582184,-3.44017,-1.979827,-0.268826,1.162776,0.9260983,-1.075229,0.7232999,0.9659502,0.2425384];

let mut ar = AR::new(1); // AR(1) model ar.fit(&x); // fit model with Yule-Walker equations println!("{:?}", ar.coeffs); // get model coefficients println!("{:?}", ar.predict(&x, 5)); // forecast 5 steps ahead ```

Numerical integration

```rust use compute::integrate::*;

let f = |x: f64| x.sqrt() + x.sin() - (3. * x).cos() - x.powi(2); println!("{}", trapz(f, 0., 1., 100)); // trapezoid integration with 100 segments println!("{}", quad5(f, 0., 1.)); // gaussian quadrature integration println!("{}", romberg(f, 0., 1., 1e-8, 10)); // romberg integration with tolerance and max steps ```

Signal processing

```rust use compute::signal::*;

let sig = vec![0.973, 0.361, 0.715, -1.158, 1.392, 0.415, 2.304, -0.844, 0.805, 0.242]; let smoothed = savitzky_golay(&sig, 5, 3) // LOESS, cubic smoothing with 5 points let impulse = delta(5, 0.5); // delta function with 5 steps and dt = 0.5 let res = convolve(&sig, &impulse, 0.5); // impulse response of signal ```

Linear algebra

```rust use compute::linalg::*;

let x = vec![ 2., 3., 4., 5., ]; let y = vec![ 5., 2., 6., 1., ]; println!("{:?}", lu(&x)); // calculate LU decomposition for x println!("{:?}", solve_sys(&x, &y)); // solve 2x2 system of equations (each column of y is a system) println!("{:?}", matmul(&x, &y, 2, 2, false, true)); // matrix multiply, transposing y ```

Data summary functions

```rust use compute::statistics::*;

let x = vec![2.2, 3.4, 5., 10., -2.1, 0.1]; let y = vec![1., 2., -2., 5.7, -0.7, 5.7];

println!("{}", mean(&x)); println!("{}", var(&x)); println!("{}", max(&y)); println!("{}", sample_std(&y)); println!("{}", covariance(&x, &y)); ```

Mathematical and statistical functions

```rust use compute::functions::*;

println!("{}", logistic(4.)); println!("{}", boxcox(5., 2.); // boxcox transform println!("{}", digamma(2.)); println!("{}", binom_coeff(10, 4)); // n choose k ```