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

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.

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 Distribution

gittins-1.0.3.tar.gz (52.1 kB view details)

Uploaded Source

Built Distributions

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

gittins-1.0.3-cp310-abi3-win_amd64.whl (180.0 kB view details)

Uploaded CPython 3.10+Windows x86-64

gittins-1.0.3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (323.2 kB view details)

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

gittins-1.0.3-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (318.0 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

gittins-1.0.3-cp310-abi3-macosx_11_0_arm64.whl (286.1 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

gittins-1.0.3-cp310-abi3-macosx_10_12_x86_64.whl (291.1 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file gittins-1.0.3.tar.gz.

File metadata

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

File hashes

Hashes for gittins-1.0.3.tar.gz
Algorithm Hash digest
SHA256 091dfcb883c0e4543c796c43a22eb983c72f743f358dd7d92b9b7bcb542a49f6
MD5 77078d28d221fb473c5e43c857f8ff12
BLAKE2b-256 379a42d1af359bdc892dc238ab74bbf2fe58d05d9f24bb7fd9c4086a8b202115

See more details on using hashes here.

Provenance

The following attestation bundles were made for gittins-1.0.3.tar.gz:

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.3-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: gittins-1.0.3-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 180.0 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.3-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 21782eb5ed4b176a855a694933321f8495f67f3ec4c61c16064b1f19e3831e3f
MD5 d8fd7af434edd136f50a496190183ba2
BLAKE2b-256 6e52d66bd3889915ce2887189fbe3aebd9a71d07b0efa498bbf712e691aeef63

See more details on using hashes here.

Provenance

The following attestation bundles were made for gittins-1.0.3-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.3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gittins-1.0.3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fb7a996fa71556d63e025010e340f51eeb32d5744f4e9290231834251808bc5d
MD5 47e9f65d964b63402d0f8a6777729add
BLAKE2b-256 8c9e918d5a2715a9e686e934b2ea83c0a1d026b965ae3c152bc8dd039b43b1ba

See more details on using hashes here.

Provenance

The following attestation bundles were made for gittins-1.0.3-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.3-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for gittins-1.0.3-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b79cd8db18931d73a5e317333ce2645d9aa85b185f5a03734186e9a8abb8ed6b
MD5 633245eb7d98cd56147b846c0f18e086
BLAKE2b-256 75caed2528cf03648c1ec5b1958f40eff6d1bc3da60081130e78efefed230002

See more details on using hashes here.

Provenance

The following attestation bundles were made for gittins-1.0.3-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.3-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for gittins-1.0.3-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7d08d925256e7d55b114681bdc06eae51ca0d140442df3b9b2b004eb666e4a6d
MD5 44fcd30c15324be4d13deea28670755a
BLAKE2b-256 91927ff6b0dd6727e4c4bba84f9f520ac4d82ca088c3becdfcc401f77c277bf2

See more details on using hashes here.

Provenance

The following attestation bundles were made for gittins-1.0.3-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.3-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for gittins-1.0.3-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 31d7ca2a01dd67671afa7ec40306d252ccfe725e04917532a3de4d9092e424ef
MD5 fdfaf3ab67eb8ab77fdb619a714ae5e5
BLAKE2b-256 9093b28f685a815e469afbf79f4c384b1ffc500ed29eee074d3b04f15948bbd8

See more details on using hashes here.

Provenance

The following attestation bundles were made for gittins-1.0.3-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