scirs2-metrics 0.4.2

Machine Learning evaluation metrics module for SciRS2 (scirs2-metrics)
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
//! Neural components for adaptive streaming metrics
//!
//! This module provides neural network-based components for parameter optimization,
//! feature extraction, and adaptive learning in streaming environments.

#![allow(clippy::too_many_arguments)]
#![allow(dead_code)]

use crate::error::Result;
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::numeric::Float;
use scirs2_core::random::{Rng, RngExt};
use serde::{Deserialize, Serialize};
use std::time::Duration;

/// Neural network configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkConfig {
    /// Hidden layer sizes for parameter optimizer
    pub optimizer_hidden_layers: Vec<usize>,
    /// Hidden layer sizes for performance predictor
    pub predictor_hidden_layers: Vec<usize>,
    /// Activation function type
    pub activation: ActivationFunction,
    /// Dropout rate for regularization
    pub dropout_rate: f64,
    /// Batch normalization enabled
    pub batch_norm: bool,
    /// Learning rate for neural networks
    pub learning_rate: f64,
    /// Weight decay for regularization
    pub weight_decay: f64,
}

impl Default for NetworkConfig {
    fn default() -> Self {
        Self {
            optimizer_hidden_layers: vec![128, 64, 32],
            predictor_hidden_layers: vec![64, 32, 16],
            activation: ActivationFunction::ReLU,
            dropout_rate: 0.1,
            batch_norm: true,
            learning_rate: 0.001,
            weight_decay: 0.0001,
        }
    }
}

/// Activation functions for neural networks
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ActivationFunction {
    ReLU,
    LeakyReLU { alpha: f64 },
    ELU { alpha: f64 },
    Swish,
    GELU,
    Tanh,
    Sigmoid,
}

/// Feature extraction configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FeatureConfig {
    /// Feature extraction method
    pub extraction_method: FeatureExtractionMethod,
    /// Number of features to extract
    pub num_features: usize,
    /// Time window for feature extraction
    pub time_window: Duration,
    /// Enable automatic feature selection
    pub auto_feature_selection: bool,
    /// Feature normalization method
    pub normalization: FeatureNormalization,
}

/// Feature extraction methods
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FeatureExtractionMethod {
    /// Statistical features (mean, std, skewness, etc.)
    Statistical,
    /// Time-series features (trends, seasonality, etc.)
    TimeSeries,
    /// Frequency domain features (FFT-based)
    FrequencyDomain,
    /// Wavelet-based features
    Wavelet { wavelet_type: String },
    /// Neural autoencoder features
    NeuralAutoencoder { encoding_dim: usize },
    /// Ensemble of multiple methods
    Ensemble {
        methods: Vec<FeatureExtractionMethod>,
    },
}

/// Feature normalization methods
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FeatureNormalization {
    None,
    StandardScore,
    MinMax,
    Robust,
    Quantile,
}

/// Multi-armed bandit for parameter exploration
#[derive(Debug)]
pub struct MultiArmedBandit<F: Float + std::fmt::Debug> {
    /// Available actions (parameter configurations)
    actions: Vec<ParameterConfiguration<F>>,
    /// Bandit algorithm
    algorithm: BanditAlgorithm<F>,
    /// Action rewards history
    rewards: Vec<Vec<F>>,
    /// Action selection counts
    counts: Vec<usize>,
    /// Current exploration rate
    exploration_rate: f64,
    /// Regret tracking
    regret_tracker: RegretTracker<F>,
}

/// Parameter configuration for bandit actions
#[derive(Debug, Clone)]
pub struct ParameterConfiguration<F: Float + std::fmt::Debug> {
    /// Parameter name
    pub name: String,
    /// Parameter value
    pub value: F,
    /// Parameter bounds
    pub bounds: (F, F),
    /// Parameter type
    pub param_type: ParameterType,
}

/// Parameter types for optimization
#[derive(Debug, Clone)]
pub enum ParameterType {
    Continuous,
    Discrete,
    Integer,
    Boolean,
    Categorical(Vec<String>),
}

/// Bandit algorithms
#[derive(Debug)]
pub enum BanditAlgorithm<F: Float> {
    /// Epsilon-greedy exploration
    EpsilonGreedy { epsilon: f64 },
    /// Upper Confidence Bound
    UCB { confidence_level: f64 },
    /// Thompson Sampling
    ThompsonSampling,
    /// Exponential-weight algorithm
    Exp3 { learning_rate: F },
}

