Skip to main content

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 PyMC, 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

Metadata

Release files for pybandits 9.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pybandits 9.0.0
File Size Uploaded Provenance Actions
pybandits-9.0.0.tar.gz 170.4 kB Without provenance Details for pybandits-9.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pybandits 9.0.0
File Interpreter ABI Platform Actions
pybandits-9.0.0-py3-none-any.whl Python 3 none any Details for pybandits-9.0.0-py3-none-any.whl

Total release size: 384.6 kB

Release files / pybandits-9.0.0.tar.gz

Download URL pybandits-9.0.0.tar.gz
Size 170.4 kB
Tags Source
SHA-256 checksum
How to use checksums
3f1ceb332f4f9f6d3ab4c2c43cdd458ceac0cee8b8c92b7cec18989c6dd77479
BLAKE2b-256 checksum
How to use checksums
a520cc28f17a83bc32e1216629f953177272f252d12f47a53da5b88e616ac41a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.11.17 Linux/7.0.0-1017-azure

Release files / pybandits-9.0.0-py3-none-any.whl

Download URL pybandits-9.0.0-py3-none-any.whl
Size 214.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4a8d1d3843a0b6be309862ae826ead80a0d9a6485fde5183b4aba3c93c6826c9
BLAKE2b-256 checksum
How to use checksums
bc11017a4ebb0d125acaa007d64fb91bf699fba526d10cfdf032bc4d3ce4d374
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.11.17 Linux/7.0.0-1017-azure

Security for pybandits 9.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

9.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page