sklears-tree 0.2.0

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

use scirs2_core::ndarray::{Array1, Array2, ArrayView1, Axis};
use sklears_core::error::{Result, SklearsError};
use std::collections::HashMap;

/// Protected attribute information
#[derive(Debug, Clone)]
pub struct ProtectedAttribute {
    /// Name of the protected attribute
    pub name: String,
    /// Column index in the dataset
    pub column_index: usize,
    /// Unique values for categorical attributes
    pub values: Vec<String>,
    /// Whether this is a binary attribute
    pub is_binary: bool,
    /// Privileged group (for binary attributes)
    pub privileged_group: Option<String>,
}

impl ProtectedAttribute {
    /// Create a new protected attribute
    pub fn new(
        name: String,
        column_index: usize,
        values: Vec<String>,
        privileged_group: Option<String>,
    ) -> Self {
        let is_binary = values.len() == 2;
        Self {
            name,
            column_index,
            values,
            is_binary,
            privileged_group,
        }
    }

    /// Create a binary protected attribute
    pub fn binary(
        name: String,
        column_index: usize,
        privileged_group: String,
        unprivileged_group: String,
    ) -> Self {
        Self {
            name,
            column_index,
            values: vec![privileged_group.clone(), unprivileged_group],
            is_binary: true,
            privileged_group: Some(privileged_group),
        }
    }

    /// Check if a value belongs to the privileged group
    pub fn is_privileged(&self, value: &str) -> bool {
        if let Some(ref priv_group) = self.privileged_group {
            value == priv_group
        } else {
            false
        }
    }
}

/// Fairness metrics for evaluating bias
#[derive(Debug, Clone)]
pub struct FairnessMetrics {
    /// Demographic parity (statistical parity)
    pub demographic_parity: f64,
    /// Equalized odds
    pub equalized_odds: f64,
    /// Equal opportunity
    pub equal_opportunity: f64,
    /// Disparate impact ratio
    pub disparate_impact: f64,
    /// Calibration difference
    pub calibration: f64,
    /// Individual fairness measure
    pub individual_fairness: f64,
}

impl FairnessMetrics {
    /// Calculate fairness metrics for predictions
    pub fn calculate(
        y_true: &Array1<i32>,
        y_pred: &Array1<i32>,
        y_prob: Option<&Array2<f64>>,
        protected_attr: &Array1<i32>,
        privileged_group: i32,
    ) -> Result<Self> {
        let demographic_parity =
            Self::calculate_demographic_parity(y_pred, protected_attr, privileged_group)?;
        let equalized_odds =
            Self::calculate_equalized_odds(y_true, y_pred, protected_attr, privileged_group)?;
        let equal_opportunity =
            Self::calculate_equal_opportunity(y_true, y_pred, protected_attr, privileged_group)?;
        let disparate_impact =
            Self::calculate_disparate_impact(y_pred, protected_attr, privileged_group)?;

        let calibration = if let Some(probs) = y_prob {
            Self::calculate_calibration(y_true, probs, protected_attr, privileged_group)?
        } else {
            0.0
        };

        let individual_fairness = Self::calculate_individual_fairness(y_pred, protected_attr)?;

        Ok(Self {
            demographic_parity,
            equalized_odds,
            equal_opportunity,
            disparate_impact,
            calibration,
            individual_fairness,
        })
    }

    /// Calculate demographic parity (difference in positive prediction rates)
    fn calculate_demographic_parity(
        y_pred: &Array1<i32>,
        protected_attr: &Array1<i32>,
        privileged_group: i32,
    ) -> Result<f64> {
        let privileged_mask = protected_attr.mapv(|x| x == privileged_group);
        let unprivileged_mask = protected_attr.mapv(|x| x != privileged_group);

        let privileged_positive_rate = Self::positive_rate(y_pred, &privileged_mask);
        let unprivileged_positive_rate = Self::positive_rate(y_pred, &unprivileged_mask);

        Ok((privileged_positive_rate - unprivileged_positive_rate).abs())
    }

