sklears-dummy 0.1.2

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

use scirs2_core::ndarray::Array1;
use scirs2_core::random::prelude::*;
// Note: SliceRandom not available, will implement manually where needed
use sklears_core::error::Result;
use sklears_core::types::{Features, Float, Int};
use std::collections::HashMap;

/// Information-theoretic strategy selection
#[derive(Debug, Clone, PartialEq)]
pub enum InformationTheoreticStrategy {
    /// Maximum entropy principle
    MaximumEntropy,
    /// Minimum description length
    MinimumDescriptionLength,
    /// Mutual information maximization
    MutualInformation,
    /// Entropy-based sampling
    EntropySampling,
    /// Information gain optimization
    InformationGain,
}

/// Maximum entropy estimator following the principle of maximum entropy
#[derive(Debug, Clone)]
pub struct MaximumEntropyEstimator {
    /// Constraints for maximum entropy optimization
    pub constraints: Option<Array1<Float>>,
    /// Lagrange multipliers
    pub lagrange_multipliers_: Option<Array1<Float>>,
    /// Estimated maximum entropy distribution
    pub max_entropy_distribution_: Option<Array1<Float>>,
    /// Entropy value of the distribution
    pub entropy_: Option<Float>,
    /// Random state for reproducibility
    pub random_state: Option<u64>,
}

impl MaximumEntropyEstimator {
    /// Create new maximum entropy estimator
    pub fn new() -> Self {
        Self {
            constraints: None,
            lagrange_multipliers_: None,
            max_entropy_distribution_: None,
            entropy_: None,
            random_state: None,
        }
    }

    /// Set constraints for maximum entropy optimization
    pub fn with_constraints(mut self, constraints: Array1<Float>) -> Self {
        self.constraints = Some(constraints);
        self
    }

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

    /// Fit maximum entropy model
    pub fn fit_classification(&mut self, y: &Array1<Int>) -> Result<()> {
        let mut class_counts: HashMap<Int, usize> = HashMap::new();
        for &label in y.iter() {
            *class_counts.entry(label).or_insert(0) += 1;
        }

        let mut classes: Vec<Int> = class_counts.keys().copied().collect();
        classes.sort();
        let n_classes = classes.len();

        // If no constraints provided, use uniform distribution (max entropy)
        let distribution = if let Some(constraints) = self.constraints.clone() {
            if constraints.len() != n_classes {
                return Err(sklears_core::error::SklearsError::InvalidInput(
                    "Constraints length must match number of classes".to_string(),
                ));
            }

            // Optimize with constraints using method of Lagrange multipliers
            self.optimize_with_constraints(&constraints, &class_counts, &classes)?
        } else {
            // No constraints - uniform distribution maximizes entropy
            Array1::from_elem(n_classes, 1.0 / n_classes as Float)
        };

        // Compute entropy
        let entropy = self.compute_entropy(&distribution);

        self.max_entropy_distribution_ = Some(distribution);
        self.entropy_ = Some(entropy);
        Ok(())
    }

    /// Optimize maximum entropy distribution with constraints
    fn optimize_with_constraints(
        &mut self,
        constraints: &Array1<Float>,
        class_counts: &HashMap<Int, usize>,
        classes: &[Int],
    ) -> Result<Array1<Float>> {
        let n_classes = classes.len();

        // Use method of Lagrange multipliers
        // For simplicity, we'll use iterative scaling (not full optimization)
        let mut distribution = Array1::from_elem(n_classes, 1.0 / n_classes as Float);
        let max_iter = 100;
        let tolerance = 1e-6;

        for _iter in 0..max_iter {
            let old_dist = distribution.clone();

            // Update distribution to satisfy constraints
            for i in 0..n_classes {
                let observed_constraint = *class_counts
                    .get(&classes[i])
                    .expect("index should be valid")
                    as Float;
                if constraints[i] > 0.0 {
                    // Scale to match constraint
                    distribution[i] *= constraints[i] / observed_constraint.max(1e-10);
                }
            }

            // Normalize to maintain probability constraint
            let sum = distribution.sum();
            distribution = distribution.mapv(|x| x / sum);

            // Check convergence
            let diff: Float = (&distribution - &old_dist).mapv(|x| x.abs()).sum();
            if diff < tolerance {
                break;
            }
        }

        Ok(distribution)
    }

