Skip to main content

Contextual bandit agents (GP-UCB and Thompson sampling) with GP surrogates, evolutionary acquisition optimisation, and Langevin/NUTS posterior sampling.

Project description

banditry

Contextual bandit agents for black-box optimisation over mixed design spaces.

banditry provides GP-UCB (optimism-in-the-face-of-uncertainty) and Thompson-sampling agents built on Gaussian-process surrogates (exact GP or sparse variational GP), with acquisition optimisation via evolutionary algorithms (pymoo) and posterior sampling via Langevin dynamics (SGLD) or NUTS (pyro).

Installation

pip install banditry

The NUTS sampler (used by the ts-nuts agent) needs pyro, which is an optional extra:

pip install "banditry[nuts]"

Requires Python 3.10+.

Quickstart

import numpy as np

from banditry import DesignSpace, OFUGPConfig, build_agent

# Objective: minimise a 2-D function over a box.
def objective(df):
    x0 = df["x0"].to_numpy(dtype=float)
    x1 = df["x1"].to_numpy(dtype=float)
    return (x0 - 0.3) ** 2 + (x1 + 0.2) ** 2

space = DesignSpace.parse([
    {"name": "x0", "type": "num", "lb": -1, "ub": 1},
    {"name": "x1", "type": "num", "lb": -1, "ub": 1},
])

agent = build_agent(OFUGPConfig(rand_sample=4, surrogate="gp"), space)

for _ in range(20):
    rec = agent.suggest(1)                                # DataFrame of suggestions
    y = np.asarray(objective(rec), dtype=float).reshape(-1)
    agent.observe(rec, y)                                 # agents minimise y

best = agent.get_best_id()

Contexts (variables fixed per round, e.g. observed environment state) are passed as agent.suggest(1, {"x0": 0.5}).

Agents

Agent Config Surrogate / sampler
GP-UCB (MACE acquisition) OFUGPConfig(surrogate="gp") Exact GP (gpytorch)
GP-UCB, sparse variational OFUGPConfig(surrogate="svgp") SVGP (gpytorch)
Thompson sampling, Langevin TSConfig(sampler="langevin") Neural value function + SGLD
Thompson sampling, NUTS TSConfig(sampler="nuts") Neural value function + NUTS (needs [nuts])

Design spaces support numeric, integer, boolean, and categorical parameters (type: "num", "int", "bool", "cat").

A runnable benchmark script lives in the repository:

python main.py --agent ofugp-gp --benchmark branin --n-iter 30 --seed 42

License

CC BY-NC-SA 4.0 — free for non-commercial use with attribution; derivatives must be shared under the same terms.

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

banditry-0.1.0.tar.gz (37.0 kB view details)

Uploaded Source

Built Distribution

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

banditry-0.1.0-py3-none-any.whl (45.7 kB view details)

Uploaded Python 3

File details

Details for the file banditry-0.1.0.tar.gz.

File metadata

  • Download URL: banditry-0.1.0.tar.gz
  • Upload date:
  • Size: 37.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for banditry-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7cc02acb1dd1fd2e25b394a285a8e4d7d84f714a14f488be25b2933d7d21f638
MD5 ca45847d18ca70c4c6d811589b1277e6
BLAKE2b-256 96b538d7974021263b774b406338c90b3b1af8d47b8e903fa7e3602d7a1d1376

See more details on using hashes here.

Provenance

The following attestation bundles were made for banditry-0.1.0.tar.gz:

Publisher: release.yml on VahanArsenian/pyBandits

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file banditry-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: banditry-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 45.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for banditry-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6c4a1306788af2a72f7b57aefc9d8ab6cc722846da285309a2b84ee5e3bed3aa
MD5 39d9cd6d0c3cd3d12bc5367b03a8e3f7
BLAKE2b-256 667c8651ad59218315aeec39547856acfc88eeb2fbec536259a62010477e0045

See more details on using hashes here.

Provenance

The following attestation bundles were made for banditry-0.1.0-py3-none-any.whl:

Publisher: release.yml on VahanArsenian/pyBandits

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page