    /// Calculate equalized odds (difference in TPR and FPR)
    fn calculate_equalized_odds(
        y_true: &Array1<i32>,
        y_pred: &Array1<i32>,
        protected_attr: &Array1<i32>,
        privileged_group: i32,
    ) -> Result<f64> {
        let privileged_mask = protected_attr.mapv(|x| x == privileged_group);
        let unprivileged_mask = protected_attr.mapv(|x| x != privileged_group);

        let (priv_tpr, priv_fpr) = Self::calculate_tpr_fpr(y_true, y_pred, &privileged_mask);
        let (unpriv_tpr, unpriv_fpr) = Self::calculate_tpr_fpr(y_true, y_pred, &unprivileged_mask);

        let tpr_diff = (priv_tpr - unpriv_tpr).abs();
        let fpr_diff = (priv_fpr - unpriv_fpr).abs();

        Ok((tpr_diff + fpr_diff) / 2.0)
    }

    /// Calculate equal opportunity (difference in TPR only)
    fn calculate_equal_opportunity(
        y_true: &Array1<i32>,
        y_pred: &Array1<i32>,
        protected_attr: &Array1<i32>,
        privileged_group: i32,
    ) -> Result<f64> {
        let privileged_mask = protected_attr.mapv(|x| x == privileged_group);
        let unprivileged_mask = protected_attr.mapv(|x| x != privileged_group);

        let (priv_tpr, _) = Self::calculate_tpr_fpr(y_true, y_pred, &privileged_mask);
        let (unpriv_tpr, _) = Self::calculate_tpr_fpr(y_true, y_pred, &unprivileged_mask);

        Ok((priv_tpr - unpriv_tpr).abs())
    }

    /// Calculate disparate impact ratio
    fn calculate_disparate_impact(
        y_pred: &Array1<i32>,
        protected_attr: &Array1<i32>,
        privileged_group: i32,
    ) -> Result<f64> {
        let privileged_mask = protected_attr.mapv(|x| x == privileged_group);
        let unprivileged_mask = protected_attr.mapv(|x| x != privileged_group);

        let privileged_positive_rate = Self::positive_rate(y_pred, &privileged_mask);
        let unprivileged_positive_rate = Self::positive_rate(y_pred, &unprivileged_mask);

        if privileged_positive_rate > 0.0 {
            Ok(unprivileged_positive_rate / privileged_positive_rate)
        } else {
            Ok(1.0)
        }
    }

    /// Calculate calibration difference
    fn calculate_calibration(
        y_true: &Array1<i32>,
        y_prob: &Array2<f64>,
        protected_attr: &Array1<i32>,
        privileged_group: i32,
    ) -> Result<f64> {
        let privileged_mask = protected_attr.mapv(|x| x == privileged_group);
        let unprivileged_mask = protected_attr.mapv(|x| x != privileged_group);

        let priv_calibration = Self::calculate_group_calibration(y_true, y_prob, &privileged_mask);
        let unpriv_calibration =
            Self::calculate_group_calibration(y_true, y_prob, &unprivileged_mask);

        Ok((priv_calibration - unpriv_calibration).abs())
    }

    /// Calculate individual fairness (consistency across similar individuals)
    fn calculate_individual_fairness(
        y_pred: &Array1<i32>,
        protected_attr: &Array1<i32>,
    ) -> Result<f64> {
        // Simplified individual fairness: variance in predictions within groups
        let privileged_mask = protected_attr.mapv(|x| x == 1); // Assume binary

        let priv_predictions: Vec<f64> = y_pred
            .iter()
            .zip(privileged_mask.iter())
            .filter(|(_, &is_priv)| is_priv)
            .map(|(&pred, _)| pred as f64)
            .collect();

        let unpriv_predictions: Vec<f64> = y_pred
            .iter()
            .zip(privileged_mask.iter())
            .filter(|(_, &is_priv)| !is_priv)
            .map(|(&pred, _)| pred as f64)
            .collect();

        let priv_var = Self::calculate_variance(&priv_predictions);
        let unpriv_var = Self::calculate_variance(&unpriv_predictions);

        Ok((priv_var - unpriv_var).abs())
    }