/// Regret tracking for bandit performance
#[derive(Debug, Clone)]
pub struct RegretTracker<F: Float + std::fmt::Debug> {
    /// Cumulative regret
    cumulative_regret: F,
    /// Instantaneous regret history
    regret_history: Vec<F>,
    /// Best possible reward (oracle)
    best_reward: F,
    /// Total rounds played
    total_rounds: usize,
}

impl<F: Float + std::fmt::Debug> Default for RegretTracker<F> {
    fn default() -> Self {
        Self {
            cumulative_regret: F::zero(),
            regret_history: Vec::new(),
            best_reward: F::zero(),
            total_rounds: 0,
        }
    }
}

impl<F: Float + std::fmt::Debug> RegretTracker<F> {
    pub fn update(&mut self, reward: F, optimal_reward: F) {
        let regret = optimal_reward - reward;
        self.cumulative_regret = self.cumulative_regret + regret;
        self.regret_history.push(regret);
        self.best_reward = F::max(self.best_reward, optimal_reward);
        self.total_rounds += 1;
    }

    pub fn get_average_regret(&self) -> F {
        if self.total_rounds > 0 {
            self.cumulative_regret / F::from(self.total_rounds).expect("Failed to convert to float")
        } else {
            F::zero()
        }
    }
}

impl<F: Float + std::fmt::Debug + std::ops::AddAssign + std::iter::Sum> MultiArmedBandit<F> {
    pub fn new(actions: Vec<ParameterConfiguration<F>>, algorithm: BanditAlgorithm<F>) -> Self {
        let num_actions = actions.len();
        Self {
            actions,
            algorithm,
            rewards: vec![Vec::new(); num_actions],
            counts: vec![0; num_actions],
            exploration_rate: 0.1,
            regret_tracker: RegretTracker::default(),
        }
    }

    pub fn select_action(&mut self) -> Result<usize> {
        match &self.algorithm {
            BanditAlgorithm::EpsilonGreedy { epsilon } => {
                let mut rng = scirs2_core::random::thread_rng();
                if rng.random::<f64>() < *epsilon {
                    // Explore: random action
                    Ok(rng.random_range(0..self.actions.len()))
                } else {
                    // Exploit: best action so far
                    self.get_best_action()
                }
            }
            BanditAlgorithm::UCB { confidence_level } => self.get_ucb_action(*confidence_level),
            _ => {
                // Default to epsilon-greedy
                self.get_best_action()
            }
        }
    }

    fn get_best_action(&self) -> Result<usize> {
        let mut best_action = 0;
        let mut best_average = F::neg_infinity();

        for (i, rewards) in self.rewards.iter().enumerate() {
            if !rewards.is_empty() {
                let average = rewards.iter().cloned().sum::<F>()
                    / F::from(rewards.len()).expect("Operation failed");
                if average > best_average {
                    best_average = average;
                    best_action = i;
                }
            }
        }

        Ok(best_action)
    }

    fn get_ucb_action(&self, confidence_level: f64) -> Result<usize> {
        let total_counts: usize = self.counts.iter().sum();
        let mut best_action = 0;
        let mut best_ucb = F::neg_infinity();

        for (i, rewards) in self.rewards.iter().enumerate() {
            let ucb = if rewards.is_empty() {
                F::infinity() // Unplayed actions have infinite UCB
            } else {
                let average = rewards.iter().cloned().sum::<F>()
                    / F::from(rewards.len()).expect("Operation failed");
                let confidence_bonus =
                    F::from(confidence_level * (total_counts as f64 / self.counts[i] as f64).ln())
                        .expect("Operation failed")
                        .sqrt();
                average + confidence_bonus
            };

            if ucb > best_ucb {
                best_ucb = ucb;
                best_action = i;
            }
        }

        Ok(best_action)
    }

    pub fn update_reward(&mut self, action: usize, reward: F) -> Result<()> {
        if action >= self.actions.len() {
            return Err(crate::error::MetricsError::InvalidInput(
                "Invalid action index".to_string(),
            ));
        }

        self.rewards[action].push(reward);
        self.counts[action] += 1;

        // Update regret tracking
        let optimal_reward = self
            .rewards
            .iter()
            .filter_map(|r| {
                if r.is_empty() {
                    None
                } else {
                    Some(r.iter().cloned().sum::<F>() / F::from(r.len()).expect("Operation failed"))
                }
            })
            .fold(F::neg_infinity(), F::max);

        self.regret_tracker.update(reward, optimal_reward);

        Ok(())
    }

