ghostflow-ml 1.0.0

Classical ML algorithms for GhostFlow
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
//! Advanced Gradient Boosting Implementations
//! 
//! This module provides XGBoost, LightGBM, and CatBoost-style gradient boosting algorithms.

use ghostflow_core::Tensor;
use rand::prelude::*;
use rayon::prelude::*;
use std::collections::HashMap;

/// XGBoost-style Gradient Boosting
/// 
/// Features:
/// - Regularized learning objective (L1 and L2)
/// - Column (feature) subsampling
/// - Shrinkage (learning rate)
/// - Tree pruning with max_depth
/// - Histogram-based split finding
pub struct XGBoostClassifier {
    pub n_estimators: usize,
    pub learning_rate: f32,
    pub max_depth: usize,
    pub min_child_weight: f32,
    pub subsample: f32,
    pub colsample_bytree: f32,
    pub reg_lambda: f32,  // L2 regularization
    pub reg_alpha: f32,   // L1 regularization
    pub gamma: f32,       // Minimum loss reduction for split
    trees: Vec<XGBTree>,
    base_score: f32,
}

#[derive(Clone)]
struct XGBTree {
    nodes: Vec<XGBNode>,
    feature_indices: Vec<usize>,
}

#[derive(Clone)]
struct XGBNode {
    is_leaf: bool,
    feature: usize,
    threshold: f32,
    left: usize,
    right: usize,
    value: f32,
    gain: f32,
}

impl XGBoostClassifier {
    pub fn new(n_estimators: usize) -> Self {
        Self {
            n_estimators,
            learning_rate: 0.3,
            max_depth: 6,
            min_child_weight: 1.0,
            subsample: 1.0,
            colsample_bytree: 1.0,
            reg_lambda: 1.0,
            reg_alpha: 0.0,
            gamma: 0.0,
            trees: Vec::new(),
            base_score: 0.5,
        }
    }

    pub fn learning_rate(mut self, lr: f32) -> Self {
        self.learning_rate = lr;
        self
    }

    pub fn max_depth(mut self, depth: usize) -> Self {
        self.max_depth = depth;
        self
    }

    pub fn subsample(mut self, ratio: f32) -> Self {
        self.subsample = ratio;
        self
    }

    pub fn colsample_bytree(mut self, ratio: f32) -> Self {
        self.colsample_bytree = ratio;
        self
    }

    pub fn reg_lambda(mut self, lambda: f32) -> Self {
        self.reg_lambda = lambda;
        self
    }

    pub fn reg_alpha(mut self, alpha: f32) -> Self {
        self.reg_alpha = alpha;
        self
    }

    pub fn fit(&mut self, x: &Tensor, y: &Tensor) {
        let n_samples = x.dims()[0];
        let n_features = x.dims()[1];
        let x_data = x.data_f32();
        let y_data = y.data_f32();

        // Initialize predictions with base score
        let mut predictions = vec![self.base_score; n_samples];
        
        // Build trees sequentially
        for _ in 0..self.n_estimators {
            // Calculate gradients and hessians (for logistic loss)
            let mut gradients = Vec::with_capacity(n_samples);
            let mut hessians = Vec::with_capacity(n_samples);
            
            for i in 0..n_samples {
                let pred = 1.0 / (1.0 + (-predictions[i]).exp());
                let grad = pred - y_data[i];
                let hess = pred * (1.0 - pred);
                gradients.push(grad);
                hessians.push(hess.max(1e-6)); // Avoid division by zero
            }

            // Row subsampling
            let mut rng = thread_rng();
            let sample_indices: Vec<usize> = if self.subsample < 1.0 {
                let n_subsample = (n_samples as f32 * self.subsample) as usize;
                (0..n_samples).choose_multiple(&mut rng, n_subsample)
            } else {
                (0..n_samples).collect()
            };

            // Column subsampling
            let feature_indices: Vec<usize> = if self.colsample_bytree < 1.0 {
                let n_features_sample = (n_features as f32 * self.colsample_bytree) as usize;
                (0..n_features).choose_multiple(&mut rng, n_features_sample)
            } else {
                (0..n_features).collect()
            };

            // Build tree
            let tree = self.build_tree(
                &x_data,
                &gradients,
                &hessians,
                &sample_indices,
                &feature_indices,
                n_features,
                0,
            );

            // Update predictions
            for i in 0..n_samples {
                let leaf_value = self.predict_tree(&tree, &x_data[i * n_features..(i + 1) * n_features]);
                predictions[i] += self.learning_rate * leaf_value;
            }

            self.trees.push(tree);
        }
    }

