ferrolearn-bayes 0.2.1

Bayesian methods for the ferrolearn ML framework — naive Bayes classifiers and conjugate priors
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
//! Gaussian Naive Bayes classifier.
//!
//! This module provides [`GaussianNB`], a Naive Bayes classifier that assumes
//! features are normally (Gaussian) distributed within each class. Each
//! feature's likelihood is modelled by the Gaussian density
//! `N(mu_ci, sigma_ci^2)` estimated from the training data.
//!
//! # Examples
//!
//! ```
//! use ferrolearn_bayes::GaussianNB;
//! use ferrolearn_core::{Fit, Predict};
//! use ndarray::{array, Array2};
//!
//! let x = Array2::from_shape_vec(
//!     (6, 2),
//!     vec![1.0, 2.0, 1.5, 1.8, 2.0, 2.5,
//!          6.0, 7.0, 6.5, 6.8, 7.0, 7.5],
//! ).unwrap();
//! let y = array![0usize, 0, 0, 1, 1, 1];
//!
//! let model = GaussianNB::<f64>::new();
//! let fitted = model.fit(&x, &y).unwrap();
//! let preds = fitted.predict(&x).unwrap();
//! assert_eq!(preds.len(), 6);
//! ```

use ferrolearn_core::error::FerroError;
use ferrolearn_core::introspection::HasClasses;
use ferrolearn_core::pipeline::{FittedPipelineEstimator, PipelineEstimator};
use ferrolearn_core::traits::{Fit, Predict};
use ndarray::{Array1, Array2};
use num_traits::{Float, FromPrimitive, ToPrimitive};

/// Gaussian Naive Bayes classifier.
///
/// Assumes features are Gaussian-distributed within each class.
/// Variance smoothing is applied to avoid numerical issues with zero variance.
///
/// # Type Parameters
///
/// - `F`: The floating-point type (`f32` or `f64`).
#[derive(Debug, Clone)]
pub struct GaussianNB<F> {
    /// Variance smoothing parameter added to all variances.
    /// Prevents division by zero when a feature has near-zero variance.
    /// Default: `1e-9`.
    pub var_smoothing: F,
    /// Optional user-supplied class priors. If set, these are used
    /// instead of computing priors from the data.
    pub class_prior: Option<Vec<F>>,
}

impl<F: Float> GaussianNB<F> {
    /// Create a new `GaussianNB` with default settings.
    ///
    /// Default: `var_smoothing = 1e-9`, `class_prior = None`.
    #[must_use]
    pub fn new() -> Self {
        Self {
            var_smoothing: F::from(1e-9).unwrap(),
            class_prior: None,
        }
    }

    /// Set the variance smoothing parameter.
    #[must_use]
    pub fn with_var_smoothing(mut self, var_smoothing: F) -> Self {
        self.var_smoothing = var_smoothing;
        self
    }

    /// Set user-supplied class priors.
    ///
    /// The priors must sum to 1.0 and have length equal to the number
    /// of classes discovered during fitting.
    #[must_use]
    pub fn with_class_prior(mut self, priors: Vec<F>) -> Self {
        self.class_prior = Some(priors);
        self
    }
}

impl<F: Float> Default for GaussianNB<F> {
    fn default() -> Self {
        Self::new()
    }
}

/// Fitted Gaussian Naive Bayes classifier.
///
/// Stores the per-class prior, mean, and variance computed during fitting.
/// Also stores sufficient statistics for incremental (partial) fitting.
#[derive(Debug, Clone)]
pub struct FittedGaussianNB<F> {
    /// Sorted unique class labels.
    classes: Vec<usize>,
    /// Log prior probability for each class, shape `(n_classes,)`.
    log_prior: Array1<F>,
    /// Per-class per-feature mean, shape `(n_classes, n_features)`.
    theta: Array2<F>,
    /// Per-class per-feature variance (smoothed), shape `(n_classes, n_features)`.
    sigma: Array2<F>,
    /// Per-class sample counts (for partial_fit updates).
    class_counts: Vec<usize>,
    /// Per-class per-feature unsmoothed variance, shape `(n_classes, n_features)`.
    /// Stored for Welford update during partial_fit.
    raw_sigma: Array2<F>,
    /// Variance smoothing parameter carried forward for partial_fit.
    var_smoothing: F,
    /// Optional user-supplied class priors.
    class_prior: Option<Vec<F>>,
}

