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
//! Common metrics for regression
//!
//! This module implements common comparison metrices for continuous variables.

use crate::{
    dataset::{AsMultiTargets, AsSingleTargets, DatasetBase},
    error::{Error, Result},
    Float,
};
use ndarray::prelude::*;
use ndarray::Data;
use std::ops::{Div, Sub};

/// Regression metrices trait for single targets.
///
/// It is possible to compute the listed mectrics between two 1D arrays.
/// To compare bi-dimensional arrays use [`MultiTargetRegression`].
pub trait SingleTargetRegression<F: Float, T: AsSingleTargets<Elem = F>>:
    AsSingleTargets<Elem = F>
{
    /// Maximal error between two continuous variables
    fn max_error(&self, compare_to: &T) -> Result<F> {
        let max_error = self
            .as_single_targets()
            .sub(&compare_to.as_single_targets())
            .iter()
            .map(|x| x.abs())
            .fold(F::neg_infinity(), F::max);
        Ok(max_error)
    }
    /// Mean error between two continuous variables
    fn mean_absolute_error(&self, compare_to: &T) -> Result<F> {
        self.as_single_targets()
            .sub(&compare_to.as_single_targets())
            .mapv(|x| x.abs())
            .mean()
            .ok_or(Error::NotEnoughSamples)
    }

    /// Mean squared error between two continuous variables
    fn mean_squared_error(&self, compare_to: &T) -> Result<F> {
        self.as_single_targets()
            .sub(&compare_to.as_single_targets())
            .mapv(|x| x * x)
            .mean()
            .ok_or(Error::NotEnoughSamples)
    }

    /// Mean squared log error between two continuous variables
    fn mean_squared_log_error(&self, compare_to: &T) -> Result<F> {
        self.as_single_targets()
            .mapv(|x| (F::one() + x).ln())
            .mean_squared_error(&compare_to.as_single_targets().mapv(|x| (F::one() + x).ln()))
    }

    /// Median absolute error between two continuous variables
    fn median_absolute_error(&self, compare_to: &T) -> Result<F> {
        let mut abs_error = self
            .as_single_targets()
            .sub(&compare_to.as_single_targets())
            .mapv(|x| x.abs())
            .to_vec();
        abs_error.sort_by(|a, b| a.partial_cmp(b).unwrap());
        let mid = abs_error.len() / 2;
        if abs_error.len() % 2 == 0 {
            Ok((abs_error[mid - 1] + abs_error[mid]) / F::cast(2.0))
        } else {
            Ok(abs_error[mid])
        }
    }

    /// Mean absolute percentage error between two continuous variables
    /// MAPE = 1/N * SUM(abs((y_hat - y) / y))
    fn mean_absolute_percentage_error(&self, compare_to: &T) -> Result<F> {
        self.as_single_targets()
            .sub(&compare_to.as_single_targets())
            .div(&self.as_single_targets())
            .mapv(|x| x.abs())
            .mean()
            .ok_or(Error::NotEnoughSamples)
    }

    /// R squared coefficient, is the proportion of the variance in the dependent variable that is
    /// predictable from the independent variable
    // r2 = 1 - sum((pred_i - y_i)^2)/sum((mean_y - y_i)^2)
    // if the mean is of `compare_to`, then the denominator
    // should compare `compare_to` and the mean, and not self and the mean
    fn r2(&self, compare_to: &T) -> Result<F> {
        let single_target_compare_to = compare_to.as_single_targets();
        let mean = single_target_compare_to
            .mean()
            .ok_or(Error::NotEnoughSamples)?;

        Ok(F::one()
            - self
                .as_single_targets()
                .sub(&single_target_compare_to)
                .mapv(|x| x * x)
                .sum()
                / (single_target_compare_to
                    .mapv(|x| (x - mean) * (x - mean))
                    .sum()
                    + F::cast(1e-10)))
    }

    /// Same as R-Squared but with biased variance
    fn explained_variance(&self, compare_to: &T) -> Result<F> {
        let single_target_compare_to = compare_to.as_single_targets();
        let diff = self.as_single_targets().sub(&single_target_compare_to);

        let mean = single_target_compare_to
            .mean()
            .ok_or(Error::NotEnoughSamples)?;
        let mean_error = diff.mean().ok_or(Error::NotEnoughSamples)?;

        Ok(F::one()
            - (diff.mapv(|x| x * x).sum() - mean_error)
                / (single_target_compare_to
                    .mapv(|x| (x - mean) * (x - mean))
                    .sum()
                    + F::cast(1e-10)))
    }
}