    /// Helper: Calculate positive prediction rate
    fn positive_rate(y_pred: &Array1<i32>, mask: &Array1<bool>) -> f64 {
        let total: i32 = mask.iter().map(|&x| if x { 1 } else { 0 }).sum();
        if total == 0 {
            return 0.0;
        }

        let positive: i32 = y_pred
            .iter()
            .zip(mask.iter())
            .map(|(&pred, &include)| if include && pred > 0 { 1 } else { 0 })
            .sum();

        positive as f64 / total as f64
    }

    /// Helper: Calculate TPR and FPR for a group
    fn calculate_tpr_fpr(
        y_true: &Array1<i32>,
        y_pred: &Array1<i32>,
        mask: &Array1<bool>,
    ) -> (f64, f64) {
        let mut tp = 0;
        let mut fp = 0;
        let mut tn = 0;
        let mut fn_count = 0;

        for ((&true_label, &pred_label), &include) in
            y_true.iter().zip(y_pred.iter()).zip(mask.iter())
        {
            if include {
                match (true_label > 0, pred_label > 0) {
                    (true, true) => tp += 1,
                    (false, true) => fp += 1,
                    (false, false) => tn += 1,
                    (true, false) => fn_count += 1,
                }
            }
        }

        let tpr = if tp + fn_count > 0 {
            tp as f64 / (tp + fn_count) as f64
        } else {
            0.0
        };

        let fpr = if fp + tn > 0 {
            fp as f64 / (fp + tn) as f64
        } else {
            0.0
        };

        (tpr, fpr)
    }

    /// Helper: Calculate calibration for a group
    fn calculate_group_calibration(
        y_true: &Array1<i32>,
        y_prob: &Array2<f64>,
        mask: &Array1<bool>,
    ) -> f64 {
        // Simplified calibration: mean absolute difference between predicted and actual rates
        let mut calibration_error = 0.0;
        let mut count = 0;

        for ((&true_label, prob_row), &include) in y_true
            .iter()
            .zip(y_prob.axis_iter(Axis(0)))
            .zip(mask.iter())
        {
            if include {
                let predicted_prob = prob_row[1]; // Assume binary classification
                let actual = if true_label > 0 { 1.0 } else { 0.0 };
                calibration_error += (predicted_prob - actual).abs();
                count += 1;
            }
        }

        if count > 0 {
            calibration_error / count as f64
        } else {
            0.0
        }
    }

    /// Helper: Calculate variance
    fn calculate_variance(values: &[f64]) -> f64 {
        if values.is_empty() {
            return 0.0;
        }

        let mean = values.iter().sum::<f64>() / values.len() as f64;
        let variance =
            values.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / values.len() as f64;

        variance
    }
}

/// Fairness constraints for tree construction
#[derive(Debug, Clone)]
pub struct FairnessConstraints {
    /// Maximum allowed demographic parity violation
    pub max_demographic_parity: f64,
    /// Maximum allowed equalized odds violation
    pub max_equalized_odds: f64,
    /// Minimum required disparate impact ratio
    pub min_disparate_impact: f64,
    /// Protected attributes to consider
    pub protected_attributes: Vec<ProtectedAttribute>,
    /// Fairness regularization weight
    pub fairness_weight: f64,
    /// Enable pre-processing fairness
    pub enable_preprocessing: bool,
    /// Enable in-processing fairness
    pub enable_inprocessing: bool,
    /// Enable post-processing fairness
    pub enable_postprocessing: bool,
}

