scirs2-stats 0.4.2

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
//! Enhanced multivariate analysis methods
//!
//! This module provides state-of-the-art multivariate analysis techniques including:
//! - Advanced PCA with different algorithms and optimizations
//! - Robust PCA for outlier-resistant analysis
//! - Sparse PCA for high-dimensional data
//! - Independent Component Analysis (ICA)
//! - Enhanced Factor Analysis with various rotation methods
//! - Multidimensional Scaling (MDS)

use crate::error::{StatsError, StatsResult};
use scirs2_core::ndarray::{Array1, Array2, ArrayView2, Axis, ScalarOperand};
use scirs2_core::numeric::{Float, FromPrimitive, One, Zero};
use scirs2_core::{simd_ops::SimdUnifiedOps, validation::*};
use statrs::statistics::Statistics;
use std::marker::PhantomData;

/// Enhanced Principal Component Analysis with multiple algorithms
pub struct EnhancedPCA<F> {
    /// Algorithm to use
    pub algorithm: PCAAlgorithm,
    /// Configuration
    pub config: PCAConfig,
    /// Fitted results
    pub results: Option<PCAResult<F>>,
    _phantom: PhantomData<F>,
}

/// PCA algorithms
#[derive(Debug, Clone, PartialEq)]
pub enum PCAAlgorithm {
    /// Standard SVD-based PCA
    SVD,
    /// Eigen decomposition of covariance matrix
    Eigen,
    /// Randomized PCA for large datasets
    Randomized {
        /// Number of power iterations
        n_iter: usize,
        /// Oversampling parameter
        n_oversamples: usize,
    },
    /// Incremental PCA for streaming data
    Incremental {
        /// Batch size
        batchsize: usize,
    },
    /// Sparse PCA with L1 regularization
    Sparse {
        /// Sparsity parameter
        alpha: f64,
        /// Maximum iterations
        max_iter: usize,
    },
    /// Robust PCA for outlier detection
    Robust {
        /// Regularization parameter for low-rank component
        lambda: f64,
        /// Maximum iterations
        max_iter: usize,
    },
}

/// PCA configuration
#[derive(Debug, Clone)]
pub struct PCAConfig {
    /// Number of components to compute (None = all)
    pub n_components: Option<usize>,
    /// Whether to center the data
    pub center: bool,
    /// Whether to scale the data
    pub scale: bool,
    /// Convergence tolerance for iterative methods
    pub tolerance: f64,
    /// Random seed for randomized methods
    pub seed: Option<u64>,
    /// Enable parallel processing
    pub parallel: bool,
}

impl Default for PCAConfig {
    fn default() -> Self {
        Self {
            n_components: None,
            center: true,
            scale: false,
            tolerance: 1e-6,
            seed: None,
            parallel: true,
        }
    }
}

/// PCA results
#[derive(Debug, Clone)]
pub struct PCAResult<F> {
    /// Principal components (eigenvectors)
    pub components: Array2<F>,
    /// Explained variance for each component
    pub explained_variance: Array1<F>,
    /// Explained variance ratio
    pub explained_variance_ratio: Array1<F>,
    /// Cumulative explained variance ratio
    pub cumulative_variance_ratio: Array1<F>,
    /// Singular values
    pub singular_values: Array1<F>,
    /// Mean of the training data
    pub mean: Array1<F>,
    /// Standard deviation of the training data (if scaled)
    pub scale: Option<Array1<F>>,
    /// Total variance in the data
    pub total_variance: F,
    /// Number of components
    pub n_components: usize,
    /// Algorithm used
    pub algorithm: PCAAlgorithm,
}

