Skip to content

Commit

Permalink
Edit kurtosis, std, and variance functions source code
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-saad-la committed Jul 1, 2024
1 parent e3a2fad commit b6c73bc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion rustapps/advanced_calculator/src/stats/kurtosis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::collections::HashMap;
/// use advanced_calculator::stats::kurtosis::kurtosis;
/// let data = [1.0, 2.0, 3.0, 4.0, 5.0];
/// let result = kurtosis(&data);
/// assert!((result - 1.7).abs() < 0.1);
/// assert!((result - 1.7).abs() < 0.1); // approximate value
/// ```
pub fn kurtosis(data: &[f64]) -> f64 {
let mean = mean::mean(data);
Expand Down
26 changes: 15 additions & 11 deletions rustapps/advanced_calculator/src/stats/std.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unused_imports)]
use crate::stats::{mean, variance};
/// Calculates the standard deviation of a slice of numbers.
///
/// # Arguments
Expand All @@ -17,15 +19,17 @@
/// assert_eq!(result, 1.5811388300841898);
/// ```
pub fn std(data: &[f64]) -> f64 {
let mean = super::mean::mean(data);
let variance = data
.iter()
.map(|value| {
let diff = mean - *value;
diff * diff
})
.sum::<f64>()
/ data.len() as f64;

variance.sqrt()
let var = variance::variance(data);
var.sqrt()
}

// #[cfg(test)]
// mod tests {
// use super::*;

// #[test]
// fn test_std() {
// let data = [1.0, 2.0, 3.0, 4.0, 5.0];
// assert!((std(&data) - 1.5811388300841898).abs() < 1e-10); // approximate value
// }
// }
11 changes: 11 additions & 0 deletions rustapps/advanced_calculator/src/stats/variance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,14 @@ pub fn variance(data: &[f64]) -> f64 {
.sum::<f64>()
/ data.len() as f64
}

// #[cfg(test)]
// mod tests {
// use super::*;

// #[test]
// fn test_variance() {
// let data = [1.0, 2.0, 3.0, 4.0, 5.0];
// assert!((variance(&data) - 2.5).abs() < 1e-10); // approximate value
// }
// }

0 comments on commit b6c73bc

Please sign in to comment.