    pub fn get_action_statistics(&self) -> Vec<(usize, F, F)> {
        self.rewards
            .iter()
            .enumerate()
            .map(|(i, rewards)| {
                if rewards.is_empty() {
                    (0, F::zero(), F::zero())
                } else {
                    let count = rewards.len();
                    let mean = rewards.iter().cloned().sum::<F>()
                        / F::from(count).expect("Failed to convert to float");
                    let variance = rewards.iter().map(|&x| (x - mean) * (x - mean)).sum::<F>()
                        / F::from(count.max(1)).expect("Operation failed");
                    (count, mean, variance)
                }
            })
            .collect()
    }
}

/// Neural feature extractor
#[derive(Debug)]
pub struct NeuralFeatureExtractor<F: Float + std::fmt::Debug> {
    /// Input dimension
    input_dim: usize,
    /// Output dimension
    output_dim: usize,
    /// Autoencoder network
    autoencoder: AutoencoderNetwork<F>,
    /// Feature selection network
    feature_selector: FeatureSelectionNetwork<F>,
    /// Attention mechanism for important features
    attention: AttentionMechanism<F>,
}

/// Autoencoder network for feature extraction
#[derive(Debug, Clone)]
pub struct AutoencoderNetwork<F: Float + std::fmt::Debug> {
    /// Encoder layers
    encoder_layers: Vec<usize>,
    /// Decoder layers
    decoder_layers: Vec<usize>,
    /// Bottleneck dimension
    encoding_dim: usize,
    /// Network weights (simplified)
    weights: Vec<Array2<F>>,
}

/// Feature selection network
#[derive(Debug, Clone)]
pub struct FeatureSelectionNetwork<F: Float + std::fmt::Debug> {
    /// Selection threshold
    threshold: F,
    /// Importance scores
    importance_scores: Array1<F>,
    /// Selected feature indices
    selected_features: Vec<usize>,
}

/// Attention mechanism for feature importance
#[derive(Debug, Clone)]
pub struct AttentionMechanism<F: Float + std::fmt::Debug> {
    /// Attention type
    attention_type: AttentionType,
    /// Attention weights
    attention_weights: Array1<F>,
    /// Key dimension
    key_dim: usize,
    /// Value dimension
    value_dim: usize,
}

/// Types of attention mechanisms
#[derive(Debug, Clone)]
pub enum AttentionType {
    SelfAttention,
    MultiHead { num_heads: usize },
    CrossAttention,
}

/// Adaptive learning rate scheduler
#[derive(Debug)]
pub struct AdaptiveLearningScheduler<F: Float + std::fmt::Debug> {
    /// Initial learning rate
    initial_lr: F,
    /// Current learning rate
    current_lr: F,
    /// Scheduler type
    scheduler_type: SchedulerType<F>,
    /// Performance history for adaptation
    performance_history: Vec<F>,
    /// Adaptation parameters
    adaptation_params: SchedulerAdaptationParams<F>,
}

impl<F: Float + std::fmt::Debug> AdaptiveLearningScheduler<F> {
    pub fn new(initial_lr: F, scheduler_type: SchedulerType<F>) -> Self {
        Self {
            initial_lr,
            current_lr: initial_lr,
            scheduler_type,
            performance_history: Vec::new(),
            adaptation_params: SchedulerAdaptationParams::default(),
        }
    }

    pub fn update(&mut self, performance: F) -> F {
        self.performance_history.push(performance);

        // Keep only recent history for memory efficiency
        if self.performance_history.len() > 100 {
            self.performance_history.remove(0);
        }

        match &self.scheduler_type {
            SchedulerType::StepLR { step_size, gamma } => {
                if self.performance_history.len().is_multiple_of(*step_size) {
                    self.current_lr = self.current_lr * *gamma;
                }
            }
            SchedulerType::ReduceLROnPlateau {
                factor, patience, ..
            } => {
                if self.performance_history.len() > *patience {
                    let recent_performance =
                        &self.performance_history[self.performance_history.len() - patience..];
                    let is_plateau = recent_performance.windows(2).all(|w| {
                        (w[1] - w[0]).abs()
                            < F::from(0.001).expect("Failed to convert constant to float")
                    });
                    if is_plateau {
                        self.current_lr = self.current_lr * *factor;
                    }
                }
            }
            _ => {}
        }

        self.current_lr
    }

