Skip to main content

Stats

Trait Stats 

Source
pub trait Stats<T> {
Show 15 methods // Required methods fn mean(&self) -> StatsResult<T>; fn median(&self) -> StatsResult<T>; fn mode(&self) -> StatsResult<Vec<T>>; fn variance(&self) -> StatsResult<T>; fn std_dev(&self) -> StatsResult<T>; fn variance_pop(&self) -> StatsResult<T>; fn std_dev_pop(&self) -> StatsResult<T>; fn min(&self) -> StatsResult<T>; fn max(&self) -> StatsResult<T>; fn range(&self) -> StatsResult<T>; fn sum(&self) -> StatsResult<T>; fn percentile(&self, p: f64) -> StatsResult<T>; fn iqr(&self) -> StatsResult<T>; // Provided methods fn quartile_1(&self) -> StatsResult<T> { ... } fn quartile_3(&self) -> StatsResult<T> { ... }
}
Expand description

Main trait providing statistical functions for collections

Required Methods§

Source

fn mean(&self) -> StatsResult<T>

Calculate the arithmetic mean (average)

Source

fn median(&self) -> StatsResult<T>

Calculate the median (middle value)

Source

fn mode(&self) -> StatsResult<Vec<T>>

Calculate the mode (most frequent value)

Source

fn variance(&self) -> StatsResult<T>

Calculate the variance

Source

fn std_dev(&self) -> StatsResult<T>

Calculate the standard deviation

Source

fn variance_pop(&self) -> StatsResult<T>

Calculate the population variance

Source

fn std_dev_pop(&self) -> StatsResult<T>

Calculate the population standard deviation

Source

fn min(&self) -> StatsResult<T>

Find the minimum value

Source

fn max(&self) -> StatsResult<T>

Find the maximum value

Source

fn range(&self) -> StatsResult<T>

Calculate the range (max - min)

Source

fn sum(&self) -> StatsResult<T>

Calculate the sum of all values

Source

fn percentile(&self, p: f64) -> StatsResult<T>

Calculate a percentile

Source

fn iqr(&self) -> StatsResult<T>

Calculate the interquartile range (Q3 - Q1)

Provided Methods§

Source

fn quartile_1(&self) -> StatsResult<T>

Calculate the first quartile (25th percentile)

Examples found in repository?
examples/basic_usage.rs (line 22)
3fn main() {
4    println!("=== Statify Statistics Library Demo ===\n");
5
6    // Basic descriptive statistics
7    let data = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
8    
9    println!("Dataset: {:?}\n", data);
10    println!("Descriptive Statistics:");
11    println!("  Mean: {:.2}", data.mean().unwrap());
12    println!("  Median: {:.2}", data.median().unwrap());
13    println!("  Std Dev: {:.2}", data.std_dev().unwrap());
14    println!("  Variance: {:.2}", data.variance().unwrap());
15    println!("  Min: {:.2}", data.min().unwrap());
16    println!("  Max: {:.2}", data.max().unwrap());
17    println!("  Range: {:.2}", data.range().unwrap());
18    println!("  Sum: {:.2}", data.sum().unwrap());
19
20    // Percentiles and quartiles
21    println!("\nPercentiles:");
22    println!("  25th percentile (Q1): {:.2}", data.quartile_1().unwrap());
23    println!("  50th percentile (Median): {:.2}", data.percentile(50.0).unwrap());
24    println!("  75th percentile (Q3): {:.2}", data.quartile_3().unwrap());
25    println!("  IQR: {:.2}", data.iqr().unwrap());
26
27    // Correlation and covariance
28    let x = vec![1.0, 2.0, 3.0, 4.0, 5.0];
29    let y = vec![2.0, 4.0, 6.0, 8.0, 10.0];
30    
31    println!("\nCorrelation Analysis:");
32    println!("  X: {:?}", x);
33    println!("  Y: {:?}", y);
34    println!("  Correlation: {:.4}", correlation(&x, &y).unwrap());
35    println!("  Covariance: {:.4}", covariance(&x, &y).unwrap());
36
37    // Z-scores
38    let sample = vec![10.0, 20.0, 30.0, 40.0, 50.0];
39    let z_vals = z_scores(&sample).unwrap();
40    
41    println!("\nZ-Scores:");
42    println!("  Data: {:?}", sample);
43    println!("  Z-scores: {:?}", z_vals.iter().map(|&x| format!("{:.2}", x)).collect::<Vec<_>>());
44
45    // Real-world example: test scores
46    let test_scores = vec![
47        85.0, 92.0, 78.0, 95.0, 88.0, 76.0, 89.0, 94.0, 81.0, 87.0,
48    ];
49    
50    println!("\n=== Real-World Example: Test Scores ===");
51    println!("Scores: {:?}", test_scores);
52    println!("  Average Score: {:.2}", test_scores.mean().unwrap());
53    println!("  Median Score: {:.2}", test_scores.median().unwrap());
54    println!("  Std Deviation: {:.2}", test_scores.std_dev().unwrap());
55    println!("  Lowest Score: {:.2}", test_scores.min().unwrap());
56    println!("  Highest Score: {:.2}", test_scores.max().unwrap());
57    println!("  Score Range: {:.2}", test_scores.range().unwrap());
58}
Source

