ternary-trees 0.1.0

Decision trees and forests for ternary classification on {-1, 0, +1}
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
#![forbid(unsafe_code)]

//! Decision trees and forests for ternary classification on {-1, 0, +1}.
//!
//! Provides TernaryDecisionTree with ternary splits, RandomForest with ternary voting,
//! feature importance, and pruning with ternary entropy.

/// A ternary value.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Ternary {
    Neg,
    Zero,
    Pos,
}

impl Ternary {
    pub fn to_i8(self) -> i8 {
        match self {
            Ternary::Neg => -1,
            Ternary::Zero => 0,
            Ternary::Pos => 1,
        }
    }

    pub fn from_i8(v: i8) -> Option<Self> {
        match v {
            -1 => Some(Ternary::Neg),
            0 => Some(Ternary::Zero),
            1 => Some(Ternary::Pos),
            _ => None,
        }
    }

    pub fn values() -> [Ternary; 3] {
        [Ternary::Neg, Ternary::Zero, Ternary::Pos]
    }
}

/// Feature value type for ternary features.
pub type Feature = i8;

/// A training sample: features + label.
pub type Sample = (Vec<Feature>, Ternary);

/// Compute ternary entropy.
pub fn ternary_entropy(counts: [usize; 3]) -> f64 {
    let total = counts[0] + counts[1] + counts[2] as usize;
    if total == 0 {
        return 0.0;
    }
    let mut h = 0.0;
    for &c in &counts {
        if c > 0 {
            let p = c as f64 / total as f64;
            h -= p * p.log2();
        }
    }
    h
}

/// Count ternary labels in a slice.
pub fn count_labels(samples: &[Sample]) -> [usize; 3] {
    let mut counts = [0usize; 3];
    for (_, label) in samples {
        match label {
            Ternary::Neg => counts[0] += 1,
            Ternary::Zero => counts[1] += 1,
            Ternary::Pos => counts[2] += 1,
        }
    }
    counts
}

/// Majority vote label.
pub fn majority(samples: &[Sample]) -> Ternary {
    let counts = count_labels(samples);
    if counts[0] >= counts[1] && counts[0] >= counts[2] {
        Ternary::Neg
    } else if counts[1] >= counts[2] {
        Ternary::Zero
    } else {
        Ternary::Pos
    }
}

/// A decision tree node.
#[derive(Debug, Clone)]
pub enum TreeNode {
    Leaf {
        label: Ternary,
        confidence: f64,
        count: usize,
    },
    Internal {
        feature_idx: usize,
        threshold: Feature,
        left: Box<TreeNode>,   // value < threshold
        middle: Box<TreeNode>, // value == threshold
        right: Box<TreeNode>,  // value > threshold
    },
}

/// Ternary decision tree.
#[derive(Debug, Clone)]
pub struct TernaryDecisionTree {
    root: Option<TreeNode>,
    max_depth: usize,
    min_samples: usize,
}

impl TernaryDecisionTree {
    pub fn new(max_depth: usize, min_samples: usize) -> Self {
        TernaryDecisionTree {
            root: None,
            max_depth,
            min_samples,
        }
    }

    pub fn fit(&mut self, samples: &[Sample]) {
        self.root = Some(self.build_tree(samples, 0));
    }