    /// Compute entropy of a distribution
    fn compute_entropy(&self, distribution: &Array1<Float>) -> Float {
        let mut entropy = 0.0;
        for &p in distribution.iter() {
            if p > 0.0 {
                entropy -= p * p.ln();
            }
        }
        entropy
    }

    /// Get maximum entropy distribution
    pub fn max_entropy_distribution(&self) -> Option<&Array1<Float>> {
        self.max_entropy_distribution_.as_ref()
    }

    /// Get entropy value
    pub fn entropy(&self) -> Option<Float> {
        self.entropy_
    }
}

/// Minimum Description Length (MDL) estimator
#[derive(Debug, Clone)]
pub struct MDLEstimator {
    /// Model complexity penalty factor
    pub complexity_penalty: Float,
    /// Selected model complexity
    pub model_complexity_: Option<Float>,
    /// Data likelihood
    pub data_likelihood_: Option<Float>,
    /// MDL score (description length)
    pub mdl_score_: Option<Float>,
    /// Optimal distribution
    pub optimal_distribution_: Option<Array1<Float>>,
    /// Random state
    pub random_state: Option<u64>,
}

impl MDLEstimator {
    /// Create new MDL estimator
    pub fn new() -> Self {
        Self {
            complexity_penalty: 1.0,
            model_complexity_: None,
            data_likelihood_: None,
            mdl_score_: None,
            optimal_distribution_: None,
            random_state: None,
        }
    }

    /// Set complexity penalty factor
    pub fn with_complexity_penalty(mut self, penalty: Float) -> Self {
        self.complexity_penalty = penalty;
        self
    }

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

    /// Fit MDL model
    pub fn fit_classification(&mut self, y: &Array1<Int>) -> Result<()> {
        let mut class_counts: HashMap<Int, usize> = HashMap::new();
        for &label in y.iter() {
            *class_counts.entry(label).or_insert(0) += 1;
        }

        let mut classes: Vec<Int> = class_counts.keys().copied().collect();
        classes.sort();
        let n_classes = classes.len();
        let n_samples = y.len() as Float;

        // Empirical distribution
        let empirical_dist: Array1<Float> = classes
            .iter()
            .map(|&class| {
                *class_counts.get(&class).expect("sampling should succeed") as Float / n_samples
            })
            .collect();

        // Compute data likelihood (negative log-likelihood)
        let mut data_likelihood = 0.0;
        for &prob in empirical_dist.iter() {
            if prob > 0.0 {
                data_likelihood -= prob * n_samples * prob.ln();
            }
        }

        // Compute model complexity (number of free parameters)
        let effective_params = (n_classes - 1) as Float; // Probability simplex has n-1 free parameters
        let model_complexity = self.complexity_penalty * effective_params * (n_samples.ln() / 2.0);

        // MDL score = data likelihood + model complexity
        let mdl_score = data_likelihood + model_complexity;

        // For baseline, use empirical distribution as optimal
        self.model_complexity_ = Some(model_complexity);
        self.data_likelihood_ = Some(data_likelihood);
        self.mdl_score_ = Some(mdl_score);
        self.optimal_distribution_ = Some(empirical_dist);
        Ok(())
    }

    /// Get model complexity
    pub fn model_complexity(&self) -> Option<Float> {
        self.model_complexity_
    }

    /// Get data likelihood
    pub fn data_likelihood(&self) -> Option<Float> {
        self.data_likelihood_
    }

    /// Get MDL score
    pub fn mdl_score(&self) -> Option<Float> {
        self.mdl_score_
    }

    /// Get optimal distribution
    pub fn optimal_distribution(&self) -> Option<&Array1<Float>> {
        self.optimal_distribution_.as_ref()
    }
}

