1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*!
Useful statistics-calculating traits for fields with numeric data.
*/
use std::ops::{Add, Mul};

use num_traits::{AsPrimitive, Zero};

use access::DataIndex;
use field::*;

/// A trait for counting NA and existing values in a field.
pub trait NaCount {
    /// Returns the number of NA (missing) values in this field.
    fn num_na(&self) -> usize;
    /// Returns the number of existing (non-missing) values in this field.
    fn num_exists(&self) -> usize;
}

impl<DI> NaCount for DI
where
    DI: DataIndex,
{
    fn num_na(&self) -> usize {
        self.iter().fold(
            0usize,
            |count, value| if value.exists() { count } else { count + 1 },
        )
    }
    fn num_exists(&self) -> usize {
        self.iter().fold(
            0usize,
            |count, value| if value.exists() { count + 1 } else { count },
        )
    }
}

/// A trait for computing the sum of values in a field.
pub trait Sum {
    /// The data type of the sum result.
    type Output;
    /// Returns the sum of values in this field. Treats missing values as `0`.
    fn sum(&self) -> Self::Output;
}

impl<DI> Sum for DI
where
    DI: DataIndex,
    DI::DType: for<'a> Add<&'a DI::DType, Output = DI::DType> + Zero,
{
    type Output = <DI as DataIndex>::DType;

    fn sum(&self) -> Self::Output {
        self.iter().fold(
            <<Self as DataIndex>::DType as Zero>::zero(),
            |sum, value| match value {
                Value::Exists(value) => sum + value,
                Value::Na => sum,
            },
        )
    }
}

/// A trait for calculating the arithmetic mean of a field.
pub trait Mean {
    /// Compute the arithmetic mean of a field. Ignores missing values in the computation. If all
    /// values missing, returns `0.0`.
    fn mean(&self) -> f64;
}

impl<DI> Mean for DI
where
    DI: DataIndex + NaCount + Sum,
    <DI as Sum>::Output: AsPrimitive<f64>,
{
    fn mean(&self) -> f64 {
        let nexists = match self.num_exists() {
            0 => {
                return 0.0;
            }
            val => val as f64,
        };
        self.sum().as_() / nexists
    }
}

/// A trait for calculating the sum of squares of values in this field.
pub trait SumSq {
    /// The data type of the sum result.
    type Output;

    /// Calculate the sum of squares of values in this field. Treats missing values as `0`.
    fn sum_sq(&self) -> Self::Output;
}

impl<DI> SumSq for DI
where
    DI: DataIndex,
    DI::DType: for<'a> Add<&'a DI::DType, Output = DI::DType> + Zero,
    for<'a, 'b> &'a DI::DType: Mul<&'b DI::DType, Output = DI::DType>,
{
    type Output = DI::DType;

    fn sum_sq(&self) -> DI::DType {
        self.iter().fold(
            <<Self as DataIndex>::DType as Zero>::zero(),
            |sum, value| match value {
                Value::Exists(value) => sum + value.clone() * value,
                Value::Na => sum,
            },
        )
    }
}

/// A trait for computing the variance and standard deviation of values in a field.
pub trait Variance {
    /// Computes sample variance of this field. Ignores missing values in this computation. If all
    /// values are missing, returns `0.0`.
    fn var(&self) -> f64;
    /// Computes population variance of this field. Ignores missing values in this computation. If
    /// all values are missing, returns `0.0`.
    fn varp(&self) -> f64;
    /// Computes sample standard deviation of this field. Ignores missing values in this
    /// computation. If all values are missing, returns `0.0`.
    fn stdev(&self) -> f64 {
        self.var().sqrt()
    }
    /// Computes population standard deviation of this field. Ignores missing values in this
    /// computation. If all values are missing, returns `0.0`.
    fn stdevp(&self) -> f64 {
        self.varp().sqrt()
    }
}

impl<DI> Variance for DI
where
    DI: DataIndex + SumSq + NaCount + Mean,
    <DI as SumSq>::Output: AsPrimitive<f64>,
{
    fn var(&self) -> f64 {
        let nexists = match self.num_exists() {
            0 => {
                return 0.0;
            }
            val => val as f64,
        };
        let sum_sq = self.sum_sq();
        let mean: f64 = self.mean().as_();
        sum_sq.as_() / (nexists - 1.0) - nexists / (nexists - 1.0) * mean * mean
    }
    fn varp(&self) -> f64 {
        let nexists = match self.num_exists() {
            0 => {
                return 0.0;
            }
            val => val as f64,
        };
        let sum_sq = self.sum_sq();
        let mean: f64 = self.mean().as_();
        sum_sq.as_() / nexists - mean * mean
    }
}

