Skip to main content

LinUcb

Struct LinUcb 

Source
pub struct LinUcb { /* private fields */ }
Available on crate feature bandit only.
Expand description

LinUCB contextual multi-armed bandit.

Maintains a per-arm ridge-regression model and selects the arm with the highest upper confidence bound on the expected reward for the given context.

§Examples

use rill_ml::bandit::{ContextualBandit, LinUcb, LinUcbConfig};
use rand::SeedableRng;
use rand_chacha::ChaCha8Rng;

let mut config = LinUcbConfig::default();
config.alpha = 1.0;
config.arm_count = 2;
config.feature_count = 2;
let mut bandit = LinUcb::new(config).unwrap();
let mut rng = ChaCha8Rng::seed_from_u64(0);

let context = [0.5, 0.8];
let arm = bandit.select(&context, &mut rng).unwrap();
bandit.update(arm, &context, 1.0).unwrap();
assert_eq!(bandit.samples_seen(), 1);

Implementations§

Source§

impl LinUcb

Source

pub fn new(config: LinUcbConfig) -> Result<Self, RillError>

Create a new LinUCB bandit from the given configuration.

§Errors

Returns RillError::InvalidArmCount if arm_count is zero. Returns RillError::InvalidFeatureCount if feature_count is zero. Returns RillError::InvalidParameter if alpha is not finite and positive.

Source

pub const fn alpha(&self) -> f64

The exploration parameter alpha.

Source

pub fn a_matrix(&self, arm: usize) -> Result<&[Vec<f64>], RillError>

Borrow the A matrix for a specific arm (diagnostic).

§Errors

Returns RillError::InvalidArm if arm is out of range.

Source

pub fn b_vector(&self, arm: usize) -> Result<&[f64], RillError>

Borrow the b vector for a specific arm (diagnostic).

§Errors

Returns RillError::InvalidArm if arm is out of range.

Source

pub fn validate(&self) -> Result<(), RillError>

Validate all persisted state invariants.

This is also run automatically during deserialization.

Source

pub fn score_arm( &self, arm: usize, context: &[f64], ) -> Result<LinUcbArmScore, RillError>

Compute the explainable UCB score for one arm.

The returned exploration bonus already includes alpha.

Source

pub fn score_all( &self, context: &[f64], ) -> Result<Vec<LinUcbArmScore>, RillError>

Compute explainable UCB scores for every arm.

Source

pub fn select_with_scores( &self, context: &[f64], rng: &mut impl Rng, ) -> Result<(usize, Vec<LinUcbArmScore>), RillError>

Select an arm with the existing randomized tie-break and return every score used by that decision.

Source

pub fn select_deterministic(&self, context: &[f64]) -> Result<usize, RillError>

Select deterministically, resolving exact score ties to the lowest arm index. This does not change the randomized ContextualBandit::select contract and is intended for replay and audit paths.

Source

pub fn condition_diagnostics( &self, arm: usize, ) -> Result<LinUcbConditionDiagnostics, RillError>

Return a bounded numerical condition diagnostic for one arm.

Trait Implementations§

Source§

impl Clone for LinUcb

Source§

fn clone(&self) -> LinUcb

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl ContextualBandit for LinUcb

Source§

fn arm_count(&self) -> usize

The number of arms (actions) available.
Source§

fn feature_count(&self) -> usize

The number of features in the context vector.
Source§

fn samples_seen(&self) -> u64

How many total samples the bandit has observed.
Source§

fn select( &self, context: &[f64], rng: &mut impl Rng, ) -> Result<usize, RillError>

Select an arm given the context vector, using the provided RNG. Read more
Source§

fn update( &mut self, arm: usize, context: &[f64], reward: f64, ) -> Result<(), RillError>

Update the bandit with the observed reward for a previously selected arm.
Source§

fn reset(&mut self)

Reset the bandit to its initial (no-data) state.
Source§

impl Debug for LinUcb

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V