1use std::fmt::Display;
2
3pub fn stdev<T: Display + std::fmt::Debug>(array: Vec<T>) -> f64 {
5 let len = array.len();
6 if len <= 1 {
7 return 0.0;
8 }
9 let mut total = 0.0;
10 let mut values = vec![];
11 for item in array {
12 let value = item.to_string().parse::<f64>().unwrap();
13 values.push(value);
14 total += value;
15 }
16 let mean = total / len.to_string().parse::<f64>().unwrap();
17 total = 0.0;
18 let _: Vec<f64> = values.iter_mut().map(|val| {
19 let t = (*val - mean).powi(2);
20 total += t;
21 t
22 }).collect();
23 (total / (len - 1) as f64).sqrt()
24}
25
26pub fn stdevp<T: Display + std::fmt::Debug>(array: Vec<T>) -> f64 {
28 let len = array.len();
29 if len <= 1 {
30 return 0.0;
31 }
32 let mut total = 0.0;
33 let mut values = vec![];
34 for item in array {
35 let value = item.to_string().parse::<f64>().unwrap();
36 values.push(value);
37 total += value;
38 }
39 let mean = total / len.to_string().parse::<f64>().unwrap();
40 total = 0.0;
41 let _: Vec<f64> = values.iter_mut().map(|val| {
42 let t = (*val - mean).powi(2);
43 total += t;
44 t
45 }).collect();
46 (total / (len) as f64).sqrt()
47}
48
49pub fn variance(array: Vec<f64>) -> f64 {
51 let len = array.len();
52 if len <= 1 {
53 return 0.0;
54 }
55 let mut total = 0.0;
56 for item in array.clone() {
57 total += item;
58 }
59 let mean = total / len.to_string().parse::<f64>().unwrap();
60 total = 0.0;
61 for item in array {
62 let t = (item - mean).powi(2);
63 total += t;
64 }
65 total / (len - 1) as f64
66}
67
68pub fn variancep(array: Vec<f64>) -> f64 {
70 let len = array.len();
71 if len <= 1 {
72 return 0.0;
73 }
74 let mut total = 0.0;
75 for item in array.clone() {
76 total += item;
77 }
78 let mean = total / len.to_string().parse::<f64>().unwrap();
79 total = 0.0;
80 for item in array {
81 let t = (item - mean).powi(2);
82 total += t;
83 }
84 total / (len) as f64
85}