rustyml 0.14.0

A high-performance machine learning & deep learning library in pure Rust, offering ML algorithms and neural network support
Documentation
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
//! Regression metrics comparing ground-truth and predicted values.
//!
//! Provides MSE, RMSE, MAE, R^2, explained variance, median absolute error, and MAPE.

use ndarray::{Array1, ArrayBase, ArrayView1, Data, Ix1};

use super::validate_pair;

/// Calculates the Mean Squared Error (MSE) between ground-truth and predicted values.
///
/// MSE is the average of the squared differences between predictions and ground truth. Because
/// the per-sample error is squared, the order of the 2 arguments does not affect the result.
///
/// # Parameters
///
/// - `y_true` - Ground-truth values for each sample
/// - `y_pred` - Predicted values for each sample
///
/// # Returns
///
/// - `f64` - Mean squared error
///
/// # Examples
///
/// ```rust
/// use ndarray::array;
/// use rustyml::metrics::mean_squared_error;
///
/// let y_true = array![3.0, -0.5, 2.0, 7.0];
/// let y_pred = array![2.5, 0.0, 2.1, 7.8];
/// let mse = mean_squared_error(&y_true, &y_pred);
/// // MSE = ((3.0 - 2.5)^2 + (-0.5 - 0.0)^2 + (2.0 - 2.1)^2 + (7.0 - 7.8)^2) / 4
/// //     = (0.25 + 0.25 + 0.01 + 0.64) / 4 = 0.2875
/// assert!((mse - 0.2875).abs() < 1e-10);
/// ```
///
/// # Panics
///
/// - Panics if `y_true` and `y_pred` have different lengths.
/// - Panics if the inputs are empty.
pub fn mean_squared_error<S1, S2>(y_true: &ArrayBase<S1, Ix1>, y_pred: &ArrayBase<S2, Ix1>) -> f64
where
    S1: Data<Elem = f64>,
    S2: Data<Elem = f64>,
{
    validate_pair(y_true.len(), y_pred.len(), "y_true and y_pred");

    let sum_squared_error = y_true.iter().zip(y_pred.iter()).fold(0.0, |acc, (&t, &p)| {
        let error = t - p;
        acc + error * error
    });

    sum_squared_error / y_true.len() as f64
}

/// Calculates the Root Mean Squared Error (RMSE) between ground-truth and predicted values.
///
/// RMSE is the square root of the [`mean_squared_error`], giving an error in the same units as
/// the original data. Because MSE is non-negative, the square root is always well-defined.
///
/// # Parameters
///
/// - `y_true` - Ground-truth values for each sample
/// - `y_pred` - Predicted values for each sample
///
/// # Returns
///
/// - `f64` - Root mean squared error
///
/// # Examples
///
/// ```rust
/// use ndarray::array;
/// use rustyml::metrics::root_mean_squared_error;
///
/// let y_true = array![1.0, 2.0, 3.0];
/// let y_pred = array![2.0, 3.0, 4.0];
/// let rmse = root_mean_squared_error(&y_true, &y_pred);
/// // RMSE = sqrt(((2 - 1)^2 + (3 - 2)^2 + (4 - 3)^2) / 3) = sqrt(3/3) = 1.0
/// assert!((rmse - 1.0).abs() < 1e-6);
/// ```
///
/// # Panics
///
/// - Panics if `y_true` and `y_pred` have different lengths.
/// - Panics if the inputs are empty.
pub fn root_mean_squared_error<S1, S2>(
    y_true: &ArrayBase<S1, Ix1>,
    y_pred: &ArrayBase<S2, Ix1>,
) -> f64
where
    S1: Data<Elem = f64>,
    S2: Data<Elem = f64>,
{
    mean_squared_error(y_true, y_pred).sqrt()
}

