sklears-dummy 0.1.2

Dummy estimators for baseline comparisons
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
use super::validation_core::*;
use super::validation_metrics::*;

use scirs2_core::ndarray::{Array1, Axis};
use scirs2_core::random::{Rng, RngExt};
use sklears_core::error::{Result, SklearsError};
use sklears_core::traits::{Fit, Predict};
use sklears_core::types::{Features, Float, Int};
use std::collections::HashMap;

use crate::{ClassifierStrategy, DummyClassifier, DummyRegressor, RegressorStrategy};

/// Bootstrap validation result
#[derive(Debug, Clone)]
pub struct BootstrapValidationResult {
    /// bootstrap_scores
    pub bootstrap_scores: Vec<Float>,
    /// mean_score
    pub mean_score: Float,
    /// std_score
    pub std_score: Float,
    /// confidence_interval
    pub confidence_interval: (Float, Float),
    /// bias
    pub bias: Float,
    /// strategy
    pub strategy: String,
    /// n_bootstrap_samples
    pub n_bootstrap_samples: usize,
}

impl BootstrapValidationResult {
    pub fn new(bootstrap_scores: Vec<Float>, strategy: String, confidence_level: Float) -> Self {
        let n_bootstrap_samples = bootstrap_scores.len();
        let mean_score = bootstrap_scores.iter().sum::<Float>() / n_bootstrap_samples as Float;

        let variance = bootstrap_scores
            .iter()
            .map(|&score| (score - mean_score).powi(2))
            .sum::<Float>()
            / n_bootstrap_samples as Float;
        let std_score = variance.sqrt();

        // Calculate confidence interval using percentile method
        let mut sorted_scores = bootstrap_scores.clone();
        sorted_scores.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let alpha = 1.0 - confidence_level;
        let lower_idx = (alpha / 2.0 * n_bootstrap_samples as Float) as usize;
        let upper_idx = ((1.0 - alpha / 2.0) * n_bootstrap_samples as Float) as usize;

        let lower_bound = sorted_scores[lower_idx.min(n_bootstrap_samples - 1)];
        let upper_bound = sorted_scores[upper_idx.min(n_bootstrap_samples - 1)];
        let confidence_interval = (lower_bound, upper_bound);

        // Bias calculation (simplified)
        let bias = 0.0; // Would require original score for proper bias calculation

        Self {
            bootstrap_scores,
            mean_score,
            std_score,
            confidence_interval,
            bias,
            strategy,
            n_bootstrap_samples,
        }
    }

    pub fn percentile(&self, p: Float) -> Float {
        let mut sorted_scores = self.bootstrap_scores.clone();
        sorted_scores.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let idx = (p * self.n_bootstrap_samples as Float) as usize;
        sorted_scores[idx.min(self.n_bootstrap_samples - 1)]
    }

    pub fn bootstrap_distribution_summary(&self) -> StatisticalSummary {
        StatisticalSummary::from_scores(&self.bootstrap_scores)
    }
}

/// Perform bootstrap validation for a dummy classifier
pub fn bootstrap_validate_classifier(
    classifier: DummyClassifier,
    x: &Features,
    y: &Array1<Int>,
    n_bootstrap: usize,
    random_state: Option<u64>,
) -> Result<BootstrapValidationResult> {
    if n_bootstrap < 1 {
        return Err(SklearsError::InvalidInput(
            "Number of bootstrap samples must be at least 1".to_string(),
        ));
    }

    let n_samples = x.nrows();
    if n_samples == 0 {
        return Err(SklearsError::InvalidInput(
            "Cannot perform bootstrap validation on empty dataset".to_string(),
        ));
    }

    let mut rng = create_rng(random_state);
    let mut bootstrap_scores = Vec::with_capacity(n_bootstrap);

    for _ in 0..n_bootstrap {
        // Create bootstrap sample
        let bootstrap_indices = create_bootstrap_sample(n_samples, &mut *rng);
        let oob_indices = create_out_of_bag_indices(&bootstrap_indices, n_samples);

        if oob_indices.is_empty() {
            continue; // Skip if no out-of-bag samples
        }

        // Extract bootstrap training data
        let x_bootstrap = x.select(Axis(0), &bootstrap_indices);
        let y_bootstrap = y.select(Axis(0), &bootstrap_indices);

        // Extract out-of-bag test data
        let x_oob = x.select(Axis(0), &oob_indices);
        let y_oob = y.select(Axis(0), &oob_indices);

        // Fit on bootstrap sample and predict on out-of-bag
        let fitted = classifier.clone().fit(&x_bootstrap, &y_bootstrap)?;
        let predictions = fitted.predict(&x_oob)?;

        // Calculate accuracy
        let correct = predictions
            .iter()
            .zip(y_oob.iter())
            .filter(|(&pred, &actual)| pred == actual)
            .count();
        let accuracy = correct as Float / oob_indices.len() as Float;
        bootstrap_scores.push(accuracy);
    }

    if bootstrap_scores.is_empty() {
        return Err(SklearsError::InvalidInput(
            "No valid bootstrap samples created".to_string(),
        ));
    }

    Ok(BootstrapValidationResult::new(
        bootstrap_scores,
        format!("{:?}", classifier.strategy),
        0.95,
    ))
}

