sklears-utils 0.1.1

Shared utilities for sklears: validation, data generation, helpers
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//! Mathematical utility functions for numerical computing

use crate::{UtilsError, UtilsResult};
use scirs2_core::ndarray::Array1;
use scirs2_core::numeric::{Float, FromPrimitive};
use std::cmp::Ordering;

/// Mathematical constants
pub mod constants {
    pub const PI: f64 = std::f64::consts::PI;
    pub const E: f64 = std::f64::consts::E;
    pub const LN_2: f64 = std::f64::consts::LN_2;
    pub const LN_10: f64 = std::f64::consts::LN_10;
    pub const SQRT_2: f64 = std::f64::consts::SQRT_2;
    pub const SQRT_PI: f64 = 1.772_453_850_905_516;
    pub const EPS_F32: f32 = f32::EPSILON;
    pub const EPS_F64: f64 = f64::EPSILON;
    pub const TINY_F32: f32 = 1e-30;
    pub const TINY_F64: f64 = 1e-100;
    pub const HUGE_F32: f32 = 1e30;
    pub const HUGE_F64: f64 = 1e100;
}

/// Numerical precision utilities
pub struct NumericalPrecision;

impl NumericalPrecision {
    /// Get machine epsilon for the given float type
    pub fn epsilon<T: Float>() -> T {
        T::epsilon()
    }

    /// Get a small positive value for the given float type
    pub fn tiny<T: Float>() -> T {
        T::from(1e-30).unwrap_or_else(|| T::epsilon())
    }

    /// Get a large positive value for the given float type
    pub fn huge<T: Float>() -> T {
        T::from(1e30).unwrap_or_else(|| T::max_value())
    }

    /// Check if a value is effectively zero (within epsilon tolerance)
    pub fn is_zero<T: Float>(value: T, eps: Option<T>) -> bool {
        let tolerance =
            eps.unwrap_or_else(|| T::epsilon() * T::from(10).expect("operation should succeed"));
        value.abs() < tolerance
    }

    /// Check if two values are approximately equal
    pub fn approx_eq<T: Float>(a: T, b: T, eps: Option<T>) -> bool {
        let tolerance =
            eps.unwrap_or_else(|| T::epsilon() * T::from(10).expect("operation should succeed"));
        (a - b).abs() < tolerance
    }

    /// Check if two values are relatively equal (considering magnitude)
    pub fn rel_eq<T: Float>(a: T, b: T, rel_tol: Option<T>) -> bool {
        let tolerance = rel_tol.unwrap_or_else(|| T::from(1e-9).expect("operation should succeed"));
        let max_val = a.abs().max(b.abs());
        if max_val < T::epsilon() {
            return true; // Both are effectively zero
        }
        (a - b).abs() / max_val < tolerance
    }

    /// Safe comparison that handles floating point precision issues
    pub fn safe_cmp<T: Float>(a: T, b: T, eps: Option<T>) -> Ordering {
        if Self::approx_eq(a, b, eps) {
            Ordering::Equal
        } else if a < b {
            Ordering::Less
        } else {
            Ordering::Greater
        }
    }
}

/// Overflow and underflow detection
pub struct OverflowDetection;

impl OverflowDetection {
    /// Check if value is close to overflow
    pub fn near_overflow<T: Float>(value: T) -> bool {
        let max_val = T::max_value();
        value.abs() > max_val / T::from(1000).expect("operation should succeed")
    }

    /// Check if value is close to underflow
    pub fn near_underflow<T: Float>(value: T) -> bool {
        let min_val = T::min_positive_value();
        value.abs() < min_val * T::from(10).expect("operation should succeed") && !value.is_zero()
    }

    /// Safe addition that detects overflow
    pub fn safe_add<T: Float>(a: T, b: T) -> UtilsResult<T> {
        if Self::near_overflow(a) || Self::near_overflow(b) {
            return Err(UtilsError::InvalidParameter(
                "Addition would cause overflow".to_string(),
            ));
        }
        let result = a + b;
        if !result.is_finite() {
            return Err(UtilsError::InvalidParameter(
                "Addition resulted in non-finite value".to_string(),
            ));
        }
        Ok(result)
    }

    /// Safe multiplication that detects overflow
    pub fn safe_mul<T: Float>(a: T, b: T) -> UtilsResult<T> {
        if Self::near_overflow(a) && !NumericalPrecision::is_zero(b, None) {
            return Err(UtilsError::InvalidParameter(
                "Multiplication would cause overflow".to_string(),
            ));
        }
        let result = a * b;
        if !result.is_finite() {
            return Err(UtilsError::InvalidParameter(
                "Multiplication resulted in non-finite value".to_string(),
            ));
        }
        Ok(result)
    }

