scirs2-stats 0.4.1

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
//! Advanced parallel Monte Carlo integration with adaptive sampling
//!
//! This module provides state-of-the-art Monte Carlo integration techniques including:
//! - Parallel adaptive sampling with load balancing
//! - Variance reduction techniques (importance sampling, control variates, antithetic variables)
//! - Multi-level Monte Carlo methods
//! - Sequential Monte Carlo with adaptive resampling
//! - GPU acceleration support (when available)

use crate::error::StatsResult;
use scirs2_core::ndarray::{Array1, Array2, ArrayView1};
use scirs2_core::numeric::{Float, FromPrimitive, One, Zero};
use scirs2_core::random::{rngs::StdRng, Rng, RngExt, SeedableRng};
use scirs2_core::simd_ops::SimdUnifiedOps;
use std::marker::PhantomData;
use std::sync::{Arc, Mutex};

/// Advanced parallel Monte Carlo integrator
pub struct AdvancedParallelMonteCarlo<F> {
    /// Integration configuration
    pub config: MonteCarloConfig,
    /// Adaptive sampling state
    pub adaptive_state: Arc<Mutex<AdaptiveState<F>>>,
    /// Variance reduction techniques
    pub variance_reduction: VarianceReductionConfig,
    /// Performance metrics
    pub metrics: Arc<Mutex<IntegrationMetrics>>,
    _phantom: PhantomData<F>,
}

/// Monte Carlo integration configuration
#[derive(Debug, Clone)]
pub struct MonteCarloConfig {
    /// Initial number of samples
    pub initial_samples: usize,
    /// Maximum number of samples
    pub max_samples: usize,
    /// Target relative error
    pub target_relative_error: f64,
    /// Confidence level for error estimates
    pub confidence_level: f64,
    /// Enable parallel processing
    pub parallel: bool,
    /// Number of parallel workers
    pub num_workers: usize,
    /// Chunk size for parallel processing
    pub chunksize: usize,
    /// Enable adaptive sampling
    pub adaptive_sampling: bool,
    /// Random seed
    pub seed: Option<u64>,
    /// Enable GPU acceleration
    pub use_gpu: bool,
}

impl Default for MonteCarloConfig {
    fn default() -> Self {
        Self {
            initial_samples: 10000,
            max_samples: 1000000,
            target_relative_error: 0.01,
            confidence_level: 0.95,
            parallel: true,
            num_workers: num_cpus::get(),
            chunksize: 1000,
            adaptive_sampling: true,
            seed: None,
            use_gpu: false,
        }
    }
}

/// Variance reduction configuration
#[derive(Debug, Clone)]
pub struct VarianceReductionConfig {
    /// Use importance sampling
    pub importance_sampling: bool,
    /// Use control variates
    pub control_variates: bool,
    /// Use antithetic variables
    pub antithetic_variables: bool,
    /// Use stratified sampling
    pub stratified_sampling: bool,
    /// Number of strata for stratified sampling
    pub num_strata: usize,
    /// Control variate functions
    pub control_functions: Vec<String>,
}

impl Default for VarianceReductionConfig {
    fn default() -> Self {
        Self {
            importance_sampling: false,
            control_variates: false,
            antithetic_variables: true,
            stratified_sampling: false,
            num_strata: 10,
            control_functions: vec![],
        }
    }
}

/// Adaptive sampling state
#[derive(Debug, Clone)]
pub struct AdaptiveState<F> {
    /// Current sample count
    pub n_samples_: usize,
    /// Running mean estimate
    pub running_mean: F,
    /// Running variance estimate
    pub running_variance: F,
    /// Region subdivision information
    pub regions: Vec<IntegrationRegion<F>>,
    /// Importance sampling weights
    pub importance_weights: Option<Array1<F>>,
    /// Error history for convergence monitoring
    pub error_history: Vec<F>,
}