    fn build_tree(&self, samples: &[Sample], depth: usize) -> TreeNode {
        let counts = count_labels(samples);
        let total = counts[0] + counts[1] + counts[2];
        let majority_label = majority(samples);
        let confidence = if total > 0 {
            let max_c = counts[0].max(counts[1]).max(counts[2]);
            max_c as f64 / total as f64
        } else {
            0.0
        };

        if depth >= self.max_depth || total <= self.min_samples || confidence >= 0.95 {
            return TreeNode::Leaf {
                label: majority_label,
                confidence,
                count: total,
            };
        }

        let n_features = samples[0].0.len();
        let mut best_gain = 0.0f64;
        let mut best_feat = 0;
        let mut best_thresh = 0i8;

        let parent_entropy = ternary_entropy(counts);

        for fi in 0..n_features {
            for &thresh in &[-1i8, 0i8] {
                let mut left_counts = [0usize; 3];
                let mut mid_counts = [0usize; 3];
                let mut right_counts = [0usize; 3];
                let mut left_total = 0;
                let mut mid_total = 0;
                let mut right_total = 0;

                for (features, label) in samples {
                    let val = features[fi];
                    let idx = match label {
                        Ternary::Neg => 0,
                        Ternary::Zero => 1,
                        Ternary::Pos => 2,
                    };
                    if val < thresh {
                        left_counts[idx] += 1;
                        left_total += 1;
                    } else if val == thresh {
                        mid_counts[idx] += 1;
                        mid_total += 1;
                    } else {
                        right_counts[idx] += 1;
                        right_total += 1;
                    }
                }

                if left_total == 0 || mid_total == 0 || right_total == 0 {
                    continue;
                }

                let child_entropy = (left_total as f64 * ternary_entropy(left_counts)
                    + mid_total as f64 * ternary_entropy(mid_counts)
                    + right_total as f64 * ternary_entropy(right_counts))
                    / total as f64;

                let gain = parent_entropy - child_entropy;
                if gain > best_gain {
                    best_gain = gain;
                    best_feat = fi;
                    best_thresh = thresh;
                }
            }
        }

        if best_gain < 1e-10 {
            return TreeNode::Leaf {
                label: majority_label,
                confidence,
                count: total,
            };
        }

        let mut left_samples = Vec::new();
        let mut mid_samples = Vec::new();
        let mut right_samples = Vec::new();
        for s in samples {
            match s.0[best_feat].cmp(&best_thresh) {
                std::cmp::Ordering::Less => left_samples.push(s.clone()),
                std::cmp::Ordering::Equal => mid_samples.push(s.clone()),
                std::cmp::Ordering::Greater => right_samples.push(s.clone()),
            }
        }

        TreeNode::Internal {
            feature_idx: best_feat,
            threshold: best_thresh,
            left: Box::new(self.build_tree(&left_samples, depth + 1)),
            middle: Box::new(self.build_tree(&mid_samples, depth + 1)),
            right: Box::new(self.build_tree(&right_samples, depth + 1)),
        }
    }

    pub fn predict(&self, features: &[Feature]) -> Option<Ternary> {
        self.root.as_ref().map(|node| Self::predict_node(node, features))
    }

    fn predict_node(node: &TreeNode, features: &[Feature]) -> Ternary {
        match node {
            TreeNode::Leaf { label, .. } => *label,
            TreeNode::Internal {
                feature_idx,
                threshold,
                left,
                middle,
                right,
            } => {
                let val = features[*feature_idx];
                match val.cmp(threshold) {
                    std::cmp::Ordering::Less => Self::predict_node(left, features),
                    std::cmp::Ordering::Equal => Self::predict_node(middle, features),
                    std::cmp::Ordering::Greater => Self::predict_node(right, features),
                }
            }
        }
    }

    /// Count the number of leaves.
    pub fn count_leaves(&self) -> usize {
        self.root.as_ref().map_or(0, Self::count_leaves_node)
    }

    fn count_leaves_node(node: &TreeNode) -> usize {
        match node {
            TreeNode::Leaf { .. } => 1,
            TreeNode::Internal {
                left, middle, right, ..
            } => {
                Self::count_leaves_node(left)
                    + Self::count_leaves_node(middle)
                    + Self::count_leaves_node(right)
            }
        }
    }

    /// Prune: convert internal nodes to leaves if it doesn't hurt accuracy.
    pub fn prune(&mut self, validation: &[Sample]) {
        if let Some(root) = self.root.take() {
            self.root = Some(self.prune_node(root, validation));
        }
    }

