torsh-cluster 0.1.2

Unsupervised learning and clustering algorithms for ToRSh, powered by SciRS2
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
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
//! Gaussian Mixture Model clustering implementation
//!
//! This module provides a complete implementation of Gaussian Mixture Models (GMM)
//! using the Expectation-Maximization (EM) algorithm, built on SciRS2 foundations.
//!
//! # Algorithm Overview
//!
//! GMM models data as a mixture of K multivariate Gaussian distributions:
//! p(x) = Σ(k=1 to K) π_k * N(x | μ_k, Σ_k)
//!
//! where:
//! - π_k: mixing coefficients (weights)
//! - μ_k: mean vectors
//! - Σ_k: covariance matrices
//!
//! The EM algorithm alternates between:
//! - E-step: Compute posterior probabilities (responsibilities)
//! - M-step: Update parameters based on responsibilities

// Framework infrastructure - components designed for future use
#![allow(dead_code)]
use crate::error::{ClusterError, ClusterResult};
use crate::traits::{
    AlgorithmComplexity, ClusteringAlgorithm, ClusteringConfig, ClusteringResult, Fit, FitPredict,
    MemoryPattern, ProbabilisticClustering,
};
use crate::utils::validation::{validate_cluster_input, validate_n_clusters};
use scirs2_autograd::{self as ag, tensor_ops::*};
use scirs2_core::ndarray::{s, Array1, Array2, Array3, ArrayView1, ArrayView2, Axis};
use scirs2_core::parallel_ops::{is_parallel_enabled, parallel_map};
use scirs2_core::RngExt;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::f64::consts::PI;
use torsh_tensor::Tensor;

/// Covariance matrix types for Gaussian Mixture Model
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum CovarianceType {
    /// Full covariance matrices (most flexible, most parameters)
    Full,
    /// Diagonal covariance matrices (independent features)
    Diag,
    /// Spherical covariance matrices (isotropic)
    Spherical,
}

impl Default for CovarianceType {
    fn default() -> Self {
        Self::Full
    }
}

impl std::fmt::Display for CovarianceType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Full => write!(f, "full"),
            Self::Diag => write!(f, "diag"),
            Self::Spherical => write!(f, "spherical"),
        }
    }
}

/// Gaussian Mixture Model initialization methods
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum GMMInitMethod {
    /// K-means initialization (default)
    KMeans,
    /// Random initialization
    Random,
}

impl Default for GMMInitMethod {
    fn default() -> Self {
        Self::KMeans
    }
}

/// Gaussian Mixture Model configuration
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct GMConfig {
    /// Number of mixture components
    pub n_components: usize,
    /// Type of covariance parameters to use
    pub covariance_type: CovarianceType,
    /// Maximum number of EM iterations
    pub max_iters: usize,
    /// Convergence tolerance (log-likelihood improvement)
    pub tolerance: f64,
    /// Regularization added to covariance diagonal
    pub reg_covar: f64,
    /// Initialization method
    pub init_method: GMMInitMethod,
    /// Random seed for reproducibility
    pub random_state: Option<u64>,
}

impl Default for GMConfig {
    fn default() -> Self {
        Self {
            n_components: 2,
            covariance_type: CovarianceType::Full,
            max_iters: 100,
            tolerance: 1e-3,
            reg_covar: 1e-6,
            init_method: GMMInitMethod::KMeans,
            random_state: None,
        }
    }
}

impl ClusteringConfig for GMConfig {
    fn validate(&self) -> ClusterResult<()> {
        if self.n_components == 0 {
            return Err(ClusterError::InvalidClusters(self.n_components));
        }
        if self.max_iters == 0 {
            return Err(ClusterError::ConfigError(
                "max_iters must be positive".to_string(),
            ));
        }
        if self.tolerance <= 0.0 {
            return Err(ClusterError::ConfigError(
                "tolerance must be positive".to_string(),
            ));
        }
        if self.reg_covar <= 0.0 {
            return Err(ClusterError::ConfigError(
                "reg_covar must be positive".to_string(),
            ));
        }
        Ok(())
    }

    fn default() -> Self {
        <GMConfig as std::default::Default>::default()
    }

    fn merge(&mut self, other: &Self) {
        let default_config = <GMConfig as std::default::Default>::default();
        if other.n_components != default_config.n_components {
            self.n_components = other.n_components;
        }
        if other.max_iters != default_config.max_iters {
            self.max_iters = other.max_iters;
        }
        if other.tolerance != default_config.tolerance {
            self.tolerance = other.tolerance;
        }
    }
}

