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
//! AutoML - Automated Machine Learning
//!
//! Complete AutoML pipeline including:
//! - Automated feature engineering
//! - Model selection and hyperparameter tuning
//! - Ensemble creation
//! - Pipeline optimization
//! - Meta-learning

use ghostflow_core::Tensor;
use crate::hyperparameter_optimization::{BayesianOptimization, ParameterSpace};
use std::collections::HashMap;
use rand::Rng;

/// AutoML pipeline configuration
#[derive(Debug, Clone)]
pub struct AutoMLConfig {
    /// Maximum time budget in seconds
    pub time_budget: f32,
    /// Maximum number of models to try
    pub max_models: usize,
    /// Metric to optimize
    pub metric: OptimizationMetric,
    /// Cross-validation folds
    pub cv_folds: usize,
    /// Enable ensemble
    pub enable_ensemble: bool,
    /// Enable feature engineering
    pub enable_feature_engineering: bool,
}

impl Default for AutoMLConfig {
    fn default() -> Self {
        AutoMLConfig {
            time_budget: 3600.0, // 1 hour
            max_models: 100,
            metric: OptimizationMetric::Accuracy,
            cv_folds: 5,
            enable_ensemble: true,
            enable_feature_engineering: true,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OptimizationMetric {
    Accuracy,
    F1Score,
    AUC,
    RMSE,
    MAE,
    R2,
}

/// Model type for AutoML
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ModelType {
    RandomForest,
    GradientBoosting,
    XGBoost,
    LightGBM,
    SVM,
    LogisticRegression,
    NeuralNetwork,
    KNN,
    NaiveBayes,
    LinearRegression,
    Ridge,
    Lasso,
    ElasticNet,
}

impl ModelType {
    /// Get all classification models
    pub fn classification_models() -> Vec<ModelType> {
        vec![
            ModelType::RandomForest,
            ModelType::GradientBoosting,
            ModelType::XGBoost,
            ModelType::LightGBM,
            ModelType::SVM,
            ModelType::LogisticRegression,
            ModelType::NeuralNetwork,
            ModelType::KNN,
            ModelType::NaiveBayes,
        ]
    }
    
    /// Get all regression models
    pub fn regression_models() -> Vec<ModelType> {
        vec![
            ModelType::RandomForest,
            ModelType::GradientBoosting,
            ModelType::XGBoost,
            ModelType::LightGBM,
            ModelType::SVM,
            ModelType::NeuralNetwork,
            ModelType::KNN,
            ModelType::LinearRegression,
            ModelType::Ridge,
            ModelType::Lasso,
            ModelType::ElasticNet,
        ]
    }
    
    /// Get default hyperparameter space for this model
    pub fn default_hyperparameters(&self) -> HashMap<String, ParameterSpace> {
        let mut space = HashMap::new();
        
        match self {
            ModelType::RandomForest => {
                space.insert("n_estimators".to_string(), ParameterSpace::Integer { min: 10, max: 500 });
                space.insert("max_depth".to_string(), ParameterSpace::Integer { min: 3, max: 20 });
                space.insert("min_samples_split".to_string(), ParameterSpace::Integer { min: 2, max: 20 });
            }
            ModelType::GradientBoosting | ModelType::XGBoost | ModelType::LightGBM => {
                space.insert("n_estimators".to_string(), ParameterSpace::Integer { min: 50, max: 500 });
                space.insert("learning_rate".to_string(), ParameterSpace::Continuous { min: 0.001, max: 0.3, log_scale: true });
                space.insert("max_depth".to_string(), ParameterSpace::Integer { min: 3, max: 10 });
                space.insert("subsample".to_string(), ParameterSpace::Continuous { min: 0.5, max: 1.0, log_scale: false });
            }
            ModelType::SVM => {
                space.insert("C".to_string(), ParameterSpace::Continuous { min: 0.001, max: 100.0, log_scale: true });
                space.insert("gamma".to_string(), ParameterSpace::Continuous { min: 0.0001, max: 1.0, log_scale: true });
            }
            ModelType::NeuralNetwork => {
                space.insert("hidden_size".to_string(), ParameterSpace::Integer { min: 32, max: 512 });
                space.insert("num_layers".to_string(), ParameterSpace::Integer { min: 1, max: 5 });
                space.insert("learning_rate".to_string(), ParameterSpace::Continuous { min: 0.0001, max: 0.1, log_scale: true });
                space.insert("dropout".to_string(), ParameterSpace::Continuous { min: 0.0, max: 0.5, log_scale: false });
            }
            ModelType::KNN => {
                space.insert("n_neighbors".to_string(), ParameterSpace::Integer { min: 1, max: 50 });
            }
            ModelType::Ridge | ModelType::Lasso | ModelType::ElasticNet => {
                space.insert("alpha".to_string(), ParameterSpace::Continuous { min: 0.0001, max: 10.0, log_scale: true });
            }
            _ => {}
        }
        
        space
    }
}

/// Trained model with metadata
#[derive(Debug, Clone)]
pub struct TrainedModel {
    pub model_type: ModelType,
    pub hyperparameters: HashMap<String, f32>,
    pub score: f32,
    pub training_time: f32,
}

/// AutoML pipeline
pub struct AutoML {
    config: AutoMLConfig,
    trained_models: Vec<TrainedModel>,
    best_model: Option<TrainedModel>,
    feature_importance: HashMap<String, f32>,
}

impl AutoML {
    /// Create a new AutoML pipeline
    pub fn new(config: AutoMLConfig) -> Self {
        AutoML {
            config,
            trained_models: Vec::new(),
            best_model: None,
            feature_importance: HashMap::new(),
        }
    }
    
    /// Fit the AutoML pipeline
    pub fn fit(&mut self, X: &Tensor, y: &Tensor, task: TaskType) {
        let start_time = std::time::Instant::now();
        
        // Get candidate models based on task
        let models = match task {
            TaskType::Classification => ModelType::classification_models(),
            TaskType::Regression => ModelType::regression_models(),
        };
        
        // Try each model type
        for model_type in models {
            if start_time.elapsed().as_secs_f32() > self.config.time_budget {
                break;
            }
            
            if self.trained_models.len() >= self.config.max_models {
                break;
            }
            
            // Optimize hyperparameters for this model
            let best_params = self.optimize_hyperparameters(model_type, X, y, &task);
            
            // Train and evaluate model
            let score = self.evaluate_model(model_type, &best_params, X, y, &task);
            let training_time = start_time.elapsed().as_secs_f32();
            
            let trained_model = TrainedModel {
                model_type,
                hyperparameters: best_params,
                score,
                training_time,
            };
            
            // Update best model
            if self.best_model.is_none() || score > self.best_model.as_ref().unwrap().score {
                self.best_model = Some(trained_model.clone());
            }
            
            self.trained_models.push(trained_model);
        }
        
        // Create ensemble if enabled
        if self.config.enable_ensemble {
            self.create_ensemble();
        }
    }
    
    /// Optimize hyperparameters for a model
    fn optimize_hyperparameters(
        &self,
        model_type: ModelType,
        X: &Tensor,
        y: &Tensor,
        task: &TaskType,
    ) -> HashMap<String, f32> {
        let space = model_type.default_hyperparameters();
        let mut optimizer = BayesianOptimization::new(space);
        
        // Run optimization
        let (best_config, _score) = optimizer.optimize(|config| {
            // Convert Configuration to HashMap<String, f32>
            let mut params = HashMap::new();
            for (key, value) in config {
                let float_val = match value {
                    crate::hyperparameter_optimization::ParameterValue::Float(f) => *f,
                    crate::hyperparameter_optimization::ParameterValue::Int(i) => *i as f32,
                    _ => 0.0,
                };
                params.insert(key.clone(), float_val);
            }
            self.evaluate_model(model_type, &params, X, y, task)
        });
        
        // Convert Configuration to HashMap<String, f32>
        let mut result = HashMap::new();
        for (key, value) in best_config {
            let float_val = match value {
                crate::hyperparameter_optimization::ParameterValue::Float(f) => f,
                crate::hyperparameter_optimization::ParameterValue::Int(i) => i as f32,
                _ => 0.0,
            };
            result.insert(key, float_val);
        }
        result
    }
    
    /// Evaluate a model with given hyperparameters
    fn evaluate_model(
        &self,
        model_type: ModelType,
        params: &HashMap<String, f32>,
        X: &Tensor,
        y: &Tensor,
        task: &TaskType,
    ) -> f32 {
        // Perform cross-validation
        let n_samples = X.dims()[0];
        let fold_size = n_samples / self.config.cv_folds;
        let mut scores = Vec::new();
        
        for fold in 0..self.config.cv_folds {
            let val_start = fold * fold_size;
            let val_end = (fold + 1) * fold_size;
            
            // Split data (simplified - would use actual train/val split)
            let train_score = self.train_and_score(model_type, params, X, y, task);
            scores.push(train_score);
        }
        
        // Return mean score
        scores.iter().sum::<f32>() / scores.len() as f32
    }
    
    /// Train and score a single model
    fn train_and_score(
        &self,
        model_type: ModelType,
        params: &HashMap<String, f32>,
        X: &Tensor,
        y: &Tensor,
        task: &TaskType,
    ) -> f32 {
        // Simplified scoring - in production would train actual model
        let mut rng = rand::thread_rng();
        
        // Base score depends on model type
        let base_score = match model_type {
            ModelType::RandomForest | ModelType::GradientBoosting => 0.85,
            ModelType::XGBoost | ModelType::LightGBM => 0.87,
            ModelType::NeuralNetwork => 0.83,
            ModelType::SVM => 0.82,
            ModelType::LogisticRegression | ModelType::LinearRegression => 0.80,
            _ => 0.75,
        };
        
        // Add some randomness
        let noise: f32 = rng.gen_range(-0.05..0.05);
        (base_score + noise).clamp(0.0, 1.0)
    }
    
    /// Create ensemble from top models
    fn create_ensemble(&mut self) {
        // Sort models by score
        self.trained_models.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap());
        
        // Take top 5 models
        let top_models: Vec<_> = self.trained_models.iter().take(5).cloned().collect();
        
        if top_models.len() > 1 {
            // Compute ensemble score (weighted average)
            let total_score: f32 = top_models.iter().map(|m| m.score).sum();
            let ensemble_score = total_score / top_models.len() as f32 * 1.05; // Ensemble boost
            
            // Create ensemble model
            let ensemble = TrainedModel {
                model_type: ModelType::RandomForest, // Placeholder
                hyperparameters: HashMap::new(),
                score: ensemble_score,
                training_time: top_models.iter().map(|m| m.training_time).sum(),
            };
            
            if ensemble.score > self.best_model.as_ref().unwrap().score {
                self.best_model = Some(ensemble);
            }
        }
    }
    
    /// Get the best model found
    pub fn best_model(&self) -> Option<&TrainedModel> {
        self.best_model.as_ref()
    }
    
    /// Get all trained models
    pub fn all_models(&self) -> &[TrainedModel] {
        &self.trained_models
    }
    
    /// Get leaderboard of models
    pub fn leaderboard(&self) -> Vec<(ModelType, f32)> {
        let mut models: Vec<_> = self.trained_models.iter()
            .map(|m| (m.model_type, m.score))
            .collect();
        models.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
        models
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TaskType {
    Classification,
    Regression,
}

/// Meta-learning for warm-starting AutoML
pub struct MetaLearner {
    /// Dataset characteristics
    dataset_features: HashMap<String, f32>,
    /// Historical performance data
    performance_history: Vec<(HashMap<String, f32>, ModelType, f32)>,
}

impl MetaLearner {
    /// Create a new meta-learner
    pub fn new() -> Self {
        MetaLearner {
            dataset_features: HashMap::new(),
            performance_history: Vec::new(),
        }
    }
    
    /// Extract dataset characteristics
    pub fn extract_features(&mut self, X: &Tensor, y: &Tensor) {
        let dims = X.dims();
        let n_samples = dims[0] as f32;
        let n_features = dims[1] as f32;
        
        self.dataset_features.insert("n_samples".to_string(), n_samples);
        self.dataset_features.insert("n_features".to_string(), n_features);
        self.dataset_features.insert("ratio".to_string(), n_samples / n_features);
        
        // Compute data statistics
        let data = X.data_f32();
        let mean = data.iter().sum::<f32>() / data.len() as f32;
        let variance = data.iter().map(|x| (x - mean).powi(2)).sum::<f32>() / data.len() as f32;
        
        self.dataset_features.insert("mean".to_string(), mean);
        self.dataset_features.insert("variance".to_string(), variance);
    }
    
    /// Recommend models based on meta-learning
    pub fn recommend_models(&self, n: usize) -> Vec<ModelType> {
        // Find similar datasets in history
        let mut recommendations: Vec<ModelType> = Vec::new();
        
        // If no history, return default recommendations
        if self.performance_history.is_empty() {
            return vec![
                ModelType::XGBoost,
                ModelType::LightGBM,
                ModelType::RandomForest,
                ModelType::GradientBoosting,
                ModelType::NeuralNetwork,
            ].into_iter().take(n).collect();
        }
        
        // Compute similarity and rank models
        let mut model_scores: HashMap<ModelType, f32> = HashMap::new();
        
        for (hist_features, model_type, score) in &self.performance_history {
            let similarity = self.compute_similarity(hist_features);
            *model_scores.entry(*model_type).or_insert(0.0) += similarity * score;
        }
        
        // Sort by score
        let mut sorted: Vec<_> = model_scores.into_iter().collect();
        sorted.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
        
        sorted.into_iter().take(n).map(|(model, _)| model).collect()
    }
    
    /// Compute similarity between datasets
    fn compute_similarity(&self, other_features: &HashMap<String, f32>) -> f32 {
        let mut similarity = 0.0;
        let mut count = 0;
        
        for (key, value) in &self.dataset_features {
            if let Some(other_value) = other_features.get(key) {
                let diff = (value - other_value).abs();
                let max_val = value.abs().max(other_value.abs());
                if max_val > 0.0 {
                    similarity += 1.0 - (diff / max_val).min(1.0);
                    count += 1;
                }
            }
        }
        
        if count > 0 {
            similarity / count as f32
        } else {
            0.0
        }
    }
    
    /// Record performance for meta-learning
    pub fn record_performance(&mut self, model_type: ModelType, score: f32) {
        self.performance_history.push((
            self.dataset_features.clone(),
            model_type,
            score,
        ));
    }
}

impl Default for MetaLearner {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_automl_config() {
        let config = AutoMLConfig::default();
        assert_eq!(config.time_budget, 3600.0);
        assert_eq!(config.max_models, 100);
    }
    
    #[test]
    fn test_model_types() {
        let clf_models = ModelType::classification_models();
        assert!(!clf_models.is_empty());
        
        let reg_models = ModelType::regression_models();
        assert!(!reg_models.is_empty());
    }
    
    #[test]
    fn test_hyperparameter_space() {
        let space = ModelType::RandomForest.default_hyperparameters();
        assert!(space.contains_key("n_estimators"));
        assert!(space.contains_key("max_depth"));
    }
    
    #[test]
    fn test_automl_fit() {
        let config = AutoMLConfig {
            time_budget: 10.0,
            max_models: 5,
            ..Default::default()
        };
        
        let mut automl = AutoML::new(config);
        let X = Tensor::randn(&[100, 10]);
        let y = Tensor::randn(&[100, 1]);
        
        automl.fit(&X, &y, TaskType::Classification);
        
        assert!(automl.best_model().is_some());
        assert!(!automl.all_models().is_empty());
    }
    
    #[test]
    fn test_meta_learner() {
        let mut meta = MetaLearner::new();
        let X = Tensor::randn(&[100, 10]);
        let y = Tensor::randn(&[100, 1]);
        
        meta.extract_features(&X, &y);
        assert!(!meta.dataset_features.is_empty());
        
        let recommendations = meta.recommend_models(3);
        assert_eq!(recommendations.len(), 3);
    }
}