impl Default for FairnessConstraints {
    fn default() -> Self {
        Self {
            max_demographic_parity: 0.1,
            max_equalized_odds: 0.1,
            min_disparate_impact: 0.8,
            protected_attributes: Vec::new(),
            fairness_weight: 0.1,
            enable_preprocessing: true,
            enable_inprocessing: true,
            enable_postprocessing: false,
        }
    }
}

/// Fair splitting criterion that considers both accuracy and fairness
#[derive(Debug, Clone)]
pub struct FairSplittingCriterion {
    /// Standard splitting criterion (gini, entropy, etc.)
    pub base_criterion: SplitCriterion,
    /// Fairness constraints
    pub fairness_constraints: FairnessConstraints,
    /// Weight for fairness vs accuracy trade-off
    pub fairness_weight: f64,
}

#[derive(Debug, Clone)]
pub enum SplitCriterion {
    Gini,
    Entropy,
    MSE,
}

impl FairSplittingCriterion {
    /// Create a new fair splitting criterion
    pub fn new(base_criterion: SplitCriterion, fairness_constraints: FairnessConstraints) -> Self {
        let fairness_weight = fairness_constraints.fairness_weight;
        Self {
            base_criterion,
            fairness_constraints,
            fairness_weight,
        }
    }

    /// Evaluate a split considering both accuracy and fairness
    pub fn evaluate_split(
        &self,
        left_y: &Array1<i32>,
        right_y: &Array1<i32>,
        left_protected: &Array1<i32>,
        right_protected: &Array1<i32>,
        protected_attr: &ProtectedAttribute,
    ) -> Result<f64> {
        // Calculate standard impurity
        let left_impurity = self.calculate_impurity(left_y)?;
        let right_impurity = self.calculate_impurity(right_y)?;

        let n_left = left_y.len() as f64;
        let n_right = right_y.len() as f64;
        let n_total = n_left + n_right;

        let weighted_impurity =
            (n_left / n_total) * left_impurity + (n_right / n_total) * right_impurity;

        // Calculate fairness penalty
        let fairness_penalty = self.calculate_fairness_penalty(
            left_y,
            right_y,
            left_protected,
            right_protected,
            protected_attr,
        )?;

        // Combine accuracy and fairness
        let score = weighted_impurity + self.fairness_weight * fairness_penalty;

        Ok(score)
    }

    /// Calculate impurity based on the base criterion
    fn calculate_impurity(&self, y: &Array1<i32>) -> Result<f64> {
        if y.is_empty() {
            return Ok(0.0);
        }

        match &self.base_criterion {
            SplitCriterion::Gini => Ok(self.gini_impurity(y)),
            SplitCriterion::Entropy => Ok(self.entropy_impurity(y)),
            SplitCriterion::MSE => Ok(self.mse_impurity(y)),
        }
    }

    /// Calculate Gini impurity
    fn gini_impurity(&self, y: &Array1<i32>) -> f64 {
        let mut class_counts = HashMap::new();
        for &label in y.iter() {
            *class_counts.entry(label).or_insert(0) += 1;
        }

        let n = y.len() as f64;
        let mut gini = 1.0;

        for &count in class_counts.values() {
            let p = count as f64 / n;
            gini -= p * p;
        }

        gini
    }

    /// Calculate entropy impurity
    fn entropy_impurity(&self, y: &Array1<i32>) -> f64 {
        let mut class_counts = HashMap::new();
        for &label in y.iter() {
            *class_counts.entry(label).or_insert(0) += 1;
        }

        let n = y.len() as f64;
        let mut entropy = 0.0;

        for &count in class_counts.values() {
            if count > 0 {
                let p = count as f64 / n;
                entropy -= p * p.log2();
            }
        }

        entropy
    }

    /// Calculate MSE impurity (for regression)
    fn mse_impurity(&self, y: &Array1<i32>) -> f64 {
        if y.is_empty() {
            return 0.0;
        }

        let mean = y.iter().map(|&x| x as f64).sum::<f64>() / y.len() as f64;
        let mse = y.iter().map(|&x| (x as f64 - mean).powi(2)).sum::<f64>() / y.len() as f64;

        mse
    }