    fn build_tree(
        &self,
        x_data: &[f32],
        gradients: &[f32],
        hessians: &[f32],
        sample_indices: &[usize],
        feature_indices: &[usize],
        n_features: usize,
        depth: usize,
    ) -> XGBTree {
        let mut nodes = Vec::new();
        
        // Build root node
        self.build_node(
            x_data,
            gradients,
            hessians,
            sample_indices,
            feature_indices,
            n_features,
            depth,
            &mut nodes,
        );

        XGBTree {
            nodes,
            feature_indices: feature_indices.to_vec(),
        }
    }

    fn build_node(
        &self,
        x_data: &[f32],
        gradients: &[f32],
        hessians: &[f32],
        sample_indices: &[usize],
        feature_indices: &[usize],
        n_features: usize,
        depth: usize,
        nodes: &mut Vec<XGBNode>,
    ) -> usize {
        let node_idx = nodes.len();

        // Calculate node statistics
        let sum_grad: f32 = sample_indices.iter().map(|&i| gradients[i]).sum();
        let sum_hess: f32 = sample_indices.iter().map(|&i| hessians[i]).sum();

        // Calculate leaf value with regularization
        let leaf_value = -sum_grad / (sum_hess + self.reg_lambda);

        // Check stopping criteria
        if depth >= self.max_depth || sample_indices.len() < 2 || sum_hess < self.min_child_weight {
            nodes.push(XGBNode {
                is_leaf: true,
                feature: 0,
                threshold: 0.0,
                left: 0,
                right: 0,
                value: leaf_value,
                gain: 0.0,
            });
            return node_idx;
        }

        // Find best split
        let (best_feature, best_threshold, best_gain, left_indices, right_indices) =
            self.find_best_split(
                x_data,
                gradients,
                hessians,
                sample_indices,
                feature_indices,
                n_features,
                sum_grad,
                sum_hess,
            );

        // Check if split is beneficial
        if best_gain < self.gamma || left_indices.is_empty() || right_indices.is_empty() {
            nodes.push(XGBNode {
                is_leaf: true,
                feature: 0,
                threshold: 0.0,
                left: 0,
                right: 0,
                value: leaf_value,
                gain: 0.0,
            });
            return node_idx;
        }

        // Create internal node
        nodes.push(XGBNode {
            is_leaf: false,
            feature: best_feature,
            threshold: best_threshold,
            left: 0,  // Will be updated
            right: 0, // Will be updated
            value: 0.0,
            gain: best_gain,
        });

        // Build left and right children
        let left_idx = self.build_node(
            x_data,
            gradients,
            hessians,
            &left_indices,
            feature_indices,
            n_features,
            depth + 1,
            nodes,
        );

        let right_idx = self.build_node(
            x_data,
            gradients,
            hessians,
            &right_indices,
            feature_indices,
            n_features,
            depth + 1,
            nodes,
        );

        // Update children indices
        nodes[node_idx].left = left_idx;
        nodes[node_idx].right = right_idx;

        node_idx
    }

    fn find_best_split(
        &self,
        x_data: &[f32],
        gradients: &[f32],
        hessians: &[f32],
        sample_indices: &[usize],
        feature_indices: &[usize],
        n_features: usize,
        sum_grad: f32,
        sum_hess: f32,
    ) -> (usize, f32, f32, Vec<usize>, Vec<usize>) {
        let mut best_gain = 0.0;
        let mut best_feature = 0;
        let mut best_threshold = 0.0;
        let mut best_left = Vec::new();
        let mut best_right = Vec::new();

        for &feature in feature_indices {
            // Get feature values for samples
            let mut feature_values: Vec<(f32, usize)> = sample_indices
                .iter()
                .map(|&i| (x_data[i * n_features + feature], i))
                .collect();
            
            feature_values.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());

            // Try splits
            let mut left_grad = 0.0;
            let mut left_hess = 0.0;

            for i in 0..feature_values.len() - 1 {
                let idx = feature_values[i].1;
                left_grad += gradients[idx];
                left_hess += hessians[idx];

                let right_grad = sum_grad - left_grad;
                let right_hess = sum_hess - left_hess;

                // Check minimum child weight
                if left_hess < self.min_child_weight || right_hess < self.min_child_weight {
                    continue;
                }

                // Calculate gain with regularization
                let gain = self.calculate_gain(left_grad, left_hess, right_grad, right_hess, sum_grad, sum_hess);

                if gain > best_gain {
                    best_gain = gain;
                    best_feature = feature;
                    best_threshold = (feature_values[i].0 + feature_values[i + 1].0) / 2.0;
                    
                    best_left = feature_values[..=i].iter().map(|&(_, idx)| idx).collect();
                    best_right = feature_values[i + 1..].iter().map(|&(_, idx)| idx).collect();
                }
            }
        }

