PyBandits
=========






**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](http://proceedings.mlr.press/v23/agrawal12/agrawal12.pdf). If context information is available we provide a generalisation of Thompson Sampling for cMAB [Agrawal and Goyal, 2014](https://arxiv.org/pdf/1209.3352.pdf) implemented with [PyMC3](https://peerj.com/articles/cs-55/), an open source probabilistic programming framework for automatic Bayesian inference on user-defined probabilistic models.
Installation
------------
This library is distributed on [PyPI](https://pypi.org/project/pybandits/) and can be installed with ``pip``.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~bash
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.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~bash
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](https://playtikaoss.github.io/pybandits/installation.html)
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.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~python
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](https://playtikaoss.github.io/pybandits/pybandits.html)
and
[tutorials](https://playtikaoss.github.io/pybandits/tutorials.html).
You can also observe on [DeepWiki](https://deepwiki.com/PlaytikaOSS/pybandits).
Info for developers
-------------------
The source code of the project is available on [GitHub](https://github.com/playtikaoss/pybandits).
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~bash
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:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~bash
poetry install # install library + dependencies
poetry install --without dev # install library + dependencies, excluding developer-dependencies
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To create the HTML documentation run the following commands:
~~~~~~~~~~~bash
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.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~bash
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](LICENSE)
Raw data
{
"_id": null,
"home_page": "https://github.com/PlaytikaOSS/pybandits",
"name": "pybandits",
"maintainer": null,
"docs_url": null,
"requires_python": "<3.13,>=3.9",
"maintainer_email": null,
"keywords": "multi-armed bandits, reinforcement-learning, optimization",
"author": "Dario d'Andrea",
"author_email": "dariod@playtika.com",
"download_url": "https://files.pythonhosted.org/packages/86/2e/bbe73198f870b127b00045fe3e2c52c1d3d549e8dd4820a7013112dc7e09/pybandits-4.0.8.tar.gz",
"platform": null,
"description": "PyBandits\n=========\n\n\n\n\n\n\n\n\n**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.\n\nFor the sMAB, we implemented a Bernoulli multi-armed bandit based on Thompson Sampling algorithm [Agrawal and Goyal, 2012](http://proceedings.mlr.press/v23/agrawal12/agrawal12.pdf). If context information is available we provide a generalisation of Thompson Sampling for cMAB [Agrawal and Goyal, 2014](https://arxiv.org/pdf/1209.3352.pdf) implemented with [PyMC3](https://peerj.com/articles/cs-55/), an open source probabilistic programming framework for automatic Bayesian inference on user-defined probabilistic models.\n\nInstallation\n------------\n\nThis library is distributed on [PyPI](https://pypi.org/project/pybandits/) and can be installed with ``pip``.\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~bash\npip install pybandits\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nBased on the guidelines of ``pymc`` authors, it is highly recommended to install the library in a conda environment via the following.\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~bash\nconda install -c conda-forge pymc\npip install pybandits\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThe command above will automatically install all the dependencies listed in ``pyproject.toml``. Please visit the\n[installation](https://playtikaoss.github.io/pybandits/installation.html)\npage for more details.\n\nGetting started\n---------------\n\nA short example, illustrating it use. Use the sMAB model to predict actions and update the model based on rewards from the environment.\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~python\nimport numpy as np\nfrom pybandits.model import Beta\nfrom pybandits.smab import SmabBernoulli\n\nn_samples=100\n\n# define action model\nactions = {\n \"a1\": Beta(),\n \"a2\": Beta(),\n}\n\n# init stochastic Multi-Armed Bandit model\nsmab = SmabBernoulli(actions=actions)\n\n# predict actions\npred_actions, _ = smab.predict(n_samples=n_samples)\nsimulated_rewards = np.random.randint(2, size=n_samples)\n\n# update model\nsmab.update(actions=pred_actions, rewards=simulated_rewards)\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nDocumentation\n-------------\n\nFor more information please read the full\n[documentation](https://playtikaoss.github.io/pybandits/pybandits.html)\nand\n[tutorials](https://playtikaoss.github.io/pybandits/tutorials.html).\n\nYou can also observe on [DeepWiki](https://deepwiki.com/PlaytikaOSS/pybandits).\n\nInfo for developers\n-------------------\n\nThe source code of the project is available on [GitHub](https://github.com/playtikaoss/pybandits).\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~bash\ngit clone https://github.com/playtikaoss/pybandits.git\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nYou can install the library and the dependencies from the source code with one of the following commands:\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~bash\npoetry install # install library + dependencies\npoetry install --without dev # install library + dependencies, excluding developer-dependencies\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nTo create the HTML documentation run the following commands:\n\n~~~~~~~~~~~bash\ncd docs/src\nmake html\n~~~~~~~~~~~\n\nRun tests\n---------\n\nTests can be executed with ``pytest`` running the following commands. Make sure to have the library installed before to\nrun any tests.\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~bash\ncd tests\npytest -vv # run all tests\npytest -vv test_testmodule.py # run all tests within a module\npytest -vv test_testmodule.py -k test_testname # run only 1 test\npytest -vv -k 'not time' # run all tests but not exec time\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nLicense\n-------\n\n[MIT License](LICENSE)\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Python Multi-Armed Bandit Library",
"version": "4.0.8",
"project_urls": {
"Homepage": "https://github.com/PlaytikaOSS/pybandits",
"Repository": "https://github.com/PlaytikaOSS/pybandits"
},
"split_keywords": [
"multi-armed bandits",
" reinforcement-learning",
" optimization"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8b7ca8de64ca664eb98707a4af283c356d1dec492dd8fa4c24f42f8b0641d582",
"md5": "cde0ae6872330bb8529b2a4725237030",
"sha256": "73965e8c7dafe572b2f7d7a736acc8bc51a02bc2bc79153b41f6bdc5ac1d966e"
},
"downloads": -1,
"filename": "pybandits-4.0.8-py3-none-any.whl",
"has_sig": false,
"md5_digest": "cde0ae6872330bb8529b2a4725237030",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<3.13,>=3.9",
"size": 88697,
"upload_time": "2025-07-13T12:37:06",
"upload_time_iso_8601": "2025-07-13T12:37:06.864401Z",
"url": "https://files.pythonhosted.org/packages/8b/7c/a8de64ca664eb98707a4af283c356d1dec492dd8fa4c24f42f8b0641d582/pybandits-4.0.8-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "862ebbe73198f870b127b00045fe3e2c52c1d3d549e8dd4820a7013112dc7e09",
"md5": "95b28195b96449402b4fbd1bada16cd2",
"sha256": "aab15afdeb1c74e93665b901b5d2e5815eda56e7135ab06b1df919c7350a93bd"
},
"downloads": -1,
"filename": "pybandits-4.0.8.tar.gz",
"has_sig": false,
"md5_digest": "95b28195b96449402b4fbd1bada16cd2",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<3.13,>=3.9",
"size": 73754,
"upload_time": "2025-07-13T12:37:07",
"upload_time_iso_8601": "2025-07-13T12:37:07.758061Z",
"url": "https://files.pythonhosted.org/packages/86/2e/bbe73198f870b127b00045fe3e2c52c1d3d549e8dd4820a7013112dc7e09/pybandits-4.0.8.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-13 12:37:07",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "PlaytikaOSS",
"github_project": "pybandits",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "pybandits"
}