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 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.2-cp310-abi3-win_amd64.whl (180.0 kB view details)

Uploaded CPython 3.10+Windows x86-64

gittins-1.0.2-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.2-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (317.9 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

gittins-1.0.2-cp310-abi3-macosx_11_0_arm64.whl (286.2 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

gittins-1.0.2-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.2-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: gittins-1.0.2-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.2-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 5f5d9b7c3619c0affb2e929c544a7411e9b6478c0105820daa697733138528a8
MD5 656167136d33d326bdcdcfc3813a5c06
BLAKE2b-256 9ba057beb378cf9355b013f91d4dde5130f07e0b9f36932c89417b19aec67666

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for gittins-1.0.2-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 df336cb09a92a513e986e37bff12b59d9ae8c8df56d07b6bb498068f7569a596
MD5 5ae6143d7a93adf6e2f926a4c5081cbf
BLAKE2b-256 bdf8fab61933a08790ff115826162ec901c436af225b891b1bb7afe7bbebff80

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for gittins-1.0.2-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 bbcee3658b38c68d61b81fe0e52099fdf291034b35f5924e42c86ee6342f9254
MD5 0fb239c91e861bd11a9a8bd31f39885e
BLAKE2b-256 8f6b068a440b378cccb0d41f036f4bc38adddce3e3eeea549c2f6587a874f9df

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for gittins-1.0.2-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b014232f4c8f48688aa5c8db37c95982e3316d4e5e78f5eb708a239d26d1c713
MD5 b00d9065b1e10f436e36ed5760241f1f
BLAKE2b-256 4791b6b62364c19c9e8b5bde5e86b97481128693743ca23f12937fb66e755d7e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for gittins-1.0.2-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 2fcfc0ddf01790fde06de3d0fe5545e2361bcb57aa77c86e39030359a5858ab8
MD5 f4123e35f0b9b06181633fb59d5fd0f6
BLAKE2b-256 63380f5550ede080090b7cd307048d0ff5fec2a403dd041e1d0453a549443ea2

See more details on using hashes here.

Provenance

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