Skip to main content

Python Multi-Armed Bandit Library

Project description

PyBandits

alt text alt text alt text alt text alt text

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. The latest release is version 0.0.2. pybandits requires a Python version >= 3.8.

$ pip install pybandits

The command above will automatically install all the dependencies listed in requirements.txt. 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
import random
from pybandits.core.smab import Smab

# init stochastic Multi-Armed Bandit model
smab = Smab(action_ids=['Action A', 'Action B', 'Action C'])

# predict actions
pred_actions, _ = smab.predict(n_samples=100)

n_successes, n_failures = {}, {}
for a in set(pred_actions):

    # simulate rewards from environment
    n_successes[a] = random.randint(0, pred_actions.count(a))
    n_failures[a] = pred_actions.count(a) - n_successes[a]

    # update model
    smab.update(action_id=a, n_successes=n_successes[a], n_failures=n_failures[a])

Documentation

For more information please read the full documentation and tutorials.


Info for developers

PyBandits is supported by the AI for gaming and entertainment apps community.

The source code of the project is available on GitHub.

$ git clone https://github.com/playtikaresearch/pybandits.git

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

$ pip install .                        # install library + dependencies
$ pip install .[develop]               # install library + dependencies + developer-dependencies
$ pip install -r requirements.txt      # install dependencies
$ pip install -r requirements-dev.txt  # install developer-dependencies

As suggested by the authors of pymc3 and pandoc, we highly recommend to install these dependencies with conda:

$ conda install -c conda-forge pandoc
$ conda install -c conda-forge pymc3

To create the file pybandits.whl for the installation with pip run the following command:

$ python setup.py sdist bdist_wheel

To create the HTML documentation run the following commands:

$ cd docs
$ 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-0.2.0.tar.gz (26.4 kB view details)

Uploaded Source

Built Distribution

pybandits-0.2.0-py3-none-any.whl (35.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pybandits-0.2.0.tar.gz
  • Upload date:
  • Size: 26.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.11.1 Darwin/21.6.0

File hashes

Hashes for pybandits-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e93a6eebe82ef4642c952bead6e03188f8876ea6f0b1a65f0b85c690d4b7a4f5
MD5 b0d49e64c8cc8ce186a18e4a7d90e008
BLAKE2b-256 0a1d37d7ce431a05da1c49d3fc0619aef6b4eba61b132c785b82e8ef1cc592ec

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: pybandits-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 35.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.11.1 Darwin/21.6.0

File hashes

Hashes for pybandits-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8c26a102a8e8d7108927b5548761828f521c1ff6f3c2081463c57856c7b60f9e
MD5 7f5d8da779cf7bb969533a39b54cdea9
BLAKE2b-256 64705e8b65b27470ea0e615036f8d1571bb85d932295fefbcb074f87d9cc8f33

See more details on using hashes here.

Provenance

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