impl<F: Float, D: Data<Elem = F>, T: AsSingleTargets<Elem = F>> SingleTargetRegression<F, T>
    for ArrayBase<D, Ix1>
{
}

impl<F: Float, T: AsSingleTargets<Elem = F>, T2: AsSingleTargets<Elem = F>, D: Data<Elem = F>>
    SingleTargetRegression<F, T2> for DatasetBase<ArrayBase<D, Ix2>, T>
{
}

/// Regression metrices trait for multiple targets.
///
/// It is possible to compute the listed mectrics between two 2D arrays.
/// To compare single-dimensional arrays use [`SingleTargetRegression`].
pub trait MultiTargetRegression<F: Float, T: AsMultiTargets<Elem = F>>:
    AsMultiTargets<Elem = F>
{
    /// Maximal error between two continuous variables
    fn max_error(&self, other: &T) -> Result<Array1<F>> {
        self.as_multi_targets()
            .axis_iter(Axis(1))
            .zip(other.as_multi_targets().axis_iter(Axis(1)))
            .map(|(a, b)| a.max_error(&b))
            .collect()
    }
    /// Mean error between two continuous variables
    fn mean_absolute_error(&self, other: &T) -> Result<Array1<F>> {
        self.as_multi_targets()
            .axis_iter(Axis(1))
            .zip(other.as_multi_targets().axis_iter(Axis(1)))
            .map(|(a, b)| a.mean_absolute_error(&b))
            .collect()
    }

    /// Mean squared error between two continuous variables
    fn mean_squared_error(&self, other: &T) -> Result<Array1<F>> {
        self.as_multi_targets()
            .axis_iter(Axis(1))
            .zip(other.as_multi_targets().axis_iter(Axis(1)))
            .map(|(a, b)| a.mean_squared_error(&b))
            .collect()
    }

    /// Mean squared log error between two continuous variables
    fn mean_squared_log_error(&self, other: &T) -> Result<Array1<F>> {
        self.as_multi_targets()
            .axis_iter(Axis(1))
            .zip(other.as_multi_targets().axis_iter(Axis(1)))
            .map(|(a, b)| a.mean_squared_log_error(&b))
            .collect()
    }

    /// Median absolute error between two continuous variables
    fn median_absolute_error(&self, other: &T) -> Result<Array1<F>> {
        self.as_multi_targets()
            .axis_iter(Axis(1))
            .zip(other.as_multi_targets().axis_iter(Axis(1)))
            .map(|(a, b)| a.median_absolute_error(&b))
            .collect()
    }

    /// Mean absolute percentage error between two continuous variables
    /// MAPE = 1/N * SUM(abs((y_hat - y) / y))
    fn mean_absolute_percentage_error(&self, other: &T) -> Result<Array1<F>> {
        self.as_multi_targets()
            .axis_iter(Axis(1))
            .zip(other.as_multi_targets().axis_iter(Axis(1)))
            .map(|(a, b)| a.mean_absolute_percentage_error(&b))
            .collect()
    }

    /// R squared coefficient, is the proportion of the variance in the dependent variable that is
    /// predictable from the independent variable
    fn r2(&self, other: &T) -> Result<Array1<F>> {
        self.as_multi_targets()
            .axis_iter(Axis(1))
            .zip(other.as_multi_targets().axis_iter(Axis(1)))
            .map(|(a, b)| a.r2(&b))
            .collect()
    }

    /// Same as R-Squared but with biased variance
    fn explained_variance(&self, other: &T) -> Result<Array1<F>> {
        self.as_multi_targets()
            .axis_iter(Axis(1))
            .zip(other.as_multi_targets().axis_iter(Axis(1)))
            .map(|(a, b)| a.explained_variance(&b))
            .collect()
    }
}