        (best_feature, best_threshold, best_gain, best_left, best_right)
    }

    fn calculate_gain(
        &self,
        left_grad: f32,
        left_hess: f32,
        right_grad: f32,
        right_hess: f32,
        sum_grad: f32,
        sum_hess: f32,
    ) -> f32 {
        let left_weight = -left_grad / (left_hess + self.reg_lambda);
        let right_weight = -right_grad / (right_hess + self.reg_lambda);
        let parent_weight = -sum_grad / (sum_hess + self.reg_lambda);

        let gain = 0.5 * (
            left_grad * left_weight +
            right_grad * right_weight -
            sum_grad * parent_weight
        ) - self.gamma;

        // L1 regularization penalty
        let l1_penalty = self.reg_alpha * (left_weight.abs() + right_weight.abs() - parent_weight.abs());

        gain - l1_penalty
    }

    fn predict_tree(&self, tree: &XGBTree, sample: &[f32]) -> f32 {
        let mut node_idx = 0;
        
        loop {
            let node = &tree.nodes[node_idx];
            
            if node.is_leaf {
                return node.value;
            }

            if sample[node.feature] <= node.threshold {
                node_idx = node.left;
            } else {
                node_idx = node.right;
            }
        }
    }

    pub fn predict(&self, x: &Tensor) -> Tensor {
        let n_samples = x.dims()[0];
        let n_features = x.dims()[1];
        let x_data = x.data_f32();

        let predictions: Vec<f32> = (0..n_samples)
            .into_par_iter()
            .map(|i| {
                let sample = &x_data[i * n_features..(i + 1) * n_features];
                let mut pred = self.base_score;
                
                for tree in &self.trees {
                    pred += self.learning_rate * self.predict_tree(tree, sample);
                }

                // Apply sigmoid for binary classification
                let prob = 1.0 / (1.0 + (-pred).exp());
                if prob >= 0.5 { 1.0 } else { 0.0 }
            })
            .collect();

        Tensor::from_slice(&predictions, &[n_samples]).unwrap()
    }

    pub fn predict_proba(&self, x: &Tensor) -> Tensor {
        let n_samples = x.dims()[0];
        let n_features = x.dims()[1];
        let x_data = x.data_f32();

        let probabilities: Vec<f32> = (0..n_samples)
            .into_par_iter()
            .map(|i| {
                let sample = &x_data[i * n_features..(i + 1) * n_features];
                let mut pred = self.base_score;
                
                for tree in &self.trees {
                    pred += self.learning_rate * self.predict_tree(tree, sample);
                }

                1.0 / (1.0 + (-pred).exp())
            })
            .collect();

        Tensor::from_slice(&probabilities, &[n_samples]).unwrap()
    }
}

/// XGBoost Regressor
pub struct XGBoostRegressor {
    pub n_estimators: usize,
    pub learning_rate: f32,
    pub max_depth: usize,
    pub min_child_weight: f32,
    pub subsample: f32,
    pub colsample_bytree: f32,
    pub reg_lambda: f32,
    pub reg_alpha: f32,
    pub gamma: f32,
    trees: Vec<XGBTree>,
    base_score: f32,
}

impl XGBoostRegressor {
    pub fn new(n_estimators: usize) -> Self {
        Self {
            n_estimators,
            learning_rate: 0.3,
            max_depth: 6,
            min_child_weight: 1.0,
            subsample: 1.0,
            colsample_bytree: 1.0,
            reg_lambda: 1.0,
            reg_alpha: 0.0,
            gamma: 0.0,
            trees: Vec::new(),
            base_score: 0.0,
        }
    }

    pub fn learning_rate(mut self, lr: f32) -> Self {
        self.learning_rate = lr;
        self
    }

    pub fn max_depth(mut self, depth: usize) -> Self {
        self.max_depth = depth;
        self
    }

