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

Download files

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

Source Distribution

pybandits-8.1.0.tar.gz (158.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pybandits-8.1.0-py3-none-any.whl (202.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pybandits-8.1.0.tar.gz
  • Upload date:
  • Size: 158.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.10.20 Linux/6.17.0-1020-azure

File hashes

Hashes for pybandits-8.1.0.tar.gz
Algorithm Hash digest
SHA256 a922b667bf37e29d4ffe3a370f4adf2d71e7b51d1df20197ddb995ff532e91fb
MD5 0f05535137c0df10ed6936578c564798
BLAKE2b-256 7172c0ec4dad0d901ee9720a917e71a27e27b9d27764d1b6fdb7f68fb4251db4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pybandits-8.1.0-py3-none-any.whl
  • Upload date:
  • Size: 202.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.10.20 Linux/6.17.0-1020-azure

File hashes

Hashes for pybandits-8.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b8eafb1cd9c357bc08535119d5e4e24235be3e32e3a649632ffea35db52618d5
MD5 fb5de03a96de67adf430a9d10b073781
BLAKE2b-256 77d33edbcc91697b77467c945505eba2d1d4fd2b977b59ed5a169252d5124672

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

8.1.0 This release

2 files

8.0.0

2 files

7.5.1

2 files

7.5.0

2 files

7.4.2

2 files

7.4.1

2 files

7.4.0

2 files

7.3.0

2 files

7.2.2

2 files

7.2.1

2 files

7.2.0

2 files

7.1.0

2 files

7.0.3

2 files

7.0.2

2 files

7.0.1

2 files

7.0.0

2 files

6.1.0

2 files

6.0.8

2 files

6.0.7

2 files

6.0.6

2 files

6.0.5

2 files

6.0.4

2 files

6.0.3

2 files

6.0.2

2 files

6.0.1

2 files

6.0.0

2 files

5.0.0

2 files

4.4.0

2 files

4.2.0

2 files

4.1.3

2 files

4.1.2

2 files

4.1.1

2 files

4.1.0

2 files

4.0.17

2 files

4.0.16

2 files

4.0.15

2 files

4.0.14

2 files

4.0.13

2 files

4.0.12

2 files

4.0.11

2 files

4.0.10

2 files

4.0.9

2 files

4.0.8

2 files

4.0.7

2 files

4.0.6

2 files

4.0.5

2 files

4.0.4

2 files

4.0.3

2 files

4.0.2

2 files

4.0.1

2 files

2.1.6

2 files

2.1.5

2 files

2.1.4

2 files

2.1.3

2 files

2.1.2

2 files

2.0.1

2 files

2.0.0

2 files

1.1.0

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

0.6.2

2 files

0.6.1

2 files

0.6.0

2 files

0.5.1

2 files

0.5.0

2 files

0.4.1

2 files

0.3.0

2 files

0.2.0

2 files

0.0.2

2 files

0.0.1

2 files

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