algos 0.6.8

A collection of algorithms in Rust
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
use rand::prelude::*;
use std::f64;

/// The training objective for XGBoost-style gradient boosting.
#[derive(Debug, Clone)]
pub enum XGBObjective {
    /// Regression with squared error loss:
    ///   gradient = (pred - label)
    ///   hessian = 1
    RegSquareError,
    /// Binary logistic classification (labels in {0,1}).
    /// We treat the model output as log-odds.
    /// gradient = p - label, hessian = p*(1-p), where p = sigmoid(pred).
    BinaryLogistic,
}

/// Configuration parameters for an XGBoost-like model.
#[derive(Debug, Clone)]
pub struct XGBConfig {
    /// Number of boosting rounds.
    pub n_estimators: usize,
    /// Maximum tree depth.
    pub max_depth: usize,
    /// L2 regularization on leaf weights (commonly called `lambda`).
    pub lambda: f64,
    /// L1 regularization on leaf weights (commonly called `alpha`).
    pub alpha: f64,
    /// Minimum loss reduction required to make a further partition on a leaf node (gamma).
    pub gamma: f64,
    /// Minimum sum of hessians needed in a leaf (min_child_weight).
    pub min_child_weight: f64,
    /// Subsample ratio of the training instances (row subsampling).
    pub subsample: f64,
    /// Subsample ratio of columns when constructing each tree (colsample_bytree).
    pub colsample_bytree: f64,
    /// Learning rate (eta).
    pub learning_rate: f64,
    /// Random seed for reproducibility.
    pub seed: Option<u64>,
}

/// A minimal XGBoost-inspired gradient boosting model for either regression or binary logistic.
#[derive(Debug)]
pub struct XGBoostModel {
    /// The objective function (determines gradients/hessians).
    pub objective: XGBObjective,
    /// A list of learned trees.
    pub trees: Vec<XGBTree>,
    /// The config used for training.
    pub config: XGBConfig,
    /// Initial prediction (bias). E.g. mean for regression, log( pos/neg ) for logistic, etc.
    pub base_score: f64,
}

/// A single regression tree node storing split structure or a leaf value.
#[derive(Debug, Clone)]
enum XGBTreeNode {
    /// Leaf node with a constant weight (score) contribution.
    Leaf(f64),
    /// Internal node that splits on feature_index <= threshold.
    Internal {
        feature_index: usize,
        threshold: f64,
        left_child: Box<XGBTreeNode>,
        right_child: Box<XGBTreeNode>,
    },
}

/// A CART-style regression tree used by XGBoost, storing node splits with
/// second-order statistics to guide splitting.
#[derive(Debug, Clone)]
pub struct XGBTree {
    root: XGBTreeNode,
}

impl XGBTree {
    /// Predict the contribution of this tree for a single sample.
    pub fn predict_one(&self, sample: &[f64]) -> f64 {
        traverse(&self.root, sample)
    }
}

/// Implementation of XGBoost model
impl XGBoostModel {
    /// Create a new model with given objective and config.
    pub fn new(objective: XGBObjective, config: XGBConfig) -> Self {
        Self {
            objective,
            trees: Vec::new(),
            config,
            base_score: 0.0,
        }
    }

