Skip to main content

ctxchain

Context-dependent Markov transition models for sequence data.

Status: pre-alpha. Everything described here works and is tested. The API is the one the design was written against, not one that grew out of the implementation.

What it is

Estimate transition structure from sequences — customer histories, visit paths, holding-size trajectories — and answer "given where they are now, where are they in N periods?" as a probability distribution, with an interval around it.

>>> import ctxchain as cx
>>> ds = cx.SequenceDataset.from_sequences(
...     [["browse", "cart", "buy"], ["browse", "browse", "cart"], ["cart", "buy"]] * 20
... )
>>> spec = cx.Order(1)
>>> model = cx.fit(cx.accumulate(ds, spec), spec)
>>> model.predict_next(["cart"]).top(2)
[('buy', 0.97...), ('cart', 0.01...)]
>>> round(float(model.forward("browse", steps=3).mean.sum()), 6)
1.0

Every model in the library has one shape:

P(next_state | context(history, covariates))

A first-order chain, a variable-length chain, a time-inhomogeneous chain and a per-segment chain differ only in how context is defined. You declare the context; estimation and prediction are shared.

>>> cx.Order(2)                                    # the previous two states
Order(2, padding='bos')
>>> cx.VariableOrder(max_depth=5).max_depth        # depth chosen per context
5
>>> (cx.Order(1) * cx.TimeIndexed(index="tenure")).state_slot   # state x tenure
0

Why not the usual implementation

Constraint ctxchain
Dense S × S matrices Counts live in a trie over observed contexts; S^k is never allocated
Order limited to ~2 Variable-length contexts, depth chosen per context
Homogeneity baked in Time-varying contexts, with adjacent periods shrunk toward each other
One pooled matrix Grouped contexts that shrink toward the pooled estimate
Censoring ignored Right-censored sequences contribute a context but no final transition
Point estimates only Posterior draws propagated through the N-step forecast

The pipeline

SequenceDataset → accumulate → CountStore → fit → FittedChain

CountStore holds sufficient statistics. Once counting is done the raw data is no longer needed, memory stops depending on how much of it there was, and any number of estimators can be run against the same counts.

>>> counts = cx.accumulate(ds, spec)
>>> counts.n_contexts            # only what was observed, never S**k
2
>>> mle = cx.fit(counts, spec, estimator=cx.estimators.MLE())
>>> backoff = cx.fit(counts, spec, estimator=cx.estimators.Backoff())
>>> mle.n_states == backoff.n_states
True

Getting answers out

The step column may be a date, as long as you say what one step means -- the unit decides what counts as a gap, so it is asked for rather than guessed.

>>> import pandas as pd
>>> frame = pd.DataFrame(
...     {
...         "id": ["u", "u", "u"],
...         "month": pd.to_datetime(["2024-01-31", "2024-02-29", "2024-03-31"]),
...         "plan": ["free", "pro", "pro"],
...     }
... )
>>> monthly = cx.SequenceDataset.from_dataframe(
...     frame, entity="id", step="month", state="plan", step_unit="M"
... )
>>> next(iter(monthly)).steps          # calendar months, not elapsed days
[24288, 24289, 24290]

A fitted model exports a labelled table, answers the long-run question, and survives the session:

>>> plans = cx.SequenceDataset.from_sequences([["free", "pro", "pro"] * 8] * 25)
>>> plan_model = cx.fit(cx.accumulate(plans, spec), spec, estimator=cx.estimators.MLE())
>>> plan_model.to_frame().round(2)     # doctest: +NORMALIZE_WHITESPACE
to     free   pro
from
free   0.00  1.00
pro    0.47  0.53
>>> plan_model.stationary().top(2)     # long-run share of time in each state
[('pro', 0.68...), ('free', 0.31...)]
>>> import tempfile, pathlib
>>> with tempfile.TemporaryDirectory() as tmp:
...     saved = plan_model.save(pathlib.Path(tmp) / "model")
...     cx.FittedChain.load(saved).stationary().top(1)
[('pro', 0.68...)]

stationary() refuses a chain with two closed classes rather than picking one: where such a process settles depends on where it started. With a posterior it solves once per draw, because π is non-linear in P -- the same reason forward never powers the mean matrix.

