scirs2-stats 0.3.4

Statistical functions module for SciRS2 (scirs2-stats)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
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
//! Hidden Markov Models (HMM)
//!
//! This module provides production-quality Hidden Markov Model implementations
//! supporting discrete and Gaussian emission distributions.
//!
//! # Algorithms
//!
//! - **Forward algorithm** – compute log-likelihood + scaled alpha matrix.
//! - **Backward algorithm** – scaled beta matrix.
//! - **Viterbi decoding** – most-probable hidden-state sequence.
//! - **Baum-Welch (EM)** – maximum-likelihood parameter estimation from
//!   one or more observation sequences.
//! - **Posterior state probabilities** – gamma matrix P(s_t | obs, hmm).
//!
//! Scaling is applied to alpha / beta passes to prevent floating-point underflow
//! for long sequences.
//!
//! # References
//! - Rabiner, L.R. (1989). "A tutorial on hidden Markov models and selected
//!   applications in speech recognition." *Proceedings of the IEEE* 77(2).
//! - Bishop, C.M. (2006). *Pattern Recognition and Machine Learning*, Chapter 13.

use crate::error::{StatsError, StatsResult};
use scirs2_core::ndarray::{Array1, Array2, Axis};

// ─────────────────────────────────────────────────────────────────────────────
// Core HMM parameter struct
// ─────────────────────────────────────────────────────────────────────────────

/// Core Hidden Markov Model parameters.
///
/// Stores the three canonical HMM matrices:
/// * **A** – `n_states × n_states` row-stochastic transition matrix.
/// * **B** – `n_states × n_obs`   row-stochastic emission matrix (discrete) or
///           an `n_states × 2` parameter matrix for Gaussian HMMs (col 0 = mean, col 1 = var).
/// * **pi** – `n_states` initial state probability vector.
#[derive(Clone, Debug)]
pub struct HmmModel {
    /// Transition matrix A[i, j] = P(s_{t+1}=j | s_t=i).
    pub transition: Array2<f64>,
    /// Emission parameters (interpretation depends on model type).
    pub emission: Array2<f64>,
    /// Initial state probabilities pi[i] = P(s_1 = i).
    pub initial: Array1<f64>,
    /// Number of hidden states.
    pub n_states: usize,
}

impl HmmModel {
    /// Create a new `HmmModel`, validating that dimensions are consistent.
    pub fn new(
        transition: Array2<f64>,
        emission: Array2<f64>,
        initial: Array1<f64>,
    ) -> StatsResult<Self> {
        let n_states = initial.len();
        if transition.nrows() != n_states || transition.ncols() != n_states {
            return Err(StatsError::DimensionMismatch(format!(
                "Transition matrix must be {}×{}, got {}×{}",
                n_states,
                n_states,
                transition.nrows(),
                transition.ncols()
            )));
        }
        if emission.nrows() != n_states {
            return Err(StatsError::DimensionMismatch(format!(
                "Emission matrix row count ({}) must equal n_states ({})",
                emission.nrows(),
                n_states
            )));
        }
        Ok(Self {
            transition,
            emission,
            initial,
            n_states,
        })
    }

