irithyll 10.0.1

Streaming ML in Rust -- gradient boosted trees, neural architectures (TTT/KAN/MoE/Mamba/SNN), AutoML, kernel methods, and composable pipelines
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
//! Thompson Sampling with Beta-Bernoulli conjugate posterior.
//!
//! Thompson Sampling is a Bayesian approach to the multi-armed bandit problem
//! that maintains a Beta posterior distribution for each arm's reward
//! probability. At each step it samples from each arm's posterior and selects
//! the arm with the highest sample — naturally balancing exploration (uncertain
//! arms have wide posteriors) with exploitation (well-rewarded arms have
//! posteriors concentrated near high values).
//!
//! # Algorithm
//!
//! For each arm *a* with posterior `Beta(alpha_a, beta_a)`:
//!
//! 1. **Select:** Sample `theta_a ~ Beta(alpha_a, beta_a)` for every arm.
//!    Return `argmax theta_a`.
//! 2. **Update:** On observing reward *r* in \[0, 1\] for arm *a*:
//!    - `alpha_a += r`
//!    - `beta_a += 1 - r`
//!
//! # Prior
//!
//! The default prior is `Beta(1, 1)` (uniform on \[0, 1\]), expressing no
//! initial preference. Use [`ThompsonSampling::with_prior`] for an informative
//! prior.
//!
//! # Example
//!
//! ```
//! use irithyll::bandits::{Bandit, ThompsonSampling};
//!
//! let mut ts = ThompsonSampling::new(3);
//! for _ in 0..200 {
//!     let arm = ts.select_arm();
//!     let reward = if arm == 0 { 0.9 } else { 0.1 };
//!     ts.update(arm, reward);
//! }
//! // Arm 0 should be selected most often.
//! assert_eq!(
//!     ts.arm_counts().iter().enumerate().max_by_key(|(_, &c)| c).unwrap().0,
//!     0
//! );
//! ```

use super::{beta_sample, Bandit};

/// Thompson Sampling bandit with Beta-Bernoulli conjugate posterior.
///
/// Each arm maintains a `Beta(alpha, beta)` posterior updated with observed
/// rewards in `[0, 1]`. Arm selection samples from each posterior and picks
/// the argmax, providing probability-matching exploration.
///
/// # Fields
///
/// - `alphas` / `betas` — posterior parameters per arm.
/// - `values` — running mean reward per arm (for [`Bandit::arm_values`]).
/// - `counts` — number of times each arm has been pulled.
#[derive(Debug, Clone)]
pub struct ThompsonSampling {
    n_arms: usize,
    alphas: Vec<f64>,
    betas: Vec<f64>,
    counts: Vec<u64>,
    values: Vec<f64>,
    total_pulls: u64,
    rng_state: u64,
    /// Initial alpha prior, stored for [`Bandit::reset`].
    alpha_prior: f64,
    /// Initial beta prior, stored for [`Bandit::reset`].
    beta_prior: f64,
}

impl ThompsonSampling {
    /// Create a new `ThompsonSampling` bandit with the default uniform
    /// `Beta(1, 1)` prior and seed 42.
    pub fn new(n_arms: usize) -> Self {
        Self::build(n_arms, 1.0, 1.0, 42)
    }

    /// Create a new `ThompsonSampling` bandit with a custom RNG seed and
    /// the default `Beta(1, 1)` prior.
    pub fn with_seed(n_arms: usize, seed: u64) -> Self {
        Self::build(n_arms, 1.0, 1.0, seed)
    }

    /// Create a new `ThompsonSampling` bandit with a custom `Beta(alpha, beta)`
    /// prior for all arms and seed 42.
    ///
    /// # Panics
    ///
    /// Panics if `alpha_prior` or `beta_prior` are not positive.
    pub fn with_prior(n_arms: usize, alpha_prior: f64, beta_prior: f64) -> Self {
        assert!(
            alpha_prior > 0.0,
            "alpha_prior must be positive, got {alpha_prior}"
        );
        assert!(
            beta_prior > 0.0,
            "beta_prior must be positive, got {beta_prior}"
        );
        Self::build(n_arms, alpha_prior, beta_prior, 42)
    }