impl<F: Float, D: Data<Elem = F>, T: AsMultiTargets<Elem = F>> MultiTargetRegression<F, T>
    for ArrayBase<D, Ix2>
{
}

impl<F: Float, T: AsMultiTargets<Elem = F>, T2: AsMultiTargets<Elem = F>, D: Data<Elem = F>>
    MultiTargetRegression<F, T2> for DatasetBase<ArrayBase<D, Ix2>, T>
{
}

#[cfg(test)]
mod tests {
    use super::SingleTargetRegression;
    use crate::dataset::DatasetBase;
    use approx::assert_abs_diff_eq;
    use ndarray::prelude::*;

    #[test]
    fn test_same() {
        let a: Array1<f32> = Array1::ones(100);

        assert_abs_diff_eq!(a.max_error(&a).unwrap(), 0.0f32);
        assert_abs_diff_eq!(a.mean_absolute_error(&a).unwrap(), 0.0f32);
        assert_abs_diff_eq!(a.mean_squared_error(&a).unwrap(), 0.0f32);
        assert_abs_diff_eq!(a.mean_squared_log_error(&a).unwrap(), 0.0f32);
        assert_abs_diff_eq!(a.median_absolute_error(&a).unwrap(), 0.0f32);
        assert_abs_diff_eq!(a.r2(&a).unwrap(), 1.0f32);
        assert_abs_diff_eq!(a.explained_variance(&a).unwrap(), 1.0f32);
        assert_abs_diff_eq!(a.mean_absolute_percentage_error(&a).unwrap(), 0.0f32);
    }

    #[test]
    fn test_max_error() {
        let a = array![0.0, 0.1, 0.2, 0.3, 0.4];
        let b = array![0.1, 0.3, 0.2, 0.5, 0.7];

        assert_abs_diff_eq!(a.max_error(&b).unwrap(), 0.3f32, epsilon = 1e-5);
    }

    #[test]
    fn test_median_absolute_error() {
        let a = array![0.0, 0.1, 0.2, 0.3, 0.4];
        let b = array![0.1, 0.3, 0.2, 0.5, 0.7];
        // 0.1, 0.2, 0.0, 0.2, 0.3 -> median error is 0.2

        assert_abs_diff_eq!(a.median_absolute_error(&b).unwrap(), 0.2f32, epsilon = 1e-5);
    }

    #[test]
    fn test_mean_squared_error() {
        let a = array![0.0, 0.1, 0.2, 0.3, 0.4];
        let b = array![0.1, 0.2, 0.3, 0.4, 0.5];

        assert_abs_diff_eq!(a.mean_squared_error(&b).unwrap(), 0.01, epsilon = 1e-5);
    }

    #[test]
    fn test_mean_absolute_percentage_error() {
        let a = array![0.5, 0.1, 0.2, 0.3, 0.4];
        let b = array![0.1, 0.2, 0.3, 0.4, 0.5];

        assert_abs_diff_eq!(
            a.mean_absolute_percentage_error(&b).unwrap(),
            0.5766666666666667,
            epsilon = 1e-5
        );
    }

    #[test]
    fn test_max_error_for_single_targets() {
        let records = array![[0.0, 0.0], [0.1, 0.1], [0.2, 0.2], [0.3, 0.3], [0.4, 0.4]];
        let targets = array![0.0, 0.1, 0.2, 0.3, 0.4];
        let st_dataset: DatasetBase<_, _> = (records.view(), targets).into();
        let prediction: Array1<f64> = array![0.1, 0.3, 0.2, 0.5, 0.7];
        let abs_err_from_arr1 = prediction.max_error(st_dataset.targets()).unwrap();
        let prediction: DatasetBase<_, _> = (records.view(), prediction.view()).into();
        let abs_err_from_ds = prediction.max_error(st_dataset.targets()).unwrap();
        assert_abs_diff_eq!(abs_err_from_arr1, 0.3);
        assert_abs_diff_eq!(abs_err_from_arr1, abs_err_from_ds);
    }

