1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![allow(missing_docs)]
13 use std::cmp::Ordering::{self, Less, Greater, Equal};
14 use std::collections::hash_map::Entry::{Occupied, Vacant};
15 use std::collections::hash_map::{self, Hasher};
20 use std::num::{Float, FromPrimitive};
22 fn local_cmp<T:Float>(x: T, y: T) -> Ordering {
23 // arbitrarily decide that NaNs are larger than everything.
26 } else if x.is_nan() {
37 fn local_sort<T: Float>(v: &mut [T]) {
38 v.sort_by(|x: &T, y: &T| local_cmp(*x, *y));
41 /// Trait that provides simple descriptive statistics on a univariate set of numeric samples.
42 pub trait Stats <T: Float + FromPrimitive> {
44 /// Sum of the samples.
46 /// Note: this method sacrifices performance at the altar of accuracy
47 /// Depends on IEEE-754 arithmetic guarantees. See proof of correctness at:
48 /// ["Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates"]
49 /// (http://www.cs.cmu.edu/~quake-papers/robust-arithmetic.ps)
50 /// *Discrete & Computational Geometry 18*, 3 (Oct 1997), 305-363, Shewchuk J.R.
53 /// Minimum value of the samples.
56 /// Maximum value of the samples.
59 /// Arithmetic mean (average) of the samples: sum divided by sample-count.
61 /// See: https://en.wikipedia.org/wiki/Arithmetic_mean
64 /// Median of the samples: value separating the lower half of the samples from the higher half.
65 /// Equal to `self.percentile(50.0)`.
67 /// See: https://en.wikipedia.org/wiki/Median
68 fn median(&self) -> T;
70 /// Variance of the samples: bias-corrected mean of the squares of the differences of each
71 /// sample from the sample mean. Note that this calculates the _sample variance_ rather than the
72 /// population variance, which is assumed to be unknown. It therefore corrects the `(n-1)/n`
73 /// bias that would appear if we calculated a population variance, by dividing by `(n-1)` rather
76 /// See: https://en.wikipedia.org/wiki/Variance
79 /// Standard deviation: the square root of the sample variance.
81 /// Note: this is not a robust statistic for non-normal distributions. Prefer the
82 /// `median_abs_dev` for unknown distributions.
84 /// See: https://en.wikipedia.org/wiki/Standard_deviation
85 fn std_dev(&self) -> T;
87 /// Standard deviation as a percent of the mean value. See `std_dev` and `mean`.
89 /// Note: this is not a robust statistic for non-normal distributions. Prefer the
90 /// `median_abs_dev_pct` for unknown distributions.
91 fn std_dev_pct(&self) -> T;
93 /// Scaled median of the absolute deviations of each sample from the sample median. This is a
94 /// robust (distribution-agnostic) estimator of sample variability. Use this in preference to
95 /// `std_dev` if you cannot assume your sample is normally distributed. Note that this is scaled
96 /// by the constant `1.4826` to allow its use as a consistent estimator for the standard
99 /// See: http://en.wikipedia.org/wiki/Median_absolute_deviation
100 fn median_abs_dev(&self) -> T;
102 /// Median absolute deviation as a percent of the median. See `median_abs_dev` and `median`.
103 fn median_abs_dev_pct(&self) -> T;
105 /// Percentile: the value below which `pct` percent of the values in `self` fall. For example,
106 /// percentile(95.0) will return the value `v` such that 95% of the samples `s` in `self`
107 /// satisfy `s <= v`.
109 /// Calculated by linear interpolation between closest ranks.
111 /// See: http://en.wikipedia.org/wiki/Percentile
112 fn percentile(&self, pct: T) -> T;
114 /// Quartiles of the sample: three values that divide the sample into four equal groups, each
115 /// with 1/4 of the data. The middle value is the median. See `median` and `percentile`. This
116 /// function may calculate the 3 quartiles more efficiently than 3 calls to `percentile`, but
117 /// is otherwise equivalent.
119 /// See also: https://en.wikipedia.org/wiki/Quartile
120 fn quartiles(&self) -> (T,T,T);
122 /// Inter-quartile range: the difference between the 25th percentile (1st quartile) and the 75th
123 /// percentile (3rd quartile). See `quartiles`.
125 /// See also: https://en.wikipedia.org/wiki/Interquartile_range
129 /// Extracted collection of all the summary statistics of a sample set.
130 #[derive(Clone, PartialEq)]
131 #[allow(missing_docs)]
132 pub struct Summary<T> {
141 pub median_abs_dev: T,
142 pub median_abs_dev_pct: T,
143 pub quartiles: (T,T,T),
147 impl<T: Float + FromPrimitive> Summary<T> {
148 /// Construct a new summary of a sample set.
149 pub fn new(samples: &[T]) -> Summary<T> {
154 mean: samples.mean(),
155 median: samples.median(),
157 std_dev: samples.std_dev(),
158 std_dev_pct: samples.std_dev_pct(),
159 median_abs_dev: samples.median_abs_dev(),
160 median_abs_dev_pct: samples.median_abs_dev_pct(),
161 quartiles: samples.quartiles(),
167 impl<T: Float + FromPrimitive> Stats<T> for [T] {
168 // FIXME #11059 handle NaN, inf and overflow
170 let mut partials = vec![];
172 for &x in self.iter() {
175 // This inner loop applies `hi`/`lo` summation to each
176 // partial so that the list of partial sums remains exact.
177 for i in range(0, partials.len()) {
178 let mut y: T = partials[i];
179 if x.abs() < y.abs() {
180 mem::swap(&mut x, &mut y);
182 // Rounded `x+y` is stored in `hi` with round-off stored in
183 // `lo`. Together `hi+lo` are exactly equal to `x+y`.
185 let lo = y - (hi - x);
186 if lo != Float::zero() {
192 if j >= partials.len() {
196 partials.truncate(j+1);
199 let zero: T = Float::zero();
200 partials.iter().fold(zero, |p, q| p + *q)
204 assert!(self.len() != 0);
205 self.iter().fold(self[0], |p, q| p.min(*q))
209 assert!(self.len() != 0);
210 self.iter().fold(self[0], |p, q| p.max(*q))
213 fn mean(&self) -> T {
214 assert!(self.len() != 0);
215 self.sum() / FromPrimitive::from_uint(self.len()).unwrap()
218 fn median(&self) -> T {
219 self.percentile(FromPrimitive::from_uint(50).unwrap())
226 let mean = self.mean();
227 let mut v: T = Float::zero();
228 for s in self.iter() {
232 // NB: this is _supposed to be_ len-1, not len. If you
233 // change it back to len, you will be calculating a
234 // population variance, not a sample variance.
235 let denom = FromPrimitive::from_uint(self.len()-1).unwrap();
240 fn std_dev(&self) -> T {
244 fn std_dev_pct(&self) -> T {
245 let hundred = FromPrimitive::from_uint(100).unwrap();
246 (self.std_dev() / self.mean()) * hundred
249 fn median_abs_dev(&self) -> T {
250 let med = self.median();
251 let abs_devs: Vec<T> = self.iter().map(|&v| (med - v).abs()).collect();
252 // This constant is derived by smarter statistics brains than me, but it is
253 // consistent with how R and other packages treat the MAD.
254 let number = FromPrimitive::from_f64(1.4826).unwrap();
255 abs_devs.median() * number
258 fn median_abs_dev_pct(&self) -> T {
259 let hundred = FromPrimitive::from_uint(100).unwrap();
260 (self.median_abs_dev() / self.median()) * hundred
263 fn percentile(&self, pct: T) -> T {
264 let mut tmp = self.to_vec();
265 local_sort(tmp.as_mut_slice());
266 percentile_of_sorted(tmp.as_slice(), pct)
269 fn quartiles(&self) -> (T,T,T) {
270 let mut tmp = self.to_vec();
271 local_sort(tmp.as_mut_slice());
272 let first = FromPrimitive::from_uint(25).unwrap();
273 let a = percentile_of_sorted(tmp.as_slice(), first);
274 let secound = FromPrimitive::from_uint(50).unwrap();
275 let b = percentile_of_sorted(tmp.as_slice(), secound);
276 let third = FromPrimitive::from_uint(75).unwrap();
277 let c = percentile_of_sorted(tmp.as_slice(), third);
282 let (a,_,c) = self.quartiles();
288 // Helper function: extract a value representing the `pct` percentile of a sorted sample-set, using
289 // linear interpolation. If samples are not sorted, return nonsensical value.
290 fn percentile_of_sorted<T: Float + FromPrimitive>(sorted_samples: &[T],
292 assert!(sorted_samples.len() != 0);
293 if sorted_samples.len() == 1 {
294 return sorted_samples[0];
296 let zero: T = Float::zero();
297 assert!(zero <= pct);
298 let hundred = FromPrimitive::from_uint(100).unwrap();
299 assert!(pct <= hundred);
301 return sorted_samples[sorted_samples.len() - 1];
303 let length = FromPrimitive::from_uint(sorted_samples.len() - 1).unwrap();
304 let rank = (pct / hundred) * length;
305 let lrank = rank.floor();
306 let d = rank - lrank;
307 let n = lrank.to_uint().unwrap();
308 let lo = sorted_samples[n];
309 let hi = sorted_samples[n+1];
314 /// Winsorize a set of samples, replacing values above the `100-pct` percentile and below the `pct`
315 /// percentile with those percentiles themselves. This is a way of minimizing the effect of
316 /// outliers, at the cost of biasing the sample. It differs from trimming in that it does not
317 /// change the number of samples, just changes the values of those that are outliers.
319 /// See: http://en.wikipedia.org/wiki/Winsorising
320 pub fn winsorize<T: Float + FromPrimitive>(samples: &mut [T], pct: T) {
321 let mut tmp = samples.to_vec();
322 local_sort(tmp.as_mut_slice());
323 let lo = percentile_of_sorted(tmp.as_slice(), pct);
324 let hundred: T = FromPrimitive::from_uint(100).unwrap();
325 let hi = percentile_of_sorted(tmp.as_slice(), hundred-pct);
326 for samp in samples.iter_mut() {
329 } else if *samp < lo {
335 /// Render writes the min, max and quartiles of the provided `Summary` to the provided `Writer`.
336 pub fn write_5_number_summary<W: Writer, T: Float + fmt::String + fmt::Show>(w: &mut W,
337 s: &Summary<T>) -> io::IoResult<()> {
338 let (q1,q2,q3) = s.quartiles;
339 write!(w, "(min={}, q1={}, med={}, q3={}, max={})",
347 /// Render a boxplot to the provided writer. The boxplot shows the min, max and quartiles of the
348 /// provided `Summary` (thus includes the mean) and is scaled to display within the range of the
349 /// nearest multiple-of-a-power-of-ten above and below the min and max of possible values, and
350 /// target `width_hint` characters of display (though it will be wider if necessary).
352 /// As an example, the summary with 5-number-summary `(min=15, q1=17, med=20, q3=24, max=31)` might
356 /// 10 | [--****#******----------] | 40
358 pub fn write_boxplot<W: Writer, T: Float + fmt::String + fmt::Show + FromPrimitive>(
362 -> io::IoResult<()> {
364 let (q1,q2,q3) = s.quartiles;
366 // the .abs() handles the case where numbers are negative
367 let ten: T = FromPrimitive::from_uint(10).unwrap();
368 let lomag = ten.powf(s.min.abs().log10().floor());
369 let himag = ten.powf(s.max.abs().log10().floor());
371 // need to consider when the limit is zero
372 let zero: T = Float::zero();
373 let lo = if lomag == Float::zero() {
376 (s.min / lomag).floor() * lomag
379 let hi = if himag == Float::zero() {
382 (s.max / himag).ceil() * himag
387 let lostr = lo.to_string();
388 let histr = hi.to_string();
390 let overhead_width = lostr.len() + histr.len() + 4;
391 let range_width = width_hint - overhead_width;
392 let range_float = FromPrimitive::from_uint(range_width).unwrap();
393 let char_step = range / range_float;
395 try!(write!(w, "{} |", lostr));
400 while c < range_width && v < s.min {
401 try!(write!(w, " "));
405 try!(write!(w, "["));
407 while c < range_width && v < q1 {
408 try!(write!(w, "-"));
412 while c < range_width && v < q2 {
413 try!(write!(w, "*"));
417 try!(write!(w, "#"));
419 while c < range_width && v < q3 {
420 try!(write!(w, "*"));
424 while c < range_width && v < s.max {
425 try!(write!(w, "-"));
429 try!(write!(w, "]"));
430 while c < range_width {
431 try!(write!(w, " "));
436 try!(write!(w, "| {}", histr));
440 /// Returns a HashMap with the number of occurrences of every element in the
441 /// sequence that the iterator exposes.
442 pub fn freq_count<T, U>(mut iter: T) -> hash_map::HashMap<U, uint>
443 where T: Iterator<Item=U>, U: Eq + Clone + Hash<Hasher>
445 let mut map: hash_map::HashMap<U,uint> = hash_map::HashMap::new();
447 match map.entry(elem) {
448 Occupied(mut entry) => { *entry.get_mut() += 1; },
449 Vacant(entry) => { entry.insert(1); },
455 // Test vectors generated from R, using the script src/etc/stat-test-vectors.r.
461 use stats::write_5_number_summary;
462 use stats::write_boxplot;
466 macro_rules! assert_approx_eq {
467 ($a:expr, $b:expr) => ({
469 let (a, b) = (&$a, &$b);
470 assert!((*a - *b).abs() < 1.0e-6,
471 "{} is not approximately equal to {}", *a, *b);
475 fn check(samples: &[f64], summ: &Summary<f64>) {
477 let summ2 = Summary::new(samples);
479 let mut w = io::stdout();
481 (write!(w, "\n")).unwrap();
482 write_5_number_summary(w, &summ2).unwrap();
483 (write!(w, "\n")).unwrap();
484 write_boxplot(w, &summ2, 50).unwrap();
485 (write!(w, "\n")).unwrap();
487 assert_eq!(summ.sum, summ2.sum);
488 assert_eq!(summ.min, summ2.min);
489 assert_eq!(summ.max, summ2.max);
490 assert_eq!(summ.mean, summ2.mean);
491 assert_eq!(summ.median, summ2.median);
493 // We needed a few more digits to get exact equality on these
494 // but they're within float epsilon, which is 1.0e-6.
495 assert_approx_eq!(summ.var, summ2.var);
496 assert_approx_eq!(summ.std_dev, summ2.std_dev);
497 assert_approx_eq!(summ.std_dev_pct, summ2.std_dev_pct);
498 assert_approx_eq!(summ.median_abs_dev, summ2.median_abs_dev);
499 assert_approx_eq!(summ.median_abs_dev_pct, summ2.median_abs_dev_pct);
501 assert_eq!(summ.quartiles, summ2.quartiles);
502 assert_eq!(summ.iqr, summ2.iqr);
506 fn test_min_max_nan() {
507 let xs = &[1.0, 2.0, f64::NAN, 3.0, 4.0];
508 let summary = Summary::new(xs);
509 assert_eq!(summary.min, 1.0);
510 assert_eq!(summary.max, 4.0);
519 let summ = &Summary {
520 sum: 1882.0000000000,
523 mean: 941.0000000000,
524 median: 941.0000000000,
526 std_dev: 24.0416305603,
527 std_dev_pct: 2.5549022912,
528 median_abs_dev: 25.2042000000,
529 median_abs_dev_pct: 2.6784484591,
530 quartiles: (932.5000000000,941.0000000000,949.5000000000),
536 fn test_norm10narrow() {
549 let summ = &Summary {
550 sum: 9996.0000000000,
552 max: 1217.0000000000,
553 mean: 999.6000000000,
554 median: 970.5000000000,
555 var: 16050.7111111111,
556 std_dev: 126.6914010938,
557 std_dev_pct: 12.6742097933,
558 median_abs_dev: 102.2994000000,
559 median_abs_dev_pct: 10.5408964451,
560 quartiles: (956.7500000000,970.5000000000,1078.7500000000),
566 fn test_norm10medium() {
579 let summ = &Summary {
580 sum: 8653.0000000000,
582 max: 1084.0000000000,
583 mean: 865.3000000000,
584 median: 911.5000000000,
585 var: 48628.4555555556,
586 std_dev: 220.5186059170,
587 std_dev_pct: 25.4846418487,
588 median_abs_dev: 195.7032000000,
589 median_abs_dev_pct: 21.4704552935,
590 quartiles: (771.0000000000,911.5000000000,1017.2500000000),
596 fn test_norm10wide() {
609 let summ = &Summary {
610 sum: 9349.0000000000,
612 max: 1591.0000000000,
613 mean: 934.9000000000,
614 median: 913.5000000000,
615 var: 239208.9888888889,
616 std_dev: 489.0899599142,
617 std_dev_pct: 52.3146817750,
618 median_abs_dev: 611.5725000000,
619 median_abs_dev_pct: 66.9482758621,
620 quartiles: (567.2500000000,913.5000000000,1331.2500000000),
626 fn test_norm25verynarrow() {
654 let summ = &Summary {
655 sum: 24926.0000000000,
657 max: 1040.0000000000,
658 mean: 997.0400000000,
659 median: 998.0000000000,
661 std_dev: 19.8294393937,
662 std_dev_pct: 1.9888308788,
663 median_abs_dev: 22.2390000000,
664 median_abs_dev_pct: 2.2283567134,
665 quartiles: (983.0000000000,998.0000000000,1013.0000000000),
684 let summ = &Summary {
689 median: 11.5000000000,
691 std_dev: 16.9643416875,
692 std_dev_pct: 101.5828843560,
693 median_abs_dev: 13.3434000000,
694 median_abs_dev_pct: 116.0295652174,
695 quartiles: (4.2500000000,11.5000000000,22.5000000000),
714 let summ = &Summary {
719 median: 24.5000000000,
721 std_dev: 19.5848580967,
722 std_dev_pct: 74.4671410520,
723 median_abs_dev: 22.9803000000,
724 median_abs_dev_pct: 93.7971428571,
725 quartiles: (9.5000000000,24.5000000000,36.5000000000),
744 let summ = &Summary {
749 median: 22.0000000000,
751 std_dev: 21.4050876611,
752 std_dev_pct: 88.4507754589,
753 median_abs_dev: 21.4977000000,
754 median_abs_dev_pct: 97.7168181818,
755 quartiles: (7.7500000000,22.0000000000,35.0000000000),
789 let summ = &Summary {
794 median: 19.0000000000,
796 std_dev: 24.5161851301,
797 std_dev_pct: 103.3565983562,
798 median_abs_dev: 19.2738000000,
799 median_abs_dev_pct: 101.4410526316,
800 quartiles: (6.0000000000,19.0000000000,31.0000000000),
834 let summ = &Summary {
839 median: 20.0000000000,
841 std_dev: 4.5650848842,
842 std_dev_pct: 22.2037202539,
843 median_abs_dev: 5.9304000000,
844 median_abs_dev_pct: 29.6520000000,
845 quartiles: (17.0000000000,20.0000000000,24.0000000000),
851 fn test_pois25lambda30() {
879 let summ = &Summary {
884 median: 32.0000000000,
886 std_dev: 5.1568724372,
887 std_dev_pct: 16.3814245145,
888 median_abs_dev: 5.9304000000,
889 median_abs_dev_pct: 18.5325000000,
890 quartiles: (28.0000000000,32.0000000000,34.0000000000),
896 fn test_pois25lambda40() {
924 let summ = &Summary {
925 sum: 1019.0000000000,
929 median: 42.0000000000,
931 std_dev: 5.8685603004,
932 std_dev_pct: 14.3978417577,
933 median_abs_dev: 5.9304000000,
934 median_abs_dev_pct: 14.1200000000,
935 quartiles: (37.0000000000,42.0000000000,45.0000000000),
941 fn test_pois25lambda50() {
969 let summ = &Summary {
970 sum: 1235.0000000000,
974 median: 50.0000000000,
976 std_dev: 5.6273143387,
977 std_dev_pct: 11.3913245723,
978 median_abs_dev: 4.4478000000,
979 median_abs_dev_pct: 8.8956000000,
980 quartiles: (44.0000000000,50.0000000000,52.0000000000),
1014 let summ = &Summary {
1015 sum: 1242.0000000000,
1018 mean: 49.6800000000,
1019 median: 45.0000000000,
1020 var: 1015.6433333333,
1021 std_dev: 31.8691595957,
1022 std_dev_pct: 64.1488719719,
1023 median_abs_dev: 45.9606000000,
1024 median_abs_dev_pct: 102.1346666667,
1025 quartiles: (29.0000000000,45.0000000000,79.0000000000),
1032 fn test_boxplot_nonpositive() {
1033 fn t(s: &Summary<f64>, expected: String) {
1034 let mut m = Vec::new();
1035 write_boxplot(&mut m, s, 30).unwrap();
1036 let out = String::from_utf8(m).unwrap();
1037 assert_eq!(out, expected);
1040 t(&Summary::new(&[-2.0f64, -1.0f64]),
1041 "-2 |[------******#*****---]| -1".to_string());
1042 t(&Summary::new(&[0.0f64, 2.0f64]),
1043 "0 |[-------*****#*******---]| 2".to_string());
1044 t(&Summary::new(&[-2.0f64, 0.0f64]),
1045 "-2 |[------******#******---]| 0".to_string());
1049 fn test_sum_f64s() {
1050 assert_eq!([0.5f64, 3.2321f64, 1.5678f64].sum(), 5.2999);
1053 fn test_sum_f64_between_ints_that_sum_to_0() {
1054 assert_eq!([1e30f64, 1.2f64, -1e30f64].sum(), 1.2);
1064 pub fn sum_three_items(b: &mut Bencher) {
1066 [1e20f64, 1.5f64, -1e20f64].sum();
1070 pub fn sum_many_f64(b: &mut Bencher) {
1071 let nums = [-1e30f64, 1e60, 1e30, 1.0, -1e60];
1072 let v = range(0, 500).map(|i| nums[i%5]).collect::<Vec<_>>();