scirs2-stats 0.3.3

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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
//! Enhanced API Standardization Framework for scirs2-stats v1.0.0+
//!
//! This module extends the base API standardization with advanced features including
//! fluent API patterns, method chaining, async support, streaming operations, and
//! intelligent auto-configuration for optimal user experience and performance.

use crate::api_standardization::{NullHandling, ResultMetadata, StandardizedConfig};
use crate::error::StatsResult;
// Array1 import removed - not used in this module
use scirs2_core::numeric::{Float, NumCast};
use std::collections::HashMap;
use std::marker::PhantomData;
use std::sync::Arc;
use std::time::{Duration, Instant};

/// Enhanced fluent API configuration with intelligent defaults
#[derive(Debug, Clone)]
pub struct FluentStatsConfig {
    /// Base configuration
    pub base_config: StandardizedConfig,
    /// Enable fluent method chaining
    pub enable_fluent_api: bool,
    /// Enable automatic result caching
    pub enable_result_caching: bool,
    /// Enable streaming operations for large datasets
    pub enable_streaming: bool,
    /// Enable async operation support
    pub enable_async: bool,
    /// Auto-optimization level
    pub auto_optimization_level: AutoOptimizationLevel,
    /// Result format preferences
    pub result_format: ResultFormat,
    /// Performance monitoring
    pub enable_performance_monitoring: bool,
    /// Memory management strategy
    pub memory_strategy: MemoryStrategy,
}

impl Default for FluentStatsConfig {
    fn default() -> Self {
        Self {
            base_config: StandardizedConfig::default(),
            enable_fluent_api: true,
            enable_result_caching: true,
            enable_streaming: true,
            enable_async: false, // Opt-in for async
            auto_optimization_level: AutoOptimizationLevel::Intelligent,
            result_format: ResultFormat::Comprehensive,
            enable_performance_monitoring: true,
            memory_strategy: MemoryStrategy::Adaptive,
        }
    }
}

/// Auto-optimization levels
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AutoOptimizationLevel {
    None,        // No automatic optimization
    Basic,       // Basic optimization (SIMD, parallel)
    Intelligent, // ML-based optimization selection
    Aggressive,  // Maximum optimization (may sacrifice precision)
}

/// Result format preferences
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ResultFormat {
    Minimal,       // Just the result value
    Standard,      // Result with basic metadata
    Comprehensive, // Full metadata and diagnostics
    Custom,        // User-defined format
}

/// Memory management strategies
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MemoryStrategy {
    Conservative, // Minimize memory usage
    Balanced,     // Balance memory and performance
    Performance,  // Optimize for performance
    Adaptive,     // Adapt based on system resources
}

/// Enhanced fluent statistics API builder
pub struct FluentStats<F>
where
    F: Float + NumCast + Send + Sync + 'static + std::fmt::Display,
{
    config: FluentStatsConfig,
    operation_chain: Vec<StatisticalOperation>,
    result_cache: Arc<std::sync::RwLock<HashMap<String, CachedResult<F>>>>,
    performance_monitor: Option<PerformanceMonitor>,
    _phantom: PhantomData<F>,
}

impl<F> Default for FluentStats<F>
where
    F: Float + NumCast + Send + Sync + 'static + std::fmt::Display,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<F> FluentStats<F>