    /// Fit the XGBoost model on the dataset (features, labels).
    ///
    /// For `BinaryLogistic`, labels must be 0.0 or 1.0.
    /// For `RegSquareError`, labels can be any numeric value.
    pub fn fit(&mut self, x: &[Vec<f64>], y: &[f64]) {
        let n = x.len();
        if n == 0 {
            panic!("No training data provided to XGBoostModel.");
        }
        if y.len() != n {
            panic!("Features and labels must match in length.");
        }

        // Validate logistic labels
        if let XGBObjective::BinaryLogistic = self.objective {
            for &lbl in y {
                if !(0.0..=1.0).contains(&lbl) {
                    panic!("BinaryLogistic expects labels in [0,1], got {}", lbl);
                }
            }
        }

        // Initialize base_score
        match self.objective {
            XGBObjective::RegSquareError => {
                // Typically mean of y
                self.base_score = mean(y);
            }
            XGBObjective::BinaryLogistic => {
                // Initialize conservatively with a small value
                // This helps prevent extreme initial predictions
                self.base_score = 0.0; // Start at decision boundary
            }
        }

        let mut rng = match self.config.seed {
            Some(s) => StdRng::seed_from_u64(s),
            None => StdRng::from_entropy(),
        };

        // Start with predictions = base_score
        let mut preds = vec![self.base_score; n];

        // Clear any existing trees
        self.trees.clear();

        for _round in 0..self.config.n_estimators {
            // 1) compute grad, hess
            let (grad, hess) = match self.objective {
                XGBObjective::RegSquareError => {
                    // gradient = (pred - y), hessian = 1
                    let mut g = vec![0.0; n];
                    let hh = vec![1.0; n];
                    for i in 0..n {
                        g[i] = preds[i] - y[i];
                    }
                    (g, hh)
                }
                XGBObjective::BinaryLogistic => {
                    // p = sigmoid(pred)
                    // gradient = p - label
                    // hessian = p*(1-p)
                    let mut g = vec![0.0; n];
                    let mut hh = vec![0.0; n];
                    for i in 0..n {
                        let p = 1.0 / (1.0 + (-preds[i]).exp());
                        g[i] = p - y[i];
                        hh[i] = p * (1.0 - p);
                    }
                    (g, hh)
                }
            };

            // 2) row and col subsampling
            let (sample_mask, col_mask) = subsample_masks(
                n,
                x[0].len(),
                self.config.subsample,
                self.config.colsample_bytree,
                &mut rng,
            );

            // 3) build a tree
            let tree = build_xgb_tree(x, &grad, &hess, &sample_mask, &col_mask, &self.config, 0);
            // 4) append the tree
            self.trees.push(XGBTree { root: tree.clone() });

            // 5) update preds: F_m = F_{m-1} + eta * Tree
            for i in 0..n {
                if sample_mask[i] {
                    let incr = traverse(&tree, &x[i]) * self.config.learning_rate;
                    preds[i] += incr;
                }
            }
        }
    }

    /// Predict for a single sample.
    /// - For `RegSquareError`, returns raw sum of base_score + trees.
    /// - For `BinaryLogistic`, returns 0.0 or 1.0 (threshold=0.5 on sigmoid).
    pub fn predict_one(&self, sample: &[f64]) -> f64 {
        let score = self.decision_function_one(sample);
        match self.objective {
            XGBObjective::RegSquareError => score,
            XGBObjective::BinaryLogistic => {
                let p = 1.0 / (1.0 + (-score).exp());
                if p >= 0.5 {
                    1.0
                } else {
                    0.0
                }
            }
        }
    }

    /// The raw model output: base_score + sum of tree outputs.
    /// For logistic, interpret as log-odds.
    pub fn decision_function_one(&self, sample: &[f64]) -> f64 {
        let mut sum_val = self.base_score;
        for tree in &self.trees {
            sum_val += tree.predict_one(sample) * self.config.learning_rate;
        }
        sum_val
    }

    /// Predict multiple samples at once.
    pub fn predict_batch(&self, data: &[Vec<f64>]) -> Vec<f64> {
        data.iter().map(|row| self.predict_one(row)).collect()
    }
}

// ---- Internal functions for building XGB trees ----

#[derive(Clone, Debug)]
struct XGBNodeSplit {
    feature_index: usize,
    threshold: f64,
    left_index: Vec<usize>,
    right_index: Vec<usize>,
    // Stats
    gain: f64,
}

struct SplitParams<'a> {
    x: &'a [Vec<f64>],
    grad: &'a [f64],
    hess: &'a [f64],
    indices: &'a [usize],
    feat_idx: usize,
    lambda: f64,
    alpha: f64,
    g_node: f64,
    h_node: f64,
    min_child_weight: f64,
    col_mask: &'a [bool],
}