/// Calculates the Mean Absolute Error (MAE) between ground-truth and predicted values.
///
/// MAE is the average absolute difference between predictions and ground truth, ignoring the
/// direction of the error. The order of the 2 arguments does not affect the result.
///
/// # Parameters
///
/// - `y_true` - Ground-truth values for each sample
/// - `y_pred` - Predicted values for each sample
///
/// # Returns
///
/// - `f64` - Mean absolute error
///
/// # Examples
///
/// ```rust
/// use ndarray::array;
/// use rustyml::metrics::mean_absolute_error;
///
/// let y_true = array![1.0, 2.0, 3.0];
/// let y_pred = array![2.0, 3.0, 4.0];
/// let mae = mean_absolute_error(&y_true, &y_pred);
/// // MAE = (|2 - 1| + |3 - 2| + |4 - 3|) / 3 = (1 + 1 + 1) / 3 = 1.0
/// assert!((mae - 1.0).abs() < 1e-6);
/// ```
///
/// # Panics
///
/// - Panics if `y_true` and `y_pred` have different lengths.
/// - Panics if the inputs are empty.
pub fn mean_absolute_error<S1, S2>(y_true: &ArrayBase<S1, Ix1>, y_pred: &ArrayBase<S2, Ix1>) -> f64
where
    S1: Data<Elem = f64>,
    S2: Data<Elem = f64>,
{
    validate_pair(y_true.len(), y_pred.len(), "y_true and y_pred");

    let sum_absolute_error = y_true
        .iter()
        .zip(y_pred.iter())
        .fold(0.0, |acc, (&t, &p)| acc + (t - p).abs());

    sum_absolute_error / y_true.len() as f64
}

/// Calculates the R-squared (coefficient of determination) score.
///
/// R^2 measures how well predictions explain the variance in the ground truth. The formula is
/// `R^2 = 1 - SSE / SST`. Here `SSE = sum(y_pred - y_true)^2` and
/// `SST = sum(y_true - mean(y_true))^2`. SST comes from `y_true` alone, so the argument order
/// changes the result.
///
/// When every entry of `y_true` is identical, the score is undefined. By convention, this returns
/// `1.0` for an exact fit (`SSE == 0`) and `0.0` otherwise, matching scikit-learn's `r2_score`.
/// The function recognizes the constant case from the values themselves. A target that varies
/// only slightly (for example `[1e-6, 2e-6, 3e-6]`) is scored normally, not mistaken for a
/// constant.
///
/// # Parameters
///
/// - `y_true` - Ground-truth values for each sample
/// - `y_pred` - Predicted values for each sample
///
/// # Returns
///
/// - `f64` - R-squared value (typically in `(-inf, 1.0]`)
///
/// # NaN Handling
///
/// Unlike [`explained_variance_score`], this does **not** skip non-finite samples. `SSE` and
/// `SST` are plain sums, so a single `NaN`/`inf` in `y_true` or `y_pred` propagates and makes the
/// result `NaN`. That surfaces bad data rather than hiding it. Prefer it when you want corruption
/// to be visible. Clean the inputs beforehand if you do not.
///
/// # Examples
///
/// ```rust
/// use ndarray::array;
/// use rustyml::metrics::r2_score;
///
/// let y_true = array![1.0, 3.0, 5.0];
/// let y_pred = array![2.0, 3.0, 4.0];
/// let r2 = r2_score(&y_true, &y_pred);
/// // mean(y_true) = 3, SST = 4 + 0 + 4 = 8, SSE = 1 + 0 + 1 = 2, so R^2 = 1 - 2/8 = 0.75
/// assert!((r2 - 0.75).abs() < 1e-6);
/// ```
///
/// # Panics
///
/// - Panics if `y_true` and `y_pred` have different lengths.
/// - Panics if the inputs are empty.
pub fn r2_score<S1, S2>(y_true: &ArrayBase<S1, Ix1>, y_pred: &ArrayBase<S2, Ix1>) -> f64
where
    S1: Data<Elem = f64>,
    S2: Data<Elem = f64>,
{
    validate_pair(y_true.len(), y_pred.len(), "y_true and y_pred");

    let mean = y_true.mean().unwrap();
    let sse: f64 = y_pred
        .iter()
        .zip(y_true.iter())
        .map(|(p, a)| (p - a).powi(2))
        .sum();

    // A constant `y_true` leaves R^2 undefined. By convention, a perfect fit scores 1.0 and
    // anything else scores 0.0.
    //
    // The check compares the raw values instead of testing `sst == 0.0`. Floating-point sums do
    // not round-trip every constant. 3 copies of `0.1` leave `sst` at 5.8e-34, not zero.
    // A threshold on `sst` could miss real constants or flag a low-variance target as constant.
    let mut values = y_true.iter();
    let first = *values.next().expect("validate_pair rejects an empty input");
    if values.all(|&v| v == first) {
        return if sse == 0.0 { 1.0 } else { 0.0 };
    }

    let sst: f64 = y_true.mapv(|x| (x - mean).powi(2)).sum();

    1.0 - sse / sst
}