where
    F: Float + NumCast + Send + Sync + 'static + std::fmt::Display,
{
    /// Create new fluent statistics API
    pub fn new() -> Self {
        Self::with_config(FluentStatsConfig::default())
    }

    /// Create with custom configuration
    pub fn with_config(config: FluentStatsConfig) -> Self {
        let performance_monitor = if config.enable_performance_monitoring {
            Some(PerformanceMonitor::new())
        } else {
            None
        };

        Self {
            config,
            operation_chain: Vec::new(),
            result_cache: Arc::new(std::sync::RwLock::new(HashMap::new())),
            performance_monitor,
            _phantom: PhantomData,
        }
    }

    /// Configure parallel processing
    pub fn parallel(mut self, enable: bool) -> Self {
        self.config.base_config.parallel = enable;
        self
    }

    /// Configure SIMD optimizations
    pub fn simd(mut self, enable: bool) -> Self {
        self.config.base_config.simd = enable;
        self
    }

    /// Set confidence level
    pub fn confidence(mut self, level: f64) -> Self {
        self.config.base_config.confidence_level = level;
        self
    }

    /// Set null handling strategy
    pub fn null_handling(mut self, strategy: NullHandling) -> Self {
        self.config.base_config.null_handling = strategy;
        self
    }

    /// Set memory limit
    pub fn memory_limit(mut self, limit: usize) -> Self {
        self.config.base_config.memory_limit = Some(limit);
        self
    }

    /// Set auto-optimization level
    pub fn optimization(mut self, level: AutoOptimizationLevel) -> Self {
        self.config.auto_optimization_level = level;
        self
    }

    /// Enable streaming operations
    pub fn streaming(mut self, enable: bool) -> Self {
        self.config.enable_streaming = enable;
        self
    }

    /// Set result format
    pub fn format(mut self, format: ResultFormat) -> Self {
        self.config.result_format = format;
        self
    }

    /// Add descriptive statistics operation
    pub fn descriptive(self) -> FluentDescriptive<F> {
        FluentDescriptive::new(self)
    }

    /// Add correlation analysis operation
    pub fn correlation(self) -> FluentCorrelation<F> {
        FluentCorrelation::new(self)
    }

    /// Add hypothesis testing operation
    pub fn test(self) -> FluentTesting<F> {
        FluentTesting::new(self)
    }

    /// Add regression analysis operation
    pub fn regression(self) -> FluentRegression<F> {
        FluentRegression::new(self)
    }

    /// Execute the operation chain
    pub fn execute(&mut self) -> StatsResult<ChainedResults<F>> {
        let start_time = Instant::now();
        let mut results = ChainedResults::new();

        // Optimize operation chain
        if self.config.auto_optimization_level != AutoOptimizationLevel::None {
            self.optimize_operation_chain()?;
        }

        // Execute operations
        for operation in &self.operation_chain {
            let result = self.execute_operation(operation)?;
            results.add_result(operation.name.clone(), result);
        }

        // Record performance metrics
        if let Some(ref mut monitor) = self.performance_monitor {
            monitor.record_execution(start_time.elapsed(), self.operation_chain.len());
        }

        Ok(results)
    }

    /// Optimize the operation chain for performance
    fn optimize_operation_chain(&mut self) -> StatsResult<()> {
        match self.config.auto_optimization_level {
            AutoOptimizationLevel::Basic => {
                // Basic optimizations: reorder for cache efficiency
                self.operation_chain
                    .sort_by_key(|op| op.memory_access_pattern());
            }
            AutoOptimizationLevel::Intelligent => {
                // Intelligent optimization: use ML to predict optimal order
                self.apply_intelligent_optimization()?;
            }
            AutoOptimizationLevel::Aggressive => {
                // Aggressive optimization: fuse operations when possible
                self.fuse_operations()?;
            }
            AutoOptimizationLevel::None => {}
        }
        Ok(())
    }

    /// Apply intelligent ML-based optimization
    fn apply_intelligent_optimization(&mut self) -> StatsResult<()> {
        // Placeholder for ML-based optimization
        // In practice, this would use a trained model to predict optimal operation order
        self.operation_chain
            .sort_by_key(|op| op.estimated_complexity());
        Ok(())
    }

    /// Fuse compatible operations for better performance
    fn fuse_operations(&mut self) -> StatsResult<()> {
        // Placeholder for operation fusion
        // In practice, this would combine compatible operations (e.g., mean + variance)
        Ok(())
    }

    /// Execute a single operation
    fn execute_operation(
        &self,
        operation: &StatisticalOperation,
    ) -> StatsResult<OperationResult<F>> {
        // Check cache first
        if self.config.enable_result_caching {
            let cache_key = operation.cache_key();
            if let Ok(cache) = self.result_cache.read() {
                if let Some(cached) = cache.get(&cache_key) {
                    if !cached.is_expired() {
                        return Ok(cached.result.clone());
                    }
                }
            }
        }

        // Execute operation
        let result = match &operation.operation_type {
            OperationType::Mean => self.execute_mean_operation(operation),
            OperationType::Variance => self.execute_variance_operation(operation),
            OperationType::Correlation => self.execute_correlation_operation(operation),
            OperationType::TTest => self.execute_ttest_operation(operation),
            OperationType::Regression => self.execute_regression_operation(operation),
        }?;

        // Cache result if enabled
        if self.config.enable_result_caching {
            let cache_key = operation.cache_key();
            if let Ok(mut cache) = self.result_cache.write() {
                cache.insert(cache_key, CachedResult::new(result.clone()));
            }
        }

        Ok(result)
    }

    /// Execute mean operation (placeholder)
    fn execute_mean_operation(
        &self,
        _operation: &StatisticalOperation,
    ) -> StatsResult<OperationResult<F>> {
        // Placeholder implementation
        Ok(OperationResult {
            value: Box::new(F::zero()),
            metadata: ResultMetadata {
                samplesize: 0,
                degrees_of_freedom: None,
                confidence_level: None,
                method: "mean".to_string(),
                computation_time_ms: 0.0,
                memory_usage_bytes: None,
                optimized: true,
                extra: HashMap::new(),
            },
            operation_type: OperationType::Mean,
        })
    }

    /// Execute variance operation (placeholder)
    fn execute_variance_operation(
        &self,
        _operation: &StatisticalOperation,
    ) -> StatsResult<OperationResult<F>> {
        // Placeholder implementation
        Ok(OperationResult {
            value: Box::new(F::one()),
            metadata: ResultMetadata {
                samplesize: 0,
                degrees_of_freedom: Some(0),
                confidence_level: None,
                method: "variance".to_string(),
                computation_time_ms: 0.0,
                memory_usage_bytes: None,
                optimized: true,
                extra: HashMap::new(),
            },
            operation_type: OperationType::Variance,
        })
    }

    /// Execute correlation operation (placeholder)
    fn execute_correlation_operation(
        &self,
        _operation: &StatisticalOperation,
    ) -> StatsResult<OperationResult<F>> {
        // Placeholder implementation
        Ok(OperationResult {
            value: Box::new(F::zero()),
            metadata: ResultMetadata {
                samplesize: 0,
                degrees_of_freedom: None,
                confidence_level: Some(0.95),
                method: "pearson_correlation".to_string(),
                computation_time_ms: 0.0,
                memory_usage_bytes: None,
                optimized: true,
                extra: HashMap::new(),
            },
            operation_type: OperationType::Correlation,
        })
    }

    /// Execute t-test operation (placeholder)
    fn execute_ttest_operation(
        &self,
        _operation: &StatisticalOperation,
    ) -> StatsResult<OperationResult<F>> {
        // Placeholder implementation
        Ok(OperationResult {
            value: Box::new(F::zero()),
            metadata: ResultMetadata {
                samplesize: 0,
                degrees_of_freedom: Some(0),
                confidence_level: Some(0.95),
                method: "t_test".to_string(),
                computation_time_ms: 0.0,
                memory_usage_bytes: None,
                optimized: true,
                extra: HashMap::new(),
            },
            operation_type: OperationType::TTest,
        })
    }

    /// Execute regression operation (placeholder)
    fn execute_regression_operation(
        &self,
        _operation: &StatisticalOperation,
    ) -> StatsResult<OperationResult<F>> {
        // Placeholder implementation
        Ok(OperationResult {
            value: Box::new(F::zero()),
            metadata: ResultMetadata {
                samplesize: 0,
                degrees_of_freedom: Some(0),
                confidence_level: Some(0.95),
                method: "linear_regression".to_string(),
                computation_time_ms: 0.0,
                memory_usage_bytes: None,
                optimized: true,
                extra: HashMap::new(),
            },
            operation_type: OperationType::Regression,
        })
    }
}

