sklears-semi-supervised 0.2.0

Semi-supervised learning algorithms
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
//! Mixture Discriminant Analysis for semi-supervised learning
//!
//! This module provides Mixture Discriminant Analysis (MDA), a probabilistic
//! semi-supervised learning method that extends discriminant analysis to
//! handle both labeled and unlabeled data through mixture modeling.

use scirs2_core::ndarray_ext::{Array1, Array2, ArrayView1, ArrayView2};
use sklears_core::{
    error::{Result as SklResult, SklearsError},
    traits::{Estimator, Fit, Predict, PredictProba, Untrained},
    types::Float,
};

/// Mixture Discriminant Analysis
///
/// MDA is a semi-supervised extension of Linear/Quadratic Discriminant Analysis
/// that uses both labeled and unlabeled data to learn class-conditional densities.
/// It models each class as a mixture of Gaussians and uses EM algorithm to
/// estimate parameters from both labeled and unlabeled samples.
///
/// # Parameters
///
/// * `n_components` - Number of mixture components per class
/// * `covariance_type` - Type of covariance matrix ('full', 'tied', 'diag', 'spherical')
/// * `reg_covar` - Regularization added to diagonal of covariance
/// * `max_iter` - Maximum number of EM iterations
/// * `tol` - Convergence tolerance
/// * `n_init` - Number of initializations
/// * `random_state` - Random state for reproducible results
///
/// # Examples
///
/// ```
/// use scirs2_core::array;
/// use sklears_semi_supervised::MixtureDiscriminantAnalysis;
/// use sklears_core::traits::{Predict, Fit};
///
///
/// let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
/// let y = array![0, 1, -1, -1]; // -1 indicates unlabeled
///
/// let mda = MixtureDiscriminantAnalysis::new()
///     .n_components(2)
///     .covariance_type("full".to_string())
///     .max_iter(100);
/// let fitted = mda.fit(&X.view(), &y.view()).unwrap();
/// let predictions = fitted.predict(&X.view()).unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct MixtureDiscriminantAnalysis<S = Untrained> {
    state: S,
    n_components: usize,
    covariance_type: String,
    reg_covar: f64,
    max_iter: usize,
    tol: f64,
    n_init: usize,
    random_state: Option<u64>,
}

impl MixtureDiscriminantAnalysis<Untrained> {
    /// Create a new MixtureDiscriminantAnalysis instance
    pub fn new() -> Self {
        Self {
            state: Untrained,
            n_components: 1,
            covariance_type: "full".to_string(),
            reg_covar: 1e-6,
            max_iter: 100,
            tol: 1e-3,
            n_init: 1,
            random_state: None,
        }
    }

    /// Set the number of mixture components per class
    pub fn n_components(mut self, n_components: usize) -> Self {
        self.n_components = n_components;
        self
    }

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

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

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

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

    /// Set the number of initializations
    pub fn n_init(mut self, n_init: usize) -> Self {
        self.n_init = n_init;
        self
    }

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