    pub fn fit(&mut self, x: &Tensor, y: &Tensor) {
        let n_samples = x.dims()[0];
        let n_features = x.dims()[1];
        let x_data = x.data_f32();
        let y_data = y.data_f32();

        // Initialize with mean
        self.base_score = y_data.iter().sum::<f32>() / n_samples as f32;
        let mut predictions = vec![self.base_score; n_samples];

        // Build trees
        for _ in 0..self.n_estimators {
            // Calculate gradients (residuals for MSE)
            let gradients: Vec<f32> = (0..n_samples)
                .map(|i| predictions[i] - y_data[i])
                .collect();
            
            // Hessians are constant 1.0 for MSE
            let hessians = vec![1.0; n_samples];

            // Row subsampling
            let mut rng = thread_rng();
            let sample_indices: Vec<usize> = if self.subsample < 1.0 {
                let n_subsample = (n_samples as f32 * self.subsample) as usize;
                (0..n_samples).choose_multiple(&mut rng, n_subsample)
            } else {
                (0..n_samples).collect()
            };

            // Column subsampling
            let feature_indices: Vec<usize> = if self.colsample_bytree < 1.0 {
                let n_features_sample = (n_features as f32 * self.colsample_bytree) as usize;
                (0..n_features).choose_multiple(&mut rng, n_features_sample)
            } else {
                (0..n_features).collect()
            };

            // Build tree (reuse XGBoostClassifier's tree building logic)
            let classifier = XGBoostClassifier {
                n_estimators: 1,
                learning_rate: self.learning_rate,
                max_depth: self.max_depth,
                min_child_weight: self.min_child_weight,
                subsample: 1.0, // Already subsampled
                colsample_bytree: 1.0, // Already subsampled
                reg_lambda: self.reg_lambda,
                reg_alpha: self.reg_alpha,
                gamma: self.gamma,
                trees: Vec::new(),
                base_score: 0.0,
            };

            let tree = classifier.build_tree(
                &x_data,
                &gradients,
                &hessians,
                &sample_indices,
                &feature_indices,
                n_features,
                0,
            );

            // Update predictions
            for i in 0..n_samples {
                let leaf_value = classifier.predict_tree(&tree, &x_data[i * n_features..(i + 1) * n_features]);
                predictions[i] += self.learning_rate * leaf_value;
            }

            self.trees.push(tree);
        }
    }

    pub fn predict(&self, x: &Tensor) -> Tensor {
        let n_samples = x.dims()[0];
        let n_features = x.dims()[1];
        let x_data = x.data_f32();

        let predictions: Vec<f32> = (0..n_samples)
            .into_par_iter()
            .map(|i| {
                let sample = &x_data[i * n_features..(i + 1) * n_features];
                let mut pred = self.base_score;
                
                let classifier = XGBoostClassifier {
                    n_estimators: 0,
                    learning_rate: self.learning_rate,
                    max_depth: 0,
                    min_child_weight: 0.0,
                    subsample: 0.0,
                    colsample_bytree: 0.0,
                    reg_lambda: 0.0,
                    reg_alpha: 0.0,
                    gamma: 0.0,
                    trees: Vec::new(),
                    base_score: 0.0,
                };

                for tree in &self.trees {
                    pred += self.learning_rate * classifier.predict_tree(tree, sample);
                }

                pred
            })
            .collect();

        Tensor::from_slice(&predictions, &[n_samples]).unwrap()
    }
}

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

    #[test]
    fn test_xgboost_classifier() {
        // Simple binary classification test
        let x = Tensor::from_slice(
            &[0.0f32, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0],
            &[4, 2],
        ).unwrap();
        let y = Tensor::from_slice(&[0.0f32, 1.0, 1.0, 0.0], &[4]).unwrap();

        let mut xgb = XGBoostClassifier::new(10)
            .learning_rate(0.1)
            .max_depth(3);
        
        xgb.fit(&x, &y);
        let predictions = xgb.predict(&x);

        assert_eq!(predictions.dims()[0], 4); // Number of samples
    }

    #[test]
    fn test_xgboost_regressor() {
        let x = Tensor::from_slice(
            &[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0],
            &[3, 2],
        ).unwrap();
        let y = Tensor::from_slice(&[2.0f32, 4.0, 6.0], &[3]).unwrap();

        let mut xgb = XGBoostRegressor::new(10)
            .learning_rate(0.1)
            .max_depth(3);
        
        xgb.fit(&x, &y);
        let predictions = xgb.predict(&x);

        assert_eq!(predictions.dims()[0], 3); // Number of samples
    }
}