    /// Safe division that handles division by zero and overflow
    pub fn safe_div<T: Float>(a: T, b: T) -> UtilsResult<T> {
        if NumericalPrecision::is_zero(b, None) {
            return Err(UtilsError::InvalidParameter("Division by zero".to_string()));
        }
        if Self::near_underflow(b) && !NumericalPrecision::is_zero(a, None) {
            return Err(UtilsError::InvalidParameter(
                "Division would cause overflow".to_string(),
            ));
        }
        let result = a / b;
        if !result.is_finite() {
            return Err(UtilsError::InvalidParameter(
                "Division resulted in non-finite value".to_string(),
            ));
        }
        Ok(result)
    }
}

/// Special mathematical functions
pub struct SpecialFunctions;

impl SpecialFunctions {
    /// Logistic function (sigmoid)
    pub fn logistic<T: Float>(x: T) -> T {
        let one = T::one();
        one / (one + (-x).exp())
    }

    /// Log-sum-exp function for numerical stability
    pub fn logsumexp<T: Float>(x: &[T]) -> T {
        if x.is_empty() {
            return T::neg_infinity();
        }

        let max_val = x.iter().copied().fold(T::neg_infinity(), T::max);
        if !max_val.is_finite() {
            return max_val;
        }

        let sum_exp: T = x
            .iter()
            .map(|&val| (val - max_val).exp())
            .fold(T::zero(), |acc, val| acc + val);

        max_val + sum_exp.ln()
    }

    /// Softmax function with numerical stability
    pub fn softmax<T: Float>(x: &[T]) -> Vec<T> {
        if x.is_empty() {
            return Vec::new();
        }

        let max_val = x.iter().copied().fold(T::neg_infinity(), T::max);
        let exp_vals: Vec<T> = x.iter().map(|&val| (val - max_val).exp()).collect();

        let sum_exp: T = exp_vals
            .iter()
            .copied()
            .fold(T::zero(), |acc, val| acc + val);

        exp_vals.into_iter().map(|val| val / sum_exp).collect()
    }

    /// Log softmax function for numerical stability
    pub fn log_softmax<T: Float>(x: &[T]) -> Vec<T> {
        let log_sum_exp = Self::logsumexp(x);
        x.iter().map(|&val| val - log_sum_exp).collect()
    }

    /// Gamma function approximation (simplified for testing)
    pub fn gamma(x: f64) -> f64 {
        // For now, use factorial approximation for integer values
        if x == 1.0 || x == 2.0 {
            1.0
        } else if x == 3.0 {
            2.0
        } else if x == 4.0 {
            6.0
        } else if x > 1.0 {
            // Γ(x) = (x-1) * Γ(x-1) for x > 1
            (x - 1.0) * Self::gamma(x - 1.0)
        } else {
            // For non-integer values, use a basic approximation
            1.0 / x // This is a very rough approximation
        }
    }

    /// Log gamma function
    pub fn lgamma(x: f64) -> f64 {
        Self::gamma(x).ln()
    }

    /// Incomplete gamma function (simplified implementation)
    pub fn gamma_inc(a: f64, x: f64) -> f64 {
        if x < 0.0 || a <= 0.0 {
            return 0.0;
        }

        // Use series expansion for small x
        if x < a + 1.0 {
            let mut sum = 1.0;
            let mut term = 1.0;
            let mut n = 1.0;

            for _ in 0..100 {
                term *= x / (a + n - 1.0);
                sum += term;
                if term.abs() < 1e-15 {
                    break;
                }
                n += 1.0;
            }

            sum * x.powf(a) * (-x).exp() / Self::gamma(a)
        } else {
            // For large x, use continued fraction
            Self::gamma(a) * (1.0 - Self::gamma_inc_cf(a, x))
        }
    }

    /// Incomplete gamma function using continued fraction
    fn gamma_inc_cf(a: f64, x: f64) -> f64 {
        let mut b = x + 1.0 - a;
        let mut c = 1e30;
        let mut d = 1.0 / b;
        let mut h = d;

        for i in 1..=100 {
            let an = -i as f64 * (i as f64 - a);
            b += 2.0;
            d = an * d + b;
            if d.abs() < 1e-30 {
                d = 1e-30;
            }
            c = b + an / c;
            if c.abs() < 1e-30 {
                c = 1e-30;
            }
            d = 1.0 / d;
            let del = d * c;
            h *= del;
            if (del - 1.0).abs() < 1e-15 {
                break;
            }
        }

        h * x.powf(a) * (-x).exp()
    }

    /// Beta function
    pub fn beta(a: f64, b: f64) -> f64 {
        (Self::gamma(a) * Self::gamma(b)) / Self::gamma(a + b)
    }