/// Perform bootstrap validation for a dummy regressor
pub fn bootstrap_validate_regressor(
    regressor: DummyRegressor,
    x: &Features,
    y: &Array1<Float>,
    n_bootstrap: usize,
    random_state: Option<u64>,
) -> Result<BootstrapValidationResult> {
    if n_bootstrap < 1 {
        return Err(SklearsError::InvalidInput(
            "Number of bootstrap samples must be at least 1".to_string(),
        ));
    }

    let n_samples = x.nrows();
    if n_samples == 0 {
        return Err(SklearsError::InvalidInput(
            "Cannot perform bootstrap validation on empty dataset".to_string(),
        ));
    }

    let mut rng = create_rng(random_state);
    let mut bootstrap_scores = Vec::with_capacity(n_bootstrap);

    for _ in 0..n_bootstrap {
        // Create bootstrap sample
        let bootstrap_indices = create_bootstrap_sample(n_samples, &mut *rng);
        let oob_indices = create_out_of_bag_indices(&bootstrap_indices, n_samples);

        if oob_indices.is_empty() {
            continue; // Skip if no out-of-bag samples
        }

        // Extract bootstrap training data
        let x_bootstrap = x.select(Axis(0), &bootstrap_indices);
        let y_bootstrap = y.select(Axis(0), &bootstrap_indices);

        // Extract out-of-bag test data
        let x_oob = x.select(Axis(0), &oob_indices);
        let y_oob = y.select(Axis(0), &oob_indices);

        // Fit on bootstrap sample and predict on out-of-bag
        let fitted = regressor.clone().fit(&x_bootstrap, &y_bootstrap)?;
        let predictions = fitted.predict(&x_oob)?;

        // Calculate negative MSE
        let mse = predictions
            .iter()
            .zip(y_oob.iter())
            .map(|(&pred, &actual)| (pred - actual).powi(2))
            .sum::<Float>()
            / oob_indices.len() as Float;
        bootstrap_scores.push(-mse);
    }

    if bootstrap_scores.is_empty() {
        return Err(SklearsError::InvalidInput(
            "No valid bootstrap samples created".to_string(),
        ));
    }

    Ok(BootstrapValidationResult::new(
        bootstrap_scores,
        format!("{:?}", regressor.strategy),
        0.95,
    ))
}

/// Create a bootstrap sample of indices
fn create_bootstrap_sample(n_samples: usize, rng: &mut dyn Rng) -> Vec<usize> {
    (0..n_samples)
        .map(|_| rng.random_range(0..n_samples))
        .collect()
}

/// Create out-of-bag indices (samples not in bootstrap sample)
fn create_out_of_bag_indices(bootstrap_indices: &[usize], n_samples: usize) -> Vec<usize> {
    let mut in_bootstrap = vec![false; n_samples];
    for &idx in bootstrap_indices {
        in_bootstrap[idx] = true;
    }

    (0..n_samples).filter(|&i| !in_bootstrap[i]).collect()
}