/// Calculates the explained variance score.
///
/// Uses `1 - Var(y_true - y_pred) / Var(y_true)`. Unlike [`r2_score`], the numerator is the
/// variance of the residuals rather than their mean square, so a constant prediction bias does
/// not lower the score. The best possible value is 1.0. When `y_true` has zero variance, the
/// score is undefined: it returns 1.0 for residuals of zero variance, and 0.0 otherwise.
///
/// # Parameters
///
/// - `y_true` - Ground-truth values for each sample
/// - `y_pred` - Predicted values for each sample
///
/// # Returns
///
/// - `f64` - Explained variance score (typically in `(-inf, 1.0]`)
///
/// # NaN Handling
///
/// This uses a NaN-skipping variance. It **silently skips** non-finite samples and averages over
/// the finite subset. Unlike [`r2_score`], where a `NaN` propagates to a `NaN` result, a few
/// `NaN`/`inf` entries here leave a normal-looking score computed from the rest. This is
/// convenient but can mask corrupt data. Validate the inputs first if a silently dropped sample
/// would be a problem.
///
/// # Examples
///
/// ```rust
/// use ndarray::array;
/// use rustyml::metrics::explained_variance_score;
///
/// let y_true = array![1.0, 2.0, 3.0];
/// let y_pred = array![2.0, 3.0, 4.0]; // a constant +1 bias
/// // The residuals are all -1, so their variance is 0 and the score is 1.0, even though the
/// // predictions are biased (r2_score would be lower here).
/// assert!((explained_variance_score(&y_true, &y_pred) - 1.0).abs() < 1e-12);
/// ```
///
/// # Panics
///
/// - Panics if `y_true` and `y_pred` have different lengths.
/// - Panics if the inputs are empty.
pub fn explained_variance_score<S1, S2>(
    y_true: &ArrayBase<S1, Ix1>,
    y_pred: &ArrayBase<S2, Ix1>,
) -> f64
where
    S1: Data<Elem = f64>,
    S2: Data<Elem = f64>,
{
    validate_pair(y_true.len(), y_pred.len(), "y_true and y_pred");

    let residuals: Array1<f64> = y_true
        .iter()
        .zip(y_pred.iter())
        .map(|(&t, &p)| t - p)
        .collect();

    // Computes population variance over the finite subset. A constant (or empty) subset has zero
    // spread by definition. The check compares the raw values instead of testing the computed
    // sum of squares for zero. Floating-point sums do not round-trip every constant (8 copies
    // of `0.1` do not), so a near-zero threshold could misjudge real data.
    let variance = |v: ArrayView1<f64>| -> f64 {
        let mut sum = 0.0_f64;
        let mut count = 0_usize;
        let mut first: Option<f64> = None;
        let mut constant = true;
        for &val in v.iter() {
            if !val.is_finite() {
                continue;
            }
            sum += val;
            count += 1;
            match first {
                None => first = Some(val),
                Some(f) => constant &= val == f,
            }
        }
        if count == 0 || constant {
            return 0.0;
        }
        let mean = sum / count as f64;
        let ss = v.iter().fold(0.0, |acc, &val| {
            if val.is_finite() {
                let d = val - mean;
                acc + d * d
            } else {
                acc
            }
        });
        ss / count as f64
    };
    let residual_variance = variance(residuals.view());
    let true_variance = variance(y_true.view());

    // Same convention as [`r2_score`]. A constant `y_true` leaves the ratio undefined: residuals
    // of zero variance score 1.0, anything else scores 0.0. Both checks use exact comparisons.
    if true_variance == 0.0 {
        return if residual_variance == 0.0 { 1.0 } else { 0.0 };
    }

    1.0 - residual_variance / true_variance
}

