Skip to content

Commit 38c17dc

Browse files
author
Jorge Aparicio
committed
libtest: DSTify Stats
1 parent 88c743d commit 38c17dc

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

src/libtest/stats.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn local_sort<T: Float>(v: &mut [T]) {
3838
}
3939

4040
/// 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? {
4242

4343
/// Sum of the samples.
4444
///
@@ -47,24 +47,24 @@ pub trait Stats <T: FloatMath + FromPrimitive>{
4747
/// ["Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates"]
4848
/// (http://www.cs.cmu.edu/~quake-papers/robust-arithmetic.ps)
4949
/// *Discrete & Computational Geometry 18*, 3 (Oct 1997), 305-363, Shewchuk J.R.
50-
fn sum(self) -> T;
50+
fn sum(&self) -> T;
5151

5252
/// Minimum value of the samples.
53-
fn min(self) -> T;
53+
fn min(&self) -> T;
5454

5555
/// Maximum value of the samples.
56-
fn max(self) -> T;
56+
fn max(&self) -> T;
5757

5858
/// Arithmetic mean (average) of the samples: sum divided by sample-count.
5959
///
6060
/// See: https://en.wikipedia.org/wiki/Arithmetic_mean
61-
fn mean(self) -> T;
61+
fn mean(&self) -> T;
6262

6363
/// Median of the samples: value separating the lower half of the samples from the higher half.
6464
/// Equal to `self.percentile(50.0)`.
6565
///
6666
/// See: https://en.wikipedia.org/wiki/Median
67-
fn median(self) -> T;
67+
fn median(&self) -> T;
6868

6969
/// Variance of the samples: bias-corrected mean of the squares of the differences of each
7070
/// 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>{
7373
/// than `n`.
7474
///
7575
/// See: https://en.wikipedia.org/wiki/Variance
76-
fn var(self) -> T;
76+
fn var(&self) -> T;
7777

7878
/// Standard deviation: the square root of the sample variance.
7979
///
8080
/// Note: this is not a robust statistic for non-normal distributions. Prefer the
8181
/// `median_abs_dev` for unknown distributions.
8282
///
8383
/// See: https://en.wikipedia.org/wiki/Standard_deviation
84-
fn std_dev(self) -> T;
84+
fn std_dev(&self) -> T;
8585

8686
/// Standard deviation as a percent of the mean value. See `std_dev` and `mean`.
8787
///
8888
/// Note: this is not a robust statistic for non-normal distributions. Prefer the
8989
/// `median_abs_dev_pct` for unknown distributions.
90-
fn std_dev_pct(self) -> T;
90+
fn std_dev_pct(&self) -> T;
9191

9292
/// Scaled median of the absolute deviations of each sample from the sample median. This is a
9393
/// robust (distribution-agnostic) estimator of sample variability. Use this in preference to
@@ -96,10 +96,10 @@ pub trait Stats <T: FloatMath + FromPrimitive>{
9696
/// deviation.
9797
///
9898
/// See: http://en.wikipedia.org/wiki/Median_absolute_deviation
99-
fn median_abs_dev(self) -> T;
99+
fn median_abs_dev(&self) -> T;
100100

101101
/// 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;
103103

104104
/// Percentile: the value below which `pct` percent of the values in `self` fall. For example,
105105
/// 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>{
108108
/// Calculated by linear interpolation between closest ranks.
109109
///
110110
/// See: http://en.wikipedia.org/wiki/Percentile
111-
fn percentile(self, pct: T) -> T;
111+
fn percentile(&self, pct: T) -> T;
112112

113113
/// Quartiles of the sample: three values that divide the sample into four equal groups, each
114114
/// with 1/4 of the data. The middle value is the median. See `median` and `percentile`. This
115115
/// function may calculate the 3 quartiles more efficiently than 3 calls to `percentile`, but
116116
/// is otherwise equivalent.
117117
///
118118
/// See also: https://en.wikipedia.org/wiki/Quartile
119-
fn quartiles(self) -> (T,T,T);
119+
fn quartiles(&self) -> (T,T,T);
120120

121121
/// Inter-quartile range: the difference between the 25th percentile (1st quartile) and the 75th
122122
/// percentile (3rd quartile). See `quartiles`.
123123
///
124124
/// See also: https://en.wikipedia.org/wiki/Interquartile_range
125-
fn iqr(self) -> T;
125+
fn iqr(&self) -> T;
126126
}
127127

128128
/// Extracted collection of all the summary statistics of a sample set.
@@ -163,9 +163,9 @@ impl<T: FloatMath + FromPrimitive> Summary<T> {
163163
}
164164
}
165165