    /// Calculate fairness penalty for a split
    fn calculate_fairness_penalty(
        &self,
        left_y: &Array1<i32>,
        right_y: &Array1<i32>,
        left_protected: &Array1<i32>,
        right_protected: &Array1<i32>,
        protected_attr: &ProtectedAttribute,
    ) -> Result<f64> {
        let privileged_value = if let Some(ref priv_group) = protected_attr.privileged_group {
            // Convert string to int (simplified)
            if priv_group == "1" || priv_group.to_lowercase() == "true" {
                1
            } else {
                0
            }
        } else {
            1 // Default privileged group
        };

        // Calculate demographic parity violation for left split
        let left_dp = if !left_y.is_empty() {
            FairnessMetrics::calculate_demographic_parity(left_y, left_protected, privileged_value)
                .unwrap_or(0.0)
        } else {
            0.0
        };

        // Calculate demographic parity violation for right split
        let right_dp = if !right_y.is_empty() {
            FairnessMetrics::calculate_demographic_parity(
                right_y,
                right_protected,
                privileged_value,
            )
            .unwrap_or(0.0)
        } else {
            0.0
        };

        // Calculate weighted fairness penalty
        let n_left = left_y.len() as f64;
        let n_right = right_y.len() as f64;
        let n_total = n_left + n_right;

        if n_total > 0.0 {
            let weighted_penalty = (n_left / n_total) * left_dp + (n_right / n_total) * right_dp;
            Ok(weighted_penalty)
        } else {
            Ok(0.0)
        }
    }
}

/// Fair decision tree builder
pub struct FairDecisionTreeBuilder {
    /// Maximum tree depth
    pub max_depth: Option<usize>,
    /// Minimum samples per split
    pub min_samples_split: usize,
    /// Minimum samples per leaf
    pub min_samples_leaf: usize,
    /// Fair splitting criterion
    pub criterion: FairSplittingCriterion,
    /// Random seed
    pub random_state: Option<u64>,
}

impl FairDecisionTreeBuilder {
    /// Create a new fair decision tree builder
    pub fn new(criterion: FairSplittingCriterion) -> Self {
        Self {
            max_depth: None,
            min_samples_split: 2,
            min_samples_leaf: 1,
            criterion,
            random_state: None,
        }
    }

    /// Build a fair decision tree
    pub fn fit(
        &self,
        x: &Array2<f64>,
        y: &Array1<i32>,
        protected_attrs: &Array2<i32>,
    ) -> Result<FairDecisionTree> {
        let n_samples = x.nrows();
        let sample_indices: Vec<usize> = (0..n_samples).collect();

        let root = self.build_tree_recursive(x, y, protected_attrs, &sample_indices, 0)?;

        Ok(FairDecisionTree {
            root,
            feature_importances: self.calculate_feature_importances(x, y)?,
            fairness_metrics: None,
            protected_attributes: self
                .criterion
                .fairness_constraints
                .protected_attributes
                .clone(),
        })
    }

    /// Recursively build the tree
    fn build_tree_recursive(
        &self,
        x: &Array2<f64>,
        y: &Array1<i32>,
        protected_attrs: &Array2<i32>,
        sample_indices: &[usize],
        depth: usize,
    ) -> Result<FairTreeNode> {
        let n_samples = sample_indices.len();

        // Check stopping criteria
        if self.should_stop(n_samples, depth, y, sample_indices) {
            return self.create_leaf_node(y, sample_indices);
        }

        // Find best fair split
        let best_split = self.find_best_fair_split(x, y, protected_attrs, sample_indices)?;

        if let Some((feature_idx, threshold, left_indices, right_indices)) = best_split {
            // Recursively build left and right subtrees
            let left_child = Box::new(self.build_tree_recursive(
                x,
                y,
                protected_attrs,
                &left_indices,
                depth + 1,
            )?);

            let right_child = Box::new(self.build_tree_recursive(
                x,
                y,
                protected_attrs,
                &right_indices,
                depth + 1,
            )?);

            Ok(FairTreeNode {
                is_leaf: false,
                feature_idx: Some(feature_idx),
                threshold: Some(threshold),
                prediction: None,
                left_child: Some(left_child),
                right_child: Some(right_child),
                sample_count: n_samples,
                fairness_score: 0.0,
            })
        } else {
            // No valid split found, create leaf
            self.create_leaf_node(y, sample_indices)
        }
    }

