optimizer 1.0.1

Bayesian and population-based optimization library with an Optuna-like API for hyperparameter tuning and black-box optimization
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
//! Wilcoxon pruner — statistically rigorous pruning for noisy objectives.
//!
//! Uses the Wilcoxon signed-rank test to compare the current trial's
//! intermediate values against the best completed trial at matching steps.
//! The test accounts for the paired, step-aligned nature of the comparison
//! and only prunes when the difference is statistically significant.
//!
//! This is more principled than [`MedianPruner`](super::MedianPruner) for
//! noisy objectives because a single bad step won't trigger pruning — the
//! test considers the full distribution of paired differences.
//!
//! # When to use
//!
//! - When intermediate values have high variance (e.g., mini-batch loss,
//!   stochastic reward signals)
//! - When you want a statistical guarantee that pruned trials are truly worse
//! - When you have enough steps (at least 6) for a meaningful test
//!
//! For less noisy objectives, [`MedianPruner`](super::MedianPruner) is simpler
//! and often sufficient.
//!
//! # Configuration
//!
//! | Option | Default | Description |
//! |--------|---------|-------------|
//! | `p_value_threshold` | 0.05 | Significance level — lower is more conservative |
//! | `n_warmup_steps` | 0 | Skip pruning in the first N steps |
//! | `n_min_trials` | 1 | Require at least N completed trials before pruning |
//!
//! # Example
//!
//! ```
//! use optimizer::Direction;
//! use optimizer::pruner::WilcoxonPruner;
//!
//! let pruner = WilcoxonPruner::new(Direction::Minimize)
//!     .p_value_threshold(0.05)
//!     .n_warmup_steps(5)
//!     .n_min_trials(1);
//! ```

use core::cmp::Ordering;

use super::Pruner;
use crate::sampler::CompletedTrial;
use crate::types::{Direction, TrialState};

/// Prune trials using a Wilcoxon signed-rank test comparing intermediate
/// values against the best completed trial.
///
/// More principled than `MedianPruner` for noisy objectives — it accounts
/// for the paired nature of step-aligned comparisons and doesn't prune
/// on random fluctuations.
///
/// The test compares intermediate values at matching steps between the
/// current trial and the best completed trial. If the current trial is
/// statistically significantly worse (p < threshold), it is pruned.
///
/// # Examples
///
/// ```
/// use optimizer::Direction;
/// use optimizer::pruner::WilcoxonPruner;
///
/// let pruner = WilcoxonPruner::new(Direction::Minimize)
///     .p_value_threshold(0.05)
///     .n_warmup_steps(5)
///     .n_min_trials(1);
/// ```
pub struct WilcoxonPruner {
    /// Significance level (default 0.05). Lower = more conservative.
    p_value_threshold: f64,
    /// Don't prune in the first N steps (let the trial warm up).
    n_warmup_steps: u64,
    /// Require at least N completed trials before pruning.
    n_min_trials: usize,
    /// The optimization direction.
    direction: Direction,
}

impl WilcoxonPruner {
    /// Create a new `WilcoxonPruner` for the given optimization direction.
    ///
    /// By default, `p_value_threshold` is 0.05, `n_warmup_steps` is 0,
    /// and `n_min_trials` is 1.
    #[must_use]
    pub fn new(direction: Direction) -> Self {
        Self {
            p_value_threshold: 0.05,
            n_warmup_steps: 0,
            n_min_trials: 1,
            direction,
        }
    }

    /// Set the p-value threshold for significance.
    ///
    /// Must be in (0.0, 1.0). Lower values are more conservative (harder to prune).
    ///
    /// # Panics
    ///
    /// Panics if `p` is not in the open interval (0.0, 1.0).
    #[must_use]
    pub fn p_value_threshold(mut self, p: f64) -> Self {
        assert!(
            p > 0.0 && p < 1.0,
            "p_value_threshold must be in (0.0, 1.0)"
        );
        self.p_value_threshold = p;
        self
    }