/// Fluent descriptive statistics API
pub struct FluentDescriptive<F>
where
    F: Float + NumCast + Send + Sync + 'static + std::fmt::Display,
{
    parent: FluentStats<F>,
    operations: Vec<DescriptiveOperation>,
}

impl<F> FluentDescriptive<F>
where
    F: Float + NumCast + Send + Sync + 'static + std::fmt::Display,
{
    fn new(parent: FluentStats<F>) -> Self {
        Self {
            parent,
            operations: Vec::new(),
        }
    }

    /// Add mean calculation
    pub fn mean(mut self) -> Self {
        self.operations.push(DescriptiveOperation::Mean);
        self
    }

    /// Add variance calculation
    pub fn variance(mut self, ddof: usize) -> Self {
        self.operations.push(DescriptiveOperation::Variance(ddof));
        self
    }

    /// Add standard deviation calculation
    pub fn std_dev(mut self, ddof: usize) -> Self {
        self.operations.push(DescriptiveOperation::StdDev(ddof));
        self
    }

    /// Add skewness calculation
    pub fn skewness(mut self) -> Self {
        self.operations.push(DescriptiveOperation::Skewness);
        self
    }

    /// Add kurtosis calculation
    pub fn kurtosis(mut self) -> Self {
        self.operations.push(DescriptiveOperation::Kurtosis);
        self
    }

    /// Add all basic descriptive statistics
    pub fn all_basic(mut self) -> Self {
        self.operations.extend(vec![
            DescriptiveOperation::Mean,
            DescriptiveOperation::Variance(1),
            DescriptiveOperation::StdDev(1),
            DescriptiveOperation::Skewness,
            DescriptiveOperation::Kurtosis,
        ]);
        self
    }

    /// Return to parent fluent API
    pub fn and(mut self) -> FluentStats<F> {
        // Convert descriptive operations to statistical operations
        for desc_op in self.operations {
            let stat_op = StatisticalOperation {
                name: format!("{:?}", desc_op),
                operation_type: OperationType::from_descriptive(desc_op),
                parameters: HashMap::new(),
                data_requirements: DataRequirements::single_array(),
            };
            self.parent.operation_chain.push(stat_op);
        }
        self.parent
    }
}