    /// Initialize mixture parameters
    #[allow(clippy::type_complexity)]
    #[allow(non_snake_case)] // standard ML notation
    fn initialize_parameters(
        &self,
        X: &Array2<f64>,
        labeled_indices: &[usize],
        y: &Array1<i32>,
        classes: &[i32],
    ) -> SklResult<(
        Vec<Vec<Array1<f64>>>,
        Vec<Vec<Array2<f64>>>,
        Vec<Array1<f64>>,
        Array1<f64>,
    )> {
        let n_features = X.ncols();
        let n_classes = classes.len();

        // Initialize means, covariances, component weights, and class priors
        let mut means = Vec::new();
        let mut covariances = Vec::new();
        let mut component_weights = Vec::new();
        let mut class_priors = Array1::zeros(n_classes);

        for (class_idx, &class_label) in classes.iter().enumerate() {
            // Find labeled samples for this class
            let class_samples: Vec<usize> = labeled_indices
                .iter()
                .filter(|&&i| y[i] == class_label)
                .copied()
                .collect();

            if class_samples.is_empty() {
                return Err(SklearsError::InvalidInput(format!(
                    "No labeled samples for class {}",
                    class_label
                )));
            }

            // Initialize means for this class
            let mut class_means = Vec::new();
            let mut class_covariances = Vec::new();

            // Simple initialization: use labeled samples as initial means
            for comp_idx in 0..self.n_components {
                let sample_idx = class_samples[comp_idx % class_samples.len()];
                let mean = X.row(sample_idx).to_owned();
                class_means.push(mean);

                // Initialize covariance based on type
                let cov = match self.covariance_type.as_str() {
                    "full" => {
                        let mut cov = Array2::eye(n_features) * self.reg_covar;
                        // Add small random perturbation
                        for i in 0..n_features {
                            for j in 0..n_features {
                                if i == j {
                                    cov[[i, j]] += 1.0;
                                }
                            }
                        }
                        cov
                    }
                    "diag" => Array2::eye(n_features),
                    "spherical" => Array2::eye(n_features),
                    "tied" => Array2::eye(n_features),
                    _ => {
                        return Err(SklearsError::InvalidInput(format!(
                            "Unknown covariance type: {}",
                            self.covariance_type
                        )));
                    }
                };
                class_covariances.push(cov);
            }

            means.push(class_means);
            covariances.push(class_covariances);

            // Initialize component weights (uniform)
            let weights = Array1::from_elem(self.n_components, 1.0 / self.n_components as f64);
            component_weights.push(weights);

            // Set class prior based on labeled samples
            class_priors[class_idx] = class_samples.len() as f64 / labeled_indices.len() as f64;
        }

        Ok((means, covariances, component_weights, class_priors))
    }

