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
633
634
635
//! Ensemble methods - Random Forest and Gradient Boosting

use ghostflow_core::Tensor;
use crate::tree::{DecisionTreeClassifier, DecisionTreeRegressor, Criterion};
use rayon::prelude::*;
use rand::prelude::*;

/// Random Forest Classifier
pub struct RandomForestClassifier {
    pub n_estimators: usize,
    pub max_depth: Option<usize>,
    pub min_samples_split: usize,
    pub min_samples_leaf: usize,
    pub max_features: Option<usize>,
    pub bootstrap: bool,
    pub n_jobs: Option<usize>,
    trees: Vec<DecisionTreeClassifier>,
    n_classes: usize,
}

impl RandomForestClassifier {
    pub fn new(n_estimators: usize) -> Self {
        RandomForestClassifier {
            n_estimators,
            max_depth: None,
            min_samples_split: 2,
            min_samples_leaf: 1,
            max_features: None, // sqrt(n_features) by default
            bootstrap: true,
            n_jobs: None,
            trees: Vec::new(),
            n_classes: 0,
        }
    }

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

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

    pub fn max_features(mut self, n: usize) -> Self {
        self.max_features = Some(n);
        self
    }

    pub fn fit(&mut self, x: &Tensor, y: &Tensor) {
        let n_samples = x.dims()[0];
        let n_features = x.dims()[1];
        
        // Determine number of classes
        let y_data = y.data_f32();
        self.n_classes = y_data.iter()
            .map(|&v| v as usize)
            .max()
            .unwrap_or(0) + 1;
        
        // Default max_features = sqrt(n_features)
        let max_features = self.max_features
            .unwrap_or_else(|| (n_features as f32).sqrt() as usize);
        
        // Build trees in parallel
        self.trees = (0..self.n_estimators)
            .into_par_iter()
            .map(|_| {
                let mut rng = thread_rng();
                
                // Bootstrap sampling
                let indices: Vec<usize> = if self.bootstrap {
                    (0..n_samples).map(|_| rng.gen_range(0..n_samples)).collect()
                } else {
                    (0..n_samples).collect()
                };
                
                // Create bootstrap sample
                let x_data = x.data_f32();
                let y_data = y.data_f32();
                
                let x_boot: Vec<f32> = indices.iter()
                    .flat_map(|&i| x_data[i * n_features..(i + 1) * n_features].to_vec())
                    .collect();
                let y_boot: Vec<f32> = indices.iter()
                    .map(|&i| y_data[i])
                    .collect();
                
                let x_tensor = Tensor::from_slice(&x_boot, &[indices.len(), n_features]).unwrap();
                let y_tensor = Tensor::from_slice(&y_boot, &[indices.len()]).unwrap();
                
                // Build tree
                let mut tree = DecisionTreeClassifier::new()
                    .criterion(Criterion::Gini);
                
                if let Some(depth) = self.max_depth {
                    tree = tree.max_depth(depth);
                }
                tree = tree.min_samples_split(self.min_samples_split)
                    .min_samples_leaf(self.min_samples_leaf);
                
                tree.max_features = Some(max_features);
                tree.fit(&x_tensor, &y_tensor);
                tree
            })
            .collect();
    }

    pub fn predict(&self, x: &Tensor) -> Tensor {
        let proba = self.predict_proba(x);
        let proba_data = proba.data_f32();
        let n_samples = x.dims()[0];
        
        let predictions: Vec<f32> = (0..n_samples)
            .map(|i| {
                let start = i * self.n_classes;
                let sample_probs = &proba_data[start..start + self.n_classes];
                sample_probs.iter()
                    .enumerate()
                    .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap())
                    .map(|(idx, _)| idx as f32)
                    .unwrap_or(0.0)
            })
            .collect();
        
        Tensor::from_slice(&predictions, &[n_samples]).unwrap()
    }

    pub fn predict_proba(&self, x: &Tensor) -> Tensor {
        let n_samples = x.dims()[0];
        
        // Collect predictions from all trees
        let all_probs: Vec<Vec<f32>> = self.trees.par_iter()
            .map(|tree| tree.predict_proba(x).data_f32())
            .collect();
        
        // Average probabilities
        let mut avg_probs = vec![0.0f32; n_samples * self.n_classes];
        
        for probs in &all_probs {
            for (i, &p) in probs.iter().enumerate() {
                avg_probs[i] += p;
            }
        }
        
        let n_trees = self.trees.len() as f32;
        for p in &mut avg_probs {
            *p /= n_trees;
        }
        
        Tensor::from_slice(&avg_probs, &[n_samples, self.n_classes]).unwrap()
    }

    /// Feature importance based on impurity decrease
    pub fn feature_importances(&self, n_features: usize) -> Vec<f32> {
        let mut importances = vec![0.0f32; n_features];
        
        // This would require tracking impurity decrease during tree building
        // For now, return uniform importances
        let uniform = 1.0 / n_features as f32;
        importances.iter_mut().for_each(|x| *x = uniform);
        
        importances
    }
}