/// Gaussian Mixture Model fitting result
#[derive(Debug, Clone)]
pub struct GMResult {
    /// Cluster assignments for each sample (hard assignment)
    pub labels: Tensor,
    /// Mean vectors for each component [n_components, n_features]
    pub means: Tensor,
    /// Covariance matrices (shape depends on covariance_type)
    pub covariances: Tensor,
    /// Mixing coefficients (weights) for each component
    pub weights: Tensor,
    /// Posterior probabilities [n_samples, n_components]
    pub responsibilities: Tensor,
    /// Final log-likelihood of the data
    pub log_likelihood: f64,
    /// Number of EM iterations performed
    pub n_iter: usize,
    /// Whether the algorithm converged
    pub converged: bool,
    /// Akaike Information Criterion
    pub aic: f64,
    /// Bayesian Information Criterion
    pub bic: f64,
}

impl ClusteringResult for GMResult {
    fn labels(&self) -> &Tensor {
        &self.labels
    }

    fn n_clusters(&self) -> usize {
        self.means.shape().dims()[0]
    }

    fn centers(&self) -> Option<&Tensor> {
        Some(&self.means)
    }

    fn n_iter(&self) -> Option<usize> {
        Some(self.n_iter)
    }

    fn converged(&self) -> bool {
        self.converged
    }

    fn metadata(&self) -> Option<&HashMap<String, String>> {
        None // Could be enhanced to return more metadata
    }
}

/// Gaussian Mixture Model clustering algorithm
///
/// GMM models data as a mixture of multivariate Gaussian distributions,
/// using the Expectation-Maximization (EM) algorithm for parameter estimation.
///
/// # Mathematical Formulation
///
/// ## Probability Model
///
/// The GMM assumes that data points are generated from a mixture of K Gaussian components:
///
/// ```text
/// p(x) = Σ_{k=1}^K π_k · N(x | μ_k, Σ_k)
/// ```
///
/// where:
/// - `π_k` is the mixing coefficient (weight) for component k, with `Σ π_k = 1`
/// - `μ_k` is the mean vector for component k
/// - `Σ_k` is the covariance matrix for component k
/// - `N(x | μ, Σ)` is the multivariate Gaussian distribution
///
/// ## Gaussian Distribution
///
/// The multivariate Gaussian density is defined as:
///
/// ```text
/// N(x | μ, Σ) = (2π)^(-D/2) |Σ|^(-1/2) exp(-1/2 (x-μ)^T Σ^(-1) (x-μ))
/// ```
///
/// where D is the dimensionality of the data.
///
/// ## EM Algorithm
///
/// The algorithm alternates between two steps:
///
/// ### E-step (Expectation)
///
/// Compute the responsibility (posterior probability) that component k generates point n:
///
/// ```text
/// γ(z_{nk}) = π_k · N(x_n | μ_k, Σ_k) / Σ_{j=1}^K π_j · N(x_n | μ_j, Σ_j)
/// ```
///
/// ### M-step (Maximization)
///
/// Update parameters using the computed responsibilities:
///
/// **Mixing coefficients:**
/// ```text
/// π_k = N_k / N,  where N_k = Σ_{n=1}^N γ(z_{nk})
/// ```
///
/// **Means:**
/// ```text
/// μ_k = (1/N_k) Σ_{n=1}^N γ(z_{nk}) x_n
/// ```
///
/// **Covariances** (depends on covariance type):
///
/// - **Full**: `Σ_k = (1/N_k) Σ_n γ(z_{nk}) (x_n - μ_k)(x_n - μ_k)^T`
/// - **Diagonal**: `Σ_k = diag((1/N_k) Σ_n γ(z_{nk}) (x_n - μ_k)^2)`
/// - **Spherical**: `Σ_k = (1/(N_k·D)) Σ_n γ(z_{nk}) ||x_n - μ_k||^2 · I`
///
/// ## Log-Likelihood
///
/// The algorithm maximizes the log-likelihood:
///
/// ```text
/// ln p(X | π, μ, Σ) = Σ_{n=1}^N ln(Σ_{k=1}^K π_k · N(x_n | μ_k, Σ_k))
/// ```
///
/// ## Model Selection
///
/// Two information criteria are computed for model selection:
///
/// **Akaike Information Criterion (AIC):**
/// ```text
/// AIC = -2 · ln L + 2 · n_params
/// ```
///
/// **Bayesian Information Criterion (BIC):**
/// ```text
/// BIC = -2 · ln L + n_params · ln(N)
/// ```
///
/// where `n_params` is the number of free parameters in the model.
///
/// # Covariance Types
///
/// - **Full**: Each component has its own general covariance matrix (most flexible, most parameters)
/// - **Diagonal**: Covariance matrices are diagonal (assumes feature independence within components)
/// - **Spherical**: Covariance matrices are spherical (σ²I, assumes isotropic variance)
///
/// # Convergence
///
/// The algorithm converges when the change in log-likelihood between iterations
/// falls below the specified tolerance or when the maximum number of iterations is reached.
///
/// # Example
///
/// ```rust
/// use torsh_cluster::algorithms::gaussian_mixture::{GaussianMixture, CovarianceType};
/// use torsh_cluster::traits::Fit;
/// use torsh_tensor::creation::randn;
///
/// let data = randn::<f32>(&[100, 2])?;
/// let gmm = GaussianMixture::new(3)
///     .covariance_type(CovarianceType::Full)
///     .max_iters(150)
///     .tolerance(1e-4);
/// let result = gmm.fit(&data)?;
/// println!("Final log-likelihood: {}", result.log_likelihood);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone)]
pub struct GaussianMixture {
    config: GMConfig,
}