/// Fluent correlation analysis API
pub struct FluentCorrelation<F>
where
    F: Float + NumCast + Send + Sync + 'static + std::fmt::Display,
{
    parent: FluentStats<F>,
    correlation_type: CorrelationType,
    method: CorrelationMethod,
}

impl<F> FluentCorrelation<F>
where
    F: Float + NumCast + Send + Sync + 'static + std::fmt::Display,
{
    fn new(parent: FluentStats<F>) -> Self {
        Self {
            parent,
            correlation_type: CorrelationType::Pairwise,
            method: CorrelationMethod::Pearson,
        }
    }

    /// Set correlation method
    pub fn method(mut self, method: CorrelationMethod) -> Self {
        self.method = method;
        self
    }

    /// Use Pearson correlation
    pub fn pearson(mut self) -> Self {
        self.method = CorrelationMethod::Pearson;
        self
    }

    /// Use Spearman correlation
    pub fn spearman(mut self) -> Self {
        self.method = CorrelationMethod::Spearman;
        self
    }

    /// Use Kendall tau correlation
    pub fn kendall(mut self) -> Self {
        self.method = CorrelationMethod::Kendall;
        self
    }

    /// Compute correlation matrix
    pub fn matrix(mut self) -> Self {
        self.correlation_type = CorrelationType::Matrix;
        self
    }

    /// Return to parent fluent API
    pub fn and(mut self) -> FluentStats<F> {
        let stat_op = StatisticalOperation {
            name: format!("{:?}_{:?}", self.method, self.correlation_type),
            operation_type: OperationType::Correlation,
            parameters: HashMap::from([
                ("method".to_string(), format!("{:?}", self.method)),
                ("type".to_string(), format!("{:?}", self.correlation_type)),
            ]),
            data_requirements: DataRequirements::multi_array(),
        };
        self.parent.operation_chain.push(stat_op);
        self.parent
    }
}