    #[test]
    fn test_mean_absolute_error_for_single_targets() {
        let records = array![[0.0, 0.0], [0.1, 0.1], [0.2, 0.2], [0.3, 0.3], [0.4, 0.4]];
        let targets = array![0.0, 0.1, 0.2, 0.3, 0.4];
        let st_dataset: DatasetBase<_, _> = (records.view(), targets).into();
        let prediction = array![0.1, 0.3, 0.2, 0.5, 0.7];
        let abs_err_from_arr1 = prediction.mean_absolute_error(&st_dataset).unwrap();
        let prediction: DatasetBase<_, _> = (records.view(), prediction).into();
        let abs_err_from_ds = prediction
            .mean_absolute_error(st_dataset.targets())
            .unwrap();
        assert_abs_diff_eq!(abs_err_from_arr1, 0.16);
        assert_abs_diff_eq!(abs_err_from_arr1, abs_err_from_ds);
    }

    #[test]
    fn test_mean_squared_error_for_single_targets() {
        let records = array![[0.0, 0.0], [0.1, 0.1], [0.2, 0.2], [0.3, 0.3], [0.4, 0.4]];
        let targets = array![0.0, 0.1, 0.2, 0.3, 0.4];
        let st_dataset: DatasetBase<_, _> = (records.view(), targets).into();
        let prediction = array![0.1, 0.3, 0.2, 0.5, 0.7];
        let abs_err_from_arr1 = prediction.mean_squared_error(st_dataset.targets()).unwrap();
        let prediction: DatasetBase<_, _> = (records.view(), prediction).into();
        let abs_err_from_ds = prediction.mean_squared_error(st_dataset.targets()).unwrap();
        assert_abs_diff_eq!(abs_err_from_arr1, 0.036);
        assert_abs_diff_eq!(abs_err_from_arr1, abs_err_from_ds);
    }

    #[test]
    fn test_mean_absolute_percentage_error_for_single_targets() {
        let records = array![[0.0, 0.0], [0.1, 0.1], [0.2, 0.2], [0.3, 0.3], [0.4, 0.4]];
        let targets = array![0.0, 0.1, 0.2, 0.3, 0.4];
        let st_dataset: DatasetBase<_, _> = (records.view(), targets).into();
        let prediction = array![0.1, 0.3, 0.2, 0.5, 0.7];
        let pct_err_from_arr1 = prediction
            .mean_absolute_percentage_error(st_dataset.targets())
            .unwrap();
        let prediction: DatasetBase<_, _> = (records.view(), prediction).into();
        let pct_err_from_ds = prediction
            .mean_absolute_percentage_error(st_dataset.targets())
            .unwrap();
        assert_abs_diff_eq!(pct_err_from_arr1, 0.49904761904761896);
        assert_abs_diff_eq!(pct_err_from_arr1, pct_err_from_ds);
    }

    #[test]
    fn test_mean_squared_log_error_for_single_targets() {
        let records = array![[0.0, 0.0], [0.1, 0.1], [0.2, 0.2], [0.3, 0.3], [0.4, 0.4]];
        let targets = array![0.0, 0.1, 0.2, 0.3, 0.4];
        let st_dataset: DatasetBase<_, _> = (records.view(), targets).into();
        let prediction = array![0.1, 0.3, 0.2, 0.5, 0.7];
        let abs_err_from_arr1 = prediction
            .mean_squared_log_error(st_dataset.targets())
            .unwrap();
        let prediction: DatasetBase<_, _> = (records.view(), prediction).into();
        let abs_err_from_ds = prediction
            .mean_squared_log_error(st_dataset.targets())
            .unwrap();
        assert_abs_diff_eq!(abs_err_from_arr1, 0.019_033, epsilon = 1e-5);
        assert_abs_diff_eq!(abs_err_from_arr1, abs_err_from_ds);
    }