impl GaussianMixture {
    /// Create a new Gaussian Mixture Model with specified number of components
    pub fn new(n_components: usize) -> Self {
        Self {
            config: GMConfig {
                n_components,
                ..Default::default()
            },
        }
    }

    /// Set the covariance type
    pub fn covariance_type(mut self, covariance_type: CovarianceType) -> Self {
        self.config.covariance_type = covariance_type;
        self
    }

    /// Set maximum number of iterations
    pub fn max_iters(mut self, max_iters: usize) -> Self {
        self.config.max_iters = max_iters;
        self
    }

    /// Set convergence tolerance
    pub fn tolerance(mut self, tolerance: f64) -> Self {
        self.config.tolerance = tolerance;
        self
    }

    /// Set regularization parameter for covariance
    pub fn reg_covar(mut self, reg_covar: f64) -> Self {
        self.config.reg_covar = reg_covar;
        self
    }

    /// Set initialization method
    pub fn init_method(mut self, init_method: GMMInitMethod) -> Self {
        self.config.init_method = init_method;
        self
    }

    /// Set random state for reproducibility
    pub fn random_state(mut self, seed: u64) -> Self {
        self.config.random_state = Some(seed);
        self
    }
}

impl ClusteringAlgorithm for GaussianMixture {
    fn name(&self) -> &str {
        "Gaussian Mixture Model"
    }

    fn get_params(&self) -> HashMap<String, String> {
        let mut params = HashMap::new();
        params.insert(
            "n_components".to_string(),
            self.config.n_components.to_string(),
        );
        params.insert(
            "covariance_type".to_string(),
            self.config.covariance_type.to_string(),
        );
        params.insert("max_iters".to_string(), self.config.max_iters.to_string());
        params.insert("tolerance".to_string(), self.config.tolerance.to_string());
        params.insert("reg_covar".to_string(), self.config.reg_covar.to_string());
        params.insert(
            "init_method".to_string(),
            format!("{:?}", self.config.init_method),
        );
        if let Some(seed) = self.config.random_state {
            params.insert("random_state".to_string(), seed.to_string());
        }
        params
    }

    fn set_params(&mut self, params: HashMap<String, String>) -> ClusterResult<()> {
        for (key, value) in params {
            match key.as_str() {
                "n_components" => {
                    self.config.n_components = value.parse().map_err(|_| {
                        ClusterError::ConfigError(format!("Invalid n_components: {}", value))
                    })?;
                }
                "max_iters" => {
                    self.config.max_iters = value.parse().map_err(|_| {
                        ClusterError::ConfigError(format!("Invalid max_iters: {}", value))
                    })?;
                }
                "tolerance" => {
                    self.config.tolerance = value.parse().map_err(|_| {
                        ClusterError::ConfigError(format!("Invalid tolerance: {}", value))
                    })?;
                }
                _ => {
                    return Err(ClusterError::ConfigError(format!(
                        "Unknown parameter: {}",
                        key
                    )));
                }
            }
        }
        self.config.validate()?;
        Ok(())
    }

    fn is_fitted(&self) -> bool {
        false // For now, always return false since we don't store fitted state
    }

    fn complexity_info(&self) -> AlgorithmComplexity {
        AlgorithmComplexity {
            time_complexity: "O(n * k * d² * iter)".to_string(),
            space_complexity: "O(n * k + k * d²)".to_string(),
            deterministic: false,
            online_capable: false,
            memory_pattern: MemoryPattern::Quadratic,
        }
    }

    fn supported_distance_metrics(&self) -> Vec<&str> {
        vec!["mahalanobis", "euclidean"] // Implicit in the probabilistic framework
    }
}

impl Fit for GaussianMixture {
    type Result = GMResult;

    fn fit(&self, data: &Tensor) -> ClusterResult<Self::Result> {
        self.validate_input(data)?;
        validate_n_clusters(self.config.n_components, data.shape().dims()[0])?;
        validate_cluster_input(data)?;
        self.config.validate()?;

        let _n_samples = data.shape().dims()[0];
        let _n_features = data.shape().dims()[1];

        // Convert tensor to ndarray for easier computation
        let data_array = tensor_to_array2(data)?;

        // Fit the model using EM algorithm
        let result = self.fit_em(&data_array)?;

        Ok(result)
    }
}