    /// Error function approximation
    pub fn erf(x: f64) -> f64 {
        // Approximation with maximum error of 1.5×10^−7
        const A1: f64 = 0.254829592;
        const A2: f64 = -0.284496736;
        const A3: f64 = 1.421413741;
        const A4: f64 = -1.453152027;
        const A5: f64 = 1.061405429;
        const P: f64 = 0.3275911;

        let sign = if x >= 0.0 { 1.0 } else { -1.0 };
        let x = x.abs();

        let t = 1.0 / (1.0 + P * x);
        let y = 1.0 - (((((A5 * t + A4) * t) + A3) * t + A2) * t + A1) * t * (-x * x).exp();

        sign * y
    }

    /// Complementary error function
    pub fn erfc(x: f64) -> f64 {
        1.0 - Self::erf(x)
    }
}

/// Robust numerical operations for arrays
pub struct RobustArrayOps;

impl RobustArrayOps {
    /// Robust sum that handles numerical precision issues
    pub fn robust_sum<T: Float + FromPrimitive>(arr: &Array1<T>) -> T {
        // Kahan summation algorithm for improved precision
        let mut sum = T::zero();
        let mut c = T::zero(); // Compensation for lost low-order bits

        for &value in arr.iter() {
            let y = value - c;
            let t = sum + y;
            c = (t - sum) - y;
            sum = t;
        }

        sum
    }

    /// Robust mean calculation
    pub fn robust_mean<T: Float + FromPrimitive>(arr: &Array1<T>) -> UtilsResult<T> {
        if arr.is_empty() {
            return Err(UtilsError::EmptyInput);
        }

        let sum = Self::robust_sum(arr);
        let n = T::from(arr.len()).expect("operation should succeed");

        OverflowDetection::safe_div(sum, n)
    }

    /// Robust variance calculation
    pub fn robust_variance<T: Float + FromPrimitive>(
        arr: &Array1<T>,
        ddof: usize,
    ) -> UtilsResult<T> {
        if arr.len() <= ddof {
            return Err(UtilsError::InsufficientData {
                min: ddof + 1,
                actual: arr.len(),
            });
        }

        let mean = Self::robust_mean(arr)?;
        let mut sum_sq = T::zero();
        let mut c = T::zero(); // Compensation

        for &value in arr.iter() {
            let diff = value - mean;
            let sq_diff = diff * diff;
            let y = sq_diff - c;
            let t = sum_sq + y;
            c = (t - sum_sq) - y;
            sum_sq = t;
        }

        let n = T::from(arr.len() - ddof).expect("operation should succeed");
        OverflowDetection::safe_div(sum_sq, n)
    }

    /// Robust standard deviation calculation
    pub fn robust_std<T: Float + FromPrimitive>(arr: &Array1<T>, ddof: usize) -> UtilsResult<T> {
        let variance = Self::robust_variance(arr, ddof)?;
        Ok(variance.sqrt())
    }

    /// Robust dot product
    pub fn robust_dot<T: Float + FromPrimitive>(a: &Array1<T>, b: &Array1<T>) -> UtilsResult<T> {
        if a.len() != b.len() {
            return Err(UtilsError::ShapeMismatch {
                expected: vec![a.len()],
                actual: vec![b.len()],
            });
        }

        let mut sum = T::zero();
        let mut c = T::zero(); // Compensation

        for (&x, &y) in a.iter().zip(b.iter()) {
            let product = OverflowDetection::safe_mul(x, y)?;
            let corrected = product - c;
            let temp = sum + corrected;
            c = (temp - sum) - corrected;
            sum = temp;
        }

        Ok(sum)
    }

    /// Robust norm calculation (Euclidean norm with overflow protection)
    pub fn robust_norm<T: Float + FromPrimitive>(arr: &Array1<T>) -> UtilsResult<T> {
        if arr.is_empty() {
            return Ok(T::zero());
        }

        // Find the maximum absolute value to scale and prevent overflow
        let max_abs = arr.iter().map(|&x| x.abs()).fold(T::zero(), T::max);

        if NumericalPrecision::is_zero(max_abs, None) {
            return Ok(T::zero());
        }

        let mut sum_sq = T::zero();
        let mut c = T::zero(); // Compensation

        for &value in arr.iter() {
            let scaled = OverflowDetection::safe_div(value, max_abs)?;
            let sq = OverflowDetection::safe_mul(scaled, scaled)?;
            let y = sq - c;
            let t = sum_sq + y;
            c = (t - sum_sq) - y;
            sum_sq = t;
        }

        let norm_scaled = sum_sq.sqrt();
        OverflowDetection::safe_mul(norm_scaled, max_abs)
    }
}