    /// Return the number of hidden states.
    #[inline]
    pub fn n_states(&self) -> usize {
        self.n_states
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Discrete HMM wrapper
// ─────────────────────────────────────────────────────────────────────────────

/// Discrete-observation Hidden Markov Model.
///
/// Observations are non-negative integers `0..n_obs`.
/// The emission matrix `B[i, k]` = P(o_t = k | s_t = i).
#[derive(Clone, Debug)]
pub struct DiscreteHmm {
    /// Underlying parameter model.
    pub model: HmmModel,
    /// Number of distinct observation symbols.
    pub n_obs: usize,
}

impl DiscreteHmm {
    /// Build a `DiscreteHmm` from explicit matrices.
    ///
    /// # Arguments
    /// * `transition` – `n_states × n_states` row-stochastic matrix.
    /// * `emission`   – `n_states × n_obs`   row-stochastic matrix.
    /// * `initial`    – `n_states` probability vector summing to 1.
    pub fn new(
        transition: Array2<f64>,
        emission: Array2<f64>,
        initial: Array1<f64>,
    ) -> StatsResult<Self> {
        let n_obs = emission.ncols();
        let model = HmmModel::new(transition, emission, initial)?;
        Ok(Self { model, n_obs })
    }

    /// Sample the emission probability for state `s` and observation `o`.
    #[inline]
    pub fn emission_prob(&self, state: usize, obs: usize) -> f64 {
        self.model.emission[[state, obs]]
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Gaussian HMM wrapper
// ─────────────────────────────────────────────────────────────────────────────

/// Gaussian-emission Hidden Markov Model.
///
/// Each hidden state emits a univariate Gaussian. The emission matrix stores
/// `[mean, variance]` per row, so `emission[i, 0]` is the mean and
/// `emission[i, 1]` is the variance for state `i`.
#[derive(Clone, Debug)]
pub struct GaussianHmm {
    /// Underlying parameter model.
    pub model: HmmModel,
}

impl GaussianHmm {
    /// Build a `GaussianHmm` from explicit matrices.
    ///
    /// # Arguments
    /// * `transition` – `n_states × n_states` row-stochastic matrix.
    /// * `means`      – `n_states` emission means.
    /// * `variances`  – `n_states` emission variances (must be positive).
    /// * `initial`    – `n_states` probability vector summing to 1.
    pub fn new(
        transition: Array2<f64>,
        means: Array1<f64>,
        variances: Array1<f64>,
        initial: Array1<f64>,
    ) -> StatsResult<Self> {
        let n_states = initial.len();
        if means.len() != n_states || variances.len() != n_states {
            return Err(StatsError::DimensionMismatch(
                "means and variances must have length n_states".into(),
            ));
        }
        for (i, &v) in variances.iter().enumerate() {
            if v <= 0.0 {
                return Err(StatsError::DomainError(format!(
                    "Variance for state {} must be positive, got {}",
                    i, v
                )));
            }
        }
        // Pack means and variances column-wise into the emission matrix.
        let mut emission = Array2::<f64>::zeros((n_states, 2));
        for i in 0..n_states {
            emission[[i, 0]] = means[i];
            emission[[i, 1]] = variances[i];
        }
        let model = HmmModel::new(transition, emission, initial)?;
        Ok(Self { model })
    }

    /// Evaluate the Gaussian PDF for state `s` at observation value `x`.
    pub fn emission_prob(&self, state: usize, x: f64) -> f64 {
        let mu = self.model.emission[[state, 0]];
        let var = self.model.emission[[state, 1]];
        let diff = x - mu;
        let norm = (2.0 * std::f64::consts::PI * var).sqrt();
        (-0.5 * diff * diff / var).exp() / norm
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Trait for emission probability evaluation
// ─────────────────────────────────────────────────────────────────────────────

/// Internal helper trait so that forward/backward/Viterbi can be generic over
/// discrete and Gaussian models.
trait EmissionModel {
    fn n_states(&self) -> usize;
    fn transition(&self) -> &Array2<f64>;
    fn initial(&self) -> &Array1<f64>;
    fn log_emission(&self, state: usize, obs_idx: usize, obs_vals: &[f64]) -> f64;
}

// ─────────────────────────────────────────────────────────────────────────────
// Forward algorithm – scaled
// ─────────────────────────────────────────────────────────────────────────────

/// Result of the forward algorithm: log-likelihood and scaled alpha matrix.
#[derive(Clone, Debug)]
pub struct ForwardResult {
    /// Log-likelihood log P(obs | model).
    pub log_likelihood: f64,
    /// `T × n_states` scaled alpha matrix where each row sums to 1.
    pub alpha: Array2<f64>,
    /// Per-step scaling coefficients (length T).
    pub scales: Array1<f64>,
}

/// Compute the forward (alpha) pass for a **discrete** HMM with scaling.
///
/// # Arguments
/// * `obs`   – integer observation sequence (values in `0..n_obs`).
/// * `model` – fitted `DiscreteHmm`.
///
/// # Returns
/// `(log_likelihood, alpha_matrix)` – the scaled alpha matrix has shape `T × n_states`.
pub fn forward_algorithm(obs: &[usize], model: &DiscreteHmm) -> StatsResult<ForwardResult> {
    let t_len = obs.len();
    if t_len == 0 {
        return Err(StatsError::InsufficientData(
            "Observation sequence must not be empty".into(),
        ));
    }
    let n = model.model.n_states;
    let n_obs = model.n_obs;

    for &o in obs {
        if o >= n_obs {
            return Err(StatsError::DomainError(format!(
                "Observation symbol {} out of range [0, {})",
                o, n_obs
            )));
        }
    }

    let mut alpha = Array2::<f64>::zeros((t_len, n));
    let mut scales = Array1::<f64>::zeros(t_len);

    // Initialisation
    for i in 0..n {
        alpha[[0, i]] = model.model.initial[i] * model.emission_prob(i, obs[0]);
    }
    let c0 = alpha.row(0).sum();
    if c0 <= 0.0 {
        return Err(StatsError::ComputationError(
            "Forward pass: scale at t=0 is zero; check emission / initial probabilities".into(),
        ));
    }
    for i in 0..n {
        alpha[[0, i]] /= c0;
    }
    scales[0] = c0;

    // Recursion
    for t in 1..t_len {
        for j in 0..n {
            let mut sum = 0.0_f64;
            for i in 0..n {
                sum += alpha[[t - 1, i]] * model.model.transition[[i, j]];
            }
            alpha[[t, j]] = sum * model.emission_prob(j, obs[t]);
        }
        let ct = alpha.row(t).sum();
        if ct <= 0.0 {
            return Err(StatsError::ComputationError(format!(
                "Forward pass: scale at t={} is zero",
                t
            )));
        }
        for j in 0..n {
            alpha[[t, j]] /= ct;
        }
        scales[t] = ct;
    }

    let log_likelihood = scales.mapv(|c| c.ln()).sum();
    Ok(ForwardResult {
        log_likelihood,
        alpha,
        scales,
    })
}

// ─────────────────────────────────────────────────────────────────────────────
// Backward algorithm – scaled
// ─────────────────────────────────────────────────────────────────────────────

/// Compute the backward (beta) pass for a **discrete** HMM, using the same
/// scaling coefficients as produced by the forward pass.
///
/// # Arguments
/// * `obs`    – integer observation sequence.
/// * `model`  – fitted `DiscreteHmm`.
/// * `scales` – per-step scaling coefficients from [`forward_algorithm`].
///
/// # Returns
/// `T × n_states` scaled beta matrix.
pub fn backward_algorithm(
    obs: &[usize],
    model: &DiscreteHmm,
    scales: &Array1<f64>,
) -> StatsResult<Array2<f64>> {
    let t_len = obs.len();
    if t_len == 0 {
        return Err(StatsError::InsufficientData(
            "Observation sequence must not be empty".into(),
        ));
    }
    if scales.len() != t_len {
        return Err(StatsError::DimensionMismatch(
            "scales length must equal observation length".into(),
        ));
    }

    let n = model.model.n_states;
    let mut beta = Array2::<f64>::zeros((t_len, n));

    // Initialisation: β_T(i) = 1 (before scaling)
    for i in 0..n {
        beta[[t_len - 1, i]] = 1.0 / scales[t_len - 1];
    }

    // Recursion (backwards)
    for t in (0..t_len - 1).rev() {
        for i in 0..n {
            let mut sum = 0.0_f64;
            for j in 0..n {
                sum += model.model.transition[[i, j]]
                    * model.emission_prob(j, obs[t + 1])
                    * beta[[t + 1, j]];
            }
            beta[[t, i]] = sum / scales[t];
        }
    }

    Ok(beta)
}

// ─────────────────────────────────────────────────────────────────────────────
// Viterbi decoding
// ─────────────────────────────────────────────────────────────────────────────

/// Viterbi decoding: find the most-likely hidden state sequence for a
/// **discrete** HMM using the log-domain Viterbi algorithm.
///
/// # Arguments
/// * `obs`   – integer observation sequence.
/// * `model` – fitted `DiscreteHmm`.
///
/// # Returns
/// Most-likely state sequence of length `T`.
pub fn viterbi(obs: &[usize], model: &DiscreteHmm) -> StatsResult<Vec<usize>> {
    let t_len = obs.len();
    if t_len == 0 {
        return Err(StatsError::InsufficientData(
            "Observation sequence must not be empty".into(),
        ));
    }
    let n = model.model.n_states;
    let n_obs = model.n_obs;

    for &o in obs {
        if o >= n_obs {
            return Err(StatsError::DomainError(format!(
                "Observation symbol {} out of range [0, {})",
                o, n_obs
            )));
        }
    }

    let log_a = model
        .model
        .transition
        .mapv(|v| if v > 0.0 { v.ln() } else { f64::NEG_INFINITY });
    let log_b: Array2<f64> = model
        .model
        .emission
        .mapv(|v| if v > 0.0 { v.ln() } else { f64::NEG_INFINITY });
    let log_pi: Array1<f64> = model
        .model
        .initial
        .mapv(|v| if v > 0.0 { v.ln() } else { f64::NEG_INFINITY });

    // delta[t, i] = max log P(s_1..s_t = *, s_t = i, o_1..o_t)
    let mut delta = Array2::<f64>::from_elem((t_len, n), f64::NEG_INFINITY);
    // psi[t, i]   = argmax predecessor state
    let mut psi = Array2::<usize>::zeros((t_len, n));

    // Initialisation
    for i in 0..n {
        delta[[0, i]] = log_pi[i] + log_b[[i, obs[0]]];
    }

    // Recursion
    for t in 1..t_len {
        for j in 0..n {
            let mut best_val = f64::NEG_INFINITY;
            let mut best_i = 0;
            for i in 0..n {
                let val = delta[[t - 1, i]] + log_a[[i, j]];
                if val > best_val {
                    best_val = val;
                    best_i = i;
                }
            }
            delta[[t, j]] = best_val + log_b[[j, obs[t]]];
            psi[[t, j]] = best_i;
        }
    }

    // Termination
    let mut states = vec![0usize; t_len];
    let mut best_last = f64::NEG_INFINITY;
    for i in 0..n {
        if delta[[t_len - 1, i]] > best_last {
            best_last = delta[[t_len - 1, i]];
            states[t_len - 1] = i;
        }
    }

    // Backtrack
    for t in (0..t_len - 1).rev() {
        states[t] = psi[[t + 1, states[t + 1]]];
    }

    Ok(states)
}

// ─────────────────────────────────────────────────────────────────────────────
// Posterior state probabilities (gamma)
// ─────────────────────────────────────────────────────────────────────────────

/// Compute posterior state probabilities γ_t(i) = P(s_t = i | obs, model).
///
/// # Arguments
/// * `obs`   – integer observation sequence.
/// * `model` – fitted `DiscreteHmm`.
///
/// # Returns
/// `T × n_states` matrix where `gamma[t, i]` = P(s_t = i | obs, model).
pub fn posterior_state_probs(obs: &[usize], model: &DiscreteHmm) -> StatsResult<Array2<f64>> {
    let fwd = forward_algorithm(obs, model)?;
    let beta = backward_algorithm(obs, model, &fwd.scales)?;

    let t_len = obs.len();
    let n = model.model.n_states;
    let mut gamma = Array2::<f64>::zeros((t_len, n));

    for t in 0..t_len {
        let mut row_sum = 0.0_f64;
        for i in 0..n {
            gamma[[t, i]] = fwd.alpha[[t, i]] * beta[[t, i]];
            row_sum += gamma[[t, i]];
        }
        if row_sum > 0.0 {
            for i in 0..n {
                gamma[[t, i]] /= row_sum;
            }
        }
    }

    Ok(gamma)
}

// ─────────────────────────────────────────────────────────────────────────────
// Baum-Welch (EM) training
// ─────────────────────────────────────────────────────────────────────────────

/// Result returned by Baum-Welch training.
#[derive(Clone, Debug)]
pub struct HmmFit {
    /// Fitted HMM model.
    pub model: DiscreteHmm,
    /// Log-likelihood of the training data under the fitted model.
    pub log_likelihood: f64,
    /// Number of EM iterations performed.
    pub n_iter: usize,
}

/// Train a discrete HMM on one or more observation sequences using the
/// Baum-Welch (Expectation-Maximisation) algorithm.
///
/// The algorithm iteratively updates the three HMM parameter matrices until
/// the change in total log-likelihood falls below `tol` or `max_iter` is reached.
///
/// # Arguments
/// * `obs_sequences` – slice of observation sequences (each is a `Vec<usize>`).
/// * `n_states`      – number of hidden states.
/// * `n_obs`         – number of distinct observation symbols.
/// * `max_iter`      – maximum number of EM iterations.
/// * `tol`           – convergence tolerance on log-likelihood improvement.
///
/// # Returns
/// An [`HmmFit`] containing the trained model and diagnostics.
pub fn baum_welch(
    obs_sequences: &[Vec<usize>],
    n_states: usize,
    n_obs: usize,
    max_iter: usize,
    tol: f64,
) -> StatsResult<HmmFit> {
    if obs_sequences.is_empty() {
        return Err(StatsError::InsufficientData(
            "At least one observation sequence is required".into(),
        ));
    }
    if n_states == 0 {
        return Err(StatsError::InvalidArgument("n_states must be > 0".into()));
    }
    if n_obs == 0 {
        return Err(StatsError::InvalidArgument("n_obs must be > 0".into()));
    }

    // Validate observations
    for (seq_idx, seq) in obs_sequences.iter().enumerate() {
        if seq.is_empty() {
            return Err(StatsError::InsufficientData(format!(
                "Observation sequence {} is empty",
                seq_idx
            )));
        }
        for &o in seq {
            if o >= n_obs {
                return Err(StatsError::DomainError(format!(
                    "Sequence {}: observation {} out of range [0, {})",
                    seq_idx, o, n_obs
                )));
            }
        }
    }

    // ── Initialise parameters (uniform with small perturbation for symmetry breaking) ──
    let mut trans = uniform_stochastic_matrix(n_states, n_states);
    let mut emiss = uniform_stochastic_matrix(n_states, n_obs);
    let mut init = uniform_stochastic_vec(n_states);

    let mut log_lik_prev = f64::NEG_INFINITY;
    let mut n_iter = 0usize;

    for iter in 0..max_iter {
        n_iter = iter + 1;

        // ── Accumulators ──
        let mut acc_trans = Array2::<f64>::zeros((n_states, n_states));
        let mut acc_emiss = Array2::<f64>::zeros((n_states, n_obs));
        let mut acc_init = Array1::<f64>::zeros(n_states);
        let mut total_log_lik = 0.0_f64;

        for seq in obs_sequences.iter() {
            let t_len = seq.len();

            // Rebuild model from current parameters
            let current_model = DiscreteHmm::new(
                trans.clone(),
                emiss.clone(),
                init.clone(),
            )?;

            let fwd = forward_algorithm(seq, &current_model)?;
            let beta = backward_algorithm(seq, &current_model, &fwd.scales)?;

            total_log_lik += fwd.log_likelihood;

            // γ_t(i)  = α_t(i) * β_t(i)  (then normalise per t)
            let mut gamma = Array2::<f64>::zeros((t_len, n_states));
            for t in 0..t_len {
                let mut row_sum = 0.0_f64;
                for i in 0..n_states {
                    gamma[[t, i]] = fwd.alpha[[t, i]] * beta[[t, i]];
                    row_sum += gamma[[t, i]];
                }
                if row_sum > 0.0 {
                    for i in 0..n_states {
                        gamma[[t, i]] /= row_sum;
                    }
                }
            }

            // ξ_t(i,j) = α_t(i) * A[i,j] * B[j, o_{t+1}] * β_{t+1}(j) (normalised)
            // Accumulate expected transitions
            for t in 0..t_len - 1 {
                let mut xi_sum = 0.0_f64;
                let mut xi_row = Array2::<f64>::zeros((n_states, n_states));
                for i in 0..n_states {
                    for j in 0..n_states {
                        let val = fwd.alpha[[t, i]]
                            * trans[[i, j]]
                            * emiss[[j, seq[t + 1]]]
                            * beta[[t + 1, j]];
                        xi_row[[i, j]] = val;
                        xi_sum += val;
                    }
                }
                if xi_sum > 0.0 {
                    for i in 0..n_states {
                        for j in 0..n_states {
                            acc_trans[[i, j]] += xi_row[[i, j]] / xi_sum;
                        }
                    }
                }
            }

            // Accumulate initial state probabilities
            for i in 0..n_states {
                acc_init[i] += gamma[[0, i]];
            }

            // Accumulate emission probabilities
            for t in 0..t_len {
                for i in 0..n_states {
                    acc_emiss[[i, seq[t]]] += gamma[[t, i]];
                }
            }
        }

        // ── M-step: normalise accumulators → new parameters ──
        init = normalise_vec(acc_init);
        trans = normalise_rows(acc_trans);
        emiss = normalise_rows(acc_emiss);

        // ── Convergence check ──
        let improvement = total_log_lik - log_lik_prev;
        if iter > 0 && improvement.abs() < tol {
            break;
        }
        log_lik_prev = total_log_lik;
    }

    let final_model = DiscreteHmm::new(trans, emiss, init)?;

    Ok(HmmFit {
        model: final_model,
        log_likelihood: log_lik_prev,
        n_iter,
    })
}

// ─────────────────────────────────────────────────────────────────────────────
// Gaussian HMM forward / Viterbi helpers
// ─────────────────────────────────────────────────────────────────────────────

/// Result of the Gaussian-HMM forward algorithm.
#[derive(Clone, Debug)]
pub struct GaussianForwardResult {
    /// Log-likelihood log P(obs | model).
    pub log_likelihood: f64,
    /// `T × n_states` scaled alpha matrix.
    pub alpha: Array2<f64>,
    /// Per-step scaling coefficients.
    pub scales: Array1<f64>,
}

/// Forward algorithm for a **Gaussian** HMM.
///
/// # Arguments
/// * `obs`   – continuous observation sequence.
/// * `model` – fitted `GaussianHmm`.
///
/// # Returns
/// [`GaussianForwardResult`] with log-likelihood, scaled alpha, and scale factors.
pub fn gaussian_forward(obs: &[f64], model: &GaussianHmm) -> StatsResult<GaussianForwardResult> {
    let t_len = obs.len();
    if t_len == 0 {
        return Err(StatsError::InsufficientData(
            "Observation sequence must not be empty".into(),
        ));
    }
    let n = model.model.n_states;

    let mut alpha = Array2::<f64>::zeros((t_len, n));
    let mut scales = Array1::<f64>::zeros(t_len);

    // Initialisation
    for i in 0..n {
        alpha[[0, i]] = model.model.initial[i] * model.emission_prob(i, obs[0]);
    }
    let c0 = alpha.row(0).sum();
    if c0 <= 0.0 {
        return Err(StatsError::ComputationError(
            "Gaussian forward: scale at t=0 is zero".into(),
        ));
    }
    for i in 0..n {
        alpha[[0, i]] /= c0;
    }
    scales[0] = c0;

    for t in 1..t_len {
        for j in 0..n {
            let mut sum = 0.0_f64;
            for i in 0..n {
                sum += alpha[[t - 1, i]] * model.model.transition[[i, j]];
            }
            alpha[[t, j]] = sum * model.emission_prob(j, obs[t]);
        }
        let ct = alpha.row(t).sum();
        if ct <= 0.0 {
            return Err(StatsError::ComputationError(format!(
                "Gaussian forward: scale at t={} is zero",
                t
            )));
        }
        for j in 0..n {
            alpha[[t, j]] /= ct;
        }
        scales[t] = ct;
    }

    let log_likelihood = scales.mapv(|c| c.ln()).sum();
    Ok(GaussianForwardResult {
        log_likelihood,
        alpha,
        scales,
    })
}

/// Viterbi decoding for a **Gaussian** HMM.
///
/// # Arguments
/// * `obs`   – continuous observation sequence.
/// * `model` – fitted `GaussianHmm`.
///
/// # Returns
/// Most-likely state sequence of length `T`.
pub fn gaussian_viterbi(obs: &[f64], model: &GaussianHmm) -> StatsResult<Vec<usize>> {
    let t_len = obs.len();
    if t_len == 0 {
        return Err(StatsError::InsufficientData(
            "Observation sequence must not be empty".into(),
        ));
    }
    let n = model.model.n_states;

    let log_a = model
        .model
        .transition
        .mapv(|v| if v > 0.0 { v.ln() } else { f64::NEG_INFINITY });
    let log_pi: Array1<f64> = model
        .model
        .initial
        .mapv(|v| if v > 0.0 { v.ln() } else { f64::NEG_INFINITY });

    let mut delta = Array2::<f64>::from_elem((t_len, n), f64::NEG_INFINITY);
    let mut psi = Array2::<usize>::zeros((t_len, n));

    for i in 0..n {
        let log_b = {
            let p = model.emission_prob(i, obs[0]);
            if p > 0.0 { p.ln() } else { f64::NEG_INFINITY }
        };
        delta[[0, i]] = log_pi[i] + log_b;
    }

    for t in 1..t_len {
        for j in 0..n {
            let log_b = {
                let p = model.emission_prob(j, obs[t]);
                if p > 0.0 { p.ln() } else { f64::NEG_INFINITY }
            };
            let mut best_val = f64::NEG_INFINITY;
            let mut best_i = 0;
            for i in 0..n {
                let val = delta[[t - 1, i]] + log_a[[i, j]];
                if val > best_val {
                    best_val = val;
                    best_i = i;
                }
            }
            delta[[t, j]] = best_val + log_b;
            psi[[t, j]] = best_i;
        }
    }

    let mut states = vec![0usize; t_len];
    let mut best_last = f64::NEG_INFINITY;
    for i in 0..n {
        if delta[[t_len - 1, i]] > best_last {
            best_last = delta[[t_len - 1, i]];
            states[t_len - 1] = i;
        }
    }
    for t in (0..t_len - 1).rev() {
        states[t] = psi[[t + 1, states[t + 1]]];
    }

    Ok(states)
}

// ─────────────────────────────────────────────────────────────────────────────
// Utility helpers (private)
// ─────────────────────────────────────────────────────────────────────────────

/// Build a row-stochastic matrix initialised to 1/cols + small noise.
fn uniform_stochastic_matrix(rows: usize, cols: usize) -> Array2<f64> {
    let base = 1.0 / cols as f64;
    let mut m = Array2::<f64>::from_elem((rows, cols), base);
    // Small deterministic perturbation based on position to break symmetry.
    for i in 0..rows {
        for j in 0..cols {
            let perturbation = 0.01 * ((i * cols + j) as f64 % 7.0 - 3.0) / 7.0;
            m[[i, j]] = (base + perturbation).max(1e-10);
        }
        // Re-normalise
        let row_sum: f64 = (0..cols).map(|j| m[[i, j]]).sum();
        for j in 0..cols {
            m[[i, j]] /= row_sum;
        }
    }
    m
}

/// Build a uniform probability vector of length `n`.
fn uniform_stochastic_vec(n: usize) -> Array1<f64> {
    Array1::<f64>::from_elem(n, 1.0 / n as f64)
}

/// Normalise a 1-D probability vector so its elements sum to 1.
fn normalise_vec(v: Array1<f64>) -> Array1<f64> {
    let s: f64 = v.sum();
    if s > 0.0 {
        v / s
    } else {
        Array1::<f64>::from_elem(v.len(), 1.0 / v.len() as f64)
    }
}

/// Normalise each row of a 2-D array so that each row sums to 1.
fn normalise_rows(mut m: Array2<f64>) -> Array2<f64> {
    let nrows = m.nrows();
    let ncols = m.ncols();
    for i in 0..nrows {
        let row_sum: f64 = (0..ncols).map(|j| m[[i, j]]).sum();
        if row_sum > 0.0 {
            for j in 0..ncols {
                m[[i, j]] /= row_sum;
            }
        } else {
            for j in 0..ncols {
                m[[i, j]] = 1.0 / ncols as f64;
            }
        }
    }
    m
}

// ─────────────────────────────────────────────────────────────────────────────
// Unit tests
// ─────────────────────────────────────────────────────────────────────────────

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

    /// Build a simple 2-state HMM with clearly separated emissions.
    fn simple_model() -> DiscreteHmm {
        // Transition: stays in same state with prob 0.9
        let a = array![[0.9, 0.1], [0.1, 0.9]];
        // Emission: state 0 emits {0,1}, state 1 emits {2,3}
        let b = array![[0.45, 0.45, 0.05, 0.05], [0.05, 0.05, 0.45, 0.45]];
        let pi = array![0.5, 0.5];
        DiscreteHmm::new(a, b, pi).expect("model construction failed")
    }

    #[test]
    fn test_forward_basic() {
        let model = simple_model();
        let obs = vec![0usize, 1, 0, 1];
        let res = forward_algorithm(&obs, &model).expect("forward failed");
        assert!(res.log_likelihood < 0.0);
        assert_eq!(res.alpha.nrows(), 4);
        assert_eq!(res.alpha.ncols(), 2);
        // Each row of alpha sums to ~1 (after scaling)
        for t in 0..4 {
            let row_sum: f64 = res.alpha.row(t).sum();
            assert!((row_sum - 1.0).abs() < 1e-10, "row {} sum = {}", t, row_sum);
        }
    }

    #[test]
    fn test_backward_shape() {
        let model = simple_model();
        let obs = vec![2usize, 3, 2, 3];
        let fwd = forward_algorithm(&obs, &model).expect("forward failed");
        let beta = backward_algorithm(&obs, &model, &fwd.scales).expect("backward failed");
        assert_eq!(beta.shape(), &[4, 2]);
    }

    #[test]
    fn test_viterbi_state1_sequence() {
        let model = simple_model();
        // Observations strongly associated with state 1
        let obs = vec![2usize, 3, 2, 3, 2];
        let states = viterbi(&obs, &model).expect("viterbi failed");
        assert_eq!(states.len(), 5);
        // Most states should be 1
        let n_state1 = states.iter().filter(|&&s| s == 1).count();
        assert!(n_state1 >= 3, "Expected mostly state 1, got {:?}", states);
    }

    #[test]
    fn test_posterior_state_probs() {
        let model = simple_model();
        let obs = vec![0usize, 0, 0, 2, 2, 2];
        let gamma = posterior_state_probs(&obs, &model).expect("posterior failed");
        assert_eq!(gamma.shape(), &[6, 2]);
        for t in 0..6 {
            let row_sum: f64 = gamma.row(t).sum();
            assert!((row_sum - 1.0).abs() < 1e-9);
        }
    }

    #[test]
    fn test_baum_welch_converges() {
        // Two sequences: first 5 obs from {0,1}, second 5 obs from {2,3}
        let seq1 = vec![0usize, 1, 0, 1, 0];
        let seq2 = vec![2usize, 3, 2, 3, 2];
        let result = baum_welch(&[seq1, seq2], 2, 4, 200, 1e-6).expect("baum_welch failed");
        assert!(result.log_likelihood.is_finite());
        assert!(result.n_iter >= 1);
        // After training, check that the emission matrix is not all-uniform
        let e = &result.model.model.emission;
        assert!(e[[0, 0]] > 0.0 || e[[1, 0]] > 0.0);
    }

    #[test]
    fn test_gaussian_hmm_emission() {
        let a = array![[0.9, 0.1], [0.1, 0.9]];
        let means = array![0.0, 5.0];
        let vars = array![1.0, 1.0];
        let pi = array![0.5, 0.5];
        let model = GaussianHmm::new(a, means, vars, pi).expect("gaussian hmm failed");

        // State 0: mean=0, var=1. PDF at 0.0 should be ~0.399
        let p0 = model.emission_prob(0, 0.0);
        assert!((p0 - 0.398_942_3).abs() < 1e-5, "p0 = {}", p0);

        // State 1: mean=5, var=1. PDF at 0.0 should be very small.
        let p1 = model.emission_prob(1, 0.0);
        assert!(p1 < 0.001, "p1 = {}", p1);
    }

    #[test]
    fn test_gaussian_viterbi() {
        let a = array![[0.9, 0.1], [0.1, 0.9]];
        let means = array![0.0, 5.0];
        let vars = array![0.5, 0.5];
        let pi = array![0.5, 0.5];
        let model = GaussianHmm::new(a, means, vars, pi).expect("gaussian hmm failed");
        // Observations near 0.0 should map to state 0
        let obs = vec![0.1_f64, -0.2, 0.3, -0.1, 0.2];
        let states = gaussian_viterbi(&obs, &model).expect("gaussian viterbi failed");
        let n_state0 = states.iter().filter(|&&s| s == 0).count();
        assert!(n_state0 >= 4, "Expected mostly state 0, got {:?}", states);
    }
}