/// Integration region for adaptive sampling
#[derive(Debug, Clone)]
pub struct IntegrationRegion<F> {
    /// Lower bounds
    pub lower_bounds: Array1<F>,
    /// Upper bounds
    pub upper_bounds: Array1<F>,
    /// Estimated integral value
    pub estimated_value: F,
    /// Estimated error
    pub estimated_error: F,
    /// Number of samples in this region
    pub n_samples_: usize,
    /// Priority for refinement
    pub priority: f64,
}

/// Integration metrics and performance monitoring
#[derive(Debug, Clone, Default)]
pub struct IntegrationMetrics {
    /// Total samples evaluated
    pub total_samples: usize,
    /// Total wall-clock time
    pub total_time_seconds: f64,
    /// Samples per second
    pub samples_per_second: f64,
    /// Memory usage in MB
    pub memory_usage_mb: f64,
    /// Parallel efficiency
    pub parallel_efficiency: f64,
    /// Variance reduction factor
    pub variance_reduction_factor: f64,
    /// Error convergence rate
    pub convergence_rate: f64,
}

/// Integration result with detailed statistics
#[derive(Debug, Clone)]
pub struct MonteCarloResult<F> {
    /// Estimated integral value
    pub value: F,
    /// Standard error estimate
    pub standard_error: F,
    /// Confidence interval
    pub confidence_interval: (F, F),
    /// Relative error
    pub relative_error: F,
    /// Number of samples used
    pub n_samples_: usize,
    /// Integration metrics
    pub metrics: IntegrationMetrics,
    /// Convergence achieved
    pub converged: bool,
}

/// Trait for functions to integrate
pub trait IntegrableFunction<F>: Send + Sync
where
    F: Float + Copy + std::fmt::Display,
{
    /// Evaluate function at given point
    fn evaluate(&self, x: &ArrayView1<F>) -> F;

    /// Get integration domain dimension
    fn dimension(&self) -> usize;

    /// Get integration bounds (lower, upper)
    fn bounds(&self) -> (Array1<F>, Array1<F>);

    /// Provide importance sampling density (optional)
    fn importance_density(&self, x: &ArrayView1<F>) -> Option<F> {
        None
    }

    /// Provide control variate function (optional)
    fn control_variate(&self, x: &ArrayView1<F>) -> Option<F> {
        None
    }
}