fn quartile_3(&self) -> StatsResult<T>

Calculate the third quartile (75th percentile)

Examples found in repository?
examples/basic_usage.rs (line 24)
3fn main() {
4    println!("=== Statify Statistics Library Demo ===\n");
5
6    // Basic descriptive statistics
7    let data = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
8    
9    println!("Dataset: {:?}\n", data);
10    println!("Descriptive Statistics:");
11    println!("  Mean: {:.2}", data.mean().unwrap());
12    println!("  Median: {:.2}", data.median().unwrap());
13    println!("  Std Dev: {:.2}", data.std_dev().unwrap());
14    println!("  Variance: {:.2}", data.variance().unwrap());
15    println!("  Min: {:.2}", data.min().unwrap());
16    println!("  Max: {:.2}", data.max().unwrap());
17    println!("  Range: {:.2}", data.range().unwrap());
18    println!("  Sum: {:.2}", data.sum().unwrap());
19
20    // Percentiles and quartiles
21    println!("\nPercentiles:");
22    println!("  25th percentile (Q1): {:.2}", data.quartile_1().unwrap());
23    println!("  50th percentile (Median): {:.2}", data.percentile(50.0).unwrap());
24    println!("  75th percentile (Q3): {:.2}", data.quartile_3().unwrap());
25    println!("  IQR: {:.2}", data.iqr().unwrap());
26
27    // Correlation and covariance
28    let x = vec![1.0, 2.0, 3.0, 4.0, 5.0];
29    let y = vec![2.0, 4.0, 6.0, 8.0, 10.0];
30    
31    println!("\nCorrelation Analysis:");
32    println!("  X: {:?}", x);
33    println!("  Y: {:?}", y);
34    println!("  Correlation: {:.4}", correlation(&x, &y).unwrap());
35    println!("  Covariance: {:.4}", covariance(&x, &y).unwrap());
36
37    // Z-scores
38    let sample = vec![10.0, 20.0, 30.0, 40.0, 50.0];
39    let z_vals = z_scores(&sample).unwrap();
40    
41    println!("\nZ-Scores:");
42    println!("  Data: {:?}", sample);
43    println!("  Z-scores: {:?}", z_vals.iter().map(|&x| format!("{:.2}", x)).collect::<Vec<_>>());
44
45    // Real-world example: test scores
46    let test_scores = vec![
47        85.0, 92.0, 78.0, 95.0, 88.0, 76.0, 89.0, 94.0, 81.0, 87.0,
48    ];
49    
50    println!("\n=== Real-World Example: Test Scores ===");
51    println!("Scores: {:?}", test_scores);
52    println!("  Average Score: {:.2}", test_scores.mean().unwrap());
53    println!("  Median Score: {:.2}", test_scores.median().unwrap());
54    println!("  Std Deviation: {:.2}", test_scores.std_dev().unwrap());
55    println!("  Lowest Score: {:.2}", test_scores.min().unwrap());
56    println!("  Highest Score: {:.2}", test_scores.max().unwrap());
57    println!("  Score Range: {:.2}", test_scores.range().unwrap());
58}

Implementations on Foreign Types§

Source§

impl Stats<f32> for Vec<f32>

Source§

impl Stats<f64> for Vec<f64>

Implementors§