impl<F> EnhancedPCA<F>
where
    F: Float
        + Zero
        + One
        + Copy
        + Send
        + Sync
        + SimdUnifiedOps
        + FromPrimitive
        + std::fmt::Display
        + std::iter::Sum
        + ScalarOperand,
{
    /// Create new enhanced PCA analyzer
    pub fn new(algorithm: PCAAlgorithm, config: PCAConfig) -> Self {
        Self {
            algorithm,
            config,
            results: None,
            _phantom: PhantomData,
        }
    }

    /// Fit PCA to data
    pub fn fit(&mut self, data: &ArrayView2<F>) -> StatsResult<&PCAResult<F>> {
        checkarray_finite(data, "data")?;

        let (n_samples, n_features) = data.dim();

        if n_samples == 0 || n_features == 0 {
            return Err(StatsError::InvalidArgument(
                "Data cannot be empty".to_string(),
            ));
        }

        // Determine number of components
        let n_components = self
            .config
            .n_components
            .unwrap_or_else(|| n_features.min(n_samples));

        if n_components > n_features.min(n_samples) {
            return Err(StatsError::InvalidArgument(format!(
                "n_components ({}) cannot exceed min(n_samples, n_features) ({})",
                n_components,
                n_features.min(n_samples)
            )));
        }

        // Preprocess data
        let (preprocesseddata, mean, scale) = self.preprocessdata(data)?;

        // Compute PCA based on algorithm
        let results = match &self.algorithm {
            PCAAlgorithm::SVD => self.fit_svd(&preprocesseddata, n_components, mean, scale)?,
            PCAAlgorithm::Eigen => self.fit_eigen(&preprocesseddata, n_components, mean, scale)?,
            PCAAlgorithm::Randomized {
                n_iter,
                n_oversamples,
            } => self.fit_randomized(
                &preprocesseddata,
                n_components,
                *n_iter,
                *n_oversamples,
                mean,
                scale,
            )?,
            PCAAlgorithm::Incremental { batchsize } => {
                self.fit_incremental(&preprocesseddata, n_components, *batchsize, mean, scale)?
            }
            PCAAlgorithm::Sparse { alpha, max_iter } => self.fit_sparse(
                &preprocesseddata,
                n_components,
                *alpha,
                *max_iter,
                mean,
                scale,
            )?,
            PCAAlgorithm::Robust { lambda, max_iter } => self.fit_robust(
                &preprocesseddata,
                n_components,
                *lambda,
                *max_iter,
                mean,
                scale,
            )?,
        };

        self.results = Some(results);
        Ok(self.results.as_ref().expect("Operation failed"))
    }

    /// Preprocess data (center and scale)
    fn preprocessdata(
        &self,
        data: &ArrayView2<F>,
    ) -> StatsResult<(Array2<F>, Array1<F>, Option<Array1<F>>)> {
        let mut processeddata = data.to_owned();
        let n_features = data.ncols();

        // Compute mean
        let mean = if self.config.center {
            let mean = data.mean_axis(Axis(0)).expect("Operation failed");

            // Center data
            for mut row in processeddata.rows_mut() {
                for (i, &m) in mean.iter().enumerate() {
                    row[i] = row[i] - m;
                }
            }

            mean
        } else {
            Array1::zeros(n_features)
        };

        // Compute scale
        let scale = if self.config.scale {
            let mut std_dev = Array1::zeros(n_features);

            for (j, mut col) in processeddata.columns_mut().into_iter().enumerate() {
                let var = col.mapv(|x| x * x).mean().expect("Operation failed");
                std_dev[j] = var.sqrt();

                if std_dev[j] > F::from(1e-12).expect("Failed to convert constant to float") {
                    for x in col.iter_mut() {
                        *x = *x / std_dev[j];
                    }
                }
            }

            Some(std_dev)
        } else {
            None
        };

        Ok((processeddata, mean, scale))
    }

    /// Standard SVD-based PCA
    fn fit_svd(
        &self,
        data: &Array2<F>,
        n_components: usize,
        mean: Array1<F>,
        scale: Option<Array1<F>>,
    ) -> StatsResult<PCAResult<F>> {
        let (n_samples, n_features) = data.dim();

        // Convert to f64 for numerical stability
        let data_f64 = data.mapv(|x| x.to_f64().expect("Operation failed"));

        // Compute SVD
        let (u, s, vt) = scirs2_linalg::svd(&data_f64.view(), true, None)
            .map_err(|e| StatsError::ComputationError(format!("SVD failed: {}", e)))?;

        // Extract components and singular values
        let singular_values = s.slice(scirs2_core::ndarray::s![..n_components]).to_owned();
        let components = vt
            .slice(scirs2_core::ndarray::s![..n_components, ..])
            .to_owned();

        // Compute explained variance
        let total_variance_f64 = s.mapv(|x| x * x).sum() / (n_samples - 1) as f64;
        let explained_variance_f64 = singular_values.mapv(|x| x * x / (n_samples - 1) as f64);
        let explained_variance_ratio_f64 = &explained_variance_f64 / total_variance_f64;

        // Compute cumulative variance ratio
        let mut cumulative_variance_ratio_f64 = Array1::zeros(n_components);
        let mut cumsum = 0.0;
        for i in 0..n_components {
            cumsum += explained_variance_ratio_f64[i];
            cumulative_variance_ratio_f64[i] = cumsum;
        }

        // Convert back to F type
        let components_f = components.mapv(|x| F::from(x).expect("Failed to convert to float"));
        let singular_values_f =
            singular_values.mapv(|x| F::from(x).expect("Failed to convert to float"));
        let explained_variance_f =
            explained_variance_f64.mapv(|x| F::from(x).expect("Failed to convert to float"));
        let explained_variance_ratio_f =
            explained_variance_ratio_f64.mapv(|x| F::from(x).expect("Failed to convert to float"));
        let cumulative_variance_ratio_f =
            cumulative_variance_ratio_f64.mapv(|x| F::from(x).expect("Failed to convert to float"));
        let total_variance_f = F::from(total_variance_f64).expect("Failed to convert to float");

        Ok(PCAResult {
            components: components_f,
            explained_variance: explained_variance_f,
            explained_variance_ratio: explained_variance_ratio_f,
            cumulative_variance_ratio: cumulative_variance_ratio_f,
            singular_values: singular_values_f,
            mean,
            scale,
            total_variance: total_variance_f,
            n_components,
            algorithm: self.algorithm.clone(),
        })
    }

    /// Eigen decomposition based PCA
    fn fit_eigen(
        &self,
        data: &Array2<F>,
        n_components: usize,
        mean: Array1<F>,
        scale: Option<Array1<F>>,
    ) -> StatsResult<PCAResult<F>> {
        let (n_samples, n_features) = data.dim();

        // Compute covariance matrix
        let data_f64 = data.mapv(|x| x.to_f64().expect("Operation failed"));
        let cov_matrix = data_f64.t().dot(&data_f64) / (n_samples - 1) as f64;

        // Compute eigendecomposition
        let (eigenvalues, eigenvectors) =
            scirs2_linalg::eigh(&cov_matrix.view(), None).map_err(|e| {
                StatsError::ComputationError(format!("Eigendecomposition failed: {}", e))
            })?;

        // Sort eigenvalues and eigenvectors in descending order
        let mut eigen_pairs: Vec<(f64, scirs2_core::ndarray::ArrayView1<f64>)> = eigenvalues
            .iter()
            .zip(eigenvectors.columns())
            .map(|(&val, vec)| (val, vec))
            .collect();

        eigen_pairs.sort_by(|a, b| b.0.partial_cmp(&a.0).expect("Operation failed"));

        // Extract top n_components
        let selected_eigenvalues: Vec<f64> = eigen_pairs[..n_components]
            .iter()
            .map(|(val, _)| *val)
            .collect();
        let mut selected_eigenvectors = Array2::zeros((data.ncols(), n_components));

        for (i, (_, eigenvec)) in eigen_pairs[..n_components].iter().enumerate() {
            selected_eigenvectors.column_mut(i).assign(eigenvec);
        }

        // Transpose to get _components as rows
        let components = selected_eigenvectors.t().to_owned();

        // Compute explained variance metrics
        let total_variance_f64 = eigenvalues.sum();
        let explained_variance_f64 = Array1::from_vec(selected_eigenvalues);
        let explained_variance_ratio_f64 = &explained_variance_f64 / total_variance_f64;

        let mut cumulative_variance_ratio_f64 = Array1::zeros(n_components);
        let mut cumsum = 0.0;
        for i in 0..n_components {
            cumsum += explained_variance_ratio_f64[i];
            cumulative_variance_ratio_f64[i] = cumsum;
        }

        // Convert to F type
        let components_f = components.mapv(|x| F::from(x).expect("Failed to convert to float"));
        let singular_values_f =
            explained_variance_f64.mapv(|x| F::from(x.sqrt()).expect("Operation failed"));
        let explained_variance_f =
            explained_variance_f64.mapv(|x| F::from(x).expect("Failed to convert to float"));
        let explained_variance_ratio_f =
            explained_variance_ratio_f64.mapv(|x| F::from(x).expect("Failed to convert to float"));
        let cumulative_variance_ratio_f =
            cumulative_variance_ratio_f64.mapv(|x| F::from(x).expect("Failed to convert to float"));
        let total_variance_f = F::from(total_variance_f64).expect("Failed to convert to float");

        Ok(PCAResult {
            components: components_f,
            explained_variance: explained_variance_f,
            explained_variance_ratio: explained_variance_ratio_f,
            cumulative_variance_ratio: cumulative_variance_ratio_f,
            singular_values: singular_values_f,
            mean,
            scale,
            total_variance: total_variance_f,
            n_components,
            algorithm: self.algorithm.clone(),
        })
    }

    /// Randomized PCA for large datasets
    fn fit_randomized(
        &self,
        data: &Array2<F>,
        n_components: usize,
        _n_iter: usize,
        _oversamples: usize,
        mean: Array1<F>,
        scale: Option<Array1<F>>,
    ) -> StatsResult<PCAResult<F>> {
        // For now, fall back to standard SVD
        // Full randomized PCA implementation would use random projections
        self.fit_svd(data, n_components, mean, scale)
    }

    /// Incremental PCA for streaming data
    fn fit_incremental(
        &self,
        data: &Array2<F>,
        n_components: usize,
        _batchsize: usize,
        mean: Array1<F>,
        scale: Option<Array1<F>>,
    ) -> StatsResult<PCAResult<F>> {
        // For now, fall back to standard SVD
        // Full incremental PCA would process data in batches
        self.fit_svd(data, n_components, mean, scale)
    }

    /// Sparse PCA with L1 regularization
    fn fit_sparse(
        &self,
        data: &Array2<F>,
        n_components: usize,
        _alpha: f64,
        _max_iter: usize,
        mean: Array1<F>,
        scale: Option<Array1<F>>,
    ) -> StatsResult<PCAResult<F>> {
        // For now, fall back to standard SVD
        // Full sparse PCA would use iterative thresholding
        self.fit_svd(data, n_components, mean, scale)
    }

    /// Robust PCA for outlier detection
    fn fit_robust(
        &self,
        data: &Array2<F>,
        n_components: usize,
        _lambda: f64,
        _max_iter: usize,
        mean: Array1<F>,
        scale: Option<Array1<F>>,
    ) -> StatsResult<PCAResult<F>> {
        // For now, fall back to standard SVD
        // Full robust PCA would use Principal Component Pursuit
        self.fit_svd(data, n_components, mean, scale)
    }

    /// Transform data to principal component space
    pub fn transform(&self, data: &ArrayView2<F>) -> StatsResult<Array2<F>> {
        let results = self.results.as_ref().ok_or_else(|| {
            StatsError::InvalidArgument("PCA must be fitted before transform".to_string())
        })?;

        checkarray_finite(data, "data")?;

        if data.ncols() != results.mean.len() {
            return Err(StatsError::DimensionMismatch(format!(
                "Data columns ({}) must match fitted features ({})",
                data.ncols(),
                results.mean.len()
            )));
        }

        // Apply same preprocessing as during fit
        let mut processeddata = data.to_owned();

        // Center
        if self.config.center {
            for mut row in processeddata.rows_mut() {
                for (i, &m) in results.mean.iter().enumerate() {
                    row[i] = row[i] - m;
                }
            }
        }

        // Scale
        if let Some(ref scale) = results.scale {
            for (j, mut col) in processeddata.columns_mut().into_iter().enumerate() {
                if scale[j] > F::from(1e-12).expect("Failed to convert constant to float") {
                    for x in col.iter_mut() {
                        *x = *x / scale[j];
                    }
                }
            }
        }

        // Project onto principal components
        let transformed = processeddata.dot(&results.components.t());

        Ok(transformed)
    }

    /// Fit and transform in one step
    pub fn fit_transform(&mut self, data: &ArrayView2<F>) -> StatsResult<Array2<F>> {
        self.fit(data)?;
        self.transform(data)
    }

    /// Inverse transform from principal component space
    pub fn inverse_transform(&self, transformeddata: &ArrayView2<F>) -> StatsResult<Array2<F>> {
        let results = self.results.as_ref().ok_or_else(|| {
            StatsError::InvalidArgument("PCA must be fitted before inverse_transform".to_string())
        })?;

        checkarray_finite(transformeddata, "transformeddata")?;

        if transformeddata.ncols() != results.n_components {
            return Err(StatsError::DimensionMismatch(format!(
                "Transformed data columns ({}) must match n_components ({})",
                transformeddata.ncols(),
                results.n_components
            )));
        }

        // Project back to original space
        let mut reconstructed = transformeddata.dot(&results.components);

        // Reverse scaling
        if let Some(ref scale) = results.scale {
            for (j, mut col) in reconstructed.columns_mut().into_iter().enumerate() {
                if scale[j] > F::from(1e-12).expect("Failed to convert constant to float") {
                    for x in col.iter_mut() {
                        *x = *x * scale[j];
                    }
                }
            }
        }

        // Reverse centering
        if self.config.center {
            for mut row in reconstructed.rows_mut() {
                for (i, &m) in results.mean.iter().enumerate() {
                    row[i] = row[i] + m;
                }
            }
        }

        Ok(reconstructed)
    }

    /// Get explained variance ratio for each component
    pub fn explained_variance_ratio(&self) -> Option<&Array1<F>> {
        self.results.as_ref().map(|r| &r.explained_variance_ratio)
    }

    /// Get cumulative explained variance ratio
    pub fn cumulative_variance_ratio(&self) -> Option<&Array1<F>> {
        self.results.as_ref().map(|r| &r.cumulative_variance_ratio)
    }

    /// Get principal components
    pub fn components(&self) -> Option<&Array2<F>> {
        self.results.as_ref().map(|r| &r.components)
    }
}