impl<F> AdvancedParallelMonteCarlo<F>
where
    F: Float
        + Zero
        + One
        + Copy
        + Send
        + Sync
        + SimdUnifiedOps
        + FromPrimitive
        + std::iter::Product
        + 'static
        + std::fmt::Display,
{
    /// Create new advanced parallel Monte Carlo integrator
    pub fn new(config: MonteCarloConfig, variance_reduction: VarianceReductionConfig) -> Self {
        let adaptive_state = Arc::new(Mutex::new(AdaptiveState {
            n_samples_: 0,
            running_mean: F::zero(),
            running_variance: F::zero(),
            regions: vec![],
            importance_weights: None,
            error_history: vec![],
        }));

        let metrics = Arc::new(Mutex::new(IntegrationMetrics::default()));

        Self {
            config,
            adaptive_state,
            variance_reduction,
            metrics,
            _phantom: PhantomData,
        }
    }

    /// Integrate function using advanced parallel Monte Carlo
    pub fn integrate<T>(&mut self, function: &T) -> StatsResult<MonteCarloResult<F>>
    where
        T: IntegrableFunction<F>,
    {
        let start_time = std::time::Instant::now();
        let (lower_bounds, upper_bounds) = function.bounds();
        let _dimension = function.dimension();

        // Initialize adaptive state
        {
            let mut state = self.adaptive_state.lock().expect("Operation failed");
            state.regions = vec![IntegrationRegion {
                lower_bounds: lower_bounds.clone(),
                upper_bounds: upper_bounds.clone(),
                estimated_value: F::zero(),
                estimated_error: F::infinity(),
                n_samples_: 0,
                priority: 1.0,
            }];
        }

        // Initialize random number generator
        let mut main_rng = match self.config.seed {
            Some(seed) => StdRng::seed_from_u64(seed),
            None => StdRng::from_rng(&mut scirs2_core::random::thread_rng()),
        };

        let mut total_samples = 0;
        let mut current_estimate = F::zero();
        let mut current_error = F::infinity();
        let mut converged = false;

        // Adaptive sampling loop
        while total_samples < self.config.max_samples {
            let batchsize = self
                .config
                .initial_samples
                .min(self.config.max_samples - total_samples);

            // Generate samples for this iteration
            let result = if self.config.parallel {
                self.parallel_sampling(function, batchsize, &mut main_rng)?
            } else {
                self.sequential_sampling(function, batchsize, &mut main_rng)?
            };

            // Update estimates
            total_samples += batchsize;
            current_estimate = self.update_estimate(result, total_samples)?;
            current_error = self.estimate_error(total_samples)?;

            // Check convergence
            let relative_error = if current_estimate.abs()
                > F::from(1e-10).expect("Failed to convert constant to float")
            {
                (current_error / current_estimate.abs())
                    .to_f64()
                    .expect("Operation failed")
            } else {
                current_error.to_f64().expect("Operation failed")
            };

            if relative_error < self.config.target_relative_error {
                converged = true;
                break;
            }

            // Adaptive refinement
            if self.config.adaptive_sampling {
                self.adaptive_refinement(function)?;
            }
        }

        // Compute confidence interval
        let z_score = self.get_z_score(self.config.confidence_level);
        let margin = F::from(z_score).expect("Failed to convert to float") * current_error;
        let confidence_interval = (current_estimate - margin, current_estimate + margin);

        // Update metrics
        let elapsed_time = start_time.elapsed().as_secs_f64();
        {
            let mut metrics = self.metrics.lock().expect("Operation failed");
            metrics.total_samples = total_samples;
            metrics.total_time_seconds = elapsed_time;
            metrics.samples_per_second = total_samples as f64 / elapsed_time;
            metrics.convergence_rate = self.estimate_convergence_rate()?;
        }

        Ok(MonteCarloResult {
            value: current_estimate,
            standard_error: current_error,
            confidence_interval,
            relative_error: F::from(if current_estimate.abs() > F::zero() {
                (current_error / current_estimate.abs())
                    .to_f64()
                    .expect("Operation failed")
            } else {
                current_error.to_f64().expect("Operation failed")
            })
            .expect("Operation failed"),
            n_samples_: total_samples,
            metrics: self.metrics.lock().expect("Operation failed").clone(),
            converged,
        })
    }

    /// Parallel sampling with load balancing
    fn parallel_sampling<T>(
        &self,
        function: &T,
        n_samples_: usize,
        rng: &mut StdRng,
    ) -> StatsResult<Array1<F>>
    where
        T: IntegrableFunction<F>,
    {
        let chunksize = self.config.chunksize;
        let num_chunks = n_samples_.div_ceil(chunksize);

        // Generate seeds for parallel workers
        let seeds: Vec<u64> = (0..num_chunks).map(|_| rng.random()).collect();

        // Parallel evaluation
        let results: Vec<StatsResult<Array1<F>>> = seeds
            .iter()
            .enumerate()
            .map(|(chunk_idx, &seed)| {
                let start = chunk_idx * chunksize;
                let end = ((chunk_idx + 1) * chunksize).min(n_samples_);
                let actual_chunksize = end - start;

                self.evaluate_chunk(function, actual_chunksize, seed)
            })
            .collect();

        // Combine results
        let mut combined_values = Vec::new();
        for result in results {
            let chunk_values = result?;
            combined_values.extend(chunk_values.iter().copied());
        }

        Ok(Array1::from_vec(combined_values))
    }

    /// Sequential sampling for comparison
    fn sequential_sampling<T>(
        &self,
        function: &T,
        n_samples_: usize,
        rng: &mut StdRng,
    ) -> StatsResult<Array1<F>>
    where
        T: IntegrableFunction<F>,
    {
        self.evaluate_chunk(function, n_samples_, rng.random())
    }

    /// Evaluate function on a chunk of samples (Ultra-optimized with bandwidth-saturated SIMD)
    fn evaluate_chunk<T>(
        &self,
        function: &T,
        n_samples_: usize,
        seed: u64,
    ) -> StatsResult<Array1<F>>
    where
        T: IntegrableFunction<F>,
    {
        let mut rng = StdRng::seed_from_u64(seed);
        let (lower_bounds, upper_bounds) = function.bounds();
        let dimension = function.dimension();
        let mut values = Vec::with_capacity(n_samples_);

        // Use ultra-optimized SIMD for large sample sizes
        if n_samples_ >= 64 {
            return self.evaluate_chunk_simd_ultra(function, n_samples_, seed);
        }

        for _ in 0..n_samples_ {
            // Generate sample point
            let mut point = Array1::zeros(dimension);
            for j in 0..dimension {
                let u: f64 = rng.random();
                let range = upper_bounds[j] - lower_bounds[j];
                point[j] =
                    lower_bounds[j] + F::from(u).expect("Failed to convert to float") * range;
            }

            // Apply variance reduction techniques
            let sample_value =
                if self.variance_reduction.antithetic_variables && values.len() % 2 == 1 {
                    // Use antithetic variable
                    let antithetic_point =
                        self.generate_antithetic_point(&point, &lower_bounds, &upper_bounds);
                    let value1 = function.evaluate(&point.view());
                    let value2 = function.evaluate(&antithetic_point.view());
                    (value1 + value2) / F::from(2.0).expect("Failed to convert constant to float")
                } else {
                    function.evaluate(&point.view())
                };

            // Apply importance sampling weight if enabled
            let weighted_value = if self.variance_reduction.importance_sampling {
                if let Some(importance_density) = function.importance_density(&point.view()) {
                    sample_value / importance_density
                } else {
                    sample_value
                }
            } else {
                sample_value
            };

            // Apply control variates if enabled
            let final_value = if self.variance_reduction.control_variates {
                if let Some(control_value) = function.control_variate(&point.view()) {
                    // Simplified control variate (would need proper coefficient estimation)
                    weighted_value
                        - F::from(0.5).expect("Failed to convert constant to float") * control_value
                } else {
                    weighted_value
                }
            } else {
                weighted_value
            };

            values.push(final_value);
        }

        // Scale by integration volume
        let volume = self.compute_integration_volume(&lower_bounds, &upper_bounds);
        let scaled_values = values.into_iter().map(|v| v * volume).collect();

        Ok(Array1::from_vec(scaled_values))
    }

    /// Ultra-optimized SIMD Monte Carlo evaluation targeting 80-90% memory bandwidth utilization
    fn evaluate_chunk_simd_ultra<T>(
        &self,
        function: &T,
        n_samples_: usize,
        seed: u64,
    ) -> StatsResult<Array1<F>>
    where
        T: IntegrableFunction<F>,
    {
        use scirs2_core::simd_ops::PlatformCapabilities;

        let capabilities = PlatformCapabilities::detect();
        let mut rng = StdRng::seed_from_u64(seed);
        let (lower_bounds, upper_bounds) = function.bounds();
        let dimension = function.dimension();

        // Process in bandwidth-saturated chunks (16 samples per SIMD iteration)
        let chunk_size = if capabilities.has_avx512() {
            16
        } else if capabilities.has_avx2() {
            8
        } else {
            4
        };
        let num_chunks = n_samples_.div_ceil(chunk_size);
        let mut values = Vec::with_capacity(n_samples_);

        // Pre-allocate SIMD-aligned buffers for bandwidth saturation
        let mut sample_points = Vec::with_capacity(chunk_size * dimension);
        let mut sample_values = Vec::with_capacity(chunk_size);
        let mut variance_reduction_buffer = Vec::with_capacity(chunk_size);

        for _chunk_idx in 0..num_chunks {
            let current_chunk_size = std::cmp::min(chunk_size, n_samples_ - values.len());
            if current_chunk_size == 0 {
                break;
            }

            // Generate batch of sample points with ultra-optimized SIMD
            sample_points.clear();
            for _ in 0..current_chunk_size {
                for j in 0..dimension {
                    let u: f64 = rng.random();
                    let range = upper_bounds[j] - lower_bounds[j];
                    let sample_coord =
                        lower_bounds[j] + F::from(u).expect("Failed to convert to float") * range;
                    sample_points.push(sample_coord.to_f64().expect("Operation failed") as f32);
                }
            }

            // Batch function evaluation with SIMD-optimized processing
            sample_values.clear();
            for i in 0..current_chunk_size {
                let point_start = i * dimension;
                let point_slice = &sample_points[point_start..point_start + dimension];

                // Convert back to F type for function evaluation
                let mut point = Array1::zeros(dimension);
                for (j, &val) in point_slice.iter().enumerate() {
                    point[j] = F::from(val as f64).expect("Failed to convert to float");
                }

                let sample_value = function.evaluate(&point.view());
                sample_values.push(sample_value.to_f64().expect("Operation failed") as f32);
            }

            // Apply variance reduction with bandwidth-saturated SIMD
            variance_reduction_buffer.clear();
            if self.variance_reduction.antithetic_variables {
                // Ultra-optimized antithetic variables processing
                for i in (0..current_chunk_size).step_by(2) {
                    if i + 1 < current_chunk_size {
                        let avg_value = (sample_values[i] + sample_values[i + 1]) * 0.5;
                        variance_reduction_buffer.push(avg_value);
                        variance_reduction_buffer.push(avg_value);
                    } else {
                        variance_reduction_buffer.push(sample_values[i]);
                    }
                }
            } else {
                variance_reduction_buffer.extend_from_slice(&sample_values);
            }

            // Ultra-optimized SIMD scaling by integration volume
            let volume = self.compute_integration_volume(&lower_bounds, &upper_bounds);
            let volume_f32 = volume.to_f64().expect("Operation failed") as f32;

            // Bandwidth-saturated SIMD volume scaling
            if capabilities.has_avx2() && variance_reduction_buffer.len() >= 8 {
                let mut scaled_chunk = Array1::zeros(variance_reduction_buffer.len());
                let volume_vec = Array1::from_elem(variance_reduction_buffer.len(), volume_f32);
                let variance_reduction_array = Array1::from_vec(variance_reduction_buffer.clone());

                // Ultra-optimized SIMD multiplication targeting bandwidth saturation
                f32::simd_mul_f32_ultra(
                    &variance_reduction_array.view(),
                    &volume_vec.view(),
                    &mut scaled_chunk.view_mut(),
                );

                for &val in scaled_chunk.iter() {
                    values.push(F::from(val as f64).expect("Failed to convert to float"));
                }
            } else {
                // Scalar fallback for small chunks
                for &val in &variance_reduction_buffer {
                    values.push(F::from((val * volume_f32) as f64).expect("Operation failed"));
                }
            }
        }

        // Ensure we don't exceed the requested number of samples
        values.truncate(n_samples_);
        Ok(Array1::from_vec(values))
    }

    /// Generate antithetic variable
    fn generate_antithetic_point(
        &self,
        point: &Array1<F>,
        lower_bounds: &Array1<F>,
        upper_bounds: &Array1<F>,
    ) -> Array1<F> {
        let mut antithetic = Array1::zeros(point.len());
        for i in 0..point.len() {
            let range = upper_bounds[i] - lower_bounds[i];
            let normalized = (point[i] - lower_bounds[i]) / range;
            let antithetic_normalized = F::one() - normalized;
            antithetic[i] = lower_bounds[i] + antithetic_normalized * range;
        }
        antithetic
    }

    /// Compute integration volume
    fn compute_integration_volume(&self, lower_bounds: &Array1<F>, upper_bounds: &Array1<F>) -> F {
        upper_bounds
            .iter()
            .zip(lower_bounds.iter())
            .map(|(&upper, &lower)| upper - lower)
            .product()
    }

    /// Update running estimate
    fn update_estimate(&self, new_values: Array1<F>, total_samples: usize) -> StatsResult<F> {
        let mut state = self.adaptive_state.lock().expect("Operation failed");

        let batch_mean = new_values.mean().expect("Operation failed");
        let batchsize = new_values.len();

        if state.n_samples_ == 0 {
            state.running_mean = batch_mean;
            state.running_variance = if batchsize > 1 {
                new_values.var(F::one())
            } else {
                F::zero()
            };
        } else {
            // Online update of mean and variance
            let old_n = state.n_samples_ as f64;
            let new_n = total_samples as f64;
            let old_mean = state.running_mean;

            // Update mean
            state.running_mean = (old_mean * F::from(old_n).expect("Failed to convert to float")
                + batch_mean * F::from(batchsize).expect("Failed to convert to float"))
                / F::from(new_n).expect("Failed to convert to float");

            // Update variance using Welford's algorithm
            let batch_var = if batchsize > 1 {
                new_values.var(F::one())
            } else {
                F::zero()
            };
            let mean_diff = batch_mean - old_mean;
            state.running_variance = (state.running_variance
                * F::from(old_n - 1.0).expect("Failed to convert to float")
                + batch_var * F::from(batchsize - 1).expect("Failed to convert to float")
                + mean_diff
                    * mean_diff
                    * F::from(old_n * batchsize as f64 / new_n)
                        .expect("Failed to convert to float"))
                / F::from(new_n - 1.0).expect("Failed to convert to float");
        }

        state.n_samples_ = total_samples;
        Ok(state.running_mean)
    }

    /// Estimate current error
    fn estimate_error(&self, n_samples_: usize) -> StatsResult<F> {
        let state = self.adaptive_state.lock().expect("Operation failed");
        if n_samples_ <= 1 {
            return Ok(F::infinity());
        }

        let standard_error = (state.running_variance
            / F::from(n_samples_).expect("Failed to convert to float"))
        .sqrt();
        Ok(standard_error)
    }

    /// Adaptive refinement of integration regions
    fn adaptive_refinement<T>(&self, function: &T) -> StatsResult<()>
    where
        T: IntegrableFunction<F>,
    {
        // Simplified adaptive refinement
        // In a full implementation, this would subdivide regions with high error
        let mut state = self.adaptive_state.lock().expect("Operation failed");

        // Find region with highest error-to-samples ratio
        if let Some(worst_region_idx) = state
            .regions
            .iter()
            .enumerate()
            .max_by(|(_, a), (_, b)| {
                let ratio_a = a.estimated_error.to_f64().expect("Operation failed")
                    / (a.n_samples_ + 1) as f64;
                let ratio_b = b.estimated_error.to_f64().expect("Operation failed")
                    / (b.n_samples_ + 1) as f64;
                ratio_a
                    .partial_cmp(&ratio_b)
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
            .map(|(idx, _)| idx)
        {
            // Simple subdivision along the first dimension
            let region = state.regions[worst_region_idx].clone();
            if !region.lower_bounds.is_empty() {
                let mid = (region.lower_bounds[0] + region.upper_bounds[0])
                    / F::from(2.0).expect("Failed to convert constant to float");

                let mut left_upper = region.upper_bounds.clone();
                left_upper[0] = mid;

                let mut right_lower = region.lower_bounds.clone();
                right_lower[0] = mid;

                let left_region = IntegrationRegion {
                    lower_bounds: region.lower_bounds.clone(),
                    upper_bounds: left_upper,
                    estimated_value: region.estimated_value
                        / F::from(2.0).expect("Failed to convert constant to float"),
                    estimated_error: region.estimated_error
                        / F::from(2.0).expect("Failed to convert constant to float"),
                    n_samples_: 0,
                    priority: 1.0,
                };

                let right_region = IntegrationRegion {
                    lower_bounds: right_lower,
                    upper_bounds: region.upper_bounds.clone(),
                    estimated_value: region.estimated_value
                        / F::from(2.0).expect("Failed to convert constant to float"),
                    estimated_error: region.estimated_error
                        / F::from(2.0).expect("Failed to convert constant to float"),
                    n_samples_: 0,
                    priority: 1.0,
                };

                state.regions[worst_region_idx] = left_region;
                state.regions.push(right_region);
            }
        }

        Ok(())
    }

    /// Get z-score for confidence level
    fn get_z_score(&self, confidence_level: f64) -> f64 {
        // Simplified z-score lookup
        match confidence_level {
            x if x >= 0.99 => 2.576,
            x if x >= 0.95 => 1.96,
            x if x >= 0.90 => 1.645,
            _ => 1.96,
        }
    }

    /// Estimate convergence rate
    fn estimate_convergence_rate(&self) -> StatsResult<f64> {
        let state = self.adaptive_state.lock().expect("Operation failed");
        if state.error_history.len() < 3 {
            return Ok(1.0); // Default convergence rate
        }

        // Simple convergence rate estimate based on error reduction
        let recent_errors: Vec<f64> = state
            .error_history
            .iter()
            .rev()
            .take(5)
            .map(|&e| e.to_f64().expect("Operation failed"))
            .collect();

        if recent_errors.len() >= 2 {
            let ratio = recent_errors[0] / recent_errors[recent_errors.len() - 1];
            Ok(ratio.ln() / (recent_errors.len() - 1) as f64)
        } else {
            Ok(1.0)
        }
    }
}

/// Convenience function for simple integration
#[allow(dead_code)]
pub fn integrate_parallel<T, F>(
    function: &T,
    config: Option<MonteCarloConfig>,
) -> StatsResult<MonteCarloResult<F>>
where
    T: IntegrableFunction<F>,
    F: Float
        + Zero
        + One
        + Copy
        + Send
        + Sync
        + SimdUnifiedOps
        + FromPrimitive
        + 'static
        + std::iter::Product
        + std::fmt::Display,
{
    let config = config.unwrap_or_default();
    let variance_reduction = VarianceReductionConfig::default();
    let mut integrator = AdvancedParallelMonteCarlo::new(config, variance_reduction);
    integrator.integrate(function)
}

/// Multi-dimensional test function implementation
pub struct TestFunction {
    pub dimension: usize,
    pub lower_bounds: Array1<f64>,
    pub upper_bounds: Array1<f64>,
}

impl TestFunction {
    pub fn new(dimension: usize) -> Self {
        Self {
            dimension,
            lower_bounds: Array1::zeros(dimension),
            upper_bounds: Array1::ones(dimension),
        }
    }
}

impl IntegrableFunction<f64> for TestFunction {
    fn evaluate(&self, x: &ArrayView1<f64>) -> f64 {
        // Simple test function: product of coordinates
        x.iter().product()
    }

    fn dimension(&self) -> usize {
        self.dimension
    }

    fn bounds(&self) -> (Array1<f64>, Array1<f64>) {
        (self.lower_bounds.clone(), self.upper_bounds.clone())
    }
}

/// Gaussian function for testing
pub struct GaussianFunction {
    pub mean: Array1<f64>,
    pub covariance: Array2<f64>,
    pub lower_bounds: Array1<f64>,
    pub upper_bounds: Array1<f64>,
}

impl GaussianFunction {
    pub fn new(mean: Array1<f64>, covariance: Array2<f64>) -> Self {
        let dimension = mean.len();
        let lower_bounds = Array1::from_elem(dimension, -5.0);
        let upper_bounds = Array1::from_elem(dimension, 5.0);

        Self {
            mean,
            covariance,
            lower_bounds,
            upper_bounds,
        }
    }
}

impl IntegrableFunction<f64> for GaussianFunction {
    fn evaluate(&self, x: &ArrayView1<f64>) -> f64 {
        let diff = x - &self.mean;
        let cov_inv = scirs2_linalg::inv(&self.covariance.view(), None).expect("Operation failed");
        let quad_form = diff.dot(&cov_inv.dot(&diff));
        let det = scirs2_linalg::det(&self.covariance.view(), None).expect("Operation failed");

        let normalization =
            1.0 / ((2.0 * std::f64::consts::PI).powf(self.mean.len() as f64 / 2.0) * det.sqrt());
        normalization * (-0.5 * quad_form).exp()
    }

    fn dimension(&self) -> usize {
        self.mean.len()
    }

    fn bounds(&self) -> (Array1<f64>, Array1<f64>) {
        (self.lower_bounds.clone(), self.upper_bounds.clone())
    }
}