impl<F: Float + Send + Sync + 'static> Fit<Array2<F>, Array1<usize>> for GaussianNB<F> {
    type Fitted = FittedGaussianNB<F>;
    type Error = FerroError;

    /// Fit the Gaussian NB model.
    ///
    /// # Errors
    ///
    /// - [`FerroError::ShapeMismatch`] if `x` and `y` have different numbers of rows.
    /// - [`FerroError::InsufficientSamples`] if there are no samples.
    fn fit(&self, x: &Array2<F>, y: &Array1<usize>) -> Result<FittedGaussianNB<F>, FerroError> {
        let (n_samples, n_features) = x.dim();

        if n_samples == 0 {
            return Err(FerroError::InsufficientSamples {
                required: 1,
                actual: 0,
                context: "GaussianNB requires at least one sample".into(),
            });
        }

        if n_samples != y.len() {
            return Err(FerroError::ShapeMismatch {
                expected: vec![n_samples],
                actual: vec![y.len()],
                context: "y length must match number of samples in X".into(),
            });
        }

        // Collect sorted unique classes.
        let mut classes: Vec<usize> = y.to_vec();
        classes.sort_unstable();
        classes.dedup();
        let n_classes = classes.len();

        let mut theta = Array2::<F>::zeros((n_classes, n_features));
        let mut sigma = Array2::<F>::zeros((n_classes, n_features));
        let mut log_prior = Array1::<F>::zeros(n_classes);

        let n_f = F::from(n_samples).unwrap();

        for (ci, &class_label) in classes.iter().enumerate() {
            // Collect indices of samples belonging to this class.
            let class_mask: Vec<usize> = y
                .iter()
                .enumerate()
                .filter_map(|(i, &label)| if label == class_label { Some(i) } else { None })
                .collect();

            let n_c = class_mask.len();
            let n_c_f = F::from(n_c).unwrap();

            // Log prior: log(count_c / n_total).
            log_prior[ci] = (n_c_f / n_f).ln();

            // Compute per-feature mean.
            for j in 0..n_features {
                let mean = class_mask.iter().fold(F::zero(), |acc, &i| acc + x[[i, j]]) / n_c_f;
                theta[[ci, j]] = mean;

                // Compute variance (population variance).
                let var = if n_c > 1 {
                    class_mask.iter().fold(F::zero(), |acc, &i| {
                        let diff = x[[i, j]] - mean;
                        acc + diff * diff
                    }) / n_c_f
                } else {
                    F::zero()
                };

                sigma[[ci, j]] = var;
            }
        }

        // Store raw (unsmoothed) variance for partial_fit.
        let raw_sigma = sigma.clone();

        // Apply variance smoothing: add var_smoothing * max(variance_all_features).
        // Following scikit-learn: epsilon = var_smoothing * max of all variances.
        let max_var = sigma
            .iter()
            .fold(F::zero(), |acc, &v| if v > acc { v } else { acc });
        let epsilon = self.var_smoothing * max_var.max(F::one());
        sigma.mapv_inplace(|v| v + epsilon);

        // Use user-supplied class priors if provided.
        if let Some(ref priors) = self.class_prior {
            if priors.len() != n_classes {
                return Err(FerroError::InvalidParameter {
                    name: "class_prior".into(),
                    reason: format!(
                        "length {} does not match number of classes {}",
                        priors.len(),
                        n_classes
                    ),
                });
            }
            for (ci, &p) in priors.iter().enumerate() {
                log_prior[ci] = p.ln();
            }
        }

        // Collect class counts for partial_fit.
        let class_counts: Vec<usize> = classes
            .iter()
            .map(|&label| y.iter().filter(|&&l| l == label).count())
            .collect();

        Ok(FittedGaussianNB {
            classes,
            log_prior,
            theta,
            sigma,
            class_counts,
            raw_sigma,
            var_smoothing: self.var_smoothing,
            class_prior: self.class_prior.clone(),
        })
    }
}