/// Calculates the median absolute error between ground-truth and predicted values.
///
/// The median of the absolute errors is robust to outliers, so a few large mistakes do not
/// dominate it the way they do in [`mean_absolute_error`].
///
/// # Parameters
///
/// - `y_true` - Ground-truth values for each sample
/// - `y_pred` - Predicted values for each sample
///
/// # Returns
///
/// - `f64` - Median absolute error (>= 0.0)
///
/// # Examples
///
/// ```rust
/// use ndarray::array;
/// use rustyml::metrics::median_absolute_error;
///
/// let y_true = array![1.0, 2.0, 3.0, 4.0];
/// let y_pred = array![1.0, 2.0, 3.0, 10.0]; // one large outlier error of 6
/// // Sorted absolute errors are [0, 0, 0, 6]. The median is 0.0, unmoved by the outlier.
/// assert!((median_absolute_error(&y_true, &y_pred) - 0.0).abs() < 1e-12);
/// ```
///
/// # Panics
///
/// - Panics if `y_true` and `y_pred` have different lengths.
/// - Panics if the inputs are empty.
pub fn median_absolute_error<S1, S2>(
    y_true: &ArrayBase<S1, Ix1>,
    y_pred: &ArrayBase<S2, Ix1>,
) -> f64
where
    S1: Data<Elem = f64>,
    S2: Data<Elem = f64>,
{
    validate_pair(y_true.len(), y_pred.len(), "y_true and y_pred");

    let mut errors: Vec<f64> = y_true
        .iter()
        .zip(y_pred.iter())
        .map(|(&t, &p)| (t - p).abs())
        .collect();
    errors.sort_by(|a, b| a.total_cmp(b));

    let n = errors.len();
    if n % 2 == 1 {
        errors[n / 2]
    } else {
        (errors[n / 2 - 1] + errors[n / 2]) / 2.0
    }
}

/// Calculates the mean absolute percentage error (MAPE) between ground-truth and predicted
/// values.
///
/// Uses `mean(|y_true - y_pred| / max(|y_true|, eps))`. The result is a fraction. Multiply by
/// 100 for a percentage. Each denominator is floored at a tiny epsilon. This keeps a zero true
/// value from causing a division by zero. The score can still be inflated when the true value
/// is near zero.
///
/// # Parameters
///
/// - `y_true` - Ground-truth values for each sample
/// - `y_pred` - Predicted values for each sample
///
/// # Returns
///
/// - `f64` - Mean absolute percentage error as a fraction (>= 0.0)
///
/// # Examples
///
/// ```rust
/// use ndarray::array;
/// use rustyml::metrics::mean_absolute_percentage_error;
///
/// let y_true = array![2.0, 4.0, 5.0];
/// let y_pred = array![1.0, 4.0, 5.0];
/// // Per-sample ratios are 0.5, 0, 0, so MAPE = 0.5 / 3 = 0.1666...
/// assert!((mean_absolute_percentage_error(&y_true, &y_pred) - 0.166666667).abs() < 1e-6);
/// ```
///
/// # Panics
///
/// - Panics if `y_true` and `y_pred` have different lengths.
/// - Panics if the inputs are empty.
pub fn mean_absolute_percentage_error<S1, S2>(
    y_true: &ArrayBase<S1, Ix1>,
    y_pred: &ArrayBase<S2, Ix1>,
) -> f64
where
    S1: Data<Elem = f64>,
    S2: Data<Elem = f64>,
{
    validate_pair(y_true.len(), y_pred.len(), "y_true and y_pred");

    const EPS: f64 = f64::EPSILON;
    let sum: f64 = y_true
        .iter()
        .zip(y_pred.iter())
        .map(|(&t, &p)| (t - p).abs() / t.abs().max(EPS))
        .sum();

    sum / y_true.len() as f64
}