/// Random Forest Regressor
pub struct RandomForestRegressor {
    pub n_estimators: usize,
    pub max_depth: Option<usize>,
    pub min_samples_split: usize,
    pub min_samples_leaf: usize,
    pub max_features: Option<usize>,
    pub bootstrap: bool,
    trees: Vec<DecisionTreeRegressor>,
}

impl RandomForestRegressor {
    pub fn new(n_estimators: usize) -> Self {
        RandomForestRegressor {
            n_estimators,
            max_depth: None,
            min_samples_split: 2,
            min_samples_leaf: 1,
            max_features: None,
            bootstrap: true,
            trees: Vec::new(),
        }
    }

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

    pub fn fit(&mut self, x: &Tensor, y: &Tensor) {
        let n_samples = x.dims()[0];
        let n_features = x.dims()[1];
        
        let max_features = self.max_features
            .unwrap_or(n_features / 3);
        
        self.trees = (0..self.n_estimators)
            .into_par_iter()
            .map(|_| {
                let mut rng = thread_rng();
                
                let indices: Vec<usize> = if self.bootstrap {
                    (0..n_samples).map(|_| rng.gen_range(0..n_samples)).collect()
                } else {
                    (0..n_samples).collect()
                };
                
                let x_data = x.data_f32();
                let y_data = y.data_f32();
                
                let x_boot: Vec<f32> = indices.iter()
                    .flat_map(|&i| x_data[i * n_features..(i + 1) * n_features].to_vec())
                    .collect();
                let y_boot: Vec<f32> = indices.iter()
                    .map(|&i| y_data[i])
                    .collect();
                
                let x_tensor = Tensor::from_slice(&x_boot, &[indices.len(), n_features]).unwrap();
                let y_tensor = Tensor::from_slice(&y_boot, &[indices.len()]).unwrap();
                
                let mut tree = DecisionTreeRegressor::new();
                if let Some(depth) = self.max_depth {
                    tree = tree.max_depth(depth);
                }
                tree.max_features = Some(max_features);
                tree.fit(&x_tensor, &y_tensor);
                tree
            })
            .collect();
    }

    pub fn predict(&self, x: &Tensor) -> Tensor {
        let n_samples = x.dims()[0];
        
        let all_preds: Vec<Vec<f32>> = self.trees.par_iter()
            .map(|tree| tree.predict(x).data_f32())
            .collect();
        
        let mut avg_preds = vec![0.0f32; n_samples];
        
        for preds in &all_preds {
            for (i, &p) in preds.iter().enumerate() {
                avg_preds[i] += p;
            }
        }
        
        let n_trees = self.trees.len() as f32;
        for p in &mut avg_preds {
            *p /= n_trees;
        }
        
        Tensor::from_slice(&avg_preds, &[n_samples]).unwrap()
    }
}

/// Gradient Boosting Classifier
pub struct GradientBoostingClassifier {
    pub n_estimators: usize,
    pub learning_rate: f32,
    pub max_depth: usize,
    pub min_samples_split: usize,
    pub subsample: f32,
    trees: Vec<Vec<DecisionTreeRegressor>>, // One set of trees per class
    initial_predictions: Vec<f32>,
    n_classes: usize,
}

