TrashPanda 🦝
A high-performance Rust implementation of contextual multi-armed bandits, inspired by Python's MABWiser library.
TrashPanda provides a type-safe, memory-efficient framework for implementing multi-armed bandit algorithms with excellent performance characteristics. It offers both contextual and non-contextual bandit algorithms with a clean, idiomatic Rust API.
Algorithms
Non-Contextual Bandits
- Random: Uniform random selection (baseline)
- Epsilon-Greedy: Classic exploration-exploitation trade-off
- UCB (Upper Confidence Bound): Optimism in the face of uncertainty
- Thompson Sampling: Bayesian approach with Beta distributions
Contextual Bandits
- LinUCB: Linear Upper Confidence Bound for contextual problems
- LinGreedy: Contextual epsilon-greedy with linear reward modeling
- LinTS: Linear Thompson Sampling with posterior sampling
Installation
Add TrashPanda to your Cargo.toml:
[]
= "0.1.0"
Quick Start
Non-Contextual Bandit
use ;
use thread_rng;
// Create a bandit with three arms
let mut bandit = new.unwrap;
// Train with historical data
let decisions = vec!;
let rewards = vec!;
bandit.fit_simple.unwrap;
// Make a prediction
let mut rng = thread_rng;
let choice = bandit.predict_simple.unwrap;
println!;
// Get expected rewards for all arms
let expectations = bandit.predict_expectations_simple;
for in expectations
Contextual Bandit
use ;
use thread_rng;
// Create a contextual bandit
let mut bandit = new.unwrap;
// Train with contexts and rewards
let contexts = vec!;
let decisions = vec!;
let rewards = vec!;
for in contexts.iter
.zip
.zip
// Make predictions with new context
let mut rng = thread_rng;
let new_context = vec!;
let choice = bandit.predict.unwrap;
println!;
Batch Operations
use Bandit;
use EpsilonGreedy;
use thread_rng;
let mut bandit = epsilon_greedy.unwrap;
// Batch training
bandit.fit_simple.unwrap;
// Batch predictions
let mut rng = thread_rng;
let predictions = bandit.predict_batch_simple.unwrap;
println!;
Examples
Explore the examples directory for more detailed usage:
# Compare different algorithms
# Demonstrate batch operations
# Contextual bandits with LinUCB
License
This project is dual-licensed under either:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Acknowledgments
- Inspired by MABWiser from Fidelity Investments