impl<F: Float + Send + Sync + 'static> FittedGaussianNB<F> {
    /// Incrementally update the model with new data using Welford's algorithm.
    ///
    /// This method updates the running mean and variance for each class using
    /// a numerically stable online algorithm.
    ///
    /// # Errors
    ///
    /// - [`FerroError::ShapeMismatch`] if `x` and `y` have different row counts
    ///   or the number of features does not match the fitted model.
    pub fn partial_fit(&mut self, x: &Array2<F>, y: &Array1<usize>) -> Result<(), FerroError> {
        let (n_samples, n_features) = x.dim();

        if n_samples == 0 {
            return Ok(());
        }

        if n_samples != y.len() {
            return Err(FerroError::ShapeMismatch {
                expected: vec![n_samples],
                actual: vec![y.len()],
                context: "y length must match number of samples in X".into(),
            });
        }

        if n_features != self.theta.ncols() {
            return Err(FerroError::ShapeMismatch {
                expected: vec![self.theta.ncols()],
                actual: vec![n_features],
                context: "number of features must match fitted GaussianNB".into(),
            });
        }

        for &class_label in y.iter() {
            let ci = match self.classes.iter().position(|&c| c == class_label) {
                Some(idx) => idx,
                None => {
                    // New class discovered: extend arrays.
                    self.classes.push(class_label);
                    let ci = self.classes.len() - 1;

                    // Sort classes and find the new index.
                    let mut sorted_classes = self.classes.clone();
                    sorted_classes.sort_unstable();

                    // Rebuild with insertion.
                    let insert_pos = sorted_classes
                        .iter()
                        .position(|&c| c == class_label)
                        .unwrap();

                    self.classes = sorted_classes;
                    let n_classes = self.classes.len();

                    // Expand theta, raw_sigma, sigma, log_prior, class_counts.
                    let mut new_theta = Array2::<F>::zeros((n_classes, n_features));
                    let mut new_sigma = Array2::<F>::zeros((n_classes, n_features));
                    let mut new_raw_sigma = Array2::<F>::zeros((n_classes, n_features));
                    let mut new_log_prior = Array1::<F>::zeros(n_classes);
                    let mut new_counts = vec![0usize; n_classes];

                    let mut old_idx = 0;
                    for new_idx in 0..n_classes {
                        if new_idx == insert_pos {
                            // New class: zero-initialized.
                            continue;
                        }
                        if old_idx < self.theta.nrows() {
                            for j in 0..n_features {
                                new_theta[[new_idx, j]] = self.theta[[old_idx, j]];
                                new_sigma[[new_idx, j]] = self.sigma[[old_idx, j]];
                                new_raw_sigma[[new_idx, j]] = self.raw_sigma[[old_idx, j]];
                            }
                            new_log_prior[new_idx] = self.log_prior[old_idx];
                            new_counts[new_idx] = self.class_counts[old_idx];
                            old_idx += 1;
                        }
                    }

                    self.theta = new_theta;
                    self.sigma = new_sigma;
                    self.raw_sigma = new_raw_sigma;
                    self.log_prior = new_log_prior;
                    self.class_counts = new_counts;

                    let _ = ci; // suppress unused
                    insert_pos
                }
            };

            // We already have the class index. Now gather samples for this class.
            let _ = ci; // Will be used below.
        }

        // Now update per class.
        for (ci, &class_label) in self.classes.iter().enumerate() {
            let new_indices: Vec<usize> = y
                .iter()
                .enumerate()
                .filter_map(|(i, &label)| if label == class_label { Some(i) } else { None })
                .collect();

            if new_indices.is_empty() {
                continue;
            }

            let old_count = self.class_counts[ci];
            let new_count = new_indices.len();
            let total_count = old_count + new_count;
            let total_f = F::from(total_count).unwrap();

            for j in 0..n_features {
                let old_mean = self.theta[[ci, j]];
                let old_var = self.raw_sigma[[ci, j]];
                let old_count_f = F::from(old_count).unwrap();
                let new_count_f = F::from(new_count).unwrap();

                // Compute new batch mean and variance.
                let new_mean_batch = new_indices
                    .iter()
                    .fold(F::zero(), |acc, &i| acc + x[[i, j]])
                    / new_count_f;

                let new_var_batch = if new_count > 1 {
                    new_indices.iter().fold(F::zero(), |acc, &i| {
                        let d = x[[i, j]] - new_mean_batch;
                        acc + d * d
                    }) / new_count_f
                } else {
                    F::zero()
                };

                // Combined mean (Welford's parallel algorithm).
                let combined_mean =
                    (old_count_f * old_mean + new_count_f * new_mean_batch) / total_f;

                // Combined variance.
                let delta = new_mean_batch - old_mean;
                let combined_var = (old_count_f * old_var
                    + new_count_f * new_var_batch
                    + old_count_f * new_count_f * delta * delta / total_f)
                    / total_f;

                self.theta[[ci, j]] = combined_mean;
                self.raw_sigma[[ci, j]] = combined_var;
            }

            self.class_counts[ci] = total_count;
        }

        // Recompute smoothed sigma.
        self.sigma = self.raw_sigma.clone();
        let max_var = self
            .sigma
            .iter()
            .fold(F::zero(), |acc, &v| if v > acc { v } else { acc });
        let epsilon = self.var_smoothing * max_var.max(F::one());
        self.sigma.mapv_inplace(|v| v + epsilon);

        // Recompute log priors.
        if self.class_prior.is_none() {
            let total_samples: usize = self.class_counts.iter().sum();
            let total_f = F::from(total_samples).unwrap();
            for (ci, &count) in self.class_counts.iter().enumerate() {
                self.log_prior[ci] = (F::from(count).unwrap() / total_f).ln();
            }
        }

        Ok(())
    }

    /// Compute the joint log-likelihood for each class.
    ///
    /// Returns an array of shape `(n_samples, n_classes)` containing the
    /// unnormalized log-posterior scores.
    fn joint_log_likelihood(&self, x: &Array2<F>) -> Array2<F> {
        let n_samples = x.nrows();
        let n_classes = self.classes.len();
        let n_features = x.ncols();

        let two = F::from(2.0).unwrap();
        let pi = F::from(std::f64::consts::PI).unwrap();
        let log_two_pi = (two * pi).ln();

        let mut scores = Array2::<F>::zeros((n_samples, n_classes));

        for ci in 0..n_classes {
            for i in 0..n_samples {
                let mut log_likelihood = self.log_prior[ci];
                for j in 0..n_features {
                    let mu = self.theta[[ci, j]];
                    let var = self.sigma[[ci, j]];
                    let diff = x[[i, j]] - mu;
                    // log N(x; mu, var) = -0.5 * (log(2*pi*var) + (x-mu)^2/var)
                    log_likelihood =
                        log_likelihood - (log_two_pi + var.ln()) / two - diff * diff / (two * var);
                }
                scores[[i, ci]] = log_likelihood;
            }
        }

        scores
    }

    /// Predict class probabilities for the given feature matrix.
    ///
    /// Returns an array of shape `(n_samples, n_classes)` where each row
    /// sums to 1.
    ///
    /// # Errors
    ///
    /// Returns [`FerroError::ShapeMismatch`] if the number of features does
    /// not match the fitted model.
    pub fn predict_proba(&self, x: &Array2<F>) -> Result<Array2<F>, FerroError> {
        let n_features_fitted = self.theta.ncols();
        if x.ncols() != n_features_fitted {
            return Err(FerroError::ShapeMismatch {
                expected: vec![n_features_fitted],
                actual: vec![x.ncols()],
                context: "number of features must match fitted GaussianNB".into(),
            });
        }

        let log_scores = self.joint_log_likelihood(x);
        let n_samples = x.nrows();
        let n_classes = self.classes.len();
        let mut proba = Array2::<F>::zeros((n_samples, n_classes));

        for i in 0..n_samples {
            // Numerically stable softmax: subtract row max before exp.
            let max_score = log_scores
                .row(i)
                .iter()
                .fold(F::neg_infinity(), |a, &b| a.max(b));

            let mut row_sum = F::zero();
            for ci in 0..n_classes {
                let p = (log_scores[[i, ci]] - max_score).exp();
                proba[[i, ci]] = p;
                row_sum = row_sum + p;
            }
            for ci in 0..n_classes {
                proba[[i, ci]] = proba[[i, ci]] / row_sum;
            }
        }

        Ok(proba)
    }
}

