@@ -38,7 +38,7 @@ fn local_sort<T: Float>(v: &mut [T]) {
38
38
}
39
39
40
40
/// Trait that provides simple descriptive statistics on a univariate set of numeric samples.
41
- pub trait Stats < T : FloatMath + FromPrimitive > {
41
+ pub trait Stats < T : FloatMath + FromPrimitive > for Sized ? {
42
42
43
43
/// Sum of the samples.
44
44
///
@@ -47,24 +47,24 @@ pub trait Stats <T: FloatMath + FromPrimitive>{
47
47
/// ["Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates"]
48
48
/// (http://www.cs.cmu.edu/~quake-papers/robust-arithmetic.ps)
49
49
/// *Discrete & Computational Geometry 18*, 3 (Oct 1997), 305-363, Shewchuk J.R.
50
- fn sum ( self ) -> T ;
50
+ fn sum ( & self ) -> T ;
51
51
52
52
/// Minimum value of the samples.
53
- fn min ( self ) -> T ;
53
+ fn min ( & self ) -> T ;
54
54
55
55
/// Maximum value of the samples.
56
- fn max ( self ) -> T ;
56
+ fn max ( & self ) -> T ;
57
57
58
58
/// Arithmetic mean (average) of the samples: sum divided by sample-count.
59
59
///
60
60
/// See: https://en.wikipedia.org/wiki/Arithmetic_mean
61
- fn mean ( self ) -> T ;
61
+ fn mean ( & self ) -> T ;
62
62
63
63
/// Median of the samples: value separating the lower half of the samples from the higher half.
64
64
/// Equal to `self.percentile(50.0)`.
65
65
///
66
66
/// See: https://en.wikipedia.org/wiki/Median
67
- fn median ( self ) -> T ;
67
+ fn median ( & self ) -> T ;
68
68
69
69
/// Variance of the samples: bias-corrected mean of the squares of the differences of each
70
70
/// sample from the sample mean. Note that this calculates the _sample variance_ rather than the
@@ -73,21 +73,21 @@ pub trait Stats <T: FloatMath + FromPrimitive>{
73
73
/// than `n`.
74
74
///
75
75
/// See: https://en.wikipedia.org/wiki/Variance
76
- fn var ( self ) -> T ;
76
+ fn var ( & self ) -> T ;
77
77
78
78
/// Standard deviation: the square root of the sample variance.
79
79
///
80
80
/// Note: this is not a robust statistic for non-normal distributions. Prefer the
81
81
/// `median_abs_dev` for unknown distributions.
82
82
///
83
83
/// See: https://en.wikipedia.org/wiki/Standard_deviation
84
- fn std_dev ( self ) -> T ;
84
+ fn std_dev ( & self ) -> T ;
85
85
86
86
/// Standard deviation as a percent of the mean value. See `std_dev` and `mean`.
87
87
///
88
88
/// Note: this is not a robust statistic for non-normal distributions. Prefer the
89
89
/// `median_abs_dev_pct` for unknown distributions.
90
- fn std_dev_pct ( self ) -> T ;
90
+ fn std_dev_pct ( & self ) -> T ;
91
91
92
92
/// Scaled median of the absolute deviations of each sample from the sample median. This is a
93
93
/// robust (distribution-agnostic) estimator of sample variability. Use this in preference to
@@ -96,10 +96,10 @@ pub trait Stats <T: FloatMath + FromPrimitive>{
96
96
/// deviation.
97
97
///
98
98
/// See: http://en.wikipedia.org/wiki/Median_absolute_deviation
99
- fn median_abs_dev ( self ) -> T ;
99
+ fn median_abs_dev ( & self ) -> T ;
100
100
101
101
/// Median absolute deviation as a percent of the median. See `median_abs_dev` and `median`.
102
- fn median_abs_dev_pct ( self ) -> T ;
102
+ fn median_abs_dev_pct ( & self ) -> T ;
103
103
104
104
/// Percentile: the value below which `pct` percent of the values in `self` fall. For example,
105
105
/// percentile(95.0) will return the value `v` such that 95% of the samples `s` in `self`
@@ -108,21 +108,21 @@ pub trait Stats <T: FloatMath + FromPrimitive>{
108
108
/// Calculated by linear interpolation between closest ranks.
109
109
///
110
110
/// See: http://en.wikipedia.org/wiki/Percentile
111
- fn percentile ( self , pct : T ) -> T ;
111
+ fn percentile ( & self , pct : T ) -> T ;
112
112
113
113
/// Quartiles of the sample: three values that divide the sample into four equal groups, each
114
114
/// with 1/4 of the data. The middle value is the median. See `median` and `percentile`. This
115
115
/// function may calculate the 3 quartiles more efficiently than 3 calls to `percentile`, but
116
116
/// is otherwise equivalent.
117
117
///
118
118
/// See also: https://en.wikipedia.org/wiki/Quartile
119
- fn quartiles ( self ) -> ( T , T , T ) ;
119
+ fn quartiles ( & self ) -> ( T , T , T ) ;
120
120
121
121
/// Inter-quartile range: the difference between the 25th percentile (1st quartile) and the 75th
122
122
/// percentile (3rd quartile). See `quartiles`.
123
123
///
124
124
/// See also: https://en.wikipedia.org/wiki/Interquartile_range
125
- fn iqr ( self ) -> T ;
125
+ fn iqr ( & self ) -> T ;
126
126
}
127
127
128
128
/// Extracted collection of all the summary statistics of a sample set.
@@ -163,9 +163,9 @@ impl<T: FloatMath + FromPrimitive> Summary<T> {
163
163
}
164
164
}
165
165
166
- impl < ' a , T : FloatMath + FromPrimitive > Stats < T > for & ' a [ T ] {
166
+ impl < T : FloatMath + FromPrimitive > Stats < T > for [ T ] {
167
167
// FIXME #11059 handle NaN, inf and overflow
168
- fn sum ( self ) -> T {
168
+ fn sum ( & self ) -> T {
169
169
let mut partials = vec ! [ ] ;
170
170
171
171
for & mut x in self . iter ( ) {
@@ -198,26 +198,26 @@ impl<'a, T: FloatMath + FromPrimitive> Stats<T> for &'a [T] {
198
198
partials. iter ( ) . fold ( zero, |p, q| p + * q)
199
199
}
200
200
201
- fn min ( self ) -> T {
201
+ fn min ( & self ) -> T {
202
202
assert ! ( self . len( ) != 0 ) ;
203
203
self . iter ( ) . fold ( self [ 0 ] , |p, q| p. min ( * q) )
204
204
}
205
205
206
- fn max ( self ) -> T {
206
+ fn max ( & self ) -> T {
207
207
assert ! ( self . len( ) != 0 ) ;
208
208
self . iter ( ) . fold ( self [ 0 ] , |p, q| p. max ( * q) )
209
209
}
210
210
211
- fn mean ( self ) -> T {
211
+ fn mean ( & self ) -> T {
212
212
assert ! ( self . len( ) != 0 ) ;
213
213
self . sum ( ) / FromPrimitive :: from_uint ( self . len ( ) ) . unwrap ( )
214
214
}
215
215
216
- fn median ( self ) -> T {
216
+ fn median ( & self ) -> T {
217
217
self . percentile ( FromPrimitive :: from_uint ( 50 ) . unwrap ( ) )
218
218
}
219
219
220
- fn var ( self ) -> T {
220
+ fn var ( & self ) -> T {
221
221
if self . len ( ) < 2 {
222
222
Float :: zero ( )
223
223
} else {
@@ -235,16 +235,16 @@ impl<'a, T: FloatMath + FromPrimitive> Stats<T> for &'a [T] {
235
235
}
236
236
}
237
237
238
- fn std_dev ( self ) -> T {
238
+ fn std_dev ( & self ) -> T {
239
239
self . var ( ) . sqrt ( )
240
240
}
241
241
242
- fn std_dev_pct ( self ) -> T {
242
+ fn std_dev_pct ( & self ) -> T {
243
243
let hundred = FromPrimitive :: from_uint ( 100 ) . unwrap ( ) ;
244
244
( self . std_dev ( ) / self . mean ( ) ) * hundred
245
245
}
246
246
247
- fn median_abs_dev ( self ) -> T {
247
+ fn median_abs_dev ( & self ) -> T {
248
248
let med = self . median ( ) ;
249
249
let abs_devs: Vec < T > = self . iter ( ) . map ( |& v| ( med - v) . abs ( ) ) . collect ( ) ;
250
250
// This constant is derived by smarter statistics brains than me, but it is
@@ -253,18 +253,18 @@ impl<'a, T: FloatMath + FromPrimitive> Stats<T> for &'a [T] {
253
253
abs_devs. as_slice ( ) . median ( ) * number
254
254
}
255
255
256
- fn median_abs_dev_pct ( self ) -> T {
256
+ fn median_abs_dev_pct ( & self ) -> T {
257
257
let hundred = FromPrimitive :: from_uint ( 100 ) . unwrap ( ) ;
258
258
( self . median_abs_dev ( ) / self . median ( ) ) * hundred
259
259
}
260
260
261
- fn percentile ( self , pct : T ) -> T {
261
+ fn percentile ( & self , pct : T ) -> T {
262
262
let mut tmp = self . to_vec ( ) ;
263
263
local_sort ( tmp. as_mut_slice ( ) ) ;
264
264
percentile_of_sorted ( tmp. as_slice ( ) , pct)
265
265
}
266
266
267
- fn quartiles ( self ) -> ( T , T , T ) {
267
+ fn quartiles ( & self ) -> ( T , T , T ) {
268
268
let mut tmp = self . to_vec ( ) ;
269
269
local_sort ( tmp. as_mut_slice ( ) ) ;
270
270
let first = FromPrimitive :: from_uint ( 25 ) . unwrap ( ) ;
@@ -276,7 +276,7 @@ impl<'a, T: FloatMath + FromPrimitive> Stats<T> for &'a [T] {
276
276
( a, b, c)
277
277
}
278
278
279
- fn iqr ( self ) -> T {
279
+ fn iqr ( & self ) -> T {
280
280
let ( a, _, c) = self . quartiles ( ) ;
281
281
c - a
282
282
}
0 commit comments