    /// Compute multivariate Gaussian PDF
    fn multivariate_gaussian_pdf(
        &self,
        x: &ArrayView1<f64>,
        mean: &Array1<f64>,
        cov: &Array2<f64>,
    ) -> f64 {
        let n_features = x.len();
        let diff = x - mean;

        // Compute determinant and inverse (simplified)
        let det = match self.covariance_type.as_str() {
            "spherical" => cov[[0, 0]].powf(n_features as f64),
            "diag" => cov.diag().iter().product(),
            _ => {
                // Simplified determinant calculation
                let mut det = 1.0;
                for i in 0..n_features {
                    det *= cov[[i, i]];
                }
                det
            }
        };

        if det <= 0.0 {
            return 1e-10; // Avoid numerical issues
        }

        // Simplified Mahalanobis distance calculation
        let mut mahal_dist = 0.0;
        match self.covariance_type.as_str() {
            "spherical" => {
                let var = cov[[0, 0]];
                mahal_dist = diff.mapv(|x| x * x).sum() / var;
            }
            "diag" => {
                for i in 0..n_features {
                    mahal_dist += diff[i] * diff[i] / cov[[i, i]];
                }
            }
            _ => {
                // Simplified full covariance
                for i in 0..n_features {
                    mahal_dist += diff[i] * diff[i] / cov[[i, i]];
                }
            }
        }

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

    /// E-step: Compute responsibilities
    #[allow(clippy::too_many_arguments, clippy::type_complexity)]
    #[allow(non_snake_case)] // standard ML notation
    fn e_step(
        &self,
        X: &Array2<f64>,
        means: &[Vec<Array1<f64>>],
        covariances: &[Vec<Array2<f64>>],
        component_weights: &[Array1<f64>],
        class_priors: &Array1<f64>,
        labeled_indices: &[usize],
        y: &Array1<i32>,
        classes: &[i32],
    ) -> (Array2<f64>, f64) {
        let n_samples = X.nrows();
        let n_classes = classes.len();
        let total_components = n_classes * self.n_components;

        let mut responsibilities = Array2::zeros((n_samples, total_components));
        let mut log_likelihood = 0.0;

        for i in 0..n_samples {
            let x = X.row(i);
            let mut total_prob = 0.0;
            let mut probs = Vec::new();

            // Compute probabilities for each class and component
            for (class_idx, &_class_label) in classes.iter().enumerate() {
                for comp_idx in 0..self.n_components {
                    let _comp_global_idx = class_idx * self.n_components + comp_idx;
                    let prob = class_priors[class_idx]
                        * component_weights[class_idx][comp_idx]
                        * self.multivariate_gaussian_pdf(
                            &x,
                            &means[class_idx][comp_idx],
                            &covariances[class_idx][comp_idx],
                        );
                    probs.push(prob);
                    total_prob += prob;
                }
            }

            // Normalize and assign responsibilities
            if total_prob > 0.0 {
                for (comp_idx, &prob) in probs.iter().enumerate() {
                    responsibilities[[i, comp_idx]] = prob / total_prob;
                }
                log_likelihood += total_prob.ln();
            } else {
                // Uniform assignment if no valid probability
                for comp_idx in 0..total_components {
                    responsibilities[[i, comp_idx]] = 1.0 / total_components as f64;
                }
            }

            // Hard assignment for labeled samples
            if labeled_indices.contains(&i) {
                if let Some(class_idx) = classes.iter().position(|&c| c == y[i]) {
                    // Set responsibility to 1 for true class, 0 for others
                    for comp_idx in 0..total_components {
                        responsibilities[[i, comp_idx]] = 0.0;
                    }
                    // Uniform distribution within the true class
                    for comp_idx in 0..self.n_components {
                        let global_comp_idx = class_idx * self.n_components + comp_idx;
                        responsibilities[[i, global_comp_idx]] = 1.0 / self.n_components as f64;
                    }
                }
            }
        }

        (responsibilities, log_likelihood)
    }

    /// M-step: Update parameters
    #[allow(clippy::type_complexity)]
    #[allow(non_snake_case)] // standard ML notation
    fn m_step(
        &self,
        X: &Array2<f64>,
        responsibilities: &Array2<f64>,
        classes: &[i32],
    ) -> (
        Vec<Vec<Array1<f64>>>,
        Vec<Vec<Array2<f64>>>,
        Vec<Array1<f64>>,
        Array1<f64>,
    ) {
        let n_samples = X.nrows();
        let n_features = X.ncols();
        let n_classes = classes.len();

        let mut means = Vec::new();
        let mut covariances = Vec::new();
        let mut component_weights = Vec::new();
        let mut class_priors = Array1::zeros(n_classes);

        for class_idx in 0..n_classes {
            let mut class_means = Vec::new();
            let mut class_covariances = Vec::new();
            let mut class_component_weights = Array1::zeros(self.n_components);

            let mut class_total_responsibility = 0.0;

            for comp_idx in 0..self.n_components {
                let global_comp_idx = class_idx * self.n_components + comp_idx;
                let comp_responsibilities = responsibilities.column(global_comp_idx);
                let comp_total_resp: f64 = comp_responsibilities.sum();

                class_total_responsibility += comp_total_resp;

                if comp_total_resp > 1e-10 {
                    // Update mean
                    let mut new_mean = Array1::zeros(n_features);
                    for i in 0..n_samples {
                        for j in 0..n_features {
                            new_mean[j] += comp_responsibilities[i] * X[[i, j]];
                        }
                    }
                    new_mean /= comp_total_resp;

                    // Update covariance
                    let mut new_cov = Array2::zeros((n_features, n_features));
                    for i in 0..n_samples {
                        let diff = &X.row(i) - &new_mean;
                        let weight = comp_responsibilities[i];
                        for j in 0..n_features {
                            for k in 0..n_features {
                                new_cov[[j, k]] += weight * diff[j] * diff[k];
                            }
                        }
                    }
                    new_cov /= comp_total_resp;

                    // Add regularization
                    for i in 0..n_features {
                        new_cov[[i, i]] += self.reg_covar;
                    }

                    class_means.push(new_mean);
                    class_covariances.push(new_cov);
                    class_component_weights[comp_idx] = comp_total_resp;
                } else {
                    // Fallback for empty components
                    class_means.push(Array1::zeros(n_features));
                    class_covariances.push(Array2::eye(n_features) * self.reg_covar);
                    class_component_weights[comp_idx] = 1e-10;
                }
            }

            // Normalize component weights
            let total_weight = class_component_weights.sum();
            if total_weight > 0.0 {
                class_component_weights /= total_weight;
            } else {
                class_component_weights.fill(1.0 / self.n_components as f64);
            }

            means.push(class_means);
            covariances.push(class_covariances);
            component_weights.push(class_component_weights);

            // Update class prior
            class_priors[class_idx] = class_total_responsibility / n_samples as f64;
        }

        // Normalize class priors
        let total_prior = class_priors.sum();
        if total_prior > 0.0 {
            class_priors /= total_prior;
        } else {
            class_priors.fill(1.0 / n_classes as f64);
        }

        (means, covariances, component_weights, class_priors)
    }
}

impl Default for MixtureDiscriminantAnalysis<Untrained> {
    fn default() -> Self {
        Self::new()
    }
}

impl Estimator for MixtureDiscriminantAnalysis<Untrained> {
    type Config = ();
    type Error = SklearsError;
    type Float = Float;