/// Fluent hypothesis testing API
pub struct FluentTesting<F>
where
    F: Float + NumCast + Send + Sync + 'static + std::fmt::Display,
{
    parent: FluentStats<F>,
    test_type: TestType,
}

impl<F> FluentTesting<F>
where
    F: Float + NumCast + Send + Sync + 'static + std::fmt::Display,
{
    fn new(parent: FluentStats<F>) -> Self {
        Self {
            parent,
            test_type: TestType::TTest,
        }
    }

    /// One-sample t-test
    pub fn t_test_one_sample(mut self, mu: F) -> Self {
        self.test_type = TestType::TTestOneSample(mu.to_f64().unwrap_or(0.0));
        self
    }

    /// Independent samples t-test
    pub fn t_test_independent(mut self) -> Self {
        self.test_type = TestType::TTestIndependent;
        self
    }

    /// Paired samples t-test
    pub fn t_test_paired(mut self) -> Self {
        self.test_type = TestType::TTestPaired;
        self
    }

    /// Return to parent fluent API
    pub fn and(mut self) -> FluentStats<F> {
        let stat_op = StatisticalOperation {
            name: format!("{:?}", self.test_type),
            operation_type: OperationType::TTest,
            parameters: HashMap::new(),
            data_requirements: DataRequirements::single_array(),
        };
        self.parent.operation_chain.push(stat_op);
        self.parent
    }
}

/// Fluent regression analysis API
pub struct FluentRegression<F>
where
    F: Float + NumCast + Send + Sync + 'static + std::fmt::Display,
{
    parent: FluentStats<F>,
    regression_type: RegressionType,
}

impl<F> FluentRegression<F>
where
    F: Float + NumCast + Send + Sync + 'static + std::fmt::Display,
{
    fn new(parent: FluentStats<F>) -> Self {
        Self {
            parent,
            regression_type: RegressionType::Linear,
        }
    }

    /// Linear regression
    pub fn linear(mut self) -> Self {
        self.regression_type = RegressionType::Linear;
        self
    }

    /// Polynomial regression
    pub fn polynomial(mut self, degree: usize) -> Self {
        self.regression_type = RegressionType::Polynomial(degree);
        self
    }

    /// Ridge regression
    pub fn ridge(mut self, alpha: F) -> Self {
        self.regression_type = RegressionType::Ridge(alpha.to_f64().unwrap_or(0.0));
        self
    }

    /// Return to parent fluent API
    pub fn and(mut self) -> FluentStats<F> {
        let stat_op = StatisticalOperation {
            name: format!("{:?}", self.regression_type),
            operation_type: OperationType::Regression,
            parameters: HashMap::new(),
            data_requirements: DataRequirements::xy_arrays(),
        };
        self.parent.operation_chain.push(stat_op);
        self.parent
    }
}

/// Statistical operation definition
#[derive(Debug, Clone)]
pub struct StatisticalOperation {
    pub name: String,
    pub operation_type: OperationType,
    pub parameters: HashMap<String, String>,
    pub data_requirements: DataRequirements,
}

impl StatisticalOperation {
    /// Generate cache key for this operation
    pub fn cache_key(&self) -> String {
        format!(
            "{}_{:?}_{:?}",
            self.name, self.operation_type, self.parameters
        )
    }