    fn prune_node(&self, node: TreeNode, validation: &[Sample]) -> TreeNode {
        match node {
            TreeNode::Leaf { .. } => node,
            TreeNode::Internal {
                feature_idx,
                threshold,
                left,
                middle,
                right,
            } => {
                let left = Box::new(self.prune_node(*left, validation));
                let middle = Box::new(self.prune_node(*middle, validation));
                let right = Box::new(self.prune_node(*right, validation));

                let internal_node = TreeNode::Internal {
                    feature_idx,
                    threshold,
                    left: left.clone(),
                    middle: middle.clone(),
                    right: right.clone(),
                };

                // Compute accuracy with internal node
                let internal_acc = validation
                    .iter()
                    .filter(|(f, l)| Self::predict_node(&internal_node, f) == *l)
                    .count();

                // Try converting to leaf
                let filtered: Vec<Sample> = validation.to_vec();
                let leaf_label = majority(&filtered);
                let leaf_node = TreeNode::Leaf {
                    label: leaf_label,
                    confidence: 1.0,
                    count: filtered.len(),
                };
                let leaf_acc = validation
                    .iter()
                    .filter(|(_, l)| leaf_label == *l)
                    .count();

                if leaf_acc >= internal_acc {
                    leaf_node
                } else {
                    internal_node
                }
            }
        }
    }
}

/// Random forest for ternary classification.
#[derive(Debug, Clone)]
pub struct RandomForest {
    pub trees: Vec<TernaryDecisionTree>,
    pub n_features_per_split: usize,
}

impl RandomForest {
    pub fn new(n_trees: usize, max_depth: usize, min_samples: usize, n_features_per_split: usize) -> Self {
        let trees = (0..n_trees)
            .map(|_| TernaryDecisionTree::new(max_depth, min_samples))
            .collect();
        RandomForest {
            trees,
            n_features_per_split,
        }
    }

    pub fn fit(&mut self, samples: &[Sample]) {
        let n = samples.len();
        for tree in &mut self.trees {
            // Bootstrap sample
            let mut bootstrap = Vec::with_capacity(n);
            for _ in 0..n {
                let idx = simple_hash(n) % n;
                bootstrap.push(samples[idx].clone());
            }
            tree.fit(&bootstrap);
        }
    }

    pub fn predict(&self, features: &[Feature]) -> Ternary {
        let mut counts = [0usize; 3];
        for tree in &self.trees {
            if let Some(label) = tree.predict(features) {
                match label {
                    Ternary::Neg => counts[0] += 1,
                    Ternary::Zero => counts[1] += 1,
                    Ternary::Pos => counts[2] += 1,
                }
            }
        }
        if counts[0] >= counts[1] && counts[0] >= counts[2] {
            Ternary::Neg
        } else if counts[1] >= counts[2] {
            Ternary::Zero
        } else {
            Ternary::Pos
        }
    }

    /// Compute feature importance via permutation.
    pub fn feature_importance(&self, samples: &[Sample], n_features: usize) -> Vec<f64> {
        let baseline_acc = self.accuracy(samples);
        let mut importance = vec![0.0; n_features];

        for fi in 0..n_features {
            let mut permuted = samples.to_vec();
            // Simple permutation: reverse order of feature fi
            let n = permuted.len();
            for i in 0..n / 2 {
                let j = n - 1 - i;
                let tmp = permuted[i].0[fi];
                permuted[i].0[fi] = permuted[j].0[fi];
                permuted[j].0[fi] = tmp;
            }
            let perm_acc = self.accuracy(&permuted);
            importance[fi] = baseline_acc - perm_acc;
        }
        importance
    }

    pub fn accuracy(&self, samples: &[Sample]) -> f64 {
        if samples.is_empty() {
            return 0.0;
        }
        let correct = samples
            .iter()
            .filter(|(f, l)| self.predict(f) == *l)
            .count();
        correct as f64 / samples.len() as f64
    }
}

/// Simple deterministic hash for bootstrap sampling.
fn simple_hash(n: usize) -> usize {
    n.wrapping_mul(1103515245).wrapping_add(12345)
}

/// Compute Gini impurity for ternary labels.
pub fn gini_impurity(samples: &[Sample]) -> f64 {
    let counts = count_labels(samples);
    let total = counts[0] + counts[1] + counts[2];
    if total == 0 {
        return 0.0;
    }
    let mut gini = 0.0;
    for &c in &counts {
        let p = c as f64 / total as f64;
        gini += p * p;
    }
    1.0 - gini
}

