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

Uploaded CPython 3.10+Windows x86-64

gittins-1.0.1-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.1-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.1-cp310-abi3-macosx_11_0_arm64.whl (286.2 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

gittins-1.0.1-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.1-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: gittins-1.0.1-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.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 364d073304b0515da0fd7eedc6a42079ae8219722538412eb90e7e28ee626250
MD5 976795ff8f095878cfdadebe5bece13e
BLAKE2b-256 d68f2f51cd3700c12610d4af3c055edae5ca1e884baaee08b3fdfec8ec1dab71

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for gittins-1.0.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5ed5c4352a37885eafdf1088114037d8de3604197678095d56c29e5e6325b9cf
MD5 747818f66414f6e04ba1882082d4ae4e
BLAKE2b-256 1a15f971c71a6fff84eba63871a1afc17a6316cd4c6d7170419d9d2f88823359

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for gittins-1.0.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 2e96ad05aee89b7ff1765ffd7aa02ed0a55bb7a6d1beb05298e7fc83cc9f7ee9
MD5 b81c7f0cd93ebe20801742d3fc42f93b
BLAKE2b-256 3962356aaf16ef329b7c160306cab641ff2eb63926bb62b8ad5f06904f8d4f71

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for gittins-1.0.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1eaaaf08bd3719b8606e6a7d356c5eb2e003951502c8a12f6959dbeabec67b83
MD5 8c84ff521cc70f3b1add282ceec7d502
BLAKE2b-256 9502b1e9087b67f50cc20aa3fac9504a62e261f9004061b1820bdab83d43b5a1

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for gittins-1.0.1-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9546d500521b9ba3bfb2b3897f4cb35ac879d3f5ea895364796d41893ff00cdc
MD5 ceacf874da568c56412f3414f04cac43
BLAKE2b-256 6fcd9a0da906f3829df50502b06aa8e5435403d7115d8f04c155db738b7ad4a3

See more details on using hashes here.

Provenance

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