    #[test]
    fn test_median_absolute_error_for_single_targets() {
        let records = array![[0.0, 0.0], [0.1, 0.1], [0.2, 0.2], [0.3, 0.3],];
        let targets = array![0.0, 0.1, 0.2, 0.4];
        let st_dataset: DatasetBase<_, _> = (records.view(), targets).into();
        let prediction = array![0.1, 0.3, 0.2, 0.7];
        // even length absolute errors
        let abs_err_from_arr1 = prediction
            .median_absolute_error(st_dataset.targets())
            .unwrap();
        let prediction: DatasetBase<_, _> = (records.view(), prediction).into();
        let abs_err_from_ds = prediction
            .median_absolute_error(st_dataset.targets())
            .unwrap();
        assert_abs_diff_eq!(abs_err_from_arr1, 0.15, epsilon = 1e-5);
        assert_abs_diff_eq!(abs_err_from_arr1, abs_err_from_ds);

        // odd length absolute errors
        let records = array![[0.0, 0.0], [0.1, 0.1], [0.2, 0.2], [0.3, 0.3], [0.4, 0.4]];
        let targets = array![0.0, 0.1, 0.2, 0.3, 0.4];
        let st_dataset: DatasetBase<_, _> = (records.view(), targets).into();
        let prediction = array![0.1, 0.3, 0.2, 0.51, 0.7];
        let abs_err_from_arr1 = prediction.median_absolute_error(&st_dataset).unwrap();
        let prediction: DatasetBase<_, _> = (records.view(), prediction).into();
        let abs_err_from_ds = prediction.median_absolute_error(&st_dataset).unwrap();
        assert_abs_diff_eq!(abs_err_from_arr1, 0.2, epsilon = 1e-5);
        assert_abs_diff_eq!(abs_err_from_arr1, abs_err_from_ds);
    }

    #[test]
    fn test_r2_for_single_targets() {
        let records = array![[0.0, 0.0], [0.1, 0.1], [0.2, 0.2], [0.3, 0.3], [0.4, 0.4]];
        let targets = array![0.0, 0.1, 0.2, 0.3, 0.4];
        let st_dataset: DatasetBase<_, _> = (records.view(), targets).into();
        let prediction = array![0.1, 0.3, 0.2, 0.5, 0.7];
        let abs_err_from_arr1 = prediction.r2(st_dataset.targets()).unwrap();
        let prediction: DatasetBase<_, _> = (records.view(), prediction).into();
        let abs_err_from_ds = prediction.r2(st_dataset.targets()).unwrap();
        assert_abs_diff_eq!(abs_err_from_arr1, -0.8, epsilon = 1e-5);
        assert_abs_diff_eq!(abs_err_from_arr1, abs_err_from_ds);
    }

    #[test]
    fn test_explained_variance_for_single_targets() {
        let records = array![[0.0, 0.0], [0.1, 0.1], [0.2, 0.2], [0.3, 0.3], [0.4, 0.4]];
        let targets = array![0.0, 0.1, 0.2, 0.3, 0.4];
        let st_dataset: DatasetBase<_, _> = (records.view(), targets).into();
        let prediction = array![0.1, 0.3, 0.2, 0.5, 0.7];
        let abs_err_from_arr1 = prediction.explained_variance(st_dataset.targets()).unwrap();
        let prediction: DatasetBase<_, _> = (records.view(), prediction).into();
        let abs_err_from_ds = prediction.explained_variance(&st_dataset).unwrap();
        assert_abs_diff_eq!(abs_err_from_arr1, 0.8, epsilon = 1e-5);
        assert_abs_diff_eq!(abs_err_from_arr1, abs_err_from_ds);
    }
}