/// Mutual Information baseline estimator
#[derive(Debug, Clone)]
pub struct MutualInformationEstimator {
    /// Number of bins for discretization
    pub n_bins: usize,
    /// Mutual information values per feature
    pub mutual_information_: Option<Array1<Float>>,
    /// Feature importance based on MI
    pub feature_importance_: Option<Array1<Float>>,
    /// Selected features based on MI threshold
    pub selected_features_: Option<Array1<usize>>,
    /// MI threshold for feature selection
    pub mi_threshold: Float,
    /// Random state
    pub random_state: Option<u64>,
}

impl MutualInformationEstimator {
    /// Create new mutual information estimator
    pub fn new() -> Self {
        Self {
            n_bins: 10,
            mutual_information_: None,
            feature_importance_: None,
            selected_features_: None,
            mi_threshold: 0.0,
            random_state: None,
        }
    }

    /// Set number of bins for discretization
    pub fn with_n_bins(mut self, n_bins: usize) -> Self {
        self.n_bins = n_bins;
        self
    }

    /// Set MI threshold for feature selection
    pub fn with_mi_threshold(mut self, threshold: Float) -> Self {
        self.mi_threshold = threshold;
        self
    }

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

    /// Fit mutual information model
    pub fn fit(&mut self, x: &Features, y: &Array1<Int>) -> Result<()> {
        if x.is_empty() || y.is_empty() {
            return Err(sklears_core::error::SklearsError::InvalidInput(
                "Input cannot be empty".to_string(),
            ));
        }

        if x.nrows() != y.len() {
            return Err(sklears_core::error::SklearsError::InvalidInput(
                "Number of samples in X and y must be equal".to_string(),
            ));
        }

        let n_features = x.ncols();
        let mut mi_values = Array1::<Float>::zeros(n_features);

        // Compute mutual information for each feature
        for feature_idx in 0..n_features {
            let feature_column = x.column(feature_idx);
            let mi = self.compute_mutual_information(&feature_column.to_owned(), y)?;
            mi_values[feature_idx] = mi;
        }

        // Normalize to get feature importance
        let max_mi = mi_values.iter().fold(0.0f64, |a, &b| a.max(b));
        let feature_importance = if max_mi > 0.0 {
            mi_values.mapv(|x| x / max_mi)
        } else {
            Array1::from_elem(n_features, 1.0 / n_features as Float)
        };

        // Select features above threshold
        let selected_features: Vec<usize> = mi_values
            .iter()
            .enumerate()
            .filter(|(_, &mi)| mi >= self.mi_threshold)
            .map(|(idx, _)| idx)
            .collect();

        self.mutual_information_ = Some(mi_values);
        self.feature_importance_ = Some(feature_importance);
        self.selected_features_ = Some(Array1::from_vec(selected_features));
        Ok(())
    }

    /// Compute mutual information between feature and target
    fn compute_mutual_information(
        &self,
        feature: &Array1<Float>,
        target: &Array1<Int>,
    ) -> Result<Float> {
        // Discretize continuous feature into bins
        let (feature_bins, _target_bins) = self.discretize_feature_target(feature, target)?;

        // Compute joint and marginal distributions
        let mut joint_counts: HashMap<(usize, Int), usize> = HashMap::new();
        let mut feature_counts: HashMap<usize, usize> = HashMap::new();
        let mut target_counts: HashMap<Int, usize> = HashMap::new();

        for (&f_bin, &t_val) in feature_bins.iter().zip(target.iter()) {
            *joint_counts.entry((f_bin, t_val)).or_insert(0) += 1;
            *feature_counts.entry(f_bin).or_insert(0) += 1;
            *target_counts.entry(t_val).or_insert(0) += 1;
        }

        let n_samples = feature.len() as Float;
        let mut mi = 0.0;

        // Compute MI = sum p(x,y) * log(p(x,y) / (p(x) * p(y)))
        for ((f_bin, t_val), &joint_count) in joint_counts.iter() {
            let p_joint = joint_count as Float / n_samples;
            let p_feature =
                *feature_counts.get(f_bin).expect("sampling should succeed") as Float / n_samples;
            let p_target =
                *target_counts.get(t_val).expect("sampling should succeed") as Float / n_samples;

            if p_joint > 0.0 && p_feature > 0.0 && p_target > 0.0 {
                mi += p_joint * (p_joint / (p_feature * p_target)).ln();
            }
        }

        Ok(mi)
    }