Estimators

use it for gives you
MLE a baseline, debugging point estimate, no smoothing
DirichletSmoothing small state spaces point estimate, optional posterior
Backoff large sparse spaces (10⁶+ contexts) point estimate, fast, deterministic
HPYP the same hierarchy, with intervals posterior draws, in memory
HierarchicalBayes small spaces, time-varying full posterior via NUTS or SVI

Install

pip install ctxchain

Optional extras: ctxchain[bayes] (NumPyro posterior inference), ctxchain[io] (pandas/parquet readers), ctxchain[viz], ctxchain[sparse].

Tutorials

Both are executable: the test suite runs them.

On uncertainty

Three things this library refuses to do quietly.

Power the mean matrix. E[P]ⁿ ≠ E[Pⁿ]. Posterior draws are propagated one at a time and summarised at the end, so intervals do not collapse as the horizon grows.

Power a matrix that does not describe the chain. A model conditioning on more than one state is not Markov on the states, so there is no S × S matrix to raise to a power at all. forward walks paths that carry their history instead, and transition_matrix asks for the states it is missing rather than quietly returning the start-of-sequence rows.

>>> deep = cx.VariableOrder(max_depth=3)
>>> chain = cx.fit(cx.accumulate(ds, deep), deep)
>>> try:
...     chain.transition_matrix()
... except ValueError as error:
...     print(str(error).split(". ")[0])
VariableOrder(...) conditions on 3 states, so a transition matrix needs the 2 preceding one(s) in history=; got 0
>>> chain.forward(["browse", "cart"], steps=4, n_paths=2000, seed=0).n_paths
2000

Report a number without its support. FittedChain.diagnostics reports per-context coverage, prior dominance and effective order, and the serious warnings appear in the chain's repr whether or not you ask for them.

>>> thin = cx.SequenceDataset.from_sequences([["a", "b", "c"]])
>>> deep = cx.VariableOrder(max_depth=3)
>>> shown = repr(cx.fit(cx.accumulate(thin, deep), deep))
>>> "[error] thin_contexts: 7 of 7 contexts (100%)" in shown
True

Call a what-if a causal effect. replace_transition recomputes a forecast under a modified matrix. Identification is not this library's job, and it does not pretend otherwise — which is why the method is not called intervene.

Not in scope

HMMs and latent-state models, continuous-time chains, deep sequence models, causal-effect estimation, plotting beyond a few inspection helpers. See §2.2 of the design document, and DECISIONS.md for the reasoning behind every choice that was not obvious.

Development

python -m venv .venv && . .venv/bin/activate
pip install -e '.[dev]'
ruff check . && ruff format --check . && mypy && pytest

The suite covers 100% of the package, and pytest --cov fails below 99%. The only coverage exclusions are pragma: no cover on nine environment-dependent or defensive lines, each with its reason written next to it.

Benchmarks are in benchmarks/ and are not run by CI:

python benchmarks/bench_accumulate.py --states 1000 --depth 5 --transitions 10_000_000

License

MIT

Download files

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

Source Distribution

ctxchain-0.1.0.tar.gz (204.2 kB view details)

Uploaded Source

Built Distribution

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

ctxchain-0.1.0-py3-none-any.whl (109.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ctxchain-0.1.0.tar.gz
Algorithm Hash digest
SHA256 005e1c168eb62f6d752a6b5958b46960110aebfa24fc2230098bfaf6cb73e1a6
MD5 d2a2861d05ca8b4bb2264e8ff21be065
BLAKE2b-256 3ee1f1d13be7916f14b81452a8d13858782687cd6b35da493ee90cc088560d69

See more details on using hashes here.

Provenance

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

Publisher: release.yml on tomtomtom1007/ctxchain

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

File details

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

File metadata

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

File hashes

Hashes for ctxchain-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8bcc04d0c3364674df95f88f18ca89dd42587af57adc4324c9320709c8904c82
MD5 9894083aea2a313f259bd63babb0bd32
BLAKE2b-256 409490ae3de7ebb2399f047b0d3977d1a6838ff2fb1f3cdf57fa70d3832f26e2

See more details on using hashes here.

Provenance

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

Publisher: release.yml on tomtomtom1007/ctxchain

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

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page