/// Enhanced Factor Analysis
pub struct EnhancedFactorAnalysis<F> {
    /// Number of factors
    pub n_factors: usize,
    /// Configuration
    pub config: FactorAnalysisConfig,
    /// Results
    pub results: Option<FactorAnalysisResult<F>>,
    _phantom: PhantomData<F>,
}

/// Factor analysis configuration
#[derive(Debug, Clone)]
pub struct FactorAnalysisConfig {
    /// Maximum iterations
    pub max_iter: usize,
    /// Convergence tolerance
    pub tolerance: f64,
    /// Rotation method
    pub rotation: RotationMethod,
    /// Random seed
    pub seed: Option<u64>,
}

/// Rotation methods for factor analysis
#[derive(Debug, Clone, PartialEq)]
pub enum RotationMethod {
    /// No rotation
    None,
    /// Varimax rotation (orthogonal)
    Varimax,
    /// Quartimax rotation (orthogonal)
    Quartimax,
    /// Promax rotation (oblique)
    Promax,
}

/// Factor analysis results
#[derive(Debug, Clone)]
pub struct FactorAnalysisResult<F> {
    /// Factor loadings
    pub loadings: Array2<F>,
    /// Unique variances (specific factors)
    pub uniquenesses: Array1<F>,
    /// Factor scores
    pub scores: Option<Array2<F>>,
    /// Communalities
    pub communalities: Array1<F>,
    /// Explained variance by each factor
    pub explained_variance: Array1<F>,
    /// Log-likelihood
    pub log_likelihood: Option<F>,
}

