sklears_svm/hyperparameter_optimization/
mod.rs

1//! Hyperparameter optimization for SVM algorithms
2//!
3//! This module provides comprehensive hyperparameter optimization utilities for SVM models,
4//! including grid search, random search, and Bayesian optimization approaches.
5//!
6//! Methods included:
7//! - Grid Search: Exhaustive search over parameter grid
8//! - Random Search: Random sampling from parameter distributions
9//! - Bayesian Optimization: Gaussian Process-based optimization
10//! - Evolutionary Algorithms: Genetic algorithm for parameter search
11//! - Cross-validation integration for robust evaluation
12
13pub mod bayesian_optimization;
14pub mod evolutionary_optimization;
15pub mod grid_search;
16pub mod random_search;
17
18pub use bayesian_optimization::BayesianOptimizationCV;
19pub use evolutionary_optimization::EvolutionaryOptimizationCV;
20pub use grid_search::GridSearchCV;
21pub use random_search::RandomSearchCV;
22
23use crate::kernels::KernelType;
24
25/// Parameter specification for optimization
26#[derive(Debug, Clone)]
27pub enum ParameterSpec {
28    /// Fixed value
29    Fixed(f64),
30    /// Uniform distribution between min and max
31    Uniform { min: f64, max: f64 },
32    /// Log-uniform distribution between min and max
33    LogUniform { min: f64, max: f64 },
34    /// Discrete choices
35    Choice(Vec<f64>),
36    /// Kernel choices
37    KernelChoice(Vec<KernelType>),
38}
39
40/// Hyperparameter search space
41#[derive(Debug, Clone)]
42pub struct SearchSpace {
43    /// Regularization parameter C
44    pub c: ParameterSpec,
45    /// Kernel parameter gamma (for RBF kernel)
46    pub gamma: Option<ParameterSpec>,
47    /// Polynomial degree (for polynomial kernel)
48    pub degree: Option<ParameterSpec>,
49    /// Kernel coefficient (for polynomial/sigmoid kernels)
50    pub coef0: Option<ParameterSpec>,
51    /// Kernel type
52    pub kernel: Option<ParameterSpec>,
53    /// Tolerance
54    pub tol: Option<ParameterSpec>,
55    /// Maximum iterations
56    pub max_iter: Option<ParameterSpec>,
57}
58
59impl Default for SearchSpace {
60    fn default() -> Self {
61        Self {
62            c: ParameterSpec::LogUniform {
63                min: 1e-3,
64                max: 1e3,
65            },
66            gamma: Some(ParameterSpec::LogUniform {
67                min: 1e-4,
68                max: 1e2,
69            }),
70            degree: Some(ParameterSpec::Choice(vec![2.0, 3.0, 4.0, 5.0])),
71            coef0: Some(ParameterSpec::Uniform { min: 0.0, max: 1.0 }),
72            kernel: Some(ParameterSpec::KernelChoice(vec![
73                KernelType::Linear,
74                KernelType::Rbf { gamma: 1.0 },
75                KernelType::Polynomial {
76                    gamma: 1.0,
77                    degree: 3.0,
78                    coef0: 1.0,
79                },
80            ])),
81            tol: Some(ParameterSpec::LogUniform {
82                min: 1e-6,
83                max: 1e-2,
84            }),
85            max_iter: Some(ParameterSpec::Choice(vec![100.0, 500.0, 1000.0, 5000.0])),
86        }
87    }
88}
89
90/// Configuration for optimization algorithms
91#[derive(Debug, Clone)]
92pub struct OptimizationConfig {
93    /// Number of iterations for optimization
94    pub n_iterations: usize,
95    /// Number of cross-validation folds
96    pub cv_folds: usize,
97    /// Scoring metric
98    pub scoring: ScoringMetric,
99    /// Random seed for reproducibility
100    pub random_state: Option<u64>,
101    /// Parallel execution
102    pub n_jobs: Option<usize>,
103    /// Verbose output
104    pub verbose: bool,
105    /// Early stopping patience
106    pub early_stopping_patience: Option<usize>,
107}
108
109impl Default for OptimizationConfig {
110    fn default() -> Self {
111        Self {
112            n_iterations: 100,
113            cv_folds: 5,
114            scoring: ScoringMetric::Accuracy,
115            random_state: Some(42),
116            n_jobs: None,
117            verbose: false,
118            early_stopping_patience: Some(10),
119        }
120    }
121}
122
123/// Scoring metrics for evaluation
124#[derive(Debug, Clone)]
125pub enum ScoringMetric {
126    Accuracy,
127    Precision,
128    Recall,
129    F1Score,
130    AUC,
131    MeanSquaredError,
132    MeanAbsoluteError,
133    R2Score,
134}
135
136/// Parameter set for SVM
137#[derive(Debug, Clone)]
138pub struct ParameterSet {
139    pub c: f64,
140    pub kernel: KernelType,
141    pub tol: f64,
142    pub max_iter: usize,
143}
144
145impl ParameterSet {
146    pub fn new() -> Self {
147        Self {
148            c: 1.0,
149            kernel: KernelType::Rbf { gamma: 1.0 },
150            tol: 1e-3,
151            max_iter: 1000,
152        }
153    }
154}
155
156impl Default for ParameterSet {
157    fn default() -> Self {
158        Self::new()
159    }
160}
161
162/// Optimization result
163#[derive(Debug, Clone)]
164pub struct OptimizationResult {
165    /// Best parameter set found
166    pub best_params: ParameterSet,
167    /// Best cross-validation score
168    pub best_score: f64,
169    /// All parameter sets and scores tried
170    pub cv_results: Vec<(ParameterSet, f64)>,
171    /// Number of iterations performed
172    pub n_iterations: usize,
173    /// Total optimization time
174    pub optimization_time: f64,
175    /// Convergence history
176    pub score_history: Vec<f64>,
177}