impl<F: Float + Send + Sync + 'static> Predict<Array2<F>> for FittedGaussianNB<F> {
    type Output = Array1<usize>;
    type Error = FerroError;

    /// Predict class labels for the given feature matrix.
    ///
    /// # Errors
    ///
    /// Returns [`FerroError::ShapeMismatch`] if the number of features does
    /// not match the fitted model.
    fn predict(&self, x: &Array2<F>) -> Result<Array1<usize>, FerroError> {
        let n_features_fitted = self.theta.ncols();
        if x.ncols() != n_features_fitted {
            return Err(FerroError::ShapeMismatch {
                expected: vec![n_features_fitted],
                actual: vec![x.ncols()],
                context: "number of features must match fitted GaussianNB".into(),
            });
        }

        let scores = self.joint_log_likelihood(x);
        let n_samples = x.nrows();
        let n_classes = self.classes.len();

        let mut predictions = Array1::<usize>::zeros(n_samples);
        for i in 0..n_samples {
            let mut best_class = 0;
            let mut best_score = scores[[i, 0]];
            for ci in 1..n_classes {
                if scores[[i, ci]] > best_score {
                    best_score = scores[[i, ci]];
                    best_class = ci;
                }
            }
            predictions[i] = self.classes[best_class];
        }

        Ok(predictions)
    }
}