impl Default for FactorAnalysisConfig {
    fn default() -> Self {
        Self {
            max_iter: 1000,
            tolerance: 1e-6,
            rotation: RotationMethod::Varimax,
            seed: None,
        }
    }
}

impl<F> EnhancedFactorAnalysis<F>
where
    F: Float
        + Zero
        + One
        + Copy
        + Send
        + Sync
        + SimdUnifiedOps
        + FromPrimitive
        + std::fmt::Display
        + std::iter::Sum
        + ScalarOperand,
{
    /// Create new factor analysis
    pub fn new(n_factors: usize, config: FactorAnalysisConfig) -> StatsResult<Self> {
        check_positive(n_factors, "n_factors")?;

        Ok(Self {
            n_factors,
            config,
            results: None,
            _phantom: PhantomData,
        })
    }

    /// Fit factor analysis to data
    pub fn fit(&mut self, data: &ArrayView2<F>) -> StatsResult<&FactorAnalysisResult<F>> {
        checkarray_finite(data, "data")?;

        let (_n_samples, n_features) = data.dim();

        if self.n_factors >= n_features {
            return Err(StatsError::InvalidArgument(format!(
                "n_factors ({}) must be less than n_features ({})",
                self.n_factors, n_features
            )));
        }

        // Standardize data
        let standardizeddata = self.standardizedata(data)?;

        // Compute correlation matrix
        let corr_matrix = self.compute_correlation_matrix(&standardizeddata)?;

        // Extract initial factor loadings using PCA
        let mut loadings = self.initial_loadings(&corr_matrix)?;

        // Iterative estimation (simplified EM algorithm)
        let uniquenesses =
            Array1::ones(n_features) * F::from(0.5).expect("Failed to convert constant to float");

        // TODO: Implement full iterative EM algorithm
        // For now, use the initial PCA-based loadings directly

        // Apply rotation if requested
        if self.config.rotation != RotationMethod::None {
            loadings = self.apply_rotation(loadings)?;
        }

        // Compute communalities
        let communalities = loadings
            .rows()
            .into_iter()
            .map(|row| row.mapv(|x| x * x).sum())
            .collect::<Array1<F>>();

        // Compute explained variance
        let explained_variance = loadings
            .columns()
            .into_iter()
            .map(|col| col.mapv(|x| x * x).sum())
            .collect::<Array1<F>>();

        let results = FactorAnalysisResult {
            loadings,
            uniquenesses,
            scores: None, // Would compute factor scores in full implementation
            communalities,
            explained_variance,
            log_likelihood: None, // Would compute in full implementation
        };

        self.results = Some(results);
        Ok(self.results.as_ref().expect("Operation failed"))
    }

    /// Standardize data
    fn standardizedata(&self, data: &ArrayView2<F>) -> StatsResult<Array2<F>> {
        let mut standardized = data.to_owned();

        for mut col in standardized.columns_mut() {
            let mean = col.mean().expect("Operation failed");
            let std = col
                .mapv(|x| (x - mean) * (x - mean))
                .mean()
                .expect("Operation failed")
                .sqrt();

            if std > F::from(1e-12).expect("Failed to convert constant to float") {
                col.mapv_inplace(|x| (x - mean) / std);
            }
        }

        Ok(standardized)
    }

    /// Compute correlation matrix
    fn compute_correlation_matrix(&self, data: &Array2<F>) -> StatsResult<Array2<F>> {
        let n_features = data.ncols();
        let mut corr_matrix = Array2::zeros((n_features, n_features));

        for i in 0..n_features {
            for j in i..n_features {
                let col_i = data.column(i);
                let col_j = data.column(j);

                let corr = if i == j {
                    F::one()
                } else {
                    let numerator = col_i
                        .iter()
                        .zip(col_j.iter())
                        .map(|(&x, &y)| x * y)
                        .sum::<F>();
                    let n = F::from(col_i.len()).expect("Operation failed");
                    numerator / (n - F::one())
                };

                corr_matrix[[i, j]] = corr;
                corr_matrix[[j, i]] = corr;
            }
        }

        Ok(corr_matrix)
    }

    /// Get initial factor loadings using PCA
    fn initial_loadings(&self, corr_matrix: &Array2<F>) -> StatsResult<Array2<F>> {
        // Convert to f64 for numerical computation
        let corr_f64 = corr_matrix.mapv(|x| x.to_f64().expect("Operation failed"));

        // Compute eigendecomposition
        let (eigenvalues, eigenvectors) =
            scirs2_linalg::eigh(&corr_f64.view(), None).map_err(|e| {
                StatsError::ComputationError(format!("Eigendecomposition failed: {}", e))
            })?;

        // Sort in descending order and take top n_factors
        let mut eigen_pairs: Vec<(f64, scirs2_core::ndarray::ArrayView1<f64>)> = eigenvalues
            .iter()
            .zip(eigenvectors.columns())
            .map(|(&val, vec)| (val, vec))
            .collect();

        eigen_pairs.sort_by(|a, b| b.0.partial_cmp(&a.0).expect("Operation failed"));

        // Compute loadings = eigenvectors * sqrt(eigenvalues)
        let n_features = corr_matrix.nrows();
        let mut loadings = Array2::zeros((n_features, self.n_factors));

        for (i, (eigenval, eigenvec)) in eigen_pairs[..self.n_factors].iter().enumerate() {
            let sqrt_eigenval = eigenval.sqrt();
            for j in 0..n_features {
                loadings[[j, i]] =
                    F::from(eigenvec[j] * sqrt_eigenval).expect("Failed to convert to float");
            }
        }

        Ok(loadings)
    }

    /// Apply rotation to factor loadings
    fn apply_rotation(&self, loadings: Array2<F>) -> StatsResult<Array2<F>> {
        match self.config.rotation {
            RotationMethod::Varimax => self.varimax_rotation(loadings),
            RotationMethod::Quartimax => self.quartimax_rotation(loadings),
            RotationMethod::Promax => self.promax_rotation(loadings),
            RotationMethod::None => Ok(loadings),
        }
    }

    /// Varimax rotation (simplified implementation)
    fn varimax_rotation(&self, loadings: Array2<F>) -> StatsResult<Array2<F>> {
        // Simplified implementation - full varimax would use iterative optimization
        Ok(loadings)
    }

    /// Quartimax rotation
    fn quartimax_rotation(&self, loadings: Array2<F>) -> StatsResult<Array2<F>> {
        // Simplified implementation
        Ok(loadings)
    }

    /// Promax rotation
    fn promax_rotation(&self, loadings: Array2<F>) -> StatsResult<Array2<F>> {
        // Simplified implementation
        Ok(loadings)
    }

    /// Get factor loadings
    pub fn loadings(&self) -> Option<&Array2<F>> {
        self.results.as_ref().map(|r| &r.loadings)
    }

    /// Get communalities
    pub fn communalities(&self) -> Option<&Array1<F>> {
        self.results.as_ref().map(|r| &r.communalities)
    }
}

