Skip to main content

Causal reinforcement learning: the 9-task causal RL taxonomy, made runnable.

Project description

causalrl

CI Docs PyPI License: MIT Python 3.11+ Ruff

Causal intervention-selection and causal-RL research tools.

causalrl provides graph algorithms for causal bandits, demonstration environments and agents, and explicit-latent structural causal models with see (L1), do (L2), and counterfactual (L3) queries — organised around the 9-task taxonomy of causal RL.

What sets it apart is honesty about scope: identification routines return None — or raise with a witnessing hedge — outside their supported class rather than guessing, and learning agents are labelled benchmark/demo, not production. See Guarantees & Scope.

Install

pip install causalrl            # core: graph, POMIS, tabular agents/environments
pip install "causalrl[torch]"   # + SCM sampling, neural mechanisms, Torch-backed demos

From a clone, for development:

uv sync --extra dev             # tests, lint, typing, notebooks
uv sync --extra docs            # local documentation site and API reference

The core graph, POMIS, tabular-agent, and tabular-environment surfaces do not require PyTorch; SCM sampling, neural mechanisms, and structural-bandit environments do. Full documentation: https://raphaelrrcoelho.github.io/causalrl/.

Quickstart

A causal agent that conditions on its "intuition" beats a confounding-naive agent on the Multi-Armed Bandit with Unobserved Confounders — even though both arms have identical interventional means.

from causalrl.agents.bandits import CausalThompsonSampling
from causalrl.envs.suite.mabuc import MABUCEnv

env = MABUCEnv(seed=1)
agent = CausalThompsonSampling(n_arms=2, n_contexts=2, seed=0)

obs, _ = env.reset(seed=1)
for _ in range(8000):
    action = agent.act(obs)
    _, reward, _, _, _ = env.step(action)
    agent.update(obs, action, reward)
    obs, _ = env.reset()
# CausalThompsonSampling -> ~0.75 reward/step; a confounding-naive baseline is stuck near 0.50.

What it does

Task (taxonomy) Capability Key entry points
Decision under confounding Counterfactual Thompson sampling on the MABUC CausalThompsonSampling
1 — Offline→online Learn from confounded logs via causal bounds UCDTR, DOVI, DeepDeconfoundedQ
2 — Where to intervene POMIS / MIS, incl. non-manipulable variables pomis, minimal_intervention_sets
3 — Counterfactual policy Act on E[Y_do(a) | intent] CounterfactualOptimalPolicy
4 — Transportability Recover effects across domains transport_formula, transported_effect
5 — Causal discovery PC / FCI structure learning discover, CPDAG
6 — Causal imitation Imitability + confounded cloning is_imitable, CausalImitator
7 — Causal curriculum Prerequisite-ordered skill learning causal_curriculum
8 — Reward shaping Policy-invariant causal potentials causal_potential, q_learning
9 — Causal games Influence diagrams + equilibria pure_nash_equilibria, CausalGame
Identification Complete ID / gID / sID / mz; partial-ID, sensitivity & decision certificates identify_effect, manski_bounds, certify_decision

A runnable example for every row is in the Tour by Task; end-to-end notebooks are in examples/ and the Tutorials.

How it compares

causalrl is causal-RL-first, where the established causal libraries are estimation-first:

  • DoWhy / EconML / CausalML target treatment-effect estimation and the identify→estimate→refute workflow on i.i.d. data — deep, mature, production-grade. causalrl instead targets sequential decision-making: intervention-set selection (POMIS), confounded offline-to-online RL, counterfactual policies, and causal curricula / shaping / games — the parts of the Bareinboim taxonomy those libraries do not cover.
  • For pure graph identification it overlaps with Ananke / pgmpy / Y0; for offline RL at real scale it defers to d3rlpy as the designated backbone rather than reimplementing it.

Use causalrl when your problem is a causal decision over time; use DoWhy/EconML when it is a treatment-effect estimate.

Stability

The public API — the names exported from the top-level causalrl package — is stable and follows semantic versioning: from v1.0.0 on, breaking changes to exported names move the major version. The 0.99.x line deliberately let the surface settle in real use first; 1.0 commits to it. See Guarantees & Scope for what each method does and does not promise.

Reproducible benchmarks

uv run --extra dev python benchmarks/scbandit_report.py confounded-chain \
  --seeds 0,1,2,3,4 --steps 8000 --tail-window 2000 --n-mc 2000

The JSON report includes each seed's result plus summary uncertainty. These maintained demonstrations validate package behaviour on the stated environments; they are not general performance guarantees.

Development

uv run pytest                               # tests
uv run ruff check .                         # lint
uv run pyright src                          # types
uv run --extra docs mkdocs build --strict   # documentation

Contributions are welcome — see CONTRIBUTING.md.

Citing

If you use causalrl in research, cite the metadata in CITATION.cff and the primary source for the method you used (each is attributed inline in the Tour by Task and its source module). See Citing causalrl.

Acknowledgements

This library would not exist without the body of work it stands on. Particular thanks to:

  • Elias Bareinboim, whose 9-task taxonomy of causal reinforcement learning is the organising spine of causalrl, and whose results with collaborators are the core of nearly every slice — do-calculus completeness (with Shpitser & Pearl), transportability and selection diagrams (with Pearl), counterfactual data fusion (with Forney & Pearl), POMIS / structural causal bandits (with Lee), and causal imitation learning (with Zhang & Kumor).
  • Judea Pearl, for the do-calculus and Pearl Causal Hierarchy that make every L1 / L2 / L3 query in this library well-defined.
  • Sanghack Lee, for the reference POMIS implementation the intervention-set engine is adapted from (MIT-licensed; attribution in src/causalrl/identification/intervention_sets.py).

Other foundational references — Spirtes, Glymour & Scheines; Zhang; Manski; Tan; Koller & Milch; Ng, Harada & Russell; Bengio et al. — are cited inline at the slice that uses each.

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

causalrl-1.0.0.tar.gz (445.8 kB view details)

Uploaded Source

Built Distribution

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

causalrl-1.0.0-py3-none-any.whl (117.1 kB view details)

Uploaded Python 3

File details

Details for the file causalrl-1.0.0.tar.gz.

File metadata

  • Download URL: causalrl-1.0.0.tar.gz
  • Upload date:
  • Size: 445.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for causalrl-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a7ec67d1933e80b1e87aaac953a304afbe9668ff99b48bc90157ea393b1a9582
MD5 4ddf9b48358d6c3c732bff94122afb50
BLAKE2b-256 f5b84be75bdc3884c32215e3c070091f40165a4ac2a9b2ffc5548946aeae95e9

See more details on using hashes here.

Provenance

The following attestation bundles were made for causalrl-1.0.0.tar.gz:

Publisher: publish.yml on raphaelrrcoelho/causalrl

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

File details

Details for the file causalrl-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: causalrl-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 117.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for causalrl-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 275ec07677a6a3b91b02f0b6a17b4488628a38313704f49d3a29ba4069785311
MD5 5c12cd146ff08d87661a0b92c5ead80e
BLAKE2b-256 40ebe15c28e28ab66452d81f9aed8d1b07055e99e92d9c2433fca801047998ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for causalrl-1.0.0-py3-none-any.whl:

Publisher: publish.yml on raphaelrrcoelho/causalrl

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