    /// Check if we should stop splitting
    fn should_stop(
        &self,
        n_samples: usize,
        depth: usize,
        y: &Array1<i32>,
        sample_indices: &[usize],
    ) -> bool {
        // Check minimum samples
        if n_samples < self.min_samples_split {
            return true;
        }

        // Check maximum depth
        if let Some(max_depth) = self.max_depth {
            if depth >= max_depth {
                return true;
            }
        }

        // Check if all samples have the same label
        let first_label = y[sample_indices[0]];
        if sample_indices.iter().all(|&idx| y[idx] == first_label) {
            return true;
        }

        false
    }

    /// Create a leaf node
    fn create_leaf_node(&self, y: &Array1<i32>, sample_indices: &[usize]) -> Result<FairTreeNode> {
        // Calculate majority class
        let mut class_counts = HashMap::new();
        for &idx in sample_indices {
            *class_counts.entry(y[idx]).or_insert(0) += 1;
        }

        let prediction = class_counts
            .iter()
            .max_by_key(|(_, &count)| count)
            .map(|(&class, _)| class)
            .unwrap_or(0);

        Ok(FairTreeNode {
            is_leaf: true,
            feature_idx: None,
            threshold: None,
            prediction: Some(prediction),
            left_child: None,
            right_child: None,
            sample_count: sample_indices.len(),
            fairness_score: 0.0,
        })
    }

    /// Find the best fair split
    fn find_best_fair_split(
        &self,
        x: &Array2<f64>,
        y: &Array1<i32>,
        protected_attrs: &Array2<i32>,
        sample_indices: &[usize],
    ) -> Result<Option<(usize, f64, Vec<usize>, Vec<usize>)>> {
        let n_features = x.ncols();
        let mut best_score = f64::INFINITY;
        let mut best_split = None;

        for feature_idx in 0..n_features {
            // Get unique values for this feature
            let mut feature_values: Vec<f64> = sample_indices
                .iter()
                .map(|&idx| x[[idx, feature_idx]])
                .collect();
            feature_values.sort_by(|a, b| a.partial_cmp(b).expect("operation should succeed"));
            feature_values.dedup_by(|a, b| (*a - *b).abs() < 1e-10);

            // Try different thresholds
            for i in 0..feature_values.len() - 1 {
                let threshold = (feature_values[i] + feature_values[i + 1]) / 2.0;

                // Split samples
                let (left_indices, right_indices) =
                    self.split_samples(x, sample_indices, feature_idx, threshold);

                // Check minimum samples constraint
                if left_indices.len() < self.min_samples_leaf
                    || right_indices.len() < self.min_samples_leaf
                {
                    continue;
                }

                // Evaluate split fairness
                let score = self.evaluate_split_fairness(
                    y,
                    protected_attrs,
                    &left_indices,
                    &right_indices,
                )?;

                if score < best_score {
                    best_score = score;
                    best_split = Some((feature_idx, threshold, left_indices, right_indices));
                }
            }
        }

        Ok(best_split)
    }

    /// Split samples based on feature and threshold
    fn split_samples(
        &self,
        x: &Array2<f64>,
        sample_indices: &[usize],
        feature_idx: usize,
        threshold: f64,
    ) -> (Vec<usize>, Vec<usize>) {
        let mut left_indices = Vec::new();
        let mut right_indices = Vec::new();

        for &idx in sample_indices {
            if x[[idx, feature_idx]] <= threshold {
                left_indices.push(idx);
            } else {
                right_indices.push(idx);
            }
        }

        (left_indices, right_indices)
    }