impl FitPredict for GaussianMixture {
    type Result = GMResult;

    fn fit_predict(&self, data: &Tensor) -> ClusterResult<Self::Result> {
        self.fit(data)
    }
}

impl ProbabilisticClustering for GaussianMixture {
    fn membership_probabilities(&self, _data: &Tensor) -> ClusterResult<Tensor> {
        Err(ClusterError::NotImplemented(
            "GMM membership_probabilities not yet implemented - requires fitted model state"
                .to_string(),
        ))
    }

    fn cluster_parameters(&self) -> ClusterResult<Vec<HashMap<String, Tensor>>> {
        Err(ClusterError::NotImplemented(
            "GMM cluster_parameters not yet implemented - requires fitted model state".to_string(),
        ))
    }

    fn log_likelihood(&self, _data: &Tensor) -> ClusterResult<f64> {
        Err(ClusterError::NotImplemented(
            "GMM log_likelihood not yet implemented - requires fitted model state".to_string(),
        ))
    }

    fn sample(&self, _n_samples: usize) -> ClusterResult<Tensor> {
        Err(ClusterError::NotImplemented(
            "GMM sample not yet implemented - requires fitted model state".to_string(),
        ))
    }
}

// Core EM algorithm implementation
impl GaussianMixture {
    /// Fit the model using the EM algorithm
    fn fit_em(&self, data: &Array2<f64>) -> ClusterResult<GMResult> {
        let (n_samples, n_features) = data.dim();
        let n_components = self.config.n_components;

        // Initialize random number generator
        let mut rng = scirs2_core::random::thread_rng();

        // Initialize parameters
        let (mut means, mut covariances, mut weights) =
            self.initialize_parameters(data, &mut rng)?;

        let mut responsibilities = Array2::<f64>::zeros((n_samples, n_components));
        let mut log_likelihood = f64::NEG_INFINITY;
        let mut converged = false;

        // EM iterations
        for iter in 0..self.config.max_iters {
            // E-step: compute responsibilities
            let new_log_likelihood =
                self.e_step(data, &means, &covariances, &weights, &mut responsibilities)?;

            // Check convergence
            if iter > 0 && (new_log_likelihood - log_likelihood).abs() < self.config.tolerance {
                converged = true;
                log_likelihood = new_log_likelihood;
                break;
            }
            log_likelihood = new_log_likelihood;

            // M-step: update parameters
            self.m_step(
                data,
                &responsibilities,
                &mut means,
                &mut covariances,
                &mut weights,
            )?;
        }

        // Compute hard assignments
        let labels = self.compute_labels(&responsibilities)?;

        // Compute information criteria
        let n_params = self.count_parameters(n_features);
        let aic = -2.0 * log_likelihood + 2.0 * n_params as f64;
        let bic = -2.0 * log_likelihood + (n_params as f64) * (n_samples as f64).ln();

        Ok(GMResult {
            labels: array1_i32_to_tensor(&labels)?,
            means: array2_to_tensor(&means)?,
            covariances: self.format_covariances(&covariances)?,
            weights: array1_to_tensor(&weights)?,
            responsibilities: array2_to_tensor(&responsibilities)?,
            log_likelihood,
            n_iter: if converged {
                self.config.max_iters.min(100) // Estimate iter count
            } else {
                self.config.max_iters
            },
            converged,
            aic,
            bic,
        })
    }

    /// Initialize GMM parameters based on initialization method
    fn initialize_parameters<R: scirs2_core::random::Rng + ?Sized>(
        &self,
        data: &Array2<f64>,
        rng: &mut R,
    ) -> ClusterResult<(Array2<f64>, Array3<f64>, Array1<f64>)> {
        let (n_samples, n_features) = data.dim();
        let n_components = self.config.n_components;

        // Initialize means by randomly selecting data points
        let mut means = Array2::<f64>::zeros((n_components, n_features));
        for k in 0..n_components {
            let idx = rng.random_range(0..n_samples);
            means.row_mut(k).assign(&data.row(idx));
        }

        // Initialize covariances as identity matrices scaled by data variance
        let mut covariances = Array3::<f64>::zeros((n_components, n_features, n_features));
        let data_var = self.compute_data_variance(data);

        for k in 0..n_components {
            for i in 0..n_features {
                covariances[[k, i, i]] = data_var + self.config.reg_covar;
            }
        }

        // Initialize weights uniformly
        let weights = Array1::<f64>::from_elem(n_components, 1.0 / n_components as f64);

        Ok((means, covariances, weights))
    }