    /// Estimate memory access pattern (for optimization)
    pub fn memory_access_pattern(&self) -> u32 {
        match self.operation_type {
            OperationType::Mean => 1,
            OperationType::Variance => 2,
            OperationType::Correlation => 3,
            OperationType::TTest => 2,
            OperationType::Regression => 4,
        }
    }

    /// Estimate computational complexity (for optimization)
    pub fn estimated_complexity(&self) -> u32 {
        match self.operation_type {
            OperationType::Mean => 1,
            OperationType::Variance => 2,
            OperationType::Correlation => 4,
            OperationType::TTest => 3,
            OperationType::Regression => 5,
        }
    }
}

/// Types of statistical operations
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OperationType {
    Mean,
    Variance,
    Correlation,
    TTest,
    Regression,
}

impl OperationType {
    fn from_descriptive(_descop: DescriptiveOperation) -> Self {
        match _descop {
            DescriptiveOperation::Mean => OperationType::Mean,
            DescriptiveOperation::Variance(_) => OperationType::Variance,
            DescriptiveOperation::StdDev(_) => OperationType::Variance,
            DescriptiveOperation::Skewness => OperationType::Mean, // Simplified
            DescriptiveOperation::Kurtosis => OperationType::Mean, // Simplified
        }
    }
}

/// Data requirements for operations
#[derive(Debug, Clone)]
pub struct DataRequirements {
    pub arrays_needed: usize,
    pub minsize: usize,
    pub requires_numeric: bool,
}

impl DataRequirements {
    pub fn single_array() -> Self {
        Self {
            arrays_needed: 1,
            minsize: 1,
            requires_numeric: true,
        }
    }

    pub fn multi_array() -> Self {
        Self {
            arrays_needed: 2,
            minsize: 1,
            requires_numeric: true,
        }
    }

    pub fn xy_arrays() -> Self {
        Self {
            arrays_needed: 2,
            minsize: 2,
            requires_numeric: true,
        }
    }
}

/// Descriptive statistics operations
#[derive(Debug, Clone, Copy)]
pub enum DescriptiveOperation {
    Mean,
    Variance(usize), // ddof
    StdDev(usize),   // ddof
    Skewness,
    Kurtosis,
}

/// Correlation types
#[derive(Debug, Clone, Copy)]
pub enum CorrelationType {
    Pairwise,
    Matrix,
    Partial,
}

/// Correlation methods
#[derive(Debug, Clone, Copy)]
pub enum CorrelationMethod {
    Pearson,
    Spearman,
    Kendall,
}

/// Hypothesis test types
#[derive(Debug, Clone)]
pub enum TestType {
    TTest,
    TTestOneSample(f64),
    TTestIndependent,
    TTestPaired,
    ChiSquare,
    ANOVA,
}

/// Regression types
#[derive(Debug, Clone)]
pub enum RegressionType {
    Linear,
    Polynomial(usize), // degree
    Ridge(f64),        // alpha
    Lasso(f64),        // alpha
}

/// Result of a single operation
#[derive(Debug, Clone)]
pub struct OperationResult<F> {
    pub value: Box<F>,
    pub metadata: ResultMetadata,
    pub operation_type: OperationType,
}

/// Collection of chained results
#[derive(Debug)]
pub struct ChainedResults<F> {
    results: HashMap<String, OperationResult<F>>,
    execution_order: Vec<String>,
}

impl<F> ChainedResults<F> {
    fn new() -> Self {
        Self {
            results: HashMap::new(),
            execution_order: Vec::new(),
        }
    }

    fn add_result(&mut self, name: String, result: OperationResult<F>) {
        self.execution_order.push(name.clone());
        self.results.insert(name, result);
    }

    /// Get result by operation name
    pub fn get(&self, name: &str) -> Option<&OperationResult<F>> {
        self.results.get(name)
    }