    /// Discretize feature into bins and return bin indices
    fn discretize_feature_target(
        &self,
        feature: &Array1<Float>,
        _target: &Array1<Int>,
    ) -> Result<(Array1<usize>, Array1<Int>)> {
        let n_samples = feature.len();

        // Find min and max for binning
        let min_val = feature.iter().fold(Float::INFINITY, |a, &b| a.min(b));
        let max_val = feature.iter().fold(Float::NEG_INFINITY, |a, &b| a.max(b));

        if min_val == max_val {
            // Constant feature - all samples in same bin
            return Ok((Array1::zeros(n_samples), Array1::from_vec(_target.to_vec())));
        }

        let bin_width = (max_val - min_val) / self.n_bins as Float;
        let mut feature_bins = Array1::<usize>::zeros(n_samples);

        for (i, &val) in feature.iter().enumerate() {
            let bin_idx = ((val - min_val) / bin_width) as usize;
            feature_bins[i] = bin_idx.min(self.n_bins - 1);
        }

        Ok((feature_bins, Array1::from_vec(_target.to_vec())))
    }

    /// Get mutual information values
    pub fn mutual_information(&self) -> Option<&Array1<Float>> {
        self.mutual_information_.as_ref()
    }

    /// Get feature importance
    pub fn feature_importance(&self) -> Option<&Array1<Float>> {
        self.feature_importance_.as_ref()
    }

    /// Get selected features
    pub fn selected_features(&self) -> Option<&Array1<usize>> {
        self.selected_features_.as_ref()
    }
}

/// Entropy-based sampling estimator
#[derive(Debug, Clone)]
pub struct EntropySamplingEstimator {
    /// Temperature parameter for entropy regularization
    pub temperature: Float,
    /// Entropy-regularized distribution
    pub entropy_distribution_: Option<Array1<Float>>,
    /// Entropy value
    pub entropy_: Option<Float>,
    /// Random state
    pub random_state: Option<u64>,
}

impl EntropySamplingEstimator {
    /// Create new entropy sampling estimator
    pub fn new() -> Self {
        Self {
            temperature: 1.0,
            entropy_distribution_: None,
            entropy_: None,
            random_state: None,
        }
    }

    /// Set temperature parameter
    pub fn with_temperature(mut self, temperature: Float) -> Self {
        self.temperature = temperature;
        self
    }

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

    /// Fit entropy sampling model
    pub fn fit_classification(&mut self, y: &Array1<Int>) -> Result<()> {
        let mut class_counts: HashMap<Int, usize> = HashMap::new();
        for &label in y.iter() {
            *class_counts.entry(label).or_insert(0) += 1;
        }

        let mut classes: Vec<Int> = class_counts.keys().copied().collect();
        classes.sort();
        let n_classes = classes.len();
        let n_samples = y.len() as Float;

        // Empirical distribution
        let empirical_probs: Array1<Float> = classes
            .iter()
            .map(|&class| {
                *class_counts.get(&class).expect("sampling should succeed") as Float / n_samples
            })
            .collect();

        // Apply temperature scaling for entropy regularization
        let entropy_probs = if self.temperature != 1.0 {
            // Temperature scaling: p_i^(1/T) / sum(p_j^(1/T))
            let scaled_probs: Array1<Float> = empirical_probs.mapv(|p| {
                if p > 0.0 {
                    p.powf(1.0 / self.temperature)
                } else {
                    0.0
                }
            });
            let sum = scaled_probs.sum();
            if sum > 0.0 {
                scaled_probs.mapv(|p| p / sum)
            } else {
                Array1::from_elem(n_classes, 1.0 / n_classes as Float)
            }
        } else {
            empirical_probs
        };

        // Compute entropy
        let entropy = self.compute_entropy(&entropy_probs);

        self.entropy_distribution_ = Some(entropy_probs);
        self.entropy_ = Some(entropy);
        Ok(())
    }

    /// Compute entropy of distribution
    fn compute_entropy(&self, distribution: &Array1<Float>) -> Float {
        let mut entropy = 0.0;
        for &p in distribution.iter() {
            if p > 0.0 {
                entropy -= p * p.ln();
            }
        }
        entropy
    }

