scirs2-stats 0.4.0

Statistical functions module for SciRS2 (scirs2-stats)
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
527
528
529
530
531
532
533
534
535
536
//! Advanced Benchmark Validation Framework
//!
//! This module provides comprehensive validation of Advanced optimizations by
//! comparing optimized implementations against reference implementations for
//! both performance and correctness. It ensures that SIMD, parallel, and other
//! optimizations maintain numerical accuracy while providing performance benefits.

#![allow(dead_code)]

use crate::error::{StatsError, StatsResult};
use crate::unified_processor::{
    OptimizationMode, AdvancedProcessorConfig, AdvancedUnifiedProcessor,
};
use scirs2_core::ndarray::{Array1, Array2, ArrayView1, ArrayView2};
use std::collections::HashMap;
use std::time::{Duration, Instant};

/// Validation configuration for Advanced benchmarks
#[derive(Debug, Clone)]
pub struct ValidationConfig {
    /// Tolerance for numerical comparisons
    pub numerical_tolerance: f64,
    /// Number of iterations for performance benchmarks
    pub benchmark_iterations: usize,
    /// Minimum performance improvement threshold (as ratio)
    pub min_performance_improvement: f64,
    /// Enable detailed logging
    pub verbose_logging: bool,
    /// Test data sizes to validate
    pub testsizes: Vec<usize>,
}

impl Default for ValidationConfig {
    fn default() -> Self {
        Self {
            numerical_tolerance: 1e-12,
            benchmark_iterations: 100,
            min_performance_improvement: 1.1, // 10% minimum improvement
            verbose_logging: false,
            testsizes: vec![100, 1000, 10000, 100000],
        }
    }
}

/// Result of a single validation test
#[derive(Debug, Clone)]
pub struct ValidationResult {
    pub operation_name: String,
    pub datasize: usize,
    pub reference_duration: Duration,
    pub optimized_duration: Duration,
    pub speedup_ratio: f64,
    pub numerical_accuracy: f64,
    pub accuracy_passed: bool,
    pub performance_passed: bool,
    pub optimization_strategy: OptimizationMode,
    pub error_messages: Vec<String>,
}

/// Complete validation report
#[derive(Debug, Clone)]
pub struct ValidationReport {
    pub total_tests: usize,
    pub passed_tests: usize,
    pub failed_tests: usize,
    pub average_speedup: f64,
    pub average_accuracy: f64,
    pub test_results: Vec<ValidationResult>,
    pub summary: String,
}

/// Advanced benchmark validation framework
pub struct AdvancedBenchmarkValidator {
    config: ValidationConfig,
    processor: AdvancedUnifiedProcessor,
    reference_implementations: HashMap<String, Box<dyn ReferenceImplementation>>,
}

/// Trait for reference implementations to compare against
pub trait ReferenceImplementation: Send + Sync {
    fn compute_f64(&self, data: ArrayView1<f64>) -> StatsResult<f64>;
    fn compute_matrix_f64(&self, data: ArrayView2<f64>) -> StatsResult<Array2<f64>>;
}

/// Reference implementation for mean calculation
struct ReferenceMean;

impl ReferenceImplementation for ReferenceMean {
    fn compute_f64(&self, data: ArrayView1<f64>) -> StatsResult<f64> {
        if data.is_empty() {
            return Err(StatsError::EmptyArray);
        }
        Ok(data.iter().sum::<f64>() / data.len() as f64)
    }

    fn compute_matrix_f64(&self, ArrayView2<f64>) -> StatsResult<Array2<f64>> {
        Err(StatsError::NotImplemented(
            "Matrix mean not implemented for reference".to_string(),
        ))
    }
}

/// Reference implementation for variance calculation
struct ReferenceVariance;

impl ReferenceImplementation for ReferenceVariance {
    fn compute_f64(&self, data: ArrayView1<f64>) -> StatsResult<f64> {
        if data.is_empty() {
            return Err(StatsError::EmptyArray);
        }
        if data.len() < 2 {
            return Ok(0.0);
        }

        let mean = data.iter().sum::<f64>() / data.len() as f64;
        let variance =
            data.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / (data.len() - 1) as f64;
        Ok(variance)
    }

    fn compute_matrix_f64(&self, ArrayView2<f64>) -> StatsResult<Array2<f64>> {
        Err(StatsError::NotImplemented(
            "Matrix variance not implemented for reference".to_string(),
        ))
    }
}