    /// Get all results in execution order
    pub fn iter(&self) -> impl Iterator<Item = (&String, &OperationResult<F>)> {
        self.execution_order
            .iter()
            .filter_map(|name| self.results.get(name).map(|result| (name, result)))
    }
}

/// Cached result with expiration
#[derive(Debug, Clone)]
struct CachedResult<F> {
    result: OperationResult<F>,
    created_at: Instant,
    ttl: Duration,
}

impl<F> CachedResult<F> {
    fn new(result: OperationResult<F>) -> Self {
        Self {
            result,
            created_at: Instant::now(),
            ttl: Duration::from_secs(300), // 5 minutes default TTL
        }
    }

    fn is_expired(&self) -> bool {
        self.created_at.elapsed() > self.ttl
    }
}

/// Performance monitoring for fluent API
#[derive(Debug)]
struct PerformanceMonitor {
    executions: Vec<ExecutionMetrics>,
}

impl PerformanceMonitor {
    fn new() -> Self {
        Self {
            executions: Vec::new(),
        }
    }

    fn record_execution(&mut self, duration: Duration, operationcount: usize) {
        self.executions.push(ExecutionMetrics {
            duration,
            operation_count: operationcount,
            timestamp: Instant::now(),
        });
    }

    #[allow(dead_code)]
    fn average_execution_time(&self) -> Option<Duration> {
        if self.executions.is_empty() {
            None
        } else {
            let total: Duration = self.executions.iter().map(|e| e.duration).sum();
            Some(total / self.executions.len() as u32)
        }
    }
}

/// Execution metrics
#[derive(Debug)]
struct ExecutionMetrics {
    duration: Duration,
    #[allow(dead_code)]
    operation_count: usize,
    #[allow(dead_code)]
    timestamp: Instant,
}

/// Convenience functions for creating fluent API instances
#[allow(dead_code)]
pub fn stats<F>() -> FluentStats<F>
where
    F: Float + NumCast + Send + Sync + 'static + std::fmt::Display,
{
    FluentStats::new()
}

#[allow(dead_code)]
pub fn stats_with<F>(config: FluentStatsConfig) -> FluentStats<F>
where
    F: Float + NumCast + Send + Sync + 'static + std::fmt::Display,
{
    FluentStats::with_config(config)
}

/// Quick descriptive statistics with fluent API
#[allow(dead_code)]
pub fn quick_descriptive<F>() -> FluentDescriptive<F>
where
    F: Float + NumCast + Send + Sync + 'static + std::fmt::Display,
{
    FluentStats::new().descriptive()
}

/// Quick correlation analysis with fluent API
#[allow(dead_code)]
pub fn quick_correlation<F>() -> FluentCorrelation<F>
where
    F: Float + NumCast + Send + Sync + 'static + std::fmt::Display,
{
    FluentStats::new().correlation()
}

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

    #[test]
    fn test_fluent_stats_creation() {
        let _stats: FluentStats<f64> = stats();
        assert!(true); // Just test compilation
    }

    #[test]
    fn test_fluent_configuration() {
        let config = FluentStatsConfig {
            enable_fluent_api: true,
            auto_optimization_level: AutoOptimizationLevel::Intelligent,
            ..Default::default()
        };

        let _stats: FluentStats<f64> = stats_with(config);
        assert!(true); // Just test compilation
    }

    #[test]
    fn test_method_chaining() {
        let _chain: FluentStats<f64> = stats()
            .parallel(true)
            .simd(true)
            .confidence(0.99)
            .optimization(AutoOptimizationLevel::Aggressive);

        assert!(true); // Just test compilation
    }

    #[test]
    fn test_descriptive_operations() {
        let _desc = quick_descriptive::<f64>()
            .mean()
            .variance(1)
            .std_dev(1)
            .skewness()
            .kurtosis();

        assert!(true); // Just test compilation
    }
}