/// Build a single XGBoost tree node recursively using second-order stats.
fn build_xgb_tree(
    x: &[Vec<f64>],
    grad: &[f64],
    hess: &[f64],
    sample_mask: &[bool],
    col_mask: &[bool],
    config: &XGBConfig,
    depth: usize,
) -> XGBTreeNode {
    // gather sample indices
    let mut indices = Vec::new();
    for (i, &m) in sample_mask.iter().enumerate() {
        if m {
            indices.push(i);
        }
    }

    // If no data or max_depth reached, make leaf
    if indices.is_empty() || depth >= config.max_depth {
        let leaf_val = compute_leaf_weight(grad, hess, &indices, config);
        return XGBTreeNode::Leaf(leaf_val);
    }

    // sum of grad/hess for this node
    let (g_node, h_node) = sum_grad_hess(grad, hess, &indices);

    // If h_node < min_child_weight, output leaf
    if h_node < config.min_child_weight {
        let leaf_val = calc_gamma(g_node, h_node, config.lambda, config.alpha);
        return XGBTreeNode::Leaf(leaf_val);
    }

    // Find best split among columns allowed by col_mask
    let best_split = find_best_xgb_split(SplitParams {
        x,
        grad,
        hess,
        indices: &indices,
        feat_idx: 0,
        lambda: config.lambda,
        alpha: config.alpha,
        g_node,
        h_node,
        min_child_weight: config.min_child_weight,
        col_mask,
    });
    match best_split {
        None => {
            // no valid split => leaf
            let leaf_val = calc_gamma(g_node, h_node, config.lambda, config.alpha);
            XGBTreeNode::Leaf(leaf_val)
        }
        Some(sp) => {
            // Check if the gain is smaller than gamma => no split
            if sp.gain < config.gamma {
                let leaf_val = calc_gamma(g_node, h_node, config.lambda, config.alpha);
                return XGBTreeNode::Leaf(leaf_val);
            }
            // build children
            let mut left_mask = vec![false; sample_mask.len()];
            for &i in sp.left_index.iter() {
                left_mask[i] = true;
            }
            let mut right_mask = vec![false; sample_mask.len()];
            for &i in sp.right_index.iter() {
                right_mask[i] = true;
            }

            let left_child = build_xgb_tree(x, grad, hess, &left_mask, col_mask, config, depth + 1);
            let right_child =
                build_xgb_tree(x, grad, hess, &right_mask, col_mask, config, depth + 1);

            XGBTreeNode::Internal {
                feature_index: sp.feature_index,
                threshold: sp.threshold,
                left_child: Box::new(left_child),
                right_child: Box::new(right_child),
            }
        }
    }
}

/// Find the best split over the allowed columns.
/// We compute approximate gain using G = sum(grad), H = sum(hess) for left/right subsets.
fn find_best_xgb_split(params: SplitParams) -> Option<XGBNodeSplit> {
    let mut best: Option<XGBNodeSplit> = None;
    let base_score = calc_gain(params.g_node, params.h_node, params.lambda, params.alpha);

    // For each feature
    for (feat_idx, &use_col) in params.col_mask.iter().enumerate() {
        if !use_col {
            continue;
        }

        let split_params = SplitParams {
            x: params.x,
            grad: params.grad,
            hess: params.hess,
            indices: params.indices,
            feat_idx,
            lambda: params.lambda,
            alpha: params.alpha,
            g_node: params.g_node,
            h_node: params.h_node,
            min_child_weight: params.min_child_weight,
            col_mask: params.col_mask,
        };

        if let Some(split) =
            find_best_split_for_feature(split_params, base_score, params.min_child_weight)
        {
            if best.is_none() || split.gain > best.as_ref().unwrap().gain {
                best = Some(split);
            }
        }
    }

    best
}