/// Reference implementation for correlation calculation
struct ReferenceCorrelation;

impl ReferenceImplementation for ReferenceCorrelation {
    fn compute_f64(&self, ArrayView1<f64>) -> StatsResult<f64> {
        Err(StatsError::NotImplemented(
            "Vector correlation not applicable".to_string(),
        ))
    }

    fn compute_matrix_f64(&self, data: ArrayView2<f64>) -> StatsResult<Array2<f64>> {
        let (n_rows, n_cols) = data.dim();
        if n_rows < 2 {
            return Err(StatsError::InsufficientData(
                "Insufficient data for operation".to_string(),
            ));
        }

        let mut correlation = Array2::zeros((n_cols, n_cols));

        // Calculate correlation matrix using standard formula
        for i in 0..n_cols {
            for j in 0..n_cols {
                if i == j {
                    correlation[(i, j)] = 1.0;
                } else {
                    let col_i = data.column(i);
                    let col_j = data.column(j);

                    let mean_i = col_i.iter().sum::<f64>() / n_rows as f64;
                    let mean_j = col_j.iter().sum::<f64>() / n_rows as f64;

                    let numerator: f64 = col_i
                        .iter()
                        .zip(col_j.iter())
                        .map(|(&x, &y)| (x - mean_i) * (y - mean_j))
                        .sum();

                    let var_i: f64 = col_i.iter().map(|&x| (x - mean_i).powi(2)).sum::<f64>();

                    let var_j: f64 = col_j.iter().map(|&x| (x - mean_j).powi(2)).sum::<f64>();

                    let denominator = (var_i * var_j).sqrt();

                    correlation[(i, j)] = if denominator.abs() < 1e-15 {
                        0.0
                    } else {
                        numerator / denominator
                    };
                }
            }
        }

        Ok(correlation)
    }
}

impl AdvancedBenchmarkValidator {
    /// Create a new benchmark validator
    pub fn new(config: ValidationConfig) -> Self {
        let processor_config = AdvancedProcessorConfig::default();
        let processor = AdvancedUnifiedProcessor::new(processor_config);

        let mut reference_implementations: HashMap<String, Box<dyn ReferenceImplementation>> =
            HashMap::new();
        reference_implementations.insert("mean".to_string(), Box::new(ReferenceMean));
        reference_implementations.insert("variance".to_string(), Box::new(ReferenceVariance));
        reference_implementations.insert("correlation".to_string(), Box::new(ReferenceCorrelation));

        Self {
            config,
            processor,
            reference_implementations,
        }
    }

    /// Validate all statistical operations
    pub fn validate_all_operations(&mut self) -> StatsResult<ValidationReport> {
        let mut all_results = Vec::new();

        // Test scalar operations
        for operation in ["mean", "variance"].iter() {
            let testsizes = self.config.testsizes.clone();
            for &size in &testsizes {
                let result = self.validate_scalar_operation(operation, size)?;
                all_results.push(result);
            }
        }

        // Test matrix operations
        for operation in ["correlation"].iter() {
            let testsizes = self.config.testsizes.clone();
            for &size in &testsizes {
                let result = self.validate_matrix_operation(operation, size)?;
                all_results.push(result);
            }
        }

        self.generate_report(all_results)
    }