    fn config(&self) -> &Self::Config {
        &()
    }
}

/// Trained state for MixtureDiscriminantAnalysis
#[derive(Debug, Clone)]
pub struct MixtureDiscriminantAnalysisTrained {
    /// means
    pub means: Vec<Vec<Array1<f64>>>,
    /// covariances
    pub covariances: Vec<Vec<Array2<f64>>>,
    /// component_weights
    pub component_weights: Vec<Array1<f64>>,
    /// class_priors
    pub class_priors: Array1<f64>,
    /// classes
    pub classes: Array1<i32>,
    /// n_components
    pub n_components: usize,
    /// covariance_type
    pub covariance_type: String,
}

impl Fit<ArrayView2<'_, Float>, ArrayView1<'_, i32>> for MixtureDiscriminantAnalysis<Untrained> {
    type Fitted = MixtureDiscriminantAnalysis<MixtureDiscriminantAnalysisTrained>;

    #[allow(non_snake_case)]
    fn fit(self, X: &ArrayView2<'_, Float>, y: &ArrayView1<'_, i32>) -> SklResult<Self::Fitted> {
        let X = X.to_owned();
        let y = y.to_owned();

        // Identify labeled samples and classes
        let mut labeled_indices = Vec::new();
        let mut classes = std::collections::HashSet::new();

        for (i, &label) in y.iter().enumerate() {
            if label != -1 {
                labeled_indices.push(i);
                classes.insert(label);
            }
        }

        if labeled_indices.is_empty() {
            return Err(SklearsError::InvalidInput(
                "No labeled samples provided".to_string(),
            ));
        }

        let classes: Vec<i32> = classes.into_iter().collect();

        // Initialize parameters
        let (mut means, mut covariances, mut component_weights, mut class_priors) =
            self.initialize_parameters(&X, &labeled_indices, &y, &classes)?;

        let mut prev_log_likelihood = f64::NEG_INFINITY;

        // EM algorithm
        for iteration in 0..self.max_iter {
            // E-step
            let (responsibilities, log_likelihood) = self.e_step(
                &X,
                &means,
                &covariances,
                &component_weights,
                &class_priors,
                &labeled_indices,
                &y,
                &classes,
            );

            // M-step
            let (new_means, new_covariances, new_component_weights, new_class_priors) =
                self.m_step(&X, &responsibilities, &classes);

            means = new_means;
            covariances = new_covariances;
            component_weights = new_component_weights;
            class_priors = new_class_priors;

            // Check convergence
            if iteration > 0 && (log_likelihood - prev_log_likelihood).abs() < self.tol {
                break;
            }

            prev_log_likelihood = log_likelihood;
        }

        Ok(MixtureDiscriminantAnalysis {
            state: MixtureDiscriminantAnalysisTrained {
                means,
                covariances,
                component_weights,
                class_priors,
                classes: Array1::from(classes),
                n_components: self.n_components,
                covariance_type: self.covariance_type.clone(),
            },
            n_components: self.n_components,
            covariance_type: self.covariance_type,
            reg_covar: self.reg_covar,
            max_iter: self.max_iter,
            tol: self.tol,
            n_init: self.n_init,
            random_state: self.random_state,
        })
    }
}

impl Predict<ArrayView2<'_, Float>, Array1<i32>>
    for MixtureDiscriminantAnalysis<MixtureDiscriminantAnalysisTrained>
{
    #[allow(non_snake_case)] // standard ML notation
    fn predict(&self, X: &ArrayView2<'_, Float>) -> SklResult<Array1<i32>> {
        let probas = self.predict_proba(X)?;
        let n_test = probas.nrows();
        let mut predictions = Array1::zeros(n_test);

        for i in 0..n_test {
            let max_idx = probas
                .row(i)
                .iter()
                .enumerate()
                .max_by(|a, b| a.1.partial_cmp(b.1).expect("operation should succeed"))
                .expect("operation should succeed")
                .0;

            predictions[i] = self.state.classes[max_idx];
        }

        Ok(predictions)
    }
}

impl PredictProba<ArrayView2<'_, Float>, Array2<f64>>
    for MixtureDiscriminantAnalysis<MixtureDiscriminantAnalysisTrained>
{
    #[allow(non_snake_case)]
    fn predict_proba(&self, X: &ArrayView2<'_, Float>) -> SklResult<Array2<f64>> {
        let X = X.to_owned();
        let n_test = X.nrows();
        let n_classes = self.state.classes.len();
        let mut probas = Array2::zeros((n_test, n_classes));

        for i in 0..n_test {
            let x = X.row(i);
            let mut class_probs = Array1::zeros(n_classes);

            // Compute probability for each class
            for class_idx in 0..n_classes {
                let mut class_prob = 0.0;

                // Sum over all components in the class
                for comp_idx in 0..self.state.n_components {
                    let component_prob = self.state.component_weights[class_idx][comp_idx]
                        * self.multivariate_gaussian_pdf(
                            &x,
                            &self.state.means[class_idx][comp_idx],
                            &self.state.covariances[class_idx][comp_idx],
                        );
                    class_prob += component_prob;
                }

                class_probs[class_idx] = self.state.class_priors[class_idx] * class_prob;
            }

            // Normalize probabilities
            let total_prob = class_probs.sum();
            if total_prob > 0.0 {
                class_probs /= total_prob;
            } else {
                class_probs.fill(1.0 / n_classes as f64);
            }

            for j in 0..n_classes {
                probas[[i, j]] = class_probs[j];
            }
        }

        Ok(probas)
    }
}

impl MixtureDiscriminantAnalysis<MixtureDiscriminantAnalysisTrained> {
    /// Compute multivariate Gaussian PDF (same as training method)
    fn multivariate_gaussian_pdf(
        &self,
        x: &ArrayView1<f64>,
        mean: &Array1<f64>,
        cov: &Array2<f64>,
    ) -> f64 {
        let n_features = x.len();
        let diff = x - mean;

        // Compute determinant and Mahalanobis distance (simplified)
        let det = match self.state.covariance_type.as_str() {
            "spherical" => cov[[0, 0]].powf(n_features as f64),
            "diag" => cov.diag().iter().product(),
            _ => {
                let mut det = 1.0;
                for i in 0..n_features {
                    det *= cov[[i, i]];
                }
                det
            }
        };

        if det <= 0.0 {
            return 1e-10;
        }

        let mut mahal_dist = 0.0;
        match self.state.covariance_type.as_str() {
            "spherical" => {
                let var = cov[[0, 0]];
                mahal_dist = diff.mapv(|x| x * x).sum() / var;
            }
            "diag" => {
                for i in 0..n_features {
                    mahal_dist += diff[i] * diff[i] / cov[[i, i]];
                }
            }
            _ => {
                for i in 0..n_features {
                    mahal_dist += diff[i] * diff[i] / cov[[i, i]];
                }
            }
        }

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

#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::array;

    #[test]
    #[allow(non_snake_case)]
    fn test_mixture_discriminant_analysis() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
        let y = array![0, 1, -1, -1]; // -1 indicates unlabeled

        let mda = MixtureDiscriminantAnalysis::new()
            .n_components(1)
            .max_iter(10); // Reduced for testing
        let fitted = mda
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        let predictions = fitted.predict(&X.view()).expect("operation should succeed");
        assert_eq!(predictions.len(), 4);

        let probas = fitted
            .predict_proba(&X.view())
            .expect("operation should succeed");
        assert_eq!(probas.dim(), (4, 2));

        // Check that probabilities sum to 1
        for i in 0..4 {
            let sum: f64 = probas.row(i).sum();
            assert!((sum - 1.0).abs() < 1e-8);
        }
    }