    /// Evaluate split fairness
    fn evaluate_split_fairness(
        &self,
        y: &Array1<i32>,
        protected_attrs: &Array2<i32>,
        left_indices: &[usize],
        right_indices: &[usize],
    ) -> Result<f64> {
        // Extract labels for left and right splits
        let left_y: Array1<i32> = left_indices.iter().map(|&idx| y[idx]).collect();
        let right_y: Array1<i32> = right_indices.iter().map(|&idx| y[idx]).collect();

        // For simplicity, use first protected attribute
        if !self
            .criterion
            .fairness_constraints
            .protected_attributes
            .is_empty()
        {
            let protected_attr = &self.criterion.fairness_constraints.protected_attributes[0];
            let attr_col = protected_attr.column_index;

            let left_protected: Array1<i32> = left_indices
                .iter()
                .map(|&idx| protected_attrs[[idx, attr_col]])
                .collect();
            let right_protected: Array1<i32> = right_indices
                .iter()
                .map(|&idx| protected_attrs[[idx, attr_col]])
                .collect();

            self.criterion.evaluate_split(
                &left_y,
                &right_y,
                &left_protected,
                &right_protected,
                protected_attr,
            )
        } else {
            // No protected attributes, use standard impurity
            let left_impurity = self.criterion.calculate_impurity(&left_y)?;
            let right_impurity = self.criterion.calculate_impurity(&right_y)?;

            let n_left = left_y.len() as f64;
            let n_right = right_y.len() as f64;
            let n_total = n_left + n_right;

            Ok((n_left / n_total) * left_impurity + (n_right / n_total) * right_impurity)
        }
    }

    /// Calculate feature importances
    fn calculate_feature_importances(
        &self,
        x: &Array2<f64>,
        y: &Array1<i32>,
    ) -> Result<Array1<f64>> {
        let n_features = x.ncols();
        Ok(Array1::ones(n_features) / n_features as f64) // Placeholder
    }
}

/// Fair decision tree node
#[derive(Debug, Clone)]
pub struct FairTreeNode {
    /// Whether this is a leaf node
    pub is_leaf: bool,
    /// Feature index for splitting (None for leaf nodes)
    pub feature_idx: Option<usize>,
    /// Threshold for splitting (None for leaf nodes)
    pub threshold: Option<f64>,
    /// Prediction value (Some for leaf nodes)
    pub prediction: Option<i32>,
    /// Left child node
    pub left_child: Option<Box<FairTreeNode>>,
    /// Right child node
    pub right_child: Option<Box<FairTreeNode>>,
    /// Number of samples in this node
    pub sample_count: usize,
    /// Fairness score for this node
    pub fairness_score: f64,
}

/// Fair decision tree
#[derive(Debug)]
pub struct FairDecisionTree {
    /// Root node of the tree
    pub root: FairTreeNode,
    /// Feature importances
    pub feature_importances: Array1<f64>,
    /// Fairness metrics
    pub fairness_metrics: Option<FairnessMetrics>,
    /// Protected attributes used during training
    pub protected_attributes: Vec<ProtectedAttribute>,
}

impl FairDecisionTree {
    /// Predict labels for new samples
    pub fn predict(&self, x: &Array2<f64>) -> Result<Array1<i32>> {
        let n_samples = x.nrows();
        let mut predictions = Array1::zeros(n_samples);

        for i in 0..n_samples {
            let sample = x.row(i);
            predictions[i] = self.predict_single(&self.root, sample.view())?;
        }

        Ok(predictions)
    }

