Skip to main content

An opinionated, highly optimised contextual bandit engine

Project description

gittins

An opinionated, highly optimised contextual bandit engine.

  • Online by nature. Learns one observation at a time, in O(1) work and fixed memory, as long as open decisions are regularly resolved.
  • Non-stationarity is expected. The engine adapts as the relationship between context and feedback drifts, and never learns anything it cannot eventually unlearn.
  • Dynamic actions and context. The candidate set can change on every call; you never declare the number of actions up front.
  • Safe reward handling. Rewards may arrive late or never. Constructing invalid training data is made hard by design.
  • Speed and determinism. Bit-identical results across platforms and language bindings, enforced by a golden test corpus.
  • Bring your own model. Swap in your own scoring or exploration and inherit everything else.

Full documentation and user guide: docs.getgittins.dev

Install

pip install gittins

Wheels are published for Linux, macOS, and Windows. The extension is built against the stable ABI (abi3), so one wheel per platform covers CPython 3.10 and up.

Usage

The state is an opaque handle updated in place — calls return only their result, and every alias of the handle observes the current state. Persist it with serialize, which returns one plain string.

import time
import gittins

state = gittins.create(bits=8, horizon=3600.0)  # model size, seconds to resolve

candidates = [
    ("banner-sale", {"discount": 0.2}),
    ("banner-new", {"discount": 0.0}),
    ("banner-plain", {}),
]
context = {"device": "mobile", "hour": 14}

record = gittins.decide(state, context, candidates, time.time(), "web-1")
arm_id = candidates[record.chosen][0]
# ... your code: act on the choice, then report the outcome

gittins.learn(state, record.decision_id, 1.0, time.time())
gittins.expire(state, time.time())  # resolve anything past its horizon

open("bandit.txt", "w").write(gittins.serialize(state))

Feature values are typed by what you pass: strings are categorical, ints, floats and bools numeric, None absent. Anything else raises ValueError naming the feature.

The serialization format is shared across implementations — a state written here loads unchanged in the Rust core or the browser/WASM build.

Bringing your own model

decide takes optional score and explore callbacks, and learn/expire take train. Each crosses the Python/Rust boundary once per call — with all candidates, all estimates, or the one resolved record — never once per candidate, so the one boundary crossing per decision is preserved.

Offline policy evaluation

log_line renders a decision record or resolution as one canonical experience-log line. Append it verbatim; it is exactly what the gittins CLI's verify / eval / sweep / replay consume.

with open("decisions.jsonl", "a") as log:
    log.write(gittins.log_line(record) + "\n")

API

Function Purpose
create(bits, horizon, default_reward=0.0, epsilon=..., forgetfulness=...) New state
decide(state, context, candidates, t, salt, score=None, explore=None) Choose; returns a DecisionRecord
learn(state, decision_id, reward, t, train=None) Resolve one decision; returns a Resolution or None
expire(state, t, train=None) Resolve everything past its horizon
serialize(state) / deserialize(text) State as one plain string
model_bits(state) The model's size in bits
log_line(record_or_resolution) One canonical experience-log line

License

MIT. Named after John Gittins, whose index (1974) established that exploration has a precise, computable value.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

gittins-1.0.0-cp310-abi3-win_amd64.whl (180.2 kB view details)

Uploaded CPython 3.10+Windows x86-64

gittins-1.0.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (323.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

gittins-1.0.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (318.1 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

gittins-1.0.0-cp310-abi3-macosx_11_0_arm64.whl (286.3 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

gittins-1.0.0-cp310-abi3-macosx_10_12_x86_64.whl (291.3 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file gittins-1.0.0-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: gittins-1.0.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 180.2 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for gittins-1.0.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 2824d078d346713fb0703206f5914bd07d17dda3a064cea8e09ba9495eb8f71e
MD5 79ef6d783c09b6b725a073132e1e49ea
BLAKE2b-256 393540a42d2dff28714501c99ca616538a796f55dd0df589b945efe0dc3a4b84

See more details on using hashes here.

Provenance

The following attestation bundles were made for gittins-1.0.0-cp310-abi3-win_amd64.whl:

Publisher: release.yml on maxpagels/gittins

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

File details

Details for the file gittins-1.0.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gittins-1.0.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 eeecd62f33ac06dfcd352a4653256646413450060dcfcf8297bcb8039a667011
MD5 cd6d9d6685e25f6b3196c3000332576f
BLAKE2b-256 e5e830fea0c506bbdce2f01f0f79dce1d20aebd78172e5a71415c957c0481f5c

See more details on using hashes here.

Provenance

The following attestation bundles were made for gittins-1.0.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on maxpagels/gittins

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

File details

Details for the file gittins-1.0.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for gittins-1.0.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 6389a7c374c23a68358d0c4a995f408682cea87ffc73a9d74050685e258b149a
MD5 ad9b15082ffb15f2de644c0b4721db54
BLAKE2b-256 c2425dece18a95d5a07168b8c17e4e32e14fdc91ab4456846e1b5ea980ff6ef2

See more details on using hashes here.

Provenance

The following attestation bundles were made for gittins-1.0.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on maxpagels/gittins

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

File details

Details for the file gittins-1.0.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for gittins-1.0.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1a2f12deb8c8753232083ecd84a5fc3294670eb9ebbb8ffc99fe2037ce525c0d
MD5 8192582dd3042a5361fd74d11aac7a7f
BLAKE2b-256 c03bb92d5b8bee9bfd41d74767c51ac9cb40c7c5aa4e5c11cdfffc94d76337da

See more details on using hashes here.

Provenance

The following attestation bundles were made for gittins-1.0.0-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on maxpagels/gittins

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

File details

Details for the file gittins-1.0.0-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for gittins-1.0.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e0013f525360d6e4451f751ac2ac55d4caaf48f00796824cc1a2e7da2e0d31ed
MD5 8ef1c1efbed30dbee9661b5e8ab2d0e5
BLAKE2b-256 0dae0d40ae167bad13023bb0273b9b6ef5274108e42fc429211186a1f5a2641e

See more details on using hashes here.

Provenance

The following attestation bundles were made for gittins-1.0.0-cp310-abi3-macosx_10_12_x86_64.whl:

Publisher: release.yml on maxpagels/gittins

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