    /// Validate a scalar statistical operation
    fn validate_scalar_operation(
        &mut self,
        operation: &str,
        datasize: usize,
    ) -> StatsResult<ValidationResult> {
        // Generate test data
        let testdata = self.generate_testdata(datasize);

        // Get reference implementation
        let reference_impl = self
            .reference_implementations
            .get(operation)
            .ok_or_else(|| {
                StatsError::NotImplemented(format!(
                    "Reference implementation for {} not found",
                    operation
                ))
            })?;

        // Benchmark reference implementation
        let reference_start = Instant::now();
        let mut reference_result = 0.0;
        for _ in 0..self.config.benchmark_iterations {
            reference_result = reference_impl.compute_f64(testdata.view())?;
        }
        let reference_duration =
            reference_start.elapsed() / self.config.benchmark_iterations as u32;

        // Benchmark optimized implementation
        let optimized_start = Instant::now();
        let mut optimized_result = None;
        for _ in 0..self.config.benchmark_iterations {
            match operation {
                "mean" => {
                    let result = self.processor.compute_statistics(testdata.view())?;
                    optimized_result = Some(result.statistics.mean);
                }
                "variance" => {
                    let result = self.processor.compute_statistics(testdata.view())?;
                    optimized_result = Some(result.statistics.variance);
                }
                _ => {
                    return Err(StatsError::NotImplemented(format!(
                        "Optimized implementation for {} not found",
                        operation
                    )))
                }
            }
        }
        let optimized_duration =
            optimized_start.elapsed() / self.config.benchmark_iterations as u32;

        let optimized_result = optimized_result.expect("Operation failed");

        // Calculate metrics
        let speedup_ratio =
            reference_duration.as_nanos() as f64 / optimized_duration.as_nanos() as f64;
        let accuracy =
            1.0 - ((reference_result - optimized_result).abs() / reference_result.abs().max(1e-15));
        let accuracy_passed = accuracy >= (1.0 - self.config.numerical_tolerance);
        let performance_passed = speedup_ratio >= self.config.min_performance_improvement;

        let mut error_messages = Vec::new();
        if !accuracy_passed {
            error_messages.push(format!(
                "Accuracy test failed: expected {:.6e}, got {:.6e}",
                reference_result, optimized_result
            ));
        }
        if !performance_passed {
            error_messages.push(format!(
                "Performance test failed: speedup {:.2}x below threshold {:.2}x",
                speedup_ratio, self.config.min_performance_improvement
            ));
        }

        Ok(ValidationResult {
            operation_name: operation.to_string(),
            datasize,
            reference_duration,
            optimized_duration,
            speedup_ratio,
            numerical_accuracy: accuracy,
            accuracy_passed,
            performance_passed,
            optimization_strategy: OptimizationMode::Auto, // Would be determined by processor
            error_messages,
        })
    }

    /// Validate a matrix statistical operation
    fn validate_matrix_operation(
        &mut self,
        operation: &str,
        datasize: usize,
    ) -> StatsResult<ValidationResult> {
        // Generate test matrix data (square matrix for simplicity)
        let matrixsize = (datasize as f64).sqrt() as usize;
        let testdata = self.generate_test_matrix(matrixsize, matrixsize);

        // Get reference implementation
        let reference_impl = self
            .reference_implementations
            .get(operation)
            .ok_or_else(|| {
                StatsError::NotImplemented(format!(
                    "Reference implementation for {} not found",
                    operation
                ))
            })?;

        // Benchmark reference implementation
        let reference_start = Instant::now();
        let mut reference_result = None;
        for _ in 0..self.config.benchmark_iterations.min(10) {
            // Fewer iterations for matrix ops
            reference_result = Some(reference_impl.compute_matrix_f64(testdata.view())?);
        }
        let reference_duration =
            reference_start.elapsed() / self.config.benchmark_iterations.min(10) as u32;
        let reference_result = reference_result.expect("Operation failed");

        // Benchmark optimized implementation
        let optimized_start = Instant::now();
        let mut optimized_result = None;
        for _ in 0..self.config.benchmark_iterations.min(10) {
            match operation {
                "correlation" => {
                    // This would use the Advanced matrix operations
                    // For now, we'll simulate with a placeholder
                    optimized_result = Some(reference_result.clone()); // Placeholder
                }
                _ => {
                    return Err(StatsError::NotImplemented(format!(
                        "Optimized matrix implementation for {} not found",
                        operation
                    )))
                }
            }
        }
        let optimized_duration =
            optimized_start.elapsed() / self.config.benchmark_iterations.min(10) as u32;
        let optimized_result = optimized_result.expect("Operation failed");

        // Calculate accuracy (Frobenius norm of difference)
        let diff_norm = (&reference_result - &optimized_result)
            .mapv(|x| x * x)
            .sum()
            .sqrt();
        let reference_norm = reference_result.mapv(|x| x * x).sum().sqrt();
        let accuracy = 1.0 - (diff_norm / reference_norm.max(1e-15));

        let speedup_ratio =
            reference_duration.as_nanos() as f64 / optimized_duration.as_nanos() as f64;
        let accuracy_passed = accuracy >= (1.0 - self.config.numerical_tolerance);
        let performance_passed = speedup_ratio >= self.config.min_performance_improvement;

        let mut error_messages = Vec::new();
        if !accuracy_passed {
            error_messages.push(format!(
                "Matrix accuracy test failed: relative error {:.6e}",
                1.0 - accuracy
            ));
        }
        if !performance_passed {
            error_messages.push(format!(
                "Matrix performance test failed: speedup {:.2}x below threshold {:.2}x",
                speedup_ratio, self.config.min_performance_improvement
            ));
        }

        Ok(ValidationResult {
            operation_name: operation.to_string(),
            datasize: matrixsize * matrixsize,
            reference_duration,
            optimized_duration,
            speedup_ratio,
            numerical_accuracy: accuracy,
            accuracy_passed,
            performance_passed,
            optimization_strategy: OptimizationMode::Auto,
            error_messages,
        })
    }