/// A trait for computing the upper and lower extrema values for a field.
pub trait Extrema {
    /// The data type of the upper and lower values.
    type Output;

    /// The minimum value in this field. Returns `None` if no values exist in this field.
    fn min(&self) -> Option<&Self::Output>;
    /// The maximum value in this field. Returns `None` if no values exist in this field.
    fn max(&self) -> Option<&Self::Output>;
}

impl<DI> Extrema for DI
where
    DI: DataIndex,
    DI::DType: PartialOrd,
{
    type Output = DI::DType;

    fn min(&self) -> Option<&DI::DType> {
        if self.num_exists() == 0 {
            return None;
        }
        let mut ret = None;
        for val in self.iter() {
            match (ret, val) {
                (None, Value::Exists(val)) => {
                    ret = Some(val);
                }
                (Some(cur_min), Value::Exists(val)) => {
                    if val < cur_min {
                        ret = Some(val);
                    }
                }
                _ => {}
            }
        }
        ret
    }
    fn max(&self) -> Option<&DI::DType> {
        if self.num_exists() == 0 {
            return None;
        }
        let mut ret = None;
        for val in self.iter() {
            match (ret, val) {
                (None, Value::Exists(val)) => {
                    ret = Some(val);
                }
                (Some(cur_max), Value::Exists(val)) => {
                    if val > cur_max {
                        ret = Some(val);
                    }
                }
                _ => {}
            }
        }
        ret
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use cons::Nil;
    use field::Value;
    use select::FieldSelect;
    use store::DataStore;

    tablespace![
        pub table foo {
            Foo: f64
        }
    ];

    #[test]
    fn na_count() {
        let dv = DataStore::<Nil>::empty()
            .push_back_from_value_iter::<foo::Foo, _, _, _>(vec![
                Value::Exists(0.0),
                Value::Exists(-5.0),
                Value::Na,
                Value::Na,
                Value::Exists(-3.0),
            ])
            .into_view();
        assert_eq!(dv.field::<foo::Foo>().num_na(), 2);
        assert_eq!(dv.field::<foo::Foo>().num_exists(), 3);
    }

    #[test]
    fn sum() {
        let dv = DataStore::<Nil>::empty()
            .push_back_from_value_iter::<foo::Foo, _, _, _>(vec![
                Value::Exists(0u64),
                Value::Exists(5),
                Value::Na,
                Value::Na,
                Value::Exists(3),
            ])
            .into_view();
        assert_eq!(dv.field::<foo::Foo>().sum(), 8);

        let dv = DataStore::<Nil>::empty()
            .push_back_from_value_iter::<foo::Foo, _, _, _>(vec![
                Value::Exists(0),
                Value::Exists(-5),
                Value::Na,
                Value::Na,
                Value::Exists(-3),
            ])
            .into_view();
        assert_eq!(dv.field::<foo::Foo>().sum(), -8);

        let dv = DataStore::<Nil>::empty()
            .push_back_from_value_iter::<foo::Foo, _, _, _>(vec![
                Value::Exists(true),
                Value::Exists(true),
                Value::Exists(false),
                Value::Na,
                Value::Exists(true),
            ])
            .into_view();
        assert_eq!(
            dv.field::<foo::Foo>()
                .iter()
                .map(|b| if b.exists() && *b.unwrap() { 1 } else { 0 })
                .collect::<FieldData<_>>()
                .sum(),
            3
        );

        let dv = DataStore::<Nil>::empty()
            .push_back_from_value_iter::<foo::Foo, _, _, _>(vec![
                Value::Exists(0.0),
                Value::Exists(-5.0),
                Value::Na,
                Value::Na,
                Value::Exists(-3.0),
            ])
            .into_view();
        assert_eq!(dv.field::<foo::Foo>().sum(), -8.0);
    }

    #[test]
    fn stdev() {
        let dv = DataStore::<Nil>::empty()
            .push_back_from_value_iter::<foo::Foo, _, _, _>(vec![
                Value::Exists(-5.0),
                Value::Exists(-4.0),
                Value::Na,
                Value::Exists(12.0),
                Value::Exists(3.0),
                Value::Na,
                Value::Exists(6.0),
                Value::Exists(0.0),
                Value::Exists(-3.1),
            ])
            .into_view();
        assert!((dv.field::<foo::Foo>().var() - 38.049048).abs() < 1e-6);
        assert!((dv.field::<foo::Foo>().stdev() - 6.168391).abs() < 1e-6);
        assert!((dv.field::<foo::Foo>().varp() - 32.613469).abs() < 1e-6);
        assert!((dv.field::<foo::Foo>().stdevp() - 5.710820).abs() < 1e-6);
        assert!((dv.field::<foo::Foo>().mean() - 1.271429).abs() < 1e-6);
        assert_eq!(dv.field::<foo::Foo>().sum(), 8.9);
    }

    #[test]
    fn min() {
        let dv = DataStore::<Nil>::empty()
            .push_back_from_value_iter::<foo::Foo, _, _, _>(vec![
                Value::Exists(0u32),
                Value::Exists(9),
                Value::Na,
                Value::Na,
                Value::Exists(3),
            ])
            .into_view();
        assert_eq!(dv.field::<foo::Foo>().min(), Some(&0));

        let dv = DataStore::<Nil>::empty()
            .push_back_from_value_iter::<foo::Foo, _, _, _>(vec![
                Value::Exists(0i32),
                Value::Exists(-9),
                Value::Na,
                Value::Na,
                Value::Exists(-3),
            ])
            .into_view();
        assert_eq!(dv.field::<foo::Foo>().min(), Some(&-9));

        let dv = DataStore::<Nil>::empty()
            .push_back_from_value_iter::<foo::Foo, _, _, _>(vec![
                Value::Exists(true),
                Value::Exists(true),
                Value::Exists(false),
                Value::Na,
                Value::Exists(true),
            ])
            .into_view();
        assert_eq!(dv.field::<foo::Foo>().min(), Some(&false));

        let dv = DataStore::<Nil>::empty()
            .push_back_from_value_iter::<foo::Foo, _, _, _>(vec![
                Value::Exists(0.0),
                Value::Exists(-9.0),
                Value::Na,
                Value::Na,
                Value::Exists(-3.0),
            ])
            .into_view();
        assert_eq!(dv.field::<foo::Foo>().min(), Some(&-9.0));

        let dv = DataStore::<Nil>::empty()
            .push_back_from_value_iter::<foo::Foo, f64, _, _>(vec![
                Value::Na,
                Value::Na,
                Value::Na,
                Value::Na,
                Value::Na,
            ])
            .into_view();
        assert_eq!(dv.field::<foo::Foo>().min(), None);
    }

    #[test]
    fn max() {
        let dv = DataStore::<Nil>::empty()
            .push_back_from_value_iter::<foo::Foo, _, _, _>(vec![
                Value::Exists(0u32),
                Value::Exists(9),
                Value::Na,
                Value::Na,
                Value::Exists(3),
            ])
            .into_view();
        assert_eq!(dv.field::<foo::Foo>().max(), Some(&9));

        let dv = DataStore::<Nil>::empty()
            .push_back_from_value_iter::<foo::Foo, _, _, _>(vec![
                Value::Exists(0i64),
                Value::Exists(-9),
                Value::Na,
                Value::Na,
                Value::Exists(-3),
            ])
            .into_view();
        assert_eq!(dv.field::<foo::Foo>().max(), Some(&0));

        let dv = DataStore::<Nil>::empty()
            .push_back_from_value_iter::<foo::Foo, _, _, _>(vec![
                Value::Exists(true),
                Value::Exists(true),
                Value::Exists(false),
                Value::Na,
                Value::Exists(true),
            ])
            .into_view();
        assert_eq!(dv.field::<foo::Foo>().max(), Some(&true));

        let dv = DataStore::<Nil>::empty()
            .push_back_from_value_iter::<foo::Foo, _, _, _>(vec![
                Value::Exists(0.0),
                Value::Exists(-9.0),
                Value::Na,
                Value::Na,
                Value::Exists(-3.0),
            ])
            .into_view();
        assert_eq!(dv.field::<foo::Foo>().max(), Some(&0.0));

        let dv = DataStore::<Nil>::empty()
            .push_back_from_value_iter::<foo::Foo, f64, _, _>(vec![
                Value::Na,
                Value::Na,
                Value::Na,
                Value::Na,
                Value::Na,
            ])
            .into_view();
        assert_eq!(dv.field::<foo::Foo>().max(), None);
    }
}