pub struct ThompsonSampling { /* private fields */ }Available on crate feature
bandit only.Expand description
Thompson Sampling multi-armed bandit (Bernoulli rewards).
Maintains a Beta posterior for each arm. On select, samples from each
arm’s posterior and returns the arm with the highest sample. On update,
applies a soft update to the arm’s alpha (success) and beta (failure)
parameters.
Rewards must be in [0, 1]. The update is alpha += reward and
beta += 1 - reward; for strict Bernoulli rewards this is the standard
success/failure update, while fractional rewards produce a weighted update.
§Examples
use rill_ml::bandit::{Bandit, ThompsonSampling, ThompsonConfig};
use rand::SeedableRng;
use rand_chacha::ChaCha8Rng;
let mut rng = ChaCha8Rng::seed_from_u64(0);
let mut bandit = ThompsonSampling::new(3, ThompsonConfig::default()).unwrap();
let arm = bandit.select(&mut rng).unwrap();
bandit.update(arm, 1.0).unwrap();
assert_eq!(bandit.samples_seen(), 1);Implementations§
Source§impl ThompsonSampling
impl ThompsonSampling
Trait Implementations§
Source§impl Bandit for ThompsonSampling
impl Bandit for ThompsonSampling
Source§fn samples_seen(&self) -> u64
fn samples_seen(&self) -> u64
How many total samples (arm pulls) the bandit has observed.
Source§fn select(&self, rng: &mut impl Rng) -> Result<usize, RillError>
fn select(&self, rng: &mut impl Rng) -> Result<usize, RillError>
Select an arm using the provided RNG for exploration. Read more
Source§impl Clone for ThompsonSampling
impl Clone for ThompsonSampling
Source§fn clone(&self) -> ThompsonSampling
fn clone(&self) -> ThompsonSampling
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for ThompsonSampling
impl RefUnwindSafe for ThompsonSampling
impl Send for ThompsonSampling
impl Sync for ThompsonSampling
impl Unpin for ThompsonSampling
impl UnsafeUnpin for ThompsonSampling
impl UnwindSafe for ThompsonSampling
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more