mirror of
https://github.com/Magnus167/rustframe.git
synced 2025-08-19 20:19:59 +00:00
Add more statistical routines and examples
This commit is contained in:
parent
ecd06eb352
commit
1a9f397702
@ -1,30 +1,54 @@
|
||||
# Compute Features
|
||||
|
||||
The `compute` module provides statistical routines like descriptive
|
||||
statistics and correlation measures.
|
||||
The `compute` module hosts numerical routines for exploratory data analysis.
|
||||
It covers descriptive statistics, correlations, probability distributions and
|
||||
some basic inferential tests.
|
||||
|
||||
## Basic Statistics
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::stats::{mean, stddev};
|
||||
use rustframe::compute::stats::{mean, mean_vertical, stddev, median};
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
let m = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||
let mean_val = mean(&m);
|
||||
let std_val = stddev(&m);
|
||||
assert_eq!(mean(&m), 2.5);
|
||||
assert_eq!(stddev(&m), 1.118033988749895);
|
||||
assert_eq!(median(&m), 2.5);
|
||||
// column averages returned as 1 x n matrix
|
||||
let col_means = mean_vertical(&m);
|
||||
assert_eq!(col_means.data(), &[1.5, 3.5]);
|
||||
```
|
||||
|
||||
## Correlation
|
||||
|
||||
Correlation functions help measure linear relationships between datasets.
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::stats::pearson;
|
||||
use rustframe::compute::stats::{pearson, covariance};
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
let x = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||
let y = Matrix::from_vec(vec![2.0, 4.0, 6.0, 8.0], 2, 2);
|
||||
let corr = pearson(&x, &y);
|
||||
let cov = covariance(&x, &y);
|
||||
assert!((corr - 1.0).abs() < 1e-8);
|
||||
assert!((cov - 2.5).abs() < 1e-8);
|
||||
```
|
||||
|
||||
## Distributions
|
||||
|
||||
Probability distribution helpers are available for common PDFs and CDFs.
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::stats::distributions::normal_pdf;
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
let x = Matrix::from_vec(vec![0.0, 1.0], 1, 2);
|
||||
let pdf = normal_pdf(x, 0.0, 1.0);
|
||||
assert_eq!(pdf.data().len(), 2);
|
||||
```
|
||||
|
||||
With the basics covered, explore predictive models in the
|
||||
|
Loading…
x
Reference in New Issue
Block a user