    /// Set the number of warmup steps. No pruning occurs before this step.
    #[must_use]
    pub fn n_warmup_steps(mut self, n: u64) -> Self {
        self.n_warmup_steps = n;
        self
    }

    /// Set the minimum number of completed trials required before pruning.
    #[must_use]
    pub fn n_min_trials(mut self, n: usize) -> Self {
        self.n_min_trials = n;
        self
    }
}

impl Pruner for WilcoxonPruner {
    fn should_prune(
        &self,
        _trial_id: u64,
        step: u64,
        intermediate_values: &[(u64, f64)],
        completed_trials: &[CompletedTrial],
    ) -> bool {
        if step < self.n_warmup_steps {
            return false;
        }

        let completed: Vec<&CompletedTrial> = completed_trials
            .iter()
            .filter(|t| t.state == TrialState::Complete)
            .collect();

        if completed.len() < self.n_min_trials {
            return false;
        }

        // Find the best completed trial by final objective value.
        let best = match self.direction {
            Direction::Minimize => completed
                .iter()
                .min_by(|a, b| a.value.partial_cmp(&b.value).unwrap_or(Ordering::Equal)),
            Direction::Maximize => completed
                .iter()
                .max_by(|a, b| a.value.partial_cmp(&b.value).unwrap_or(Ordering::Equal)),
        };

        let Some(best) = best else {
            return false;
        };

        // Pair intermediate values at matching steps.
        let pairs: Vec<(f64, f64)> = intermediate_values
            .iter()
            .filter_map(|&(s, current_v)| {
                best.intermediate_values
                    .iter()
                    .find(|(bs, _)| *bs == s)
                    .map(|&(_, best_v)| (current_v, best_v))
            })
            .collect();

        // Need at least 6 pairs for a meaningful test.
        if pairs.len() < 6 {
            return false;
        }

        // Compute signed differences: current - best.
        // For minimization: positive diff means current is worse.
        // For maximization: negative diff means current is worse.
        let differences: Vec<f64> = pairs
            .iter()
            .map(|&(current, best_v)| current - best_v)
            .collect();

        // Run the Wilcoxon signed-rank test.
        let p_value = wilcoxon_signed_rank_test(&differences, self.direction);

        p_value < self.p_value_threshold
    }
}

/// Perform a one-sided Wilcoxon signed-rank test.
///
/// Tests whether the values tend to be worse than zero (positive for
/// minimization, negative for maximization).
///
/// Returns a p-value. Small p-values indicate the current trial is
/// significantly worse.
fn wilcoxon_signed_rank_test(differences: &[f64], direction: Direction) -> f64 {
    // 1. Remove zero differences.
    let nonzero: Vec<f64> = differences.iter().copied().filter(|d| *d != 0.0).collect();
    let n = nonzero.len();

    if n < 6 {
        return 1.0; // Not enough data
    }

    // 2. Rank by absolute value.
    let mut abs_ranked: Vec<(usize, f64, f64)> = nonzero
        .iter()
        .enumerate()
        .map(|(i, &d)| (i, d.abs(), d))
        .collect();
    abs_ranked.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal));

    // 3. Assign ranks with tie correction.
    let ranks = assign_ranks(&abs_ranked);

    // 4. Compute W+ (sum of ranks for positive differences) and
    //    W- (sum of ranks for negative differences).
    let mut w_plus = 0.0;
    let mut w_minus = 0.0;
    for (i, &(_, _, orig)) in abs_ranked.iter().enumerate() {
        if orig > 0.0 {
            w_plus += ranks[i];
        } else {
            w_minus += ranks[i];
        }
    }

    // For one-sided test:
    // - Minimization: we want to detect positive diffs (current worse).
    //   Large W+ means significantly worse. Test statistic = W-.
    // - Maximization: we want to detect negative diffs (current worse).
    //   Large W- means significantly worse. Test statistic = W+.
    let w = match direction {
        Direction::Minimize => w_minus,
        Direction::Maximize => w_plus,
    };

    // 5. Normal approximation for the p-value.
    #[allow(clippy::cast_precision_loss)]
    let n_f = n as f64;
    let mean = n_f * (n_f + 1.0) / 4.0;
    let variance = n_f * (n_f + 1.0) * (2.0 * n_f + 1.0) / 24.0;

    // Tie correction for variance.
    let tie_correction = compute_tie_correction(&ranks);
    let adjusted_variance = variance - tie_correction;

    if adjusted_variance <= 0.0 {
        return 1.0;
    }

    let std_dev = adjusted_variance.sqrt();

    // Continuity correction: shift W by 0.5 towards mean.
    let continuity = if w < mean { 0.5 } else { -0.5 };
    let z = (w + continuity - mean) / std_dev;

    // One-sided p-value (lower tail): probability that the test statistic
    // is this small or smaller under H0.
    normal_cdf(z)
}