    /// Internal constructor.
    fn build(n_arms: usize, alpha_prior: f64, beta_prior: f64, seed: u64) -> Self {
        Self {
            n_arms,
            alphas: vec![alpha_prior; n_arms],
            betas: vec![beta_prior; n_arms],
            counts: vec![0; n_arms],
            values: vec![0.0; n_arms],
            total_pulls: 0,
            rng_state: seed,
            alpha_prior,
            beta_prior,
        }
    }

    /// Per-arm posterior alpha parameters.
    #[inline]
    pub fn arm_alphas(&self) -> &[f64] {
        &self.alphas
    }

    /// Per-arm posterior beta parameters.
    #[inline]
    pub fn arm_betas(&self) -> &[f64] {
        &self.betas
    }
}

impl Bandit for ThompsonSampling {
    fn select_arm(&mut self) -> usize {
        let mut best_arm = 0;
        let mut best_sample = f64::NEG_INFINITY;

        for a in 0..self.n_arms {
            let sample = beta_sample(self.alphas[a], self.betas[a], &mut self.rng_state);
            if sample > best_sample {
                best_sample = sample;
                best_arm = a;
            }
        }

        best_arm
    }

    fn update(&mut self, arm: usize, reward: f64) {
        // Clamp reward to [0, 1] for Beta posterior validity.
        let r = reward.clamp(0.0, 1.0);

        // Update pull count and total.
        self.counts[arm] += 1;
        self.total_pulls += 1;

        // Incremental mean: value = value + (r - value) / count.
        let n = self.counts[arm] as f64;
        self.values[arm] += (r - self.values[arm]) / n;

        // Update posterior: alpha += r, beta += (1 - r).
        self.alphas[arm] += r;
        self.betas[arm] += 1.0 - r;
    }

    #[inline]
    fn n_arms(&self) -> usize {
        self.n_arms
    }

    #[inline]
    fn n_pulls(&self) -> u64 {
        self.total_pulls
    }

    fn reset(&mut self) {
        self.alphas.fill(self.alpha_prior);
        self.betas.fill(self.beta_prior);
        self.counts.fill(0);
        self.values.fill(0.0);
        self.total_pulls = 0;
    }

    #[inline]
    fn arm_values(&self) -> &[f64] {
        &self.values
    }

