Function std_dev

Source
pub fn std_dev<T>(data: &[T]) -> Option<f64>
where T: ToPrimitive + Debug,
Expand description

Calculate the standard deviation of a dataset.

The standard deviation is a measure of the amount of variation or dispersion of a set of values. It is calculated as the square root of the variance.

§Arguments

  • data - A slice of numeric values implementing ToPrimitive

§Returns

  • Some(f64) - The standard deviation as a f64 if the input slice is non-empty
  • None - If the input slice is empty

§Errors

Returns None if:

  • The input slice is empty
  • Any value cannot be converted to f64

§Examples

use rs_stats::prob::std_dev;

// Calculate standard deviation of integers
let int_data = [1, 2, 3, 4, 5];
let sd = std_dev(&int_data).unwrap();
println!("Standard deviation of integers: {}", sd);

// Calculate standard deviation of floats
let float_data = [1.0, 2.5, 3.0, 4.5, 5.0];
let sd = std_dev(&float_data).unwrap();
println!("Standard deviation of floats: {}", sd);

// Handle empty input
let empty_data: &[i32] = &[];
assert!(std_dev(empty_data).is_none());