fn find_best_split_for_feature(
    params: SplitParams,
    base_score: f64,
    min_child_weight: f64,
) -> Option<XGBNodeSplit> {
    let mut vals = Vec::with_capacity(params.indices.len());
    for &i in params.indices.iter() {
        vals.push((params.x[i][params.feat_idx], i));
    }
    vals.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));

    let mut best_gain = 0.0;
    let mut best_split = None;

    let mut g_left = 0.0;
    let mut h_left = 0.0;
    let mut left_idx = Vec::new();

    for i in 0..vals.len() - 1 {
        let (v, idx) = vals[i];
        g_left += params.grad[idx];
        h_left += params.hess[idx];
        left_idx.push(idx);

        // check if next value is distinct => potential threshold
        let next_val = vals[i + 1].0;
        if (v - next_val).abs() > f64::EPSILON {
            // evaluate gain
            let g_right = params.g_node - g_left;
            let h_right = params.h_node - h_left;

            if h_left >= min_child_weight && h_right >= min_child_weight {
                let gain_left = calc_gain(g_left, h_left, params.lambda, params.alpha);
                let gain_right = calc_gain(g_right, h_right, params.lambda, params.alpha);
                let gain = gain_left + gain_right - base_score;

                if gain > best_gain {
                    best_gain = gain;
                    let right_idx: Vec<usize> =
                        vals[(i + 1)..].iter().map(|(_, idx)| *idx).collect();
                    best_split = Some(XGBNodeSplit {
                        feature_index: params.feat_idx,
                        threshold: (v + next_val) / 2.0,
                        left_index: left_idx.clone(),
                        right_index: right_idx,
                        gain,
                    });
                }
            }
        }
    }

    best_split
}

/// Subsample a fraction of rows and columns for the tree.
fn subsample_masks(
    n_rows: usize,
    n_cols: usize,
    subsample_ratio: f64,
    colsample_ratio: f64,
    rng: &mut impl Rng,
) -> (Vec<bool>, Vec<bool>) {
    // row sampling
    let mut row_mask = vec![false; n_rows];
    let sample_size = (subsample_ratio * n_rows as f64).ceil() as usize;
    if sample_size >= n_rows {
        // use all
        for mask in row_mask.iter_mut().take(n_rows) {
            *mask = true;
        }
    } else {
        // pick randomly
        let mut indices: Vec<usize> = (0..n_rows).collect();
        indices.shuffle(rng);
        for i in 0..sample_size.min(n_rows) {
            row_mask[indices[i]] = true;
        }
    }

    // column sampling
    let mut col_mask = vec![false; n_cols];
    let col_sample_size = (colsample_ratio * n_cols as f64).ceil() as usize;
    if col_sample_size >= n_cols {
        // use all
        for mask in col_mask.iter_mut().take(n_cols) {
            *mask = true;
        }
    } else {
        let mut indices: Vec<usize> = (0..n_cols).collect();
        indices.shuffle(rng);
        for i in 0..col_sample_size.min(n_cols) {
            col_mask[indices[i]] = true;
        }
    }
    (row_mask, col_mask)
}

/// Summation of gradient/hessian over the specified subset indices.
fn sum_grad_hess(grad: &[f64], hess: &[f64], indices: &[usize]) -> (f64, f64) {
    let mut g = 0.0;
    let mut hh = 0.0;
    for &i in indices {
        g += grad[i];
        hh += hess[i];
    }
    (g, hh)
}

/// Leaf weight formula for XGBoost:
///   w* = -G / (H + lambda), ignoring gamma here because that's for the splitting gain threshold.
fn compute_leaf_weight(grad: &[f64], hess: &[f64], indices: &[usize], cfg: &XGBConfig) -> f64 {
    let (g, hh) = sum_grad_hess(grad, hess, indices);
    calc_gamma(g, hh, cfg.lambda, cfg.alpha)
}

/// The final leaf weight in presence of L1 (alpha) in XGBoost can be solved by "soft thresholding".
/// In a simplified approach:
///   w* = - sign(G) * max(0, |G| - alpha) / (H + lambda)
fn calc_gamma(g: f64, h: f64, lambda: f64, alpha: f64) -> f64 {
    if h.abs() < f64::EPSILON {
        return 0.0;
    }
    let sign_g = if g > 0.0 { 1.0 } else { -1.0 };
    let abs_g = g.abs();
    let res = (abs_g - alpha).max(0.0) / (h + lambda);
    -sign_g * res
}