    #[inline]
    fn arm_counts(&self) -> &[u64] {
        &self.counts
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn selects_best_arm_over_time() {
        let mut ts = ThompsonSampling::with_seed(4, 123);

        for _ in 0..300 {
            let arm = ts.select_arm();
            // Arm 0 always returns reward 1.0, others return 0.0.
            let reward = if arm == 0 { 1.0 } else { 0.0 };
            ts.update(arm, reward);
        }

        let counts = ts.arm_counts();
        assert!(
            counts[0] > counts[1] && counts[0] > counts[2] && counts[0] > counts[3],
            "arm 0 (always reward 1.0) should have the most pulls, got counts={:?}",
            counts
        );
    }

    #[test]
    fn prior_is_uniform() {
        let ts = ThompsonSampling::new(5);

        for a in 0..5 {
            assert_eq!(
                ts.arm_alphas()[a],
                1.0,
                "fresh alpha[{a}] should be 1.0, got {}",
                ts.arm_alphas()[a]
            );
            assert_eq!(
                ts.arm_betas()[a],
                1.0,
                "fresh beta[{a}] should be 1.0, got {}",
                ts.arm_betas()[a]
            );
        }
    }

    #[test]
    fn update_adjusts_posterior() {
        let mut ts = ThompsonSampling::new(3);

        ts.update(0, 1.0);

        assert_eq!(
            ts.arm_alphas()[0],
            2.0,
            "alpha[0] after reward=1.0 should be 2.0, got {}",
            ts.arm_alphas()[0]
        );
        assert_eq!(
            ts.arm_betas()[0],
            1.0,
            "beta[0] after reward=1.0 should be 1.0 (unchanged), got {}",
            ts.arm_betas()[0]
        );

        // Other arms should be untouched.
        assert_eq!(
            ts.arm_alphas()[1],
            1.0,
            "alpha[1] should remain at prior, got {}",
            ts.arm_alphas()[1]
        );
        assert_eq!(
            ts.arm_betas()[1],
            1.0,
            "beta[1] should remain at prior, got {}",
            ts.arm_betas()[1]
        );
    }

    #[test]
    fn reset_restores_prior() {
        let mut ts = ThompsonSampling::with_prior(3, 2.0, 5.0);

        // Train for a while.
        for _ in 0..50 {
            let arm = ts.select_arm();
            ts.update(arm, 0.7);
        }
        assert!(ts.n_pulls() > 0, "should have pulls before reset");

        // Reset.
        ts.reset();

        assert_eq!(ts.n_pulls(), 0, "total_pulls should be 0 after reset");
        for a in 0..3 {
            assert_eq!(
                ts.arm_alphas()[a],
                2.0,
                "alpha[{a}] should be restored to prior 2.0 after reset, got {}",
                ts.arm_alphas()[a]
            );
            assert_eq!(
                ts.arm_betas()[a],
                5.0,
                "beta[{a}] should be restored to prior 5.0 after reset, got {}",
                ts.arm_betas()[a]
            );
            assert_eq!(
                ts.arm_counts()[a],
                0,
                "count[{a}] should be 0 after reset, got {}",
                ts.arm_counts()[a]
            );
            assert_eq!(
                ts.arm_values()[a],
                0.0,
                "value[{a}] should be 0.0 after reset, got {}",
                ts.arm_values()[a]
            );
        }
    }

    #[test]
    fn custom_prior_works() {
        let ts = ThompsonSampling::with_prior(3, 2.0, 5.0);

        for a in 0..3 {
            assert_eq!(
                ts.arm_alphas()[a],
                2.0,
                "alpha[{a}] should start at custom prior 2.0, got {}",
                ts.arm_alphas()[a]
            );
            assert_eq!(
                ts.arm_betas()[a],
                5.0,
                "beta[{a}] should start at custom prior 5.0, got {}",
                ts.arm_betas()[a]
            );
        }
    }

    #[test]
    fn rewards_clamped_to_unit_interval() {
        let mut ts = ThompsonSampling::new(2);

        // Reward > 1.0 should be clamped to 1.0.
        ts.update(0, 5.0);
        assert_eq!(
            ts.arm_alphas()[0],
            2.0,
            "alpha[0] after reward=5.0 (clamped to 1.0) should be 2.0, got {}",
            ts.arm_alphas()[0]
        );
        assert_eq!(
            ts.arm_betas()[0],
            1.0,
            "beta[0] after reward=5.0 (clamped) should be 1.0, got {}",
            ts.arm_betas()[0]
        );

        // Reward < 0.0 should be clamped to 0.0.
        ts.update(1, -3.0);
        assert_eq!(
            ts.arm_alphas()[1],
            1.0,
            "alpha[1] after reward=-3.0 (clamped to 0.0) should be 1.0, got {}",
            ts.arm_alphas()[1]
        );
        assert_eq!(
            ts.arm_betas()[1],
            2.0,
            "beta[1] after reward=-3.0 (clamped) should be 2.0, got {}",
            ts.arm_betas()[1]
        );
    }

    #[test]
    fn arm_values_track_mean() {
        let mut ts = ThompsonSampling::new(2);

        // Arm 0: rewards 0.2, 0.4, 0.6 -> mean = 0.4
        ts.update(0, 0.2);
        ts.update(0, 0.4);
        ts.update(0, 0.6);

        let expected_mean = (0.2 + 0.4 + 0.6) / 3.0;
        let actual = ts.arm_values()[0];
        assert!(
            (actual - expected_mean).abs() < 1e-12,
            "arm 0 mean should be ~{expected_mean}, got {actual}"
        );

        // Arm 1: single reward 0.8 -> mean = 0.8
        ts.update(1, 0.8);
        assert!(
            (ts.arm_values()[1] - 0.8).abs() < 1e-12,
            "arm 1 mean should be 0.8 after single update, got {}",
            ts.arm_values()[1]
        );

        // Verify counts are correct.
        assert_eq!(
            ts.arm_counts()[0],
            3,
            "arm 0 should have 3 pulls, got {}",
            ts.arm_counts()[0]
        );
        assert_eq!(
            ts.arm_counts()[1],
            1,
            "arm 1 should have 1 pull, got {}",
            ts.arm_counts()[1]
        );
        assert_eq!(
            ts.n_pulls(),
            4,
            "total pulls should be 4, got {}",
            ts.n_pulls()
        );
    }
}