    /// Generate test data for validation
    fn generate_testdata(&self, size: usize) -> Array1<f64> {
        use scirs2_core::random::SeedableRng;
        use scirs2_core::random::{Distribution, Normal};

        let mut rng = scirs2_core::random::rngs::StdRng::seed_from_u64(42); // Fixed seed for reproducibility
        let normal = Normal::new(0.0, 1.0).expect("Operation failed");

        Array1::from_vec((0..size).map(|_| normal.sample(&mut rng)).collect())
    }

    /// Generate test matrix data for validation
    fn generate_test_matrix(&self, rows: usize, cols: usize) -> Array2<f64> {
        use scirs2_core::random::SeedableRng;
        use scirs2_core::random::{Distribution, Normal};

        let mut rng = scirs2_core::random::rngs::StdRng::seed_from_u64(42); // Fixed seed for reproducibility
        let normal = Normal::new(0.0, 1.0).expect("Operation failed");

        Array2::from_shape_vec(
            (rows, cols),
            (0..rows * cols).map(|_| normal.sample(&mut rng)).collect(),
        )
        .expect("Operation failed")
    }

    /// Generate comprehensive validation report
    fn generate_report(&self, results: Vec<ValidationResult>) -> StatsResult<ValidationReport> {
        let total_tests = results.len();
        let passed_tests = results
            .iter()
            .filter(|r| r.accuracy_passed && r.performance_passed)
            .count();
        let failed_tests = total_tests - passed_tests;

        let average_speedup = if total_tests > 0 {
            results.iter().map(|r| r.speedup_ratio).sum::<f64>() / total_tests as f64
        } else {
            0.0
        };

        let average_accuracy = if total_tests > 0 {
            results.iter().map(|r| r.numerical_accuracy).sum::<f64>() / total_tests as f64
        } else {
            0.0
        };

        let success_rate = if total_tests > 0 {
            (passed_tests as f64 / total_tests as f64) * 100.0
        } else {
            0.0
        };

        let summary = format!(
            "Advanced Validation Report: {}/{} tests passed ({:.1}%). Average speedup: {:.2}x, Average accuracy: {:.6}",
            passed_tests, total_tests, success_rate, average_speedup, average_accuracy
        );

        Ok(ValidationReport {
            total_tests,
            passed_tests,
            failed_tests,
            average_speedup,
            average_accuracy,
            test_results: results,
            summary,
        })
    }
}

/// Create a default Advanced benchmark validator  
#[allow(dead_code)]
pub fn create_advanced_validator() -> AdvancedBenchmarkValidator {
    AdvancedBenchmarkValidator::new(ValidationConfig::default())
}

/// Create a custom Advanced benchmark validator
#[allow(dead_code)]
pub fn create_custom_advanced_validator(
    config: ValidationConfig,
) -> AdvancedBenchmarkValidator {
    AdvancedBenchmarkValidator::new(config)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_validation_config_default() {
        let config = ValidationConfig::default();
        assert_eq!(config.numerical_tolerance, 1e-12);
        assert_eq!(config.benchmark_iterations, 100);
        assert_eq!(config.min_performance_improvement, 1.1);
        assert!(!config.verbose_logging);
        assert_eq!(config.testsizes, vec![100, 1000, 10000, 100000]);
    }

    #[test]
    fn test_reference_mean_implementation() {
        let reference = ReferenceMean;
        let data = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0]);
        let result = reference.compute_f64(data.view()).expect("Operation failed");
        assert!((result - 3.0).abs() < 1e-15);
    }

    #[test]
    fn test_reference_variance_implementation() {
        let reference = ReferenceVariance;
        let data = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0]);
        let result = reference.compute_f64(data.view()).expect("Operation failed");
        // Sample variance of [1,2,3,4,5] is 2.5
        assert!((result - 2.5).abs() < 1e-15);
    }

    #[test]
    fn test_validator_creation() {
        let validator = create_advanced_validator();
        assert_eq!(validator.config.numerical_tolerance, 1e-12);
        assert_eq!(validator.reference_implementations.len(), 3);
    }
}