use std::fmt::Display;
pub fn stddev<T: Display + std::fmt::Debug>(array: Vec<T>) -> f64 {
let len = array.len();
if len <= 1 {
return 0.0;
}
let mut total = 0.0;
let mut values = vec![];
for item in array {
let value = item.to_string().parse::<f64>().unwrap();
values.push(value);
total += value;
}
let mean = total / len.to_string().parse::<f64>().unwrap();
total = 0.0;
let _: Vec<f64> = values.iter_mut().map(|val| {
let t = (*val - mean).powi(2);
total += t;
t
}).collect();
(total / (len - 1) as f64).sqrt()
}
pub fn stdevp<T: Display + std::fmt::Debug>(array: Vec<T>) -> f64 {
let len = array.len();
if len <= 1 {
return 0.0;
}
let mut total = 0.0;
let mut values = vec![];
for item in array {
let value = item.to_string().parse::<f64>().unwrap();
values.push(value);
total += value;
}
let mean = total / len.to_string().parse::<f64>().unwrap();
total = 0.0;
let _: Vec<f64> = values.iter_mut().map(|val| {
let t = (*val - mean).powi(2);
total += t;
t
}).collect();
(total / (len) as f64).sqrt()
}
pub fn variance(array: Vec<f64>) -> f64 {
let len = array.len();
if len <= 1 {
return 0.0;
}
let mut total = 0.0;
for item in array.clone() {
total += item;
}
let mean = total / len.to_string().parse::<f64>().unwrap();
total = 0.0;
for item in array {
let t = (item - mean).powi(2);
total += t;
}
total / (len - 1) as f64
}
pub fn variancep(array: Vec<f64>) -> f64 {
let len = array.len();
if len <= 1 {
return 0.0;
}
let mut total = 0.0;
for item in array.clone() {
total += item;
}
let mean = total / len.to_string().parse::<f64>().unwrap();
total = 0.0;
for item in array {
let t = (item - mean).powi(2);
total += t;
}
total / (len) as f64
}