    pub fn get_learning_rate(&self) -> F {
        self.current_lr
    }

    pub fn reset(&mut self) {
        self.current_lr = self.initial_lr;
        self.performance_history.clear();
    }
}

/// Learning rate scheduler types
#[derive(Debug, Clone)]
pub enum SchedulerType<F: Float> {
    /// Constant learning rate
    Constant,
    /// Step-wise decay
    StepLR { step_size: usize, gamma: F },
    /// Exponential decay
    ExponentialLR { gamma: F },
    /// Reduce on plateau
    ReduceLROnPlateau {
        factor: F,
        patience: usize,
        threshold: F,
    },
    /// Cosine annealing
    CosineAnnealingLR { t_max: usize },
}

/// Scheduler adaptation parameters
#[derive(Debug, Clone)]
pub struct SchedulerAdaptationParams<F: Float + std::fmt::Debug> {
    /// Minimum learning rate
    pub min_lr: F,
    /// Maximum learning rate
    pub max_lr: F,
    /// Adaptation sensitivity
    pub sensitivity: F,
}

impl<F: Float + std::fmt::Debug> Default for SchedulerAdaptationParams<F> {
    fn default() -> Self {
        Self {
            min_lr: F::from(1e-6).expect("Failed to convert constant to float"),
            max_lr: F::from(1e-1).expect("Failed to convert constant to float"),
            sensitivity: F::from(0.1).expect("Failed to convert constant to float"),
        }
    }
}

// Simplified implementations for the other neural components
impl<F: Float + std::fmt::Debug + Send + Sync + scirs2_core::ndarray::ScalarOperand>
    AutoencoderNetwork<F>
{
    pub fn new(input_dim: usize, encoding_dim: usize) -> Self {
        Self {
            encoder_layers: vec![input_dim, encoding_dim * 2, encoding_dim],
            decoder_layers: vec![encoding_dim, encoding_dim * 2, input_dim],
            encoding_dim,
            weights: Vec::new(), // Would be properly initialized in real implementation
        }
    }

    pub fn encode(&self, _input: &Array1<F>) -> Result<Array1<F>> {
        // Simplified encoding - would implement actual forward pass
        Ok(Array1::zeros(self.encoding_dim))
    }
}

impl<F: Float + std::fmt::Debug> AttentionMechanism<F> {
    pub fn new(key_dim: usize, value_dim: usize, attention_type: AttentionType) -> Self {
        Self {
            attention_type,
            attention_weights: Array1::zeros(key_dim),
            key_dim,
            value_dim,
        }
    }
}

impl<F: Float + std::fmt::Debug + Send + Sync + scirs2_core::ndarray::ScalarOperand>
    FeatureSelectionNetwork<F>
{
    pub fn new(num_features: usize, threshold: F) -> Self {
        Self {
            threshold,
            importance_scores: Array1::zeros(num_features),
            selected_features: Vec::new(),
        }
    }

    pub fn select_features(&mut self, scores: &Array1<F>) -> Vec<usize> {
        self.importance_scores = scores.clone();
        self.selected_features = scores
            .iter()
            .enumerate()
            .filter(|(_, &score)| score > self.threshold)
            .map(|(i, _)| i)
            .collect();
        self.selected_features.clone()
    }
}

impl<F: Float + std::fmt::Debug + Send + Sync + scirs2_core::ndarray::ScalarOperand>
    NeuralFeatureExtractor<F>
{
    pub fn new(input_dim: usize, output_dim: usize) -> Self {
        Self {
            input_dim,
            output_dim,
            autoencoder: AutoencoderNetwork::new(input_dim, output_dim),
            feature_selector: FeatureSelectionNetwork::new(
                input_dim,
                F::from(0.1).expect("Failed to convert constant to float"),
            ),
            attention: AttentionMechanism::new(input_dim, output_dim, AttentionType::SelfAttention),
        }
    }

    pub fn extract_features(&self, input: &Array1<F>) -> Result<Array1<F>> {
        // Simplified feature extraction
        self.autoencoder.encode(input)
    }
}