impl GradientBoostingClassifier {
    pub fn new(n_estimators: usize) -> Self {
        GradientBoostingClassifier {
            n_estimators,
            learning_rate: 0.1,
            max_depth: 3,
            min_samples_split: 2,
            subsample: 1.0,
            trees: Vec::new(),
            initial_predictions: Vec::new(),
            n_classes: 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();
        
        self.n_classes = y_data.iter()
            .map(|&v| v as usize)
            .max()
            .unwrap_or(0) + 1;
        
        // Initialize with log-odds
        self.initial_predictions = vec![0.0; self.n_classes];
        for c in 0..self.n_classes {
            let count = y_data.iter().filter(|&&v| v as usize == c).count();
            let prob = count as f32 / n_samples as f32;
            self.initial_predictions[c] = (prob / (1.0 - prob + 1e-10)).ln();
        }
        
        // Initialize predictions
        let mut f = vec![vec![0.0f32; n_samples]; self.n_classes];
        #[allow(clippy::needless_range_loop)]
        for c in 0..self.n_classes {
            for i in 0..n_samples {
                f[c][i] = self.initial_predictions[c];
            }
        }
        
        self.trees = (0..self.n_classes).map(|_| Vec::new()).collect();
        
        // Boosting iterations
        for _ in 0..self.n_estimators {
            // Compute probabilities using softmax
            let probs = self.softmax_predictions(&f);
            
            // For each class, fit a tree to the negative gradient
            for c in 0..self.n_classes {
                // Compute residuals (negative gradient)
                let residuals: Vec<f32> = (0..n_samples)
                    .map(|i| {
                        let y_true = if y_data[i] as usize == c { 1.0 } else { 0.0 };
                        y_true - probs[c][i]
                    })
                    .collect();
                
                let _residual_tensor = Tensor::from_slice(&residuals, &[n_samples]).unwrap();
                
                // Subsample
                let sample_indices: Vec<usize> = if self.subsample < 1.0 {
                    let mut rng = thread_rng();
                    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()
                };
                
                let x_sub: Vec<f32> = sample_indices.iter()
                    .flat_map(|&i| x_data[i * n_features..(i + 1) * n_features].to_vec())
                    .collect();
                let r_sub: Vec<f32> = sample_indices.iter()
                    .map(|&i| residuals[i])
                    .collect();
                
                let x_tensor = Tensor::from_slice(&x_sub, &[sample_indices.len(), n_features]).unwrap();
                let r_tensor = Tensor::from_slice(&r_sub, &[sample_indices.len()]).unwrap();
                
                // Fit tree
                let mut tree = DecisionTreeRegressor::new()
                    .max_depth(self.max_depth);
                tree.min_samples_split = self.min_samples_split;
                tree.fit(&x_tensor, &r_tensor);
                
                // Update predictions
                let tree_preds = tree.predict(x);
                let tree_pred_data = tree_preds.data_f32();
                
                #[allow(clippy::needless_range_loop)]
                for i in 0..n_samples {
                    f[c][i] += self.learning_rate * tree_pred_data[i];
                }
                
                self.trees[c].push(tree);
            }
        }
    }

    fn softmax_predictions(&self, f: &[Vec<f32>]) -> Vec<Vec<f32>> {
        let n_samples = f[0].len();
        let mut probs = vec![vec![0.0f32; n_samples]; self.n_classes];
        
        for i in 0..n_samples {
            let max_f = (0..self.n_classes).map(|c| f[c][i]).fold(f32::NEG_INFINITY, f32::max);
            let sum_exp: f32 = (0..self.n_classes).map(|c| (f[c][i] - max_f).exp()).sum();
            
            for c in 0..self.n_classes {
                probs[c][i] = (f[c][i] - max_f).exp() / sum_exp;
            }
        }
        
        probs
    }

    pub fn predict(&self, x: &Tensor) -> Tensor {
        let proba = self.predict_proba(x);
        let proba_data = proba.data_f32();
        let n_samples = x.dims()[0];
        
        let predictions: Vec<f32> = (0..n_samples)
            .map(|i| {
                let start = i * self.n_classes;
                let sample_probs = &proba_data[start..start + self.n_classes];
                sample_probs.iter()
                    .enumerate()
                    .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap())
                    .map(|(idx, _)| idx as f32)
                    .unwrap_or(0.0)
            })
            .collect();
        
        Tensor::from_slice(&predictions, &[n_samples]).unwrap()
    }

    pub fn predict_proba(&self, x: &Tensor) -> Tensor {
        let n_samples = x.dims()[0];
        
        // Initialize with initial predictions
        let mut f = vec![vec![0.0f32; n_samples]; self.n_classes];
        #[allow(clippy::needless_range_loop)]
        for c in 0..self.n_classes {
            for i in 0..n_samples {
                f[c][i] = self.initial_predictions[c];
            }
        }
        
        // Add tree predictions
        for c in 0..self.n_classes {
            for tree in &self.trees[c] {
                let preds = tree.predict(x);
                let pred_data = preds.data_f32();
                for i in 0..n_samples {
                    f[c][i] += self.learning_rate * pred_data[i];
                }
            }
        }
        
        // Softmax
        let probs = self.softmax_predictions(&f);
        
        let mut result = Vec::with_capacity(n_samples * self.n_classes);
        for i in 0..n_samples {
            for c in 0..self.n_classes {
                result.push(probs[c][i]);
            }
        }
        
        Tensor::from_slice(&result, &[n_samples, self.n_classes]).unwrap()
    }
}

/// Gradient Boosting Regressor
pub struct GradientBoostingRegressor {
    pub n_estimators: usize,
    pub learning_rate: f32,
    pub max_depth: usize,
    pub min_samples_split: usize,
    pub subsample: f32,
    pub loss: GBLoss,
    trees: Vec<DecisionTreeRegressor>,
    initial_prediction: f32,
}