/// The gain from splitting one node into two, ignoring gamma here, is:
/// gain = 1/2 * [G_L^2/(H_L + lambda) + G_R^2/(H_R + lambda) - G^2/(H + lambda)]
fn calc_gain(g: f64, h: f64, lambda: f64, _alpha: f64) -> f64 {
    // ignoring alpha for gain formula except in leaf weight.
    // This is a simplified approach.
    // Typically alpha affects the leaf weight, thus the net gain is also influenced indirectly.
    // We'll do standard formula ignoring alpha's direct role in splitting gain.
    if h.abs() < f64::EPSILON {
        return 0.0;
    }
    0.5 * ((g * g) / (h + lambda))
}

/// Tree traversal to get leaf value.
fn traverse(node: &XGBTreeNode, sample: &[f64]) -> f64 {
    match node {
        XGBTreeNode::Leaf(w) => *w,
        XGBTreeNode::Internal {
            feature_index,
            threshold,
            left_child,
            right_child,
        } => {
            if sample[*feature_index] <= *threshold {
                traverse(left_child, sample)
            } else {
                traverse(right_child, sample)
            }
        }
    }
}

/// Helper: Mean of a slice.
fn mean(arr: &[f64]) -> f64 {
    if arr.is_empty() {
        0.0
    } else {
        arr.iter().sum::<f64>() / (arr.len() as f64)
    }
}

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

    #[test]
    fn test_xgb_reg_square_error() {
        // Simple function y = x1 + 2*x2
        let x = vec![
            vec![1.0, 1.0],
            vec![2.0, 2.0],
            vec![3.0, 1.0],
            vec![0.0, 0.0],
            vec![4.0, 2.0],
        ];
        let y: Vec<f64> = x.iter().map(|row| row[0] + 2.0 * row[1]).collect();

        let config = XGBConfig {
            n_estimators: 100,
            max_depth: 3,
            lambda: 1.0,
            alpha: 0.0,
            gamma: 0.0,
            min_child_weight: 0.1,
            subsample: 1.0,
            colsample_bytree: 1.0,
            learning_rate: 0.3,
            seed: Some(42),
        };
        let mut model = XGBoostModel::new(XGBObjective::RegSquareError, config);
        model.fit(&x, &y);

        for i in 0..x.len() {
            let pred = model.predict_one(&x[i]);
            let err = (pred - y[i]).abs();
            assert!(err < 1.0, "Prediction error is too large: err={}", err);
        }
    }

    #[test]
    fn test_xgb_binary_logistic() {
        // We'll define a simple classification: label=1 if x1 + x2>3, else 0
        let x = vec![
            vec![0.0, 0.0], // sum=0 => clearly 0
            vec![5.0, 5.0], // sum=10 => clearly 1
            vec![0.0, 1.0], // sum=1 => clearly 0
            vec![5.0, 4.0], // sum=9 => clearly 1
            vec![0.5, 0.5], // sum=1 => clearly 0
        ];
        let y: Vec<f64> = x
            .iter()
            .map(|row| if row[0] + row[1] > 3.0 { 1.0 } else { 0.0 })
            .collect();

        let config = XGBConfig {
            n_estimators: 100,
            max_depth: 3,
            lambda: 1.0,
            alpha: 0.0,
            gamma: 0.0,
            min_child_weight: 0.1,
            subsample: 1.0,
            colsample_bytree: 1.0,
            learning_rate: 0.3,
            seed: Some(123),
        };
        let mut model = XGBoostModel::new(XGBObjective::BinaryLogistic, config);
        model.fit(&x, &y);

        // Check predictions
        for i in 0..x.len() {
            let pred = model.predict_one(&x[i]);
            let truth = y[i];
            let is_correct = (pred - truth).abs() < 0.5;
            assert!(
                is_correct,
                "Wrong classification for row {} => pred={}",
                i, pred
            );
        }
    }
}