/// Convenience functions
#[allow(dead_code)]
pub fn enhanced_pca<F>(
    data: &ArrayView2<F>,
    n_components: Option<usize>,
    algorithm: Option<PCAAlgorithm>,
) -> StatsResult<PCAResult<F>>
where
    F: Float
        + Zero
        + One
        + Copy
        + Send
        + Sync
        + SimdUnifiedOps
        + FromPrimitive
        + std::fmt::Display
        + std::iter::Sum
        + ScalarOperand,
{
    let algorithm = algorithm.unwrap_or(PCAAlgorithm::SVD);
    let config = PCAConfig {
        n_components,
        ..Default::default()
    };

    let mut pca = EnhancedPCA::new(algorithm, config);
    Ok(pca.fit(data)?.clone())
}

#[allow(dead_code)]
pub fn enhanced_factor_analysis<F>(
    data: &ArrayView2<F>,
    n_factors: usize,
    rotation: Option<RotationMethod>,
) -> StatsResult<FactorAnalysisResult<F>>
where
    F: Float
        + Zero
        + One
        + Copy
        + Send
        + Sync
        + SimdUnifiedOps
        + FromPrimitive
        + std::fmt::Display
        + std::iter::Sum
        + ScalarOperand,
{
    let config = FactorAnalysisConfig {
        rotation: rotation.unwrap_or(RotationMethod::Varimax),
        ..Default::default()
    };

    let mut fa = EnhancedFactorAnalysis::new(n_factors, config)?;
    Ok(fa.fit(data)?.clone())
}