/// Perform bootstrap validation with multiple strategies
pub fn bootstrap_compare_strategies(
    strategies: &[String],
    x: &Features,
    y: &Array1<Float>,
    n_bootstrap: usize,
    random_state: Option<u64>,
) -> Result<Vec<BootstrapValidationResult>> {
    if strategies.is_empty() {
        return Err(SklearsError::InvalidInput(
            "At least one strategy must be provided".to_string(),
        ));
    }

    let mut results = Vec::new();
    let is_classification = is_classification_task(y);

    if is_classification {
        let y_int: Array1<Int> = y.mapv(|x| x as Int);

        for strategy_name in strategies {
            let strategy = parse_classifier_strategy(strategy_name)?;
            let classifier = DummyClassifier::new(strategy);
            let result =
                bootstrap_validate_classifier(classifier, x, &y_int, n_bootstrap, random_state)?;
            results.push(result);
        }
    } else {
        for strategy_name in strategies {
            let strategy = parse_regressor_strategy(strategy_name)?;
            let regressor = DummyRegressor::new(strategy);
            let result = bootstrap_validate_regressor(regressor, x, y, n_bootstrap, random_state)?;
            results.push(result);
        }
    }

    Ok(results)
}

/// Bootstrap hypothesis test for comparing two strategies
pub fn bootstrap_hypothesis_test(
    strategy1: DummyClassifier,
    strategy2: DummyClassifier,
    x: &Features,
    y: &Array1<Int>,
    n_bootstrap: usize,
    random_state: Option<u64>,
) -> Result<BootstrapHypothesisTest> {
    let mut rng = create_rng(random_state);
    let n_samples = x.nrows();

    let mut differences = Vec::with_capacity(n_bootstrap);

    for _ in 0..n_bootstrap {
        // Create bootstrap sample
        let bootstrap_indices = create_bootstrap_sample(n_samples, &mut *rng);
        let oob_indices = create_out_of_bag_indices(&bootstrap_indices, n_samples);

        if oob_indices.is_empty() {
            continue;
        }

        // Extract data
        let x_bootstrap = x.select(Axis(0), &bootstrap_indices);
        let y_bootstrap = y.select(Axis(0), &bootstrap_indices);
        let x_oob = x.select(Axis(0), &oob_indices);
        let y_oob = y.select(Axis(0), &oob_indices);

        // Evaluate both strategies
        let fitted1 = strategy1.clone().fit(&x_bootstrap, &y_bootstrap)?;
        let predictions1 = fitted1.predict(&x_oob)?;
        let score1 = calculate_classification_score(&predictions1, &y_oob, "accuracy")?;

        let fitted2 = strategy2.clone().fit(&x_bootstrap, &y_bootstrap)?;
        let predictions2 = fitted2.predict(&x_oob)?;
        let score2 = calculate_classification_score(&predictions2, &y_oob, "accuracy")?;

        differences.push(score1 - score2);
    }

    Ok(BootstrapHypothesisTest::new(differences))
}

/// Bootstrap hypothesis test result
#[derive(Debug, Clone)]
pub struct BootstrapHypothesisTest {
    /// differences
    pub differences: Vec<Float>,
    /// mean_difference
    pub mean_difference: Float,
    /// std_difference
    pub std_difference: Float,
    /// p_value
    pub p_value: Float,
    /// confidence_interval
    pub confidence_interval: (Float, Float),
}

impl BootstrapHypothesisTest {
    pub fn new(differences: Vec<Float>) -> Self {
        let n = differences.len();
        let mean_difference = differences.iter().sum::<Float>() / n as Float;

        let variance = differences
            .iter()
            .map(|&d| (d - mean_difference).powi(2))
            .sum::<Float>()
            / n as Float;
        let std_difference = variance.sqrt();

        // Calculate p-value (two-tailed test for H0: difference = 0)
        let negative_count = differences.iter().filter(|&&d| d < 0.0).count();
        let positive_count = differences.iter().filter(|&&d| d > 0.0).count();
        let p_value = 2.0 * (negative_count.min(positive_count) as Float / n as Float);

        // Calculate 95% confidence interval
        let mut sorted_diffs = differences.clone();
        sorted_diffs.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let lower_idx = (0.025 * n as Float) as usize;
        let upper_idx = (0.975 * n as Float) as usize;
        let confidence_interval = (
            sorted_diffs[lower_idx.min(n - 1)],
            sorted_diffs[upper_idx.min(n - 1)],
        );

        Self {
            differences,
            mean_difference,
            std_difference,
            p_value,
            confidence_interval,
        }
    }