#[derive(Clone, Copy)]
pub enum GBLoss {
    SquaredError,
    AbsoluteError,
    Huber,
}

impl GradientBoostingRegressor {
    pub fn new(n_estimators: usize) -> Self {
        GradientBoostingRegressor {
            n_estimators,
            learning_rate: 0.1,
            max_depth: 3,
            min_samples_split: 2,
            subsample: 1.0,
            loss: GBLoss::SquaredError,
            trees: Vec::new(),
            initial_prediction: 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 loss(mut self, loss: GBLoss) -> Self {
        self.loss = loss;
        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.initial_prediction = y_data.iter().sum::<f32>() / n_samples as f32;
        
        let mut predictions = vec![self.initial_prediction; n_samples];
        
        for _ in 0..self.n_estimators {
            // Compute residuals
            let residuals: Vec<f32> = match self.loss {
                GBLoss::SquaredError => {
                    (0..n_samples).map(|i| y_data[i] - predictions[i]).collect()
                }
                GBLoss::AbsoluteError => {
                    (0..n_samples).map(|i| {
                        let diff = y_data[i] - predictions[i];
                        if diff > 0.0 { 1.0 } else if diff < 0.0 { -1.0 } else { 0.0 }
                    }).collect()
                }
                GBLoss::Huber => {
                    let delta = 1.0;
                    (0..n_samples).map(|i| {
                        let diff = y_data[i] - predictions[i];
                        if diff.abs() <= delta {
                            diff
                        } else {
                            delta * diff.signum()
                        }
                    }).collect()
                }
            };
            
            // Subsample
            let sample_indices: Vec<usize> = if self.subsample < 1.0 {
                let mut rng = thread_rng();
                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()
            };
            
            let x_sub: Vec<f32> = sample_indices.iter()
                .flat_map(|&i| x_data[i * n_features..(i + 1) * n_features].to_vec())
                .collect();
            let r_sub: Vec<f32> = sample_indices.iter()
                .map(|&i| residuals[i])
                .collect();
            
            let x_tensor = Tensor::from_slice(&x_sub, &[sample_indices.len(), n_features]).unwrap();
            let r_tensor = Tensor::from_slice(&r_sub, &[sample_indices.len()]).unwrap();
            
            // Fit tree
            let mut tree = DecisionTreeRegressor::new()
                .max_depth(self.max_depth);
            tree.min_samples_split = self.min_samples_split;
            tree.fit(&x_tensor, &r_tensor);
            
            // Update predictions
            let tree_preds = tree.predict(x);
            let tree_pred_data = tree_preds.data_f32();
            
            for i in 0..n_samples {
                predictions[i] += self.learning_rate * tree_pred_data[i];
            }
            
            self.trees.push(tree);
        }
    }

    pub fn predict(&self, x: &Tensor) -> Tensor {
        let n_samples = x.dims()[0];
        let mut predictions = vec![self.initial_prediction; n_samples];
        
        for tree in &self.trees {
            let tree_preds = tree.predict(x);
            let tree_pred_data = tree_preds.data_f32();
            
            for i in 0..n_samples {
                predictions[i] += self.learning_rate * tree_pred_data[i];
            }
        }
        
        Tensor::from_slice(&predictions, &[n_samples]).unwrap()
    }
}

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

    #[test]
    fn test_random_forest_classifier() {
        let x = Tensor::from_slice(&[0.0f32, 0.0,
            0.0, 1.0,
            1.0, 0.0,
            1.0, 1.0,
            0.5, 0.5,
        ], &[5, 2]).unwrap();
        
        let y = Tensor::from_slice(&[0.0f32, 1.0, 1.0, 0.0, 0.0], &[5]).unwrap();
        
        let mut rf = RandomForestClassifier::new(10).max_depth(3);
        rf.fit(&x, &y);
        
        let predictions = rf.predict(&x);
        assert_eq!(predictions.dims(), &[5]);
    }

    #[test]
    fn test_gradient_boosting_regressor() {
        let x = Tensor::from_slice(&[1.0f32, 2.0, 3.0, 4.0, 5.0,
        ], &[5, 1]).unwrap();
        
        let y = Tensor::from_slice(&[2.0f32, 4.0, 6.0, 8.0, 10.0], &[5]).unwrap();
        
        let mut gb = GradientBoostingRegressor::new(50)
            .learning_rate(0.1)
            .max_depth(3);
        gb.fit(&x, &y);
        
        let predictions = gb.predict(&x);
        assert_eq!(predictions.dims(), &[5]);
    }
}