impl<F: Float + Send + Sync + 'static> HasClasses for FittedGaussianNB<F> {
    fn classes(&self) -> &[usize] {
        &self.classes
    }

    fn n_classes(&self) -> usize {
        self.classes.len()
    }
}

// Pipeline integration.
impl<F: Float + ToPrimitive + FromPrimitive + Send + Sync + 'static> PipelineEstimator<F>
    for GaussianNB<F>
{
    fn fit_pipeline(
        &self,
        x: &Array2<F>,
        y: &Array1<F>,
    ) -> Result<Box<dyn FittedPipelineEstimator<F>>, FerroError> {
        let y_usize: Array1<usize> = y.mapv(|v| v.to_usize().unwrap_or(0));
        let fitted = self.fit(x, &y_usize)?;
        Ok(Box::new(FittedGaussianNBPipeline(fitted)))
    }
}

struct FittedGaussianNBPipeline<F: Float + Send + Sync + 'static>(FittedGaussianNB<F>);

unsafe impl<F: Float + Send + Sync + 'static> Send for FittedGaussianNBPipeline<F> {}
unsafe impl<F: Float + Send + Sync + 'static> Sync for FittedGaussianNBPipeline<F> {}

impl<F: Float + ToPrimitive + FromPrimitive + Send + Sync + 'static> FittedPipelineEstimator<F>
    for FittedGaussianNBPipeline<F>
{
    fn predict_pipeline(&self, x: &Array2<F>) -> Result<Array1<F>, FerroError> {
        let preds = self.0.predict(x)?;
        Ok(preds.mapv(|v| F::from_usize(v).unwrap_or(F::nan())))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;
    use ndarray::array;

    fn make_2class_data() -> (Array2<f64>, Array1<usize>) {
        // Two well-separated Gaussian clusters.
        let x = Array2::from_shape_vec(
            (8, 2),
            vec![
                1.0, 1.0, 1.2, 0.8, 0.9, 1.1, 1.1, 0.9, // class 0
                5.0, 5.0, 5.1, 4.9, 4.8, 5.2, 5.0, 4.8, // class 1
            ],
        )
        .unwrap();
        let y = array![0usize, 0, 0, 0, 1, 1, 1, 1];
        (x, y)
    }

    #[test]
    fn test_gaussian_nb_fit_predict_2class() {
        let (x, y) = make_2class_data();
        let model = GaussianNB::<f64>::new();
        let fitted = model.fit(&x, &y).unwrap();
        let preds = fitted.predict(&x).unwrap();
        // Should classify the training data correctly.
        let correct = preds.iter().zip(y.iter()).filter(|(p, a)| p == a).count();
        assert_eq!(correct, 8);
    }

    #[test]
    fn test_gaussian_nb_predict_proba_sums_to_one() {
        let (x, y) = make_2class_data();
        let model = GaussianNB::<f64>::new();
        let fitted = model.fit(&x, &y).unwrap();
        let proba = fitted.predict_proba(&x).unwrap();
        assert_eq!(proba.nrows(), 8);
        assert_eq!(proba.ncols(), 2);
        for i in 0..proba.nrows() {
            assert_relative_eq!(proba.row(i).sum(), 1.0, epsilon = 1e-10);
        }
    }

    #[test]
    fn test_gaussian_nb_predict_proba_ordering() {
        let (x, y) = make_2class_data();
        let model = GaussianNB::<f64>::new();
        let fitted = model.fit(&x, &y).unwrap();
        let proba = fitted.predict_proba(&x).unwrap();
        // First 4 samples should have higher probability of class 0.
        for i in 0..4 {
            assert!(proba[[i, 0]] > proba[[i, 1]]);
        }
        // Last 4 samples should have higher probability of class 1.
        for i in 4..8 {
            assert!(proba[[i, 1]] > proba[[i, 0]]);
        }
    }

    #[test]
    fn test_gaussian_nb_has_classes() {
        let (x, y) = make_2class_data();
        let model = GaussianNB::<f64>::new();
        let fitted = model.fit(&x, &y).unwrap();
        assert_eq!(fitted.classes(), &[0, 1]);
        assert_eq!(fitted.n_classes(), 2);
    }

    #[test]
    fn test_gaussian_nb_three_classes() {
        let x = Array2::from_shape_vec(
            (9, 2),
            vec![
                0.0, 0.0, 0.1, 0.0, 0.0, 0.1, // class 0
                5.0, 0.0, 5.1, 0.0, 5.0, 0.1, // class 1
                0.0, 5.0, 0.1, 5.0, 0.0, 5.1, // class 2
            ],
        )
        .unwrap();
        let y = array![0usize, 0, 0, 1, 1, 1, 2, 2, 2];

        let model = GaussianNB::<f64>::new();
        let fitted = model.fit(&x, &y).unwrap();
        assert_eq!(fitted.n_classes(), 3);
        assert_eq!(fitted.classes(), &[0, 1, 2]);

        let preds = fitted.predict(&x).unwrap();
        let correct = preds.iter().zip(y.iter()).filter(|(p, a)| p == a).count();
        assert_eq!(correct, 9);
    }

    #[test]
    fn test_gaussian_nb_var_smoothing_effect() {
        // When all samples in a class have identical features (zero variance),
        // var_smoothing prevents division by zero.
        let x = Array2::from_shape_vec((4, 1), vec![1.0, 1.0, 5.0, 5.0]).unwrap();
        let y = array![0usize, 0, 1, 1];

        let model_default = GaussianNB::<f64>::new();
        let fitted = model_default.fit(&x, &y).unwrap();
        // Should not panic — var_smoothing handles zero variance.
        let preds = fitted.predict(&x).unwrap();
        assert_eq!(preds.len(), 4);

        // With higher smoothing the model still predicts.
        let model_high = GaussianNB::<f64>::new().with_var_smoothing(0.1);
        let fitted_high = model_high.fit(&x, &y).unwrap();
        let preds_high = fitted_high.predict(&x).unwrap();
        assert_eq!(preds_high.len(), 4);
    }

    #[test]
    fn test_gaussian_nb_shape_mismatch_fit() {
        let x =
            Array2::from_shape_vec((4, 2), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]).unwrap();
        let y = array![0usize, 1, 0]; // Wrong length
        let model = GaussianNB::<f64>::new();
        assert!(model.fit(&x, &y).is_err());
    }

    #[test]
    fn test_gaussian_nb_shape_mismatch_predict() {
        let (x, y) = make_2class_data();
        let model = GaussianNB::<f64>::new();
        let fitted = model.fit(&x, &y).unwrap();

        // Wrong number of features.
        let x_bad = Array2::from_shape_vec((4, 3), vec![1.0; 12]).unwrap();
        assert!(fitted.predict(&x_bad).is_err());
        assert!(fitted.predict_proba(&x_bad).is_err());
    }

    #[test]
    fn test_gaussian_nb_single_class() {
        // Single class — still fits, always predicts that class.
        let x = Array2::from_shape_vec((3, 2), vec![1.0, 2.0, 1.5, 2.5, 0.8, 1.8]).unwrap();
        let y = array![0usize, 0, 0];

        let model = GaussianNB::<f64>::new();
        let fitted = model.fit(&x, &y).unwrap();
        assert_eq!(fitted.classes(), &[0]);
        let preds = fitted.predict(&x).unwrap();
        assert!(preds.iter().all(|&p| p == 0));
    }

    #[test]
    fn test_gaussian_nb_empty_data() {
        let x = Array2::<f64>::zeros((0, 2));
        let y = Array1::<usize>::zeros(0);
        let model = GaussianNB::<f64>::new();
        assert!(model.fit(&x, &y).is_err());
    }

    #[test]
    fn test_gaussian_nb_default() {
        let model = GaussianNB::<f64>::default();
        assert_relative_eq!(model.var_smoothing, 1e-9, epsilon = 1e-15);
    }

    #[test]
    fn test_gaussian_nb_pipeline() {
        let x = Array2::from_shape_vec((6, 1), vec![-3.0, -2.0, -1.0, 1.0, 2.0, 3.0]).unwrap();
        let y = Array1::from_vec(vec![0.0, 0.0, 0.0, 1.0, 1.0, 1.0]);
        let model = GaussianNB::<f64>::new();
        let fitted = model.fit_pipeline(&x, &y).unwrap();
        let preds = fitted.predict_pipeline(&x).unwrap();
        assert_eq!(preds.len(), 6);
    }

    #[test]
    fn test_gaussian_nb_unordered_classes() {
        // Classes are not 0..n, and not in order in y.
        let x = Array2::from_shape_vec((6, 1), vec![1.0, 1.2, 1.1, 5.0, 4.9, 5.1]).unwrap();
        let y = array![3usize, 3, 3, 7, 7, 7];
        let model = GaussianNB::<f64>::new();
        let fitted = model.fit(&x, &y).unwrap();
        assert_eq!(fitted.classes(), &[3, 7]);
        let preds = fitted.predict(&x).unwrap();
        assert!(preds[0] == 3 || preds[0] == 7);
        assert_eq!(preds[0], 3);
        assert_eq!(preds[3], 7);
    }

    #[test]
    fn test_gaussian_nb_partial_fit() {
        // Fit on first batch, then partial_fit on second batch.
        let x1 =
            Array2::from_shape_vec((4, 2), vec![1.0, 1.0, 1.2, 0.8, 5.0, 5.0, 5.1, 4.9]).unwrap();
        let y1 = array![0usize, 0, 1, 1];

        let model = GaussianNB::<f64>::new();
        let mut fitted = model.fit(&x1, &y1).unwrap();

        let x2 =
            Array2::from_shape_vec((4, 2), vec![0.9, 1.1, 1.1, 0.9, 4.8, 5.2, 5.0, 4.8]).unwrap();
        let y2 = array![0usize, 0, 1, 1];

        fitted.partial_fit(&x2, &y2).unwrap();

        // Should still classify correctly after partial_fit.
        let x_test = Array2::from_shape_vec((2, 2), vec![1.0, 1.0, 5.0, 5.0]).unwrap();
        let preds = fitted.predict(&x_test).unwrap();
        assert_eq!(preds[0], 0);
        assert_eq!(preds[1], 1);
    }

    #[test]
    fn test_gaussian_nb_partial_fit_shape_mismatch() {
        let (x, y) = make_2class_data();
        let model = GaussianNB::<f64>::new();
        let mut fitted = model.fit(&x, &y).unwrap();

        // Wrong number of features.
        let x_bad = Array2::from_shape_vec((2, 3), vec![1.0; 6]).unwrap();
        let y_bad = array![0usize, 1];
        assert!(fitted.partial_fit(&x_bad, &y_bad).is_err());

        // Wrong y length.
        let x_ok = Array2::from_shape_vec((2, 2), vec![1.0; 4]).unwrap();
        let y_wrong = array![0usize];
        assert!(fitted.partial_fit(&x_ok, &y_wrong).is_err());
    }

    #[test]
    fn test_gaussian_nb_partial_fit_empty() {
        let (x, y) = make_2class_data();
        let model = GaussianNB::<f64>::new();
        let mut fitted = model.fit(&x, &y).unwrap();

        let x_empty = Array2::<f64>::zeros((0, 2));
        let y_empty = Array1::<usize>::zeros(0);
        // Should succeed without changes.
        assert!(fitted.partial_fit(&x_empty, &y_empty).is_ok());
    }

    #[test]
    fn test_gaussian_nb_class_prior() {
        let (x, y) = make_2class_data();
        let model = GaussianNB::<f64>::new().with_class_prior(vec![0.9, 0.1]);
        let fitted = model.fit(&x, &y).unwrap();

        // With strongly biased prior toward class 0, predictions should favor class 0.
        // Even class 1 samples might be classified as class 0.
        let preds = fitted.predict(&x).unwrap();
        assert_eq!(preds.len(), 8);
    }

    #[test]
    fn test_gaussian_nb_class_prior_wrong_length() {
        let (x, y) = make_2class_data();
        let model = GaussianNB::<f64>::new().with_class_prior(vec![0.5, 0.3, 0.2]);
        assert!(model.fit(&x, &y).is_err());
    }
}