    #[test]
    fn test_mda_parameters() {
        let mda = MixtureDiscriminantAnalysis::new()
            .n_components(3)
            .covariance_type("diag".to_string())
            .reg_covar(1e-5)
            .max_iter(200)
            .tol(1e-6)
            .n_init(5)
            .random_state(42);

        assert_eq!(mda.n_components, 3);
        assert_eq!(mda.covariance_type, "diag");
        assert_eq!(mda.reg_covar, 1e-5);
        assert_eq!(mda.max_iter, 200);
        assert_eq!(mda.tol, 1e-6);
        assert_eq!(mda.n_init, 5);
        assert_eq!(mda.random_state, Some(42));
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_mda_covariance_types() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
        let y = array![0, 1, -1, -1];

        for cov_type in &["full", "diag", "spherical", "tied"] {
            let mda = MixtureDiscriminantAnalysis::new()
                .covariance_type(cov_type.to_string())
                .max_iter(5);
            let fitted = mda
                .fit(&X.view(), &y.view())
                .expect("operation should succeed");

            let predictions = fitted.predict(&X.view()).expect("operation should succeed");
            assert_eq!(predictions.len(), 4);
        }
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_mda_multiple_components() {
        let X = array![
            [1.0, 1.0],
            [1.1, 1.1],
            [1.2, 1.2],
            [5.0, 5.0],
            [5.1, 5.1],
            [5.2, 5.2],
            [3.0, 3.0],
            [3.1, 3.1]
        ];
        let y = array![0, 0, -1, 1, 1, -1, -1, -1];

        let mda = MixtureDiscriminantAnalysis::new()
            .n_components(2)
            .max_iter(20);
        let fitted = mda
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        let predictions = fitted.predict(&X.view()).expect("operation should succeed");
        assert_eq!(predictions.len(), 8);

        let probas = fitted
            .predict_proba(&X.view())
            .expect("operation should succeed");
        assert_eq!(probas.dim(), (8, 2));
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_mda_error_cases() {
        let X = array![[1.0, 2.0], [2.0, 3.0]];
        let y = array![-1, -1]; // No labeled samples

        let mda = MixtureDiscriminantAnalysis::new();
        let result = mda.fit(&X.view(), &y.view());
        assert!(result.is_err());
    }

    #[test]
    fn test_mda_gaussian_pdf() {
        let mda = MixtureDiscriminantAnalysis::new().covariance_type("diag".to_string());

        let x = array![1.0, 2.0];
        let mean = array![1.0, 2.0];
        let cov = Array2::eye(2);

        let pdf = mda.multivariate_gaussian_pdf(&x.view(), &mean, &cov);
        assert!(pdf > 0.0);
        assert!(pdf <= 1.0);
    }
}