david-cortes/contextualbandits

Python implementations of contextual bandits algorithms

What it solves

Contextual bandits are a type of sequential decision problem where at each step you observe features (covariates), choose an arm (action or label), and only receive a reward for the chosen arm—not for the alternatives. The goal is to maximize cumulative rewards by balancing exploration of uncertain arms and exploitation of known good arms. This package provides implementations of many published algorithms for online decision-making, learning from historical data collected under a different policy (off-policy), and evaluating policies offline. It specifically handles discrete rewards (0 or 1) and assumes all arms see the same covariates.

How it works

The package is organized into three main areas: online policies, off-policy learning, and policy evaluation. Most online methods are meta-heuristics that take an existing binary classifier (such as logistic regression or XGBoost) as a black-box oracle and adapt it to bandit scenarios. For example, Upper Confidence Bound (UCB), Thompson Sampling, Epsilon Greedy, and Explore-Then-Exploit are implemented. There are also linear-model specializations like LinUCB and linear Thompson sampling. Off-policy methods include Offset Tree and Doubly-Robust Policy Optimization, which learn from logged data where the policy that generated the data differs from the one being trained. Evaluation methods provide estimates of how well a policy would perform using only partially labeled data. Many algorithms support streaming/batch training when the classifier has a partial_fit method, and the package includes a custom linear regression class that can be updated incrementally while producing the same result as fitting all data at once.

Who it's for

The package is aimed at researchers, data scientists, and students who want to prototype, compare, or reproduce contextual bandit algorithms. It is explicitly research-oriented, prioritising flexibility over execution speed. Users should be comfortable with scikit-learn-style classifiers and be able to install a package that requires a C compiler. The README notes that many of the included methods do not consistently beat simple baselines, so it is better suited for experimentation and benchmarking than for immediate deployment.

Highlights

  • Implements a wide range of algorithms from research papers, including LinUCB, Thompson Sampling, UCB, Epsilon Greedy, Offset Tree, and Doubly-Robust methods.
  • Uses any binary classifier with fit/predict_proba as a black-box oracle, making it easy to plug in your own models.
  • Supports online learning with streaming batches via partial_fit, as well as full refitting on historical data.
  • Includes off-policy learning from logged data and offline policy evaluation techniques.
  • Provides a linear regression class that can be updated incrementally without losing exact equivalence to batch fitting.
  • Can handle sparse matrices, rank top-N arms, and offers per-arm hyperparameter tuning.
  • Serialization via pickle may fail; cloudpickle or dill is recommended.
  • Also available on Fedora via dnf, and earlier pure-Python version 0.1.8.5 for install without compiling.

Related

  • Project
  • Project
  • Project
  • Project