/// Compute information gain for a split.
pub fn information_gain(parent: &[Sample], children: &[&[Sample]]) -> f64 {
    let parent_h = ternary_entropy(count_labels(parent));
    let parent_n = parent.len() as f64;
    let mut child_h = 0.0;
    for child in children {
        let w = child.len() as f64 / parent_n;
        child_h += w * ternary_entropy(count_labels(child));
    }
    parent_h - child_h
}

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

    #[test]
    fn test_ternary_values() {
        assert_eq!(Ternary::Neg.to_i8(), -1);
        assert_eq!(Ternary::Zero.to_i8(), 0);
        assert_eq!(Ternary::Pos.to_i8(), 1);
    }

    #[test]
    fn test_ternary_entropy_uniform() {
        let h = ternary_entropy([10, 10, 10]);
        assert!((h - (3.0f64.log2())).abs() < 1e-10);
    }

    #[test]
    fn test_ternary_entropy_pure() {
        let h = ternary_entropy([100, 0, 0]);
        assert!(h.abs() < 1e-10);
    }

    #[test]
    fn test_count_labels() {
        let samples = vec![
            (vec![0], Ternary::Neg),
            (vec![0], Ternary::Neg),
            (vec![0], Ternary::Pos),
        ];
        let counts = count_labels(&samples);
        assert_eq!(counts, [2, 0, 1]);
    }

    #[test]
    fn test_majority() {
        let samples = vec![
            (vec![0], Ternary::Pos),
            (vec![0], Ternary::Pos),
            (vec![0], Ternary::Neg),
        ];
        assert_eq!(majority(&samples), Ternary::Pos);
    }

    #[test]
    fn test_decision_tree_fit_predict() {
        let samples = vec![
            (vec![-1, -1], Ternary::Neg),
            (vec![-1, 0], Ternary::Neg),
            (vec![1, 1], Ternary::Pos),
            (vec![1, 0], Ternary::Pos),
            (vec![0, 0], Ternary::Zero),
            (vec![0, 0], Ternary::Zero),
        ];
        let mut tree = TernaryDecisionTree::new(5, 1);
        tree.fit(&samples);
        assert_eq!(tree.predict(&[-1, -1]), Some(Ternary::Neg));
        assert_eq!(tree.predict(&[1, 1]), Some(Ternary::Pos));
    }

    #[test]
    fn test_decision_tree_single_class() {
        let samples = vec![
            (vec![1], Ternary::Pos),
            (vec![1], Ternary::Pos),
            (vec![1], Ternary::Pos),
        ];
        let mut tree = TernaryDecisionTree::new(5, 1);
        tree.fit(&samples);
        assert_eq!(tree.predict(&[1]), Some(Ternary::Pos));
    }

    #[test]
    fn test_tree_leaves_count() {
        let samples = vec![
            (vec![-1], Ternary::Neg),
            (vec![0], Ternary::Zero),
            (vec![1], Ternary::Pos),
        ];
        let mut tree = TernaryDecisionTree::new(10, 1);
        tree.fit(&samples);
        assert!(tree.count_leaves() >= 1);
    }

    #[test]
    fn test_gini_impurity() {
        let pure = vec![(vec![0], Ternary::Pos)];
        assert!(gini_impurity(&pure).abs() < 1e-10);

        let mixed = vec![
            (vec![0], Ternary::Neg),
            (vec![0], Ternary::Zero),
            (vec![0], Ternary::Pos),
        ];
        let g = gini_impurity(&mixed);
        assert!((g - (1.0_f64 - 3.0_f64 * (1.0_f64 / 3.0_f64).powi(2))).abs() < 1e-10);
    }

    #[test]
    fn test_information_gain() {
        let parent = vec![
            (vec![0], Ternary::Neg),
            (vec![0], Ternary::Neg),
            (vec![0], Ternary::Pos),
            (vec![0], Ternary::Pos),
        ];
        let left = &parent[0..2];
        let right = &parent[2..4];
        let gain = information_gain(&parent, &[left, right]);
        assert!(gain > 0.0);
    }

    #[test]
    fn test_random_forest() {
        let samples = vec![
            (vec![-1, -1], Ternary::Neg),
            (vec![-1, 0], Ternary::Neg),
            (vec![1, 1], Ternary::Pos),
            (vec![1, 0], Ternary::Pos),
            (vec![0, 0], Ternary::Zero),
            (vec![0, 0], Ternary::Zero),
            (vec![-1, -1], Ternary::Neg),
            (vec![1, 1], Ternary::Pos),
        ];
        let mut rf = RandomForest::new(5, 5, 1, 2);
        rf.fit(&samples);
        let pred = rf.predict(&[-1, -1]);
        assert!(pred == Ternary::Neg);
    }

    #[test]
    fn test_random_forest_accuracy() {
        let samples = vec![
            (vec![-1], Ternary::Neg),
            (vec![0], Ternary::Zero),
            (vec![1], Ternary::Pos),
            (vec![-1], Ternary::Neg),
            (vec![0], Ternary::Zero),
            (vec![1], Ternary::Pos),
        ];
        let mut rf = RandomForest::new(5, 5, 1, 1);
        rf.fit(&samples);
        let acc = rf.accuracy(&samples);
        assert!(acc > 0.0);
    }

    #[test]
    fn test_feature_importance() {
        let samples = vec![
            (vec![-1, 0], Ternary::Neg),
            (vec![1, 0], Ternary::Pos),
            (vec![0, -1], Ternary::Zero),
            (vec![-1, 0], Ternary::Neg),
            (vec![1, 0], Ternary::Pos),
            (vec![0, 1], Ternary::Zero),
        ];
        let mut rf = RandomForest::new(5, 5, 1, 2);
        rf.fit(&samples);
        let importance = rf.feature_importance(&samples, 2);
        assert_eq!(importance.len(), 2);
    }

    #[test]
    fn test_tree_prune() {
        let samples = vec![
            (vec![-1], Ternary::Neg),
            (vec![0], Ternary::Zero),
            (vec![1], Ternary::Pos),
            (vec![-1], Ternary::Neg),
            (vec![0], Ternary::Zero),
            (vec![1], Ternary::Pos),
        ];
        let mut tree = TernaryDecisionTree::new(10, 1);
        tree.fit(&samples);
        let leaves_before = tree.count_leaves();
        tree.prune(&samples);
        // Pruning should not increase leaf count beyond reasonable bounds
        assert!(tree.count_leaves() <= leaves_before || leaves_before <= 3);
    }

    #[test]
    fn test_empty_samples_entropy() {
        let h = ternary_entropy([0, 0, 0]);
        assert!(h.abs() < 1e-10);
    }

    #[test]
    fn test_predict_no_fit() {
        let tree = TernaryDecisionTree::new(5, 1);
        assert!(tree.predict(&[0]).is_none());
    }

    #[test]
    fn test_from_i8() {
        assert_eq!(Ternary::from_i8(-1), Some(Ternary::Neg));
        assert_eq!(Ternary::from_i8(2), None);
    }

    #[test]
    fn test_forest_empty_accuracy() {
        let rf = RandomForest::new(3, 5, 1, 1);
        assert_eq!(rf.accuracy(&[]), 0.0);
    }

    #[test]
    fn test_ternary_values_array() {
        let vals = Ternary::values();
        assert_eq!(vals.len(), 3);
        assert_eq!(vals[0], Ternary::Neg);
        assert_eq!(vals[1], Ternary::Zero);
        assert_eq!(vals[2], Ternary::Pos);
    }

    #[test]
    fn test_gini_pure_vs_mixed() {
        let pure = vec![(vec![0], Ternary::Pos), (vec![0], Ternary::Pos)];
        let mixed = vec![(vec![0], Ternary::Neg), (vec![0], Ternary::Pos)];
        assert!(gini_impurity(&pure) < gini_impurity(&mixed));
    }

    #[test]
    fn test_decision_tree_zero_features() {
        let samples = vec![
            (vec![], Ternary::Neg),
            (vec![], Ternary::Pos),
        ];
        let mut tree = TernaryDecisionTree::new(5, 1);
        tree.fit(&samples);
        // Should still produce a prediction
        assert!(tree.predict(&[]).is_some());
    }
}