Skip to main content

Python Multi-Armed Bandit Library

Project description

PyBandits

GitHub Actions Workflow Status PyPI - Version PyPI - Python Version MIT License Ask DeepWiki Coverage

PyBandits is a Python library for Multi-Armed Bandit. It provides an implementation of stochastic Multi-Armed Bandit (sMAB) and contextual Multi-Armed Bandit (cMAB) based on Thompson Sampling.

For the sMAB, we implemented a Bernoulli multi-armed bandit based on Thompson Sampling algorithm Agrawal and Goyal, 2012. If context information is available we provide a generalisation of Thompson Sampling for cMAB Agrawal and Goyal, 2014 implemented with PyMC3, an open source probabilistic programming framework for automatic Bayesian inference on user-defined probabilistic models.

Installation

This library is distributed on PyPI and can be installed with pip.

pip install pybandits

Based on the guidelines of pymc authors, it is highly recommended to install the library in a conda environment via the following.

conda install -c conda-forge pymc
pip install pybandits

The command above will automatically install all the dependencies listed in pyproject.toml. Please visit the installation page for more details.

Getting started

A short example, illustrating it use. Use the sMAB model to predict actions and update the model based on rewards from the environment.

import numpy as np
from pybandits.model import Beta
from pybandits.smab import SmabBernoulli

n_samples=100

# define action model
actions = {
    "a1": Beta(),
    "a2": Beta(),
}

# init stochastic Multi-Armed Bandit model
smab = SmabBernoulli(actions=actions)

# predict actions
pred_actions, _ = smab.predict(n_samples=n_samples)
simulated_rewards = np.random.randint(2, size=n_samples)

# update model
smab.update(actions=pred_actions, rewards=simulated_rewards)

Documentation

For more information please read the full documentation and tutorials.

You can also observe on DeepWiki.

Info for developers

The source code of the project is available on GitHub.

git clone https://github.com/playtikaoss/pybandits.git

You can install the library and the dependencies from the source code with one of the following commands:

poetry install                # install library + dependencies
poetry install --without dev     # install library + dependencies, excluding developer-dependencies

To create the HTML documentation run the following commands:

cd docs/src
make html

Run tests

Tests can be executed with pytest running the following commands. Make sure to have the library installed before to run any tests.

cd tests
pytest -vv                                      # run all tests
pytest -vv test_testmodule.py                   # run all tests within a module
pytest -vv test_testmodule.py -k test_testname  # run only 1 test
pytest -vv -k 'not time'                        # run all tests but not exec time

License

MIT License

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

pybandits-4.0.6.tar.gz (73.7 kB view details)

Uploaded Source

Built Distribution

pybandits-4.0.6-py3-none-any.whl (88.5 kB view details)

Uploaded Python 3

File details

Details for the file pybandits-4.0.6.tar.gz.

File metadata

  • Download URL: pybandits-4.0.6.tar.gz
  • Upload date:
  • Size: 73.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.10.18 Linux/6.11.0-1015-azure

File hashes

Hashes for pybandits-4.0.6.tar.gz
Algorithm Hash digest
SHA256 cd576906a16cf0f4ad2ec341d16c7749641dbc55d417e97cbffb812aa5698c21
MD5 add3f04aaa6fffe9d1ff694c594e8f64
BLAKE2b-256 62fee8e9215b1361cbf31c2090e50bf6494f20c68628b83159a8aad16f13c8b2

See more details on using hashes here.

File details

Details for the file pybandits-4.0.6-py3-none-any.whl.

File metadata

  • Download URL: pybandits-4.0.6-py3-none-any.whl
  • Upload date:
  • Size: 88.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.10.18 Linux/6.11.0-1015-azure

File hashes

Hashes for pybandits-4.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 151b1077620cc2a78b706d84d99983f42dbe6874da472e3670270cf2065ee117
MD5 0fd6b6341c84fc73813f1677953943f3
BLAKE2b-256 8cf41099d98479d295931dc6fe4eaecc9d9ef5085593a2cbd6ef77db83b6d8c8

See more details on using hashes here.

Supported by

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