    /// Sample from entropy-regularized distribution
    pub fn sample(&self, n_samples: usize) -> Result<Array1<Int>> {
        let distribution = self.entropy_distribution_.as_ref().ok_or_else(|| {
            sklears_core::error::SklearsError::InvalidInput("Model not fitted".to_string())
        })?;

        let mut rng = if let Some(seed) = self.random_state {
            StdRng::seed_from_u64(seed)
        } else {
            StdRng::seed_from_u64(0)
        };

        let cumulative: Vec<f64> = distribution
            .iter()
            .scan(0.0, |acc, &x| {
                *acc += x;
                Some(*acc)
            })
            .collect();

        let mut samples = Array1::<Int>::zeros(n_samples);
        for i in 0..n_samples {
            let rand_val: f64 = rng.random();
            let idx = cumulative
                .iter()
                .position(|&x| x >= rand_val)
                .unwrap_or(cumulative.len() - 1);
            samples[i] = idx as Int;
        }

        Ok(samples)
    }

    /// Get entropy distribution
    pub fn entropy_distribution(&self) -> Option<&Array1<Float>> {
        self.entropy_distribution_.as_ref()
    }

    /// Get entropy value
    pub fn entropy(&self) -> Option<Float> {
        self.entropy_
    }
}

/// Information Gain estimator for feature selection and prediction
#[derive(Debug, Clone)]
pub struct InformationGainEstimator {
    /// Number of bins for discretization
    pub n_bins: usize,
    /// Information gain values per feature
    pub information_gain_: Option<Array1<Float>>,
    /// Feature ranking based on information gain
    pub feature_ranking_: Option<Array1<usize>>,
    /// Threshold for feature selection
    pub ig_threshold: Float,
    /// Random state
    pub random_state: Option<u64>,
}

impl InformationGainEstimator {
    /// Create new information gain estimator
    pub fn new() -> Self {
        Self {
            n_bins: 10,
            information_gain_: None,
            feature_ranking_: None,
            ig_threshold: 0.0,
            random_state: None,
        }
    }

    /// Set number of bins
    pub fn with_n_bins(mut self, n_bins: usize) -> Self {
        self.n_bins = n_bins;
        self
    }

    /// Set IG threshold
    pub fn with_ig_threshold(mut self, threshold: Float) -> Self {
        self.ig_threshold = threshold;
        self
    }

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

    /// Fit information gain model
    pub fn fit(&mut self, x: &Features, y: &Array1<Int>) -> Result<()> {
        if x.is_empty() || y.is_empty() {
            return Err(sklears_core::error::SklearsError::InvalidInput(
                "Input cannot be empty".to_string(),
            ));
        }

        let n_features = x.ncols();
        let mut ig_values = Array1::<Float>::zeros(n_features);

        // Compute target entropy (H(Y))
        let target_entropy = self.compute_target_entropy(y);

        // Compute information gain for each feature
        for feature_idx in 0..n_features {
            let feature_column = x.column(feature_idx);
            let conditional_entropy =
                self.compute_conditional_entropy(&feature_column.to_owned(), y)?;
            ig_values[feature_idx] = target_entropy - conditional_entropy;
        }

        // Create feature ranking
        let mut indexed_ig: Vec<(usize, Float)> = ig_values
            .iter()
            .enumerate()
            .map(|(i, &ig)| (i, ig))
            .collect();
        indexed_ig.sort_by(|a, b| b.1.partial_cmp(&a.1).expect("operation should succeed")); // Sort by IG descending
        let feature_ranking: Array1<usize> = indexed_ig.iter().map(|(idx, _)| *idx).collect();

        self.information_gain_ = Some(ig_values);
        self.feature_ranking_ = Some(feature_ranking);
        Ok(())
    }

    /// Compute target entropy H(Y)
    fn compute_target_entropy(&self, target: &Array1<Int>) -> Float {
        let mut target_counts: HashMap<Int, usize> = HashMap::new();
        for &label in target.iter() {
            *target_counts.entry(label).or_insert(0) += 1;
        }

        let n_samples = target.len() as Float;
        let mut entropy = 0.0;

        for &count in target_counts.values() {
            let p = count as Float / n_samples;
            if p > 0.0 {
                entropy -= p * p.ln();
            }
        }

        entropy
    }