#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;
    use scirs2_core::ndarray::array;

    #[test]
    fn test_numerical_precision() {
        assert!(NumericalPrecision::is_zero(1e-16, None));
        assert!(!NumericalPrecision::is_zero(1e-6, None));

        assert!(NumericalPrecision::approx_eq(1.0, 1.0 + 1e-15, None));
        assert!(!NumericalPrecision::approx_eq(1.0, 1.1, None));

        assert!(NumericalPrecision::rel_eq(1000.0, 1000.0001, Some(1e-6)));
        assert!(!NumericalPrecision::rel_eq(1000.0, 1001.0, Some(1e-6)));
    }

    #[test]
    fn test_overflow_detection() {
        // Test with values closer to actual overflow
        assert!(OverflowDetection::safe_add(f64::MAX / 2.0, f64::MAX / 2.0).is_err());
        assert!(OverflowDetection::safe_add(1.0, 2.0).is_ok());

        assert!(OverflowDetection::safe_mul(f64::MAX / 2.0, 2.0).is_err());
        assert!(OverflowDetection::safe_mul(2.0, 3.0).is_ok());

        assert!(OverflowDetection::safe_div(1.0, 0.0).is_err());
        assert!(OverflowDetection::safe_div(1.0, f64::MIN_POSITIVE).is_err());
        assert_relative_eq!(
            OverflowDetection::safe_div(6.0, 2.0).expect("operation should succeed"),
            3.0
        );
    }

    #[test]
    fn test_special_functions() {
        // Test logistic function
        assert_relative_eq!(SpecialFunctions::logistic(0.0), 0.5, epsilon = 1e-10);
        assert!(SpecialFunctions::logistic(10.0) > 0.99);
        assert!(SpecialFunctions::logistic(-10.0) < 0.01);

        // Test logsumexp
        let x = [1.0, 2.0, 3.0];
        let result = SpecialFunctions::logsumexp(&x);
        let expected = (1.0_f64.exp() + 2.0_f64.exp() + 3.0_f64.exp()).ln();
        assert_relative_eq!(result, expected, epsilon = 1e-10);

        // Test softmax
        let softmax_result = SpecialFunctions::softmax(&x);
        let sum: f64 = softmax_result.iter().sum();
        assert_relative_eq!(sum, 1.0, epsilon = 1e-10);

        // Test gamma function
        assert_relative_eq!(SpecialFunctions::gamma(1.0), 1.0, epsilon = 1e-8);
        assert_relative_eq!(SpecialFunctions::gamma(2.0), 1.0, epsilon = 1e-8);
        assert_relative_eq!(SpecialFunctions::gamma(3.0), 2.0, epsilon = 1e-8);
        assert_relative_eq!(SpecialFunctions::gamma(4.0), 6.0, epsilon = 1e-8);

        // Test error function
        assert_relative_eq!(SpecialFunctions::erf(0.0), 0.0, epsilon = 1e-9);
        assert!(SpecialFunctions::erf(1.0) > 0.8);
        assert!(SpecialFunctions::erf(-1.0) < -0.8);
    }

    #[test]
    fn test_robust_array_ops() {
        let arr = array![1.0, 2.0, 3.0, 4.0, 5.0];

        // Test robust sum
        let sum = RobustArrayOps::robust_sum(&arr);
        assert_relative_eq!(sum, 15.0, epsilon = 1e-10);

        // Test robust mean
        let mean = RobustArrayOps::robust_mean(&arr).expect("operation should succeed");
        assert_relative_eq!(mean, 3.0, epsilon = 1e-10);

        // Test robust variance
        let var = RobustArrayOps::robust_variance(&arr, 1).expect("operation should succeed");
        assert_relative_eq!(var, 2.5, epsilon = 1e-10);

        // Test robust standard deviation
        let std = RobustArrayOps::robust_std(&arr, 1).expect("operation should succeed");
        assert_relative_eq!(std, 2.5_f64.sqrt(), epsilon = 1e-10);

        // Test robust dot product
        let a = array![1.0, 2.0, 3.0];
        let b = array![4.0, 5.0, 6.0];
        let dot = RobustArrayOps::robust_dot(&a, &b).expect("operation should succeed");
        assert_relative_eq!(dot, 32.0, epsilon = 1e-10); // 1*4 + 2*5 + 3*6 = 32

        // Test robust norm
        let norm = RobustArrayOps::robust_norm(&a).expect("operation should succeed");
        let expected_norm = (1.0 + 4.0 + 9.0_f64).sqrt(); // sqrt(1^2 + 2^2 + 3^2)
        assert_relative_eq!(norm, expected_norm, epsilon = 1e-10);
    }
}