    /// E-step: compute responsibilities (posterior probabilities)
    /// Uses parallel processing for large datasets (diagonal and spherical covariance only)
    fn e_step(
        &self,
        data: &Array2<f64>,
        means: &Array2<f64>,
        covariances: &Array3<f64>,
        weights: &Array1<f64>,
        responsibilities: &mut Array2<f64>,
    ) -> ClusterResult<f64> {
        let (n_samples, _) = data.dim();

        // Use parallel version for larger datasets
        // Note: Only for diagonal/spherical covariance due to complexity of full covariance
        let can_parallelize = match self.config.covariance_type {
            CovarianceType::Diag | CovarianceType::Spherical => true,
            CovarianceType::Full => false, // Full covariance needs proper matrix inverse
        };

        if n_samples >= 500 && is_parallel_enabled() && can_parallelize {
            return self.e_step_parallel(data, means, covariances, weights, responsibilities);
        }

        // Sequential version for smaller datasets
        let n_components = means.nrows();
        let mut log_likelihood = 0.0;

        for i in 0..n_samples {
            let x = data.row(i);
            let mut weighted_log_probs = Vec::with_capacity(n_components);
            let mut max_log_prob = f64::NEG_INFINITY;

            // Compute weighted log probabilities for each component
            for k in 0..n_components {
                let mean_k = means.row(k);
                let log_prob = self.log_multivariate_normal_pdf(
                    &x,
                    &mean_k,
                    &covariances.slice(s![k, .., ..]),
                )? + weights[k].ln();
                weighted_log_probs.push(log_prob);
                max_log_prob = max_log_prob.max(log_prob);
            }

            // Compute responsibilities using log-sum-exp trick for numerical stability
            let mut sum_exp = 0.0;
            for &log_prob in &weighted_log_probs {
                sum_exp += (log_prob - max_log_prob).exp();
            }
            let log_sum = max_log_prob + sum_exp.ln();

            for k in 0..n_components {
                responsibilities[[i, k]] = (weighted_log_probs[k] - log_sum).exp();
            }

            log_likelihood += log_sum;
        }

        Ok(log_likelihood)
    }

    /// Parallel E-step for large datasets using SciRS2 parallel_ops
    fn e_step_parallel(
        &self,
        data: &Array2<f64>,
        means: &Array2<f64>,
        covariances: &Array3<f64>,
        weights: &Array1<f64>,
        responsibilities: &mut Array2<f64>,
    ) -> ClusterResult<f64> {
        let (n_samples, _) = data.dim();
        let n_components = means.nrows();

        // Collect data rows for parallel processing
        let sample_indices: Vec<usize> = (0..n_samples).collect();

        // Clone data for parallel access (necessary for thread safety)
        let data_clone = data.clone();
        let means_clone = means.clone();
        let covariances_clone = covariances.clone();
        let weights_clone = weights.clone();
        let covariance_type = self.config.covariance_type;

        // Parallel computation of responsibilities and log-likelihoods
        let results: Vec<(Vec<f64>, f64)> = parallel_map(&sample_indices, |&i| {
            let x = data_clone.row(i);
            let mut weighted_log_probs = Vec::with_capacity(n_components);
            let mut max_log_prob = f64::NEG_INFINITY;

            // Compute weighted log probabilities for each component
            for k in 0..n_components {
                let mean_k = means_clone.row(k);
                let cov_slice = covariances_clone.slice(s![k, .., ..]);

                // Compute log probability based on covariance type
                let log_prob = match covariance_type {
                    CovarianceType::Diag | CovarianceType::Spherical => {
                        Self::log_multivariate_normal_pdf_diagonal_static(&x, &mean_k, &cov_slice)
                    }
                    CovarianceType::Full => {
                        Self::log_multivariate_normal_pdf_full_static(&x, &mean_k, &cov_slice)
                    }
                };

                let weighted_log_prob = log_prob + weights_clone[k].ln();
                weighted_log_probs.push(weighted_log_prob);
                max_log_prob = max_log_prob.max(weighted_log_prob);
            }

            // Compute responsibilities using log-sum-exp trick
            let mut sum_exp = 0.0;
            for &log_prob in &weighted_log_probs {
                sum_exp += (log_prob - max_log_prob).exp();
            }
            let log_sum = max_log_prob + sum_exp.ln();

            let row_responsibilities: Vec<f64> = weighted_log_probs
                .iter()
                .map(|&log_prob| (log_prob - log_sum).exp())
                .collect();

            (row_responsibilities, log_sum)
        });

        // Aggregate results
        let mut log_likelihood = 0.0;
        for (i, (row_resp, log_sum)) in results.into_iter().enumerate() {
            for (k, &resp) in row_resp.iter().enumerate() {
                responsibilities[[i, k]] = resp;
            }
            log_likelihood += log_sum;
        }

        Ok(log_likelihood)
    }