    /// Compute conditional entropy H(Y|X)
    fn compute_conditional_entropy(
        &self,
        feature: &Array1<Float>,
        target: &Array1<Int>,
    ) -> Result<Float> {
        // Discretize feature
        let feature_bins = self.discretize_feature(feature)?;

        // Compute conditional entropy
        let mut feature_bin_counts: HashMap<usize, usize> = HashMap::new();
        let mut conditional_counts: HashMap<(usize, Int), usize> = HashMap::new();

        for (&bin, &label) in feature_bins.iter().zip(target.iter()) {
            *feature_bin_counts.entry(bin).or_insert(0) += 1;
            *conditional_counts.entry((bin, label)).or_insert(0) += 1;
        }

        let n_samples = feature.len() as Float;
        let mut conditional_entropy = 0.0;

        for (&bin, &bin_count) in feature_bin_counts.iter() {
            let bin_prob = bin_count as Float / n_samples;
            let mut bin_entropy = 0.0;

            for &label in target.iter() {
                if let Some(&joint_count) = conditional_counts.get(&(bin, label)) {
                    let conditional_prob = joint_count as Float / bin_count as Float;
                    if conditional_prob > 0.0 {
                        bin_entropy -= conditional_prob * conditional_prob.ln();
                    }
                }
            }

            conditional_entropy += bin_prob * bin_entropy;
        }

        Ok(conditional_entropy)
    }

    /// Discretize feature into bins
    fn discretize_feature(&self, feature: &Array1<Float>) -> Result<Array1<usize>> {
        let n_samples = feature.len();
        let min_val = feature.iter().fold(Float::INFINITY, |a, &b| a.min(b));
        let max_val = feature.iter().fold(Float::NEG_INFINITY, |a, &b| a.max(b));

        if min_val == max_val {
            return Ok(Array1::zeros(n_samples));
        }

        let bin_width = (max_val - min_val) / self.n_bins as Float;
        let mut bins = Array1::<usize>::zeros(n_samples);

        for (i, &val) in feature.iter().enumerate() {
            let bin_idx = ((val - min_val) / bin_width) as usize;
            bins[i] = bin_idx.min(self.n_bins - 1);
        }

        Ok(bins)
    }

    /// Get information gain values
    pub fn information_gain(&self) -> Option<&Array1<Float>> {
        self.information_gain_.as_ref()
    }

    /// Get feature ranking
    pub fn feature_ranking(&self) -> Option<&Array1<usize>> {
        self.feature_ranking_.as_ref()
    }
}

/// Default implementations
impl Default for MaximumEntropyEstimator {
    fn default() -> Self {
        Self::new()
    }
}

impl Default for MDLEstimator {
    fn default() -> Self {
        Self::new()
    }
}

impl Default for MutualInformationEstimator {
    fn default() -> Self {
        Self::new()
    }
}

impl Default for EntropySamplingEstimator {
    fn default() -> Self {
        Self::new()
    }
}

impl Default for InformationGainEstimator {
    fn default() -> Self {
        Self::new()
    }
}

