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.1.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.1.0
File Size Uploaded Provenance Actions
pybandits-9.1.0.tar.gz 172.9 kB Without provenance Details for pybandits-9.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 389.4 kB

Release files / pybandits-9.1.0.tar.gz

Download URL pybandits-9.1.0.tar.gz
Size 172.9 kB
Tags Source
SHA-256 checksum
How to use checksums
9d397486eaf55084245ce75dde9b88e68ce15540b56c588ec3b7f8652e1a79f7
BLAKE2b-256 checksum
How to use checksums
4dd274b6c5d80a2770f32f4dd0dc5b3e1d139584f64591a21611abb7cb80f033
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.11.18 Linux/7.0.0-1016-aws

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

Download URL pybandits-9.1.0-py3-none-any.whl
Size 216.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c053b1d38dc44f83e498f5507fa5811df26a1aeda9f1e777e8d42d84ad5421d0
BLAKE2b-256 checksum
How to use checksums
f3e8e0403fee3fd231b6a82fd82eb2891b87ba0a09b679d1e58699a2335e38dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.11.18 Linux/7.0.0-1016-aws

Security for pybandits 9.1.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.1.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