    pub fn is_significant(&self, alpha: Float) -> bool {
        self.p_value < alpha
    }

    pub fn effect_size(&self) -> Float {
        if self.std_difference > 0.0 {
            self.mean_difference / self.std_difference
        } else {
            0.0
        }
    }
}

/// Stratified bootstrap validation
pub fn stratified_bootstrap_validate_classifier(
    classifier: DummyClassifier,
    x: &Features,
    y: &Array1<Int>,
    n_bootstrap: usize,
    random_state: Option<u64>,
) -> Result<BootstrapValidationResult> {
    let mut rng = create_rng(random_state);

    // Group indices by class
    let mut class_indices: HashMap<Int, Vec<usize>> = HashMap::new();
    for (i, &class) in y.iter().enumerate() {
        class_indices.entry(class).or_default().push(i);
    }

    let mut bootstrap_scores = Vec::with_capacity(n_bootstrap);

    for _ in 0..n_bootstrap {
        let mut bootstrap_indices = Vec::new();
        let mut oob_indices = Vec::new();

        // Create stratified bootstrap sample
        for indices in class_indices.values() {
            let class_bootstrap = create_bootstrap_sample(indices.len(), &mut *rng);
            let class_bootstrap_indices: Vec<usize> =
                class_bootstrap.iter().map(|&i| indices[i]).collect();

            let class_oob = create_out_of_bag_indices(&class_bootstrap, indices.len());
            let class_oob_indices: Vec<usize> = class_oob.iter().map(|&i| indices[i]).collect();

            bootstrap_indices.extend(class_bootstrap_indices);
            oob_indices.extend(class_oob_indices);
        }

        if oob_indices.is_empty() {
            continue;
        }

        // Extract data and evaluate
        let x_bootstrap = x.select(Axis(0), &bootstrap_indices);
        let y_bootstrap = y.select(Axis(0), &bootstrap_indices);
        let x_oob = x.select(Axis(0), &oob_indices);
        let y_oob = y.select(Axis(0), &oob_indices);

        let fitted = classifier.clone().fit(&x_bootstrap, &y_bootstrap)?;
        let predictions = fitted.predict(&x_oob)?;

        let correct = predictions
            .iter()
            .zip(y_oob.iter())
            .filter(|(&pred, &actual)| pred == actual)
            .count();
        let accuracy = correct as Float / oob_indices.len() as Float;
        bootstrap_scores.push(accuracy);
    }

    if bootstrap_scores.is_empty() {
        return Err(SklearsError::InvalidInput(
            "No valid bootstrap samples created".to_string(),
        ));
    }

    Ok(BootstrapValidationResult::new(
        bootstrap_scores,
        format!("{:?}", classifier.strategy),
        0.95,
    ))
}

/// Helper functions for parsing strategies (simplified versions)
fn parse_classifier_strategy(strategy: &str) -> Result<ClassifierStrategy> {
    match strategy.to_lowercase().as_str() {
        "mostfrequent" | "most_frequent" => Ok(ClassifierStrategy::MostFrequent),
        "stratified" => Ok(ClassifierStrategy::Stratified),
        "uniform" => Ok(ClassifierStrategy::Uniform),
        "constant" => Ok(ClassifierStrategy::Constant),
        _ => Err(SklearsError::InvalidInput(format!(
            "Unknown classifier strategy: {}",
            strategy
        ))),
    }
}

fn parse_regressor_strategy(strategy: &str) -> Result<RegressorStrategy> {
    match strategy.to_lowercase().as_str() {
        "mean" => Ok(RegressorStrategy::Mean),
        "median" => Ok(RegressorStrategy::Median),
        "quantile" => Ok(RegressorStrategy::Quantile(0.5)),
        "constant" => Ok(RegressorStrategy::Constant(0.0)),
        _ => Err(SklearsError::InvalidInput(format!(
            "Unknown regressor strategy: {}",
            strategy
        ))),
    }
}