/// Assign average ranks, handling ties.
fn assign_ranks(sorted: &[(usize, f64, f64)]) -> Vec<f64> {
    let n = sorted.len();
    let mut ranks = vec![0.0; n];
    let mut i = 0;

    while i < n {
        let mut j = i;
        // Find all items tied with sorted[i].
        while j < n
            && (sorted[j].1 - sorted[i].1).abs() < f64::EPSILON * sorted[i].1.max(1.0) * 100.0
        {
            j += 1;
        }
        // Average rank for the tie group. Ranks are 1-based.
        #[allow(clippy::cast_precision_loss)]
        let avg_rank = (i + 1 + j) as f64 / 2.0;
        for rank in ranks.iter_mut().take(j).skip(i) {
            *rank = avg_rank;
        }
        i = j;
    }

    ranks
}

/// Compute the tie correction term for the variance.
/// For each tie group of size t, subtract t^3 - t from the sum,
/// then divide by 48.
fn compute_tie_correction(ranks: &[f64]) -> f64 {
    let mut correction = 0.0;
    let mut i = 0;

    while i < ranks.len() {
        let mut j = i;
        while j < ranks.len() && (ranks[j] - ranks[i]).abs() < f64::EPSILON {
            j += 1;
        }
        #[allow(clippy::cast_precision_loss)]
        let t = (j - i) as f64;
        if t > 1.0 {
            correction += t * t * t - t;
        }
        i = j;
    }

    correction / 48.0
}

/// Standard normal CDF using an approximation (Abramowitz & Stegun).
fn normal_cdf(x: f64) -> f64 {
    // Use the complementary error function relationship:
    // Φ(x) = 0.5 * erfc(-x / √2)
    0.5 * erfc(-x / core::f64::consts::SQRT_2)
}

