Skip to main content

A Pythonic microframework for Multi-Armed Bandit algorithms.

Project description

bayesianbandits

Downloads codecov Documentation Status

Bayesian Multi-Armed Bandits for Python

Problem: Despite having a conceptually simple interface, putting together a multi-armed bandit in Python is a daunting task.

Solution: bayesianbandits is a Python package that provides a simple interface for creating and running Bayesian multi-armed bandits. It is built on top of scikit-learn and scipy, taking advantage of conjugate priors to provide fast and accurate inference.

While the API is still evolving, this library is already being used in production for marketing optimization, dynamic pricing, and other applications. Are you using bayesianbandits in your project? Let us know!

Features

  • Simple API: bayesianbandits provides a simple interface - most users will only need to call pull and update to get started.
  • Fast: bayesianbandits is built on top of already fast scientific Python libraries, but, if installed, will also use SuiteSparse to further speed up matrix operations on sparse matrices. Handling tens or even hundreds of thousands of features in a sparse model is no problem.
  • scikit-learn compatible: Use sklearn pipelines and transformers to preprocess data before feeding it into your bandit.
  • Flexible: Pick from a variety of policy algorithms, including Thompson sampling, upper confidence bound, and epsilon-greedy. Pick from a variety of prior distributions, including beta, gamma, normal, and normal-inverse-gamma.
  • Extensible: bayesianbandits provides simple interfaces for creating custom policies and priors.
  • Well-tested: bayesianbandits is well-tested, with nearly 100% test coverage.

Compatibility

bayesianbandits is tested with Python 3.9, 3.10, 3.11, and 3.12 with scikit-learn 1.2.2, 1.3.2, 1.4.2, and 1.5.1.

Getting Started

Install this package from PyPI.

pip install -U bayesianbandits

Define a LinearUCB contextual bandit with a normal prior.

import numpy as np
from bayesianbandits import (
    Arm,
    NormalInverseGammaRegressor,
    ContextualAgent,
    UpperConfidenceBound,
)

arms = [
    Arm(1, learner=NormalInverseGammaRegressor()),
    Arm(2, learner=NormalInverseGammaRegressor()),
    Arm(3, learner=NormalInverseGammaRegressor()),
    Arm(4, learner=NormalInverseGammaRegressor()),
]

policy = UpperConfidenceBound(alpha=0.84)

Instantiate the agent and pull an arm with context.

agent = ContextualAgent(arms, policy)

context = np.array([[1, 0, 0, 0]])

# Can be constructed with sklearn, formulaic, patsy, etc...
# context = formulaic.Formula("1 + article_number").get_model_matrix(data)
# context = sklearn.preprocessing.OneHotEncoder().fit_transform(data)

agent.pull(context)

Update the bandit with the reward.

agent.update(context, np.array([15.0]))

That's it! Check out the documentation for more examples.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bayesianbandits-0.8.0.tar.gz (29.2 kB view details)

Uploaded Source

Built Distribution

bayesianbandits-0.8.0-py3-none-any.whl (32.7 kB view details)

Uploaded Python 3

File details

Details for the file bayesianbandits-0.8.0.tar.gz.

File metadata

  • Download URL: bayesianbandits-0.8.0.tar.gz
  • Upload date:
  • Size: 29.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.9.20 Linux/6.5.0-1025-azure

File hashes

Hashes for bayesianbandits-0.8.0.tar.gz
Algorithm Hash digest
SHA256 f19a9a1f8d98bcdbf7568034e0fcbd881be0647633a364f09da9103402f2d1af
MD5 8b9fa9e9811a5cc2925bdf63b4916840
BLAKE2b-256 de20904488ad5e20ebaef8aacd5c6a2294b25fae3b776f90652d1acbb500140b

See more details on using hashes here.

File details

Details for the file bayesianbandits-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: bayesianbandits-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 32.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.9.20 Linux/6.5.0-1025-azure

File hashes

Hashes for bayesianbandits-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b0b3e9892c10648aed0d35b6d7ae9508dcd2c26e7bd070a31b9bb284ed8462c3
MD5 cc4eb98f0b4a99bc7ecbb7940a39d414
BLAKE2b-256 f7319139c2370ad5e1a83498d9c89ee9920e772061b38702ad97783f639dc16d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page