166-
impl<'a, T: FloatMath + FromPrimitive> Stats<T> for &'a [T] {
166+
impl<T: FloatMath + FromPrimitive> Stats<T> for [T] {
167167
// FIXME #11059 handle NaN, inf and overflow
168-
fn sum(self) -> T {
168+
fn sum(&self) -> T {
169169
let mut partials = vec![];
170170

171171
for &mut x in self.iter() {
@@ -198,26 +198,26 @@ impl<'a, T: FloatMath + FromPrimitive> Stats<T> for &'a [T] {
198198
partials.iter().fold(zero, |p, q| p + *q)
199199
}
200200

201-
fn min(self) -> T {
201+
fn min(&self) -> T {
202202
assert!(self.len() != 0);
203203
self.iter().fold(self[0], |p, q| p.min(*q))
204204
}
205205

206-
fn max(self) -> T {
206+
fn max(&self) -> T {
207207
assert!(self.len() != 0);
208208
self.iter().fold(self[0], |p, q| p.max(*q))
209209
}
210210

211-
fn mean(self) -> T {
211+
fn mean(&self) -> T {
212212
assert!(self.len() != 0);
213213
self.sum() / FromPrimitive::from_uint(self.len()).unwrap()
214214
}
215215

216-
fn median(self) -> T {
216+
fn median(&self) -> T {
217217
self.percentile(FromPrimitive::from_uint(50).unwrap())
218218
}
219219

220-
fn var(self) -> T {
220+
fn var(&self) -> T {
221221
if self.len() < 2 {
222222
Float::zero()
223223
} else {
@@ -235,16 +235,16 @@ impl<'a, T: FloatMath + FromPrimitive> Stats<T> for &'a [T] {
235235
}
236236
}
237237

238-
fn std_dev(self) -> T {
238+
fn std_dev(&self) -> T {
239239
self.var().sqrt()
240240
}
241241

242-
fn std_dev_pct(self) -> T {
242+
fn std_dev_pct(&self) -> T {
243243
let hundred = FromPrimitive::from_uint(100).unwrap();
244244
(self.std_dev() / self.mean()) * hundred
245245
}
246246

247-
fn median_abs_dev(self) -> T {
247+
fn median_abs_dev(&self) -> T {
248248
let med = self.median();
249249
let abs_devs: Vec<T> = self.iter().map(|&v| (med - v).abs()).collect();
250250
// 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] {
253253
abs_devs.as_slice().median() * number
254254
}
255255

256-
fn median_abs_dev_pct(self) -> T {
256+
fn median_abs_dev_pct(&self) -> T {
257257
let hundred = FromPrimitive::from_uint(100).unwrap();
258258
(self.median_abs_dev() / self.median()) * hundred
259259
}
260260

261-
fn percentile(self, pct: T) -> T {
261+
fn percentile(&self, pct: T) -> T {
262262
let mut tmp = self.to_vec();
263263
local_sort(tmp.as_mut_slice());
264264
percentile_of_sorted(tmp.as_slice(), pct)
265265
}
266266

267-
fn quartiles(self) -> (T,T,T) {
267+
fn quartiles(&self) -> (T,T,T) {
268268
let mut tmp = self.to_vec();
269269
local_sort(tmp.as_mut_slice());
270270
let first = FromPrimitive::from_uint(25).unwrap();
@@ -276,7 +276,7 @@ impl<'a, T: FloatMath + FromPrimitive> Stats<T> for &'a [T] {
276276
(a,b,c)
277277
}
278278

279-
fn iqr(self) -> T {
279+
fn iqr(&self) -> T {
280280
let (a,_,c) = self.quartiles();
281281
c - a
282282
}

0 commit comments

Comments
 (0)