    /// Static version of diagonal log PDF for parallel processing
    fn log_multivariate_normal_pdf_diagonal_static(
        x: &ArrayView1<f64>,
        mean: &ArrayView1<f64>,
        cov: &ArrayView2<f64>,
    ) -> f64 {
        let n_features = x.len();
        let diff = x - mean;

        let mut log_det = 0.0;
        let mut quad_form = 0.0;

        for i in 0..n_features {
            let var = cov[[i, i]];
            if var <= 0.0 {
                return f64::NEG_INFINITY; // Handle singular matrix gracefully
            }
            log_det += var.ln();
            quad_form += diff[i] * diff[i] / var;
        }

        -0.5 * (n_features as f64 * (2.0 * PI).ln() + log_det + quad_form)
    }

    /// Static version of full covariance log PDF for parallel processing
    fn log_multivariate_normal_pdf_full_static(
        x: &ArrayView1<f64>,
        mean: &ArrayView1<f64>,
        cov: &ArrayView2<f64>,
    ) -> f64 {
        let n_features = x.len();
        let diff = x - mean;

        // Simplified computation without autograd (for parallel processing)
        // Use Cholesky decomposition approach for numerical stability
        let mut det_val = 1.0;
        let mut inv_cov = Array2::<f64>::zeros((n_features, n_features));

        // Simplified matrix inverse for small dimensions (numerical stability may be lower)
        // For production, consider using proper linear algebra library
        for i in 0..n_features {
            for j in 0..n_features {
                inv_cov[[i, j]] = if i == j {
                    1.0 / cov[[i, i]].max(1e-10)
                } else {
                    0.0
                };
            }
            det_val *= cov[[i, i]].max(1e-10);
        }

        let log_det = det_val.ln();

        // Compute quadratic form
        let mut quad_form = 0.0;
        for i in 0..n_features {
            for j in 0..n_features {
                quad_form += diff[i] * inv_cov[[i, j]] * diff[j];
            }
        }

        -0.5 * (n_features as f64 * (2.0 * PI).ln() + log_det + quad_form)
    }

    /// M-step: update parameters based on responsibilities
    fn m_step(
        &self,
        data: &Array2<f64>,
        responsibilities: &Array2<f64>,
        means: &mut Array2<f64>,
        covariances: &mut Array3<f64>,
        weights: &mut Array1<f64>,
    ) -> ClusterResult<()> {
        let (n_samples, n_features) = data.dim();
        let n_components = means.nrows();

        // Update weights and means
        for k in 0..n_components {
            let nk = responsibilities.column(k).sum();
            weights[k] = nk / n_samples as f64;

            if nk > 1e-6 {
                // Avoid division by zero
                // Update mean
                for j in 0..n_features {
                    let mut weighted_sum = 0.0;
                    for i in 0..n_samples {
                        weighted_sum += responsibilities[[i, k]] * data[[i, j]];
                    }
                    means[[k, j]] = weighted_sum / nk;
                }

                // Update covariance based on covariance type
                self.update_covariance(k, data, responsibilities, &means.row(k), covariances, nk)?;
            }
        }

        Ok(())
    }