    /// Predict a single sample
    fn predict_single(&self, node: &FairTreeNode, sample: ArrayView1<f64>) -> Result<i32> {
        if node.is_leaf {
            Ok(node.prediction.unwrap_or(0))
        } else {
            let feature_idx = node.feature_idx.expect("operation should succeed");
            let threshold = node.threshold.expect("operation should succeed");

            if sample[feature_idx] <= threshold {
                if let Some(ref left_child) = node.left_child {
                    self.predict_single(left_child, sample)
                } else {
                    Ok(0)
                }
            } else {
                if let Some(ref right_child) = node.right_child {
                    self.predict_single(right_child, sample)
                } else {
                    Ok(0)
                }
            }
        }
    }

    /// Evaluate fairness metrics on test data
    pub fn evaluate_fairness(
        &mut self,
        x: &Array2<f64>,
        y_true: &Array1<i32>,
        protected_attrs: &Array2<i32>,
    ) -> Result<&FairnessMetrics> {
        let y_pred = self.predict(x)?;

        // For simplicity, use first protected attribute
        if !self.protected_attributes.is_empty() {
            let protected_attr = &self.protected_attributes[0];
            let attr_col = protected_attr.column_index;
            let protected_values: Array1<i32> = (0..x.nrows())
                .map(|i| protected_attrs[[i, attr_col]])
                .collect();

            let privileged_value = if let Some(ref priv_group) = protected_attr.privileged_group {
                if priv_group == "1" || priv_group.to_lowercase() == "true" {
                    1
                } else {
                    0
                }
            } else {
                1
            };

            let metrics = FairnessMetrics::calculate(
                y_true,
                &y_pred,
                None,
                &protected_values,
                privileged_value,
            )?;

            self.fairness_metrics = Some(metrics);
            Ok(self.fairness_metrics.as_ref().expect("operation should succeed"))
        } else {
            Err(sklears_core::error::SklearsError::InvalidData {
                reason: "No protected attributes defined".to_string(),
            })
        }
    }

    /// Get feature importances
    pub fn feature_importances(&self) -> &Array1<f64> {
        &self.feature_importances
    }
}

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

    #[test]
    fn test_protected_attribute_creation() {
        let attr = ProtectedAttribute::binary(
            "gender".to_string(),
            0,
            "male".to_string(),
            "female".to_string(),
        );

        assert_eq!(attr.name, "gender");
        assert!(attr.is_binary);
        assert!(attr.is_privileged("male"));
        assert!(!attr.is_privileged("female"));
    }

    #[test]
    fn test_fairness_metrics_calculation() {
        let y_true = Array1::from(vec![1, 1, 0, 0, 1, 0]);
        let y_pred = Array1::from(vec![1, 0, 0, 1, 1, 0]);
        let protected = Array1::from(vec![1, 1, 0, 0, 1, 0]);

        let metrics = FairnessMetrics::calculate(&y_true, &y_pred, None, &protected, 1);

        assert!(metrics.is_ok());
        let metrics = metrics.expect("operation should succeed");
        assert!(metrics.demographic_parity >= 0.0);
    }

    #[test]
    fn test_fair_splitting_criterion() {
        let constraints = FairnessConstraints::default();
        let criterion = FairSplittingCriterion::new(SplitCriterion::Gini, constraints);

        let left_y = Array1::from(vec![1, 1, 0]);
        let right_y = Array1::from(vec![0, 0, 1]);
        let left_protected = Array1::from(vec![1, 0, 1]);
        let right_protected = Array1::from(vec![0, 1, 0]);

        let attr =
            ProtectedAttribute::binary("test".to_string(), 0, "1".to_string(), "0".to_string());

        let score =
            criterion.evaluate_split(&left_y, &right_y, &left_protected, &right_protected, &attr);

        assert!(score.is_ok());
    }

    #[test]
    fn test_fair_decision_tree_creation() {
        let constraints = FairnessConstraints::default();
        let criterion = FairSplittingCriterion::new(SplitCriterion::Gini, constraints);

        let builder = FairDecisionTreeBuilder::new(criterion);
        assert_eq!(builder.min_samples_split, 2);
        assert_eq!(builder.min_samples_leaf, 1);
    }
}