#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_abs_diff_eq;
    use scirs2_core::ndarray::{array, Array2};

    #[test]
    fn test_maximum_entropy_basic() {
        let y = array![0, 0, 0, 1, 1, 2]; // 3 classes
        let mut estimator = MaximumEntropyEstimator::new().with_random_state(42);

        let result = estimator.fit_classification(&y);
        assert!(result.is_ok());

        let distribution = estimator
            .max_entropy_distribution()
            .expect("operation should succeed");
        assert_eq!(distribution.len(), 3);

        // Distribution should sum to 1
        let sum: Float = distribution.sum();
        assert_abs_diff_eq!(sum, 1.0, epsilon = 1e-10);

        let entropy = estimator.entropy().expect("operation should succeed");
        assert!(entropy >= 0.0);
    }

    #[test]
    fn test_maximum_entropy_with_constraints() {
        let y = array![0, 0, 1, 1];
        let constraints = array![0.6, 0.4]; // Favor class 0

        let mut estimator = MaximumEntropyEstimator::new()
            .with_constraints(constraints)
            .with_random_state(42);

        let result = estimator.fit_classification(&y);
        assert!(result.is_ok());

        let distribution = estimator
            .max_entropy_distribution()
            .expect("operation should succeed");
        assert_eq!(distribution.len(), 2);

        // Should be close to constraints
        assert!(distribution[0] > distribution[1]);
    }

    #[test]
    fn test_mdl_estimator_basic() {
        let y = array![0, 0, 0, 1, 1, 2];
        let mut estimator = MDLEstimator::new()
            .with_complexity_penalty(0.5)
            .with_random_state(42);

        let result = estimator.fit_classification(&y);
        assert!(result.is_ok());

        assert!(estimator.model_complexity().is_some());
        assert!(estimator.data_likelihood().is_some());
        assert!(estimator.mdl_score().is_some());

        let distribution = estimator
            .optimal_distribution()
            .expect("operation should succeed");
        assert_eq!(distribution.len(), 3);

        let sum: Float = distribution.sum();
        assert_abs_diff_eq!(sum, 1.0, epsilon = 1e-10);
    }

    #[test]
    fn test_mutual_information_estimator() {
        let x = Array2::from_shape_vec(
            (6, 2),
            vec![
                1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
            ],
        )
        .expect("operation should succeed");
        let y = array![0, 0, 0, 1, 1, 1];

        let mut estimator = MutualInformationEstimator::new()
            .with_n_bins(3)
            .with_mi_threshold(0.1)
            .with_random_state(42);

        let result = estimator.fit(&x, &y);
        assert!(result.is_ok());

        let mi_values = estimator
            .mutual_information()
            .expect("operation should succeed");
        assert_eq!(mi_values.len(), 2);

        // All MI values should be non-negative
        for &mi in mi_values.iter() {
            assert!(mi >= 0.0);
        }

        let importance = estimator
            .feature_importance()
            .expect("operation should succeed");
        assert_eq!(importance.len(), 2);

        let selected = estimator
            .selected_features()
            .expect("operation should succeed");
        assert!(selected.len() <= 2);
    }

    #[test]
    fn test_entropy_sampling_estimator() {
        let y = array![0, 0, 0, 1, 1, 2];
        let mut estimator = EntropySamplingEstimator::new()
            .with_temperature(0.5)
            .with_random_state(42);

        let result = estimator.fit_classification(&y);
        assert!(result.is_ok());

        let distribution = estimator
            .entropy_distribution()
            .expect("operation should succeed");
        assert_eq!(distribution.len(), 3);

        let sum: Float = distribution.sum();
        assert_abs_diff_eq!(sum, 1.0, epsilon = 1e-10);

        let entropy = estimator.entropy().expect("operation should succeed");
        assert!(entropy >= 0.0);

        // Test sampling
        let samples = estimator.sample(10).expect("sampling should succeed");
        assert_eq!(samples.len(), 10);

        // All samples should be valid class labels
        for &sample in samples.iter() {
            assert!((0..=2).contains(&sample));
        }
    }

    #[test]
    fn test_information_gain_estimator() {
        let x = Array2::from_shape_vec(
            (6, 2),
            vec![
                1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
            ],
        )
        .expect("operation should succeed");
        let y = array![0, 0, 0, 1, 1, 1];

        let mut estimator = InformationGainEstimator::new()
            .with_n_bins(3)
            .with_ig_threshold(0.1)
            .with_random_state(42);

        let result = estimator.fit(&x, &y);
        assert!(result.is_ok());

        let ig_values = estimator
            .information_gain()
            .expect("operation should succeed");
        assert_eq!(ig_values.len(), 2);

        // Information gain should be non-negative
        for &ig in ig_values.iter() {
            assert!(ig >= 0.0);
        }

        let ranking = estimator
            .feature_ranking()
            .expect("operation should succeed");
        assert_eq!(ranking.len(), 2);

        // Ranking should contain valid feature indices
        for &rank in ranking.iter() {
            assert!(rank < 2);
        }
    }
}