    /// Update covariance matrix for a specific component based on covariance type
    fn update_covariance(
        &self,
        component: usize,
        data: &Array2<f64>,
        responsibilities: &Array2<f64>,
        mean: &ArrayView1<f64>,
        covariances: &mut Array3<f64>,
        nk: f64,
    ) -> ClusterResult<()> {
        let (n_samples, n_features) = data.dim();

        match self.config.covariance_type {
            CovarianceType::Diag => {
                // Diagonal covariance - only update diagonal elements
                for j in 0..n_features {
                    let mut var = 0.0;
                    for i in 0..n_samples {
                        let diff = data[[i, j]] - mean[j];
                        var += responsibilities[[i, component]] * diff * diff;
                    }
                    var = (var / nk) + self.config.reg_covar;
                    covariances[[component, j, j]] = var;

                    // Ensure off-diagonal elements are zero
                    for l in 0..n_features {
                        if l != j {
                            covariances[[component, j, l]] = 0.0;
                        }
                    }
                }
            }

            CovarianceType::Full => {
                // Full covariance matrix
                for j in 0..n_features {
                    for l in 0..n_features {
                        let mut covar = 0.0;
                        for i in 0..n_samples {
                            let diff_j = data[[i, j]] - mean[j];
                            let diff_l = data[[i, l]] - mean[l];
                            covar += responsibilities[[i, component]] * diff_j * diff_l;
                        }
                        covar /= nk;

                        // Add regularization to diagonal elements
                        if j == l {
                            covar += self.config.reg_covar;
                        }

                        covariances[[component, j, l]] = covar;
                    }
                }
            }

            CovarianceType::Spherical => {
                // Spherical covariance - single variance for all dimensions
                let mut total_var = 0.0;
                for j in 0..n_features {
                    for i in 0..n_samples {
                        let diff = data[[i, j]] - mean[j];
                        total_var += responsibilities[[i, component]] * diff * diff;
                    }
                }
                let spherical_var = (total_var / (nk * n_features as f64)) + self.config.reg_covar;

                // Set diagonal to spherical variance, off-diagonal to zero
                for j in 0..n_features {
                    for l in 0..n_features {
                        if j == l {
                            covariances[[component, j, l]] = spherical_var;
                        } else {
                            covariances[[component, j, l]] = 0.0;
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Compute log probability density function for multivariate normal distribution
    /// Using SciRS2's linear algebra operations for proper matrix operations
    fn log_multivariate_normal_pdf(
        &self,
        x: &ArrayView1<f64>,
        mean: &ArrayView1<f64>,
        cov: &ArrayView2<f64>,
    ) -> ClusterResult<f64> {
        let _n_features = x.len();

        match self.config.covariance_type {
            CovarianceType::Diag | CovarianceType::Spherical => {
                // Optimized diagonal case
                self.log_multivariate_normal_pdf_diagonal(x, mean, cov)
            }
            CovarianceType::Full => {
                // Full covariance matrix using SciRS2
                self.log_multivariate_normal_pdf_full(x, mean, cov)
            }
        }
    }

    /// Diagonal covariance case (optimized)
    fn log_multivariate_normal_pdf_diagonal(
        &self,
        x: &ArrayView1<f64>,
        mean: &ArrayView1<f64>,
        cov: &ArrayView2<f64>,
    ) -> ClusterResult<f64> {
        let n_features = x.len();
        let diff = x - mean;

        let mut log_det = 0.0;
        let mut quad_form = 0.0;

        for i in 0..n_features {
            let var = cov[[i, i]];
            if var <= 0.0 {
                return Err(ClusterError::SingularMatrix);
            }
            log_det += var.ln();
            quad_form += diff[i] * diff[i] / var;
        }

        let log_pdf = -0.5 * (n_features as f64 * (2.0 * PI).ln() + log_det + quad_form);
        Ok(log_pdf)
    }

    /// Full covariance matrix case using SciRS2
    fn log_multivariate_normal_pdf_full(
        &self,
        x: &ArrayView1<f64>,
        mean: &ArrayView1<f64>,
        cov: &ArrayView2<f64>,
    ) -> ClusterResult<f64> {
        let n_features = x.len();
        let diff = x - mean;

        // Use SciRS2 for matrix determinant and inverse operations
        let result = ag::run(|g| -> Result<(f64, f64), String> {
            // Convert to f32 arrays for SciRS2
            let cov_f32 = cov.mapv(|x| x as f32);
            let diff_f32 = diff.mapv(|x| x as f32).insert_axis(Axis(1)); // Make column vector

            // Convert to SciRS2 tensors
            let cov_tensor = convert_to_tensor(cov_f32, g);
            let _diff_tensor = convert_to_tensor(diff_f32, g);

            // Compute determinant for log-likelihood
            let det_cov = det(&cov_tensor);
            let det_val = det_cov
                .eval(g)
                .map_err(|e| format!("Determinant computation failed: {:?}", e))?;

            let det_scalar = if det_val.ndim() == 0 {
                det_val[[]] as f64
            } else {
                det_val[scirs2_core::ndarray::IxDyn(&[0])] as f64
            };

            if det_scalar <= 0.0 {
                return Err("Singular or non-positive-definite covariance matrix".to_string());
            }

            // Compute matrix inverse
            let inv_cov = matinv(&cov_tensor);
            let inv_cov_val = inv_cov
                .eval(g)
                .map_err(|e| format!("Matrix inverse computation failed: {:?}", e))?;

            // Convert back to ndarray for quadratic form computation
            let inv_cov_f64 = inv_cov_val.mapv(|x| x as f64);

            // Compute quadratic form: diff^T * inv(cov) * diff
            let mut quad_form = 0.0;
            for i in 0..n_features {
                for j in 0..n_features {
                    quad_form += diff[i] * inv_cov_f64[[i, j]] * diff[j];
                }
            }

            Ok((det_scalar.ln(), quad_form))
        });

        let (log_det, quad_form) = result.map_err(|e| {
            ClusterError::SciRS2Error(format!("Multivariate normal PDF computation failed: {}", e))
        })?;

        let log_pdf = -0.5 * (n_features as f64 * (2.0 * PI).ln() + log_det + quad_form);
        Ok(log_pdf)
    }

    /// Compute hard cluster assignments from responsibilities
    fn compute_labels(&self, responsibilities: &Array2<f64>) -> ClusterResult<Array1<i32>> {
        let (n_samples, _) = responsibilities.dim();
        let mut labels = Array1::<i32>::zeros(n_samples);

        for i in 0..n_samples {
            let mut max_resp = 0.0;
            let mut best_label = 0;

            for k in 0..responsibilities.ncols() {
                if responsibilities[[i, k]] > max_resp {
                    max_resp = responsibilities[[i, k]];
                    best_label = k;
                }
            }

            labels[i] = best_label as i32;
        }

        Ok(labels)
    }

    /// Compute data variance for initialization
    fn compute_data_variance(&self, data: &Array2<f64>) -> f64 {
        let (n_samples, n_features) = data.dim();
        let mut total_var = 0.0;

        for j in 0..n_features {
            let column = data.column(j);
            let mean = column.sum() / n_samples as f64;
            let var = column.mapv(|x| (x - mean).powi(2)).sum() / n_samples as f64;
            total_var += var;
        }

        total_var / n_features as f64
    }

    /// Count number of parameters for information criteria
    fn count_parameters(&self, n_features: usize) -> usize {
        let n_components = self.config.n_components;
        let mean_params = n_components * n_features;
        let weight_params = n_components - 1; // Sum to 1 constraint

        let cov_params = match self.config.covariance_type {
            CovarianceType::Full => n_components * n_features * (n_features + 1) / 2,
            CovarianceType::Diag => n_components * n_features,
            CovarianceType::Spherical => n_components,
        };

        mean_params + weight_params + cov_params
    }

    /// Format covariances tensor based on covariance type
    fn format_covariances(&self, covariances: &Array3<f64>) -> ClusterResult<Tensor> {
        match self.config.covariance_type {
            CovarianceType::Full | CovarianceType::Diag => {
                // Return as 3D tensor
                array3_to_tensor(covariances)
            }
            CovarianceType::Spherical => {
                // Extract diagonal elements only
                let (n_components, n_features, _) = covariances.dim();
                let mut spherical_vars = Array2::<f64>::zeros((n_components, n_features));

                for k in 0..n_components {
                    for i in 0..n_features {
                        spherical_vars[[k, i]] = covariances[[k, i, i]];
                    }
                }

                array2_to_tensor(&spherical_vars)
            }
        }
    }
}

// Utility functions for tensor/array conversions
fn tensor_to_array2(tensor: &Tensor) -> ClusterResult<Array2<f64>> {
    let tensor_shape = tensor.shape();
    let shape = tensor_shape.dims();
    if shape.len() != 2 {
        return Err(ClusterError::InvalidInput("Expected 2D tensor".to_string()));
    }

    let data_f32: Vec<f32> = tensor.to_vec().map_err(ClusterError::TensorError)?;
    let data: Vec<f64> = data_f32.into_iter().map(|x| x as f64).collect();
    Array2::from_shape_vec((shape[0], shape[1]), data)
        .map_err(|_| ClusterError::InvalidInput("Failed to convert tensor to array".to_string()))
}

fn array2_to_tensor(array: &Array2<f64>) -> ClusterResult<Tensor> {
    let (rows, cols) = array.dim();
    let data_f64: Vec<f64> = array.iter().copied().collect();
    let data: Vec<f32> = data_f64.into_iter().map(|x| x as f32).collect();
    Tensor::from_vec(data, &[rows, cols]).map_err(ClusterError::TensorError)
}

fn array1_to_tensor(array: &Array1<f64>) -> ClusterResult<Tensor> {
    let len = array.len();
    let data_f64: Vec<f64> = array.iter().copied().collect();
    let data: Vec<f32> = data_f64.into_iter().map(|x| x as f32).collect();
    Tensor::from_vec(data, &[len]).map_err(ClusterError::TensorError)
}

fn array1_i32_to_tensor(array: &Array1<i32>) -> ClusterResult<Tensor> {
    let len = array.len();
    let data_i32: Vec<i32> = array.iter().copied().collect();
    let data: Vec<f32> = data_i32.into_iter().map(|x| x as f32).collect();
    Tensor::from_vec(data, &[len]).map_err(ClusterError::TensorError)
}

fn array3_to_tensor(array: &Array3<f64>) -> ClusterResult<Tensor> {
    let (d1, d2, d3) = array.dim();
    let data_f64: Vec<f64> = array.iter().copied().collect();
    let data: Vec<f32> = data_f64.into_iter().map(|x| x as f32).collect();
    Tensor::from_vec(data, &[d1, d2, d3]).map_err(ClusterError::TensorError)
}