/// Complementary error function approximation.
/// Maximum error: 1.5 × 10⁻⁷ (Abramowitz & Stegun formula 7.1.26).
fn erfc(x: f64) -> f64 {
    let t = 1.0 / (1.0 + 0.327_591_1 * x.abs());
    let poly = t
        * (0.254_829_592
            + t * (-0.284_496_736
                + t * (1.421_413_741 + t * (-1.453_152_027 + t * 1.061_405_429))));
    let result = poly * (-x * x).exp();

    if x >= 0.0 { result } else { 2.0 - result }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use super::*;

    fn trial_with_values(
        id: u64,
        value: f64,
        intermediate_values: Vec<(u64, f64)>,
    ) -> CompletedTrial {
        CompletedTrial::with_intermediate_values(
            id,
            HashMap::new(),
            HashMap::new(),
            HashMap::new(),
            value,
            intermediate_values,
            HashMap::new(),
        )
    }

    #[test]
    fn no_prune_during_warmup() {
        let pruner = WilcoxonPruner::new(Direction::Minimize).n_warmup_steps(10);
        let completed = vec![trial_with_values(
            0,
            0.1,
            (0..20).map(|s| (s, 0.1)).collect(),
        )];
        let current: Vec<(u64, f64)> = (0..8).map(|s| (s, 100.0)).collect();
        assert!(!pruner.should_prune(1, 7, &current, &completed));
    }

    #[test]
    fn no_prune_with_insufficient_trials() {
        let pruner = WilcoxonPruner::new(Direction::Minimize).n_min_trials(5);
        let completed = vec![trial_with_values(
            0,
            0.1,
            (0..20).map(|s| (s, 0.1)).collect(),
        )];
        let current: Vec<(u64, f64)> = (0..10).map(|s| (s, 100.0)).collect();
        assert!(!pruner.should_prune(1, 9, &current, &completed));
    }

    #[test]
    fn no_prune_with_fewer_than_6_pairs() {
        let pruner = WilcoxonPruner::new(Direction::Minimize);
        let completed = vec![trial_with_values(
            0,
            0.1,
            (0..5).map(|s| (s, 0.1)).collect(),
        )];
        // Only 5 matching steps
        let current: Vec<(u64, f64)> = (0..5).map(|s| (s, 100.0)).collect();
        assert!(!pruner.should_prune(1, 4, &current, &completed));
    }

    #[test]
    fn prune_when_consistently_worse_minimize() {
        let pruner = WilcoxonPruner::new(Direction::Minimize);

        // Best trial has low values.
        let best_values: Vec<(u64, f64)> = (0..20).map(|s| (s, 0.1)).collect();
        let completed = vec![trial_with_values(0, 0.1, best_values)];

        // Current trial is consistently much worse.
        let current: Vec<(u64, f64)> = (0..20).map(|s| (s, 10.0)).collect();
        assert!(pruner.should_prune(1, 19, &current, &completed));
    }

    #[test]
    fn prune_when_consistently_worse_maximize() {
        let pruner = WilcoxonPruner::new(Direction::Maximize);

        // Best trial has high values.
        let best_values: Vec<(u64, f64)> = (0..20).map(|s| (s, 10.0)).collect();
        let completed = vec![trial_with_values(0, 10.0, best_values)];

        // Current trial is consistently much worse.
        let current: Vec<(u64, f64)> = (0..20).map(|s| (s, 0.1)).collect();
        assert!(pruner.should_prune(1, 19, &current, &completed));
    }

    #[test]
    fn no_prune_when_statistically_similar() {
        let pruner = WilcoxonPruner::new(Direction::Minimize);

        // Best trial and current trial have very similar values with noise.
        let best_values: Vec<(u64, f64)> = (0..20_u64)
            .map(|s| {
                let noise = if s.is_multiple_of(2) { 0.01 } else { -0.01 };
                (s, 1.0 + noise)
            })
            .collect();
        let completed = vec![trial_with_values(0, 1.0, best_values)];

        // Current trial is similar — alternating above/below.
        let current: Vec<(u64, f64)> = (0..20_u64)
            .map(|s| {
                let noise = if s.is_multiple_of(2) { -0.01 } else { 0.01 };
                (s, 1.0 + noise)
            })
            .collect();
        assert!(!pruner.should_prune(1, 19, &current, &completed));
    }

    #[test]
    fn selects_best_trial_minimize() {
        let pruner = WilcoxonPruner::new(Direction::Minimize);

        // Two completed trials: trial 0 is better (lower).
        let completed = vec![
            trial_with_values(0, 0.1, (0..20).map(|s| (s, 0.1)).collect()),
            trial_with_values(1, 5.0, (0..20).map(|s| (s, 5.0)).collect()),
        ];

        // Current trial is worse than the best but similar to the second.
        let current: Vec<(u64, f64)> = (0..20).map(|s| (s, 5.0)).collect();
        assert!(pruner.should_prune(2, 19, &current, &completed));
    }

    #[test]
    fn selects_best_trial_maximize() {
        let pruner = WilcoxonPruner::new(Direction::Maximize);

        // Two completed trials: trial 1 is better (higher).
        let completed = vec![
            trial_with_values(0, 0.1, (0..20).map(|s| (s, 0.1)).collect()),
            trial_with_values(1, 10.0, (0..20).map(|s| (s, 10.0)).collect()),
        ];

        // Current trial is worse than the best.
        let current: Vec<(u64, f64)> = (0..20).map(|s| (s, 0.1)).collect();
        assert!(pruner.should_prune(2, 19, &current, &completed));
    }

    #[test]
    fn ignores_pruned_trials() {
        let pruner = WilcoxonPruner::new(Direction::Minimize);

        // Only a pruned trial — no complete trials.
        let mut trial = trial_with_values(0, 0.1, (0..20).map(|s| (s, 0.1)).collect());
        trial.state = TrialState::Pruned;
        let completed = vec![trial];

        let current: Vec<(u64, f64)> = (0..20).map(|s| (s, 100.0)).collect();
        assert!(!pruner.should_prune(1, 19, &current, &completed));
    }

    #[test]
    fn lower_p_value_is_more_conservative() {
        let strict = WilcoxonPruner::new(Direction::Minimize).p_value_threshold(0.001);
        let lenient = WilcoxonPruner::new(Direction::Minimize).p_value_threshold(0.1);

        let completed = vec![trial_with_values(
            0,
            0.1,
            (0..20).map(|s| (s, 0.1)).collect(),
        )];

        // Moderately worse — should pass lenient but maybe not strict.
        let current: Vec<(u64, f64)> = (0..20)
            .map(|s| if s < 15 { (s, 0.2) } else { (s, 0.15) })
            .collect();

        let lenient_prunes = lenient.should_prune(1, 19, &current, &completed);
        let strict_prunes = strict.should_prune(1, 19, &current, &completed);

        // A stricter threshold should never prune when a lenient one doesn't.
        if !lenient_prunes {
            assert!(!strict_prunes);
        }
    }

    #[test]
    #[should_panic(expected = "p_value_threshold must be in (0.0, 1.0)")]
    fn panics_on_zero_p_value() {
        let _ = WilcoxonPruner::new(Direction::Minimize).p_value_threshold(0.0);
    }

    #[test]
    #[should_panic(expected = "p_value_threshold must be in (0.0, 1.0)")]
    fn panics_on_one_p_value() {
        let _ = WilcoxonPruner::new(Direction::Minimize).p_value_threshold(1.0);
    }

    #[test]
    fn correct_signed_rank_statistic() {
        // Known example: differences [1, 2, 3, 4, 5, 6] (all positive).
        // Ranks: 1, 2, 3, 4, 5, 6. W+ = 21, W- = 0.
        // For minimization (testing if positive = worse), W- = 0.
        // This should give a very small p-value.
        let diffs = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
        let p = wilcoxon_signed_rank_test(&diffs, Direction::Minimize);
        assert!(
            p < 0.05,
            "p-value {p} should be < 0.05 for all-positive diffs"
        );
    }

    #[test]
    fn symmetric_differences_not_significant() {
        // Balanced differences: half positive, half negative.
        let diffs = vec![1.0, -1.0, 2.0, -2.0, 3.0, -3.0, 4.0, -4.0];
        let p = wilcoxon_signed_rank_test(&diffs, Direction::Minimize);
        assert!(p > 0.05, "p-value {p} should be > 0.05 for symmetric diffs");
    }

    #[test]
    fn normal_cdf_known_values() {
        assert!((normal_cdf(0.0) - 0.5).abs() < 1e-6);
        assert!(normal_cdf(-10.0) < 1e-6);
        assert!((normal_cdf(10.0) - 1.0).abs() < 1e-6);
        assert!((normal_cdf(-1.96) - 0.025).abs() < 0.001);
    }

    #[test]
    fn no_intermediate_values() {
        let pruner = WilcoxonPruner::new(Direction::Minimize);
        let completed = vec![trial_with_values(
            0,
            0.1,
            (0..20).map(|s| (s, 0.1)).collect(),
        )];
        assert!(!pruner.should_prune(1, 0, &[], &completed));
    }

    #[test]
    fn no_completed_trials() {
        let pruner = WilcoxonPruner::new(Direction::Minimize);
        let current: Vec<(u64, f64)> = (0..20).map(|s| (s, 1.0)).collect();
        assert!(!pruner.should_prune(1, 19, &current, &[]));
    }
}