Skip to main content

cleverly

Read the cleverly documentation →

Python 3.11+ License: MIT Status: alpha

cleverly is the Python toolbox for causal studies built on targeted maximum likelihood estimation (TMLE), organized around the causal question before the estimation method.

It is named for TMLE's clever covariate. Even the jokes in this toolbox are targeted.

study design -> typed estimand -> identified effect -> estimation method -> causal result

cleverly supports point and longitudinal treatment settings, influence-curve inference, cross-fitting, diagnostics, sensitivity analysis, and structured persistence. It accepts pandas, polars, Arrow-backed pandas, and pyarrow.Table inputs through narwhals.

[!WARNING] cleverly is alpha software, and releases use 0.1.N versions. Pin a version for reproducible work. Unsupported combinations fail before nuisance fitting instead of returning an approximation to another causal question.

Install

Install the core package from PyPI:

python -m pip install cleverly

Add pandas, polars, and plotting support with the all extra. Third-party nuisance estimators such as XGBoost or LightGBM can be installed separately and passed as sklearn-compatible objects:

python -m pip install "cleverly[all]"

Python 3.11 or newer is required. See Installation for a development environment and reproducible version-pinned installs.

Quickstart

Declare the observed-data design and the causal estimand separately, inspect identification, then estimate:

from sklearn.linear_model import LinearRegression, LogisticRegression
from cleverly import ATE, CausalStudy, PointTreatment
from cleverly.datasets import make_nonlinear_ate

frame, truth = make_nonlinear_ate(n=2_000, seed=7)
study = CausalStudy(
    frame,
    design=PointTreatment(
        outcome="Y",
        treatment="A",
        adjustment=("W1", "W2", "W3", "W4"),
    ),
)

effect = study.identify(ATE())
print(effect.summary())

result = effect.estimate(random_state=7)
print(result.summary())
print(result["ate"].ci)

The identified effect states the observed-data functional, assumptions, nuisance requirements, and available methods before any learner is fit. The result retains estimates, influence curves, joint covariance, structured parameter keys, normalized method configuration, provenance, and post-fit assessment.

Continue with the full quickstart or the analysis workflow.

Documentation

The Sphinx/MyST documentation is published on GitHub Pages and builds from docs/.

section use it for
Getting started installation, first fit, and result basics
Workflow moving from a causal question through identification, estimation, assessment, and reporting
User guide data roles, estimands, learners, methods, longitudinal designs, results, and refusals
Technical reference every implementation family, with theory, citations, local source, external provenance, and evidence
Examples complete point, intervention, longitudinal, and post-fit workflows
Python API generated signatures, attributes, methods, and return types

The development reference contains the roadmap, the architecture invariants, the test tiers and gates, and the method-benchmarking strategy. The test-enforced evidence manifest lives in the Technical reference.

Implemented analysis families

This table summarises the package. The technical implementation matrix is the authoritative inventory, and it names the evidence for each row.

family what cleverly ships
point-treatment effects counterfactual means, ATE, ATT, ATC, risk and odds ratios, natural-course means, population-attributable effects, multi-valued treatments, missing outcomes, and controlled direct effects
interventions static, dynamic, and stochastic regimes; continuous modified treatment policies; incremental propensity-score interventions; and point and longitudinal MSM projections
longitudinal outcomes regimen means and contrasts for end-of-study, survival, and competing-risk outcomes
design and inference observation weights, strata, cluster-robust inference, cross-fitting, repeated cross-fitting, CV-TMLE, simultaneous intervals, and bootstrap inference
estimator variants ordinary TMLE, collaborative TMLE, and DR-TMLE, each over its documented compatible estimands
post-fit work positivity, nuisance, and score diagnostics; omitted-variable, E-value, and missingness sensitivity analyses; refutation; variable importance; and trusted whole-result persistence

Roadmap

  • Extend sensitivity and validation with source-backed refutations and longitudinal analyses.
  • Add optional DoWhy identification and graph validation while the core package stays standalone.
  • Add EP learning for conditional average treatment effects and conditional relative risks.
  • Extend current methods through persistence, design, splitting, and new evidenced estimands.
  • Build the nested Riesz engine and expand its catalog one evidenced target at a time.

These are accepted directions, not implemented release claims. The roadmap gives their binding order. It keeps work without published theory in a separate future grid.

Method configuration

Named shortcuts normalize into immutable configuration groups:

from cleverly import CrossFitting, Inference, ModelSpec, Runtime, TMLEMethod

method = TMLEMethod(
    models=ModelSpec(
        outcome_learner=LinearRegression(), treatment_learner=LogisticRegression(max_iter=1000)
    ),
    cross_fitting=CrossFitting(n_folds=5, learner_folds=3),
    inference=Inference(alpha=0.05, simultaneous=False),
    runtime=Runtime(random_state=7, n_jobs=1),
)
result = effect.estimate(method=method)

CollaborativeTMLEMethod and DRTMLEMethod select estimator variants without changing the identified causal question. effect.available_methods() reports support and refusal reasons.

Development

uv venv
uv pip install -e ".[dev,docs]"
ruff check .
ruff format --check .
python -m tests.prose
mypy
pytest -m "not slow" -q
sphinx-build -W --keep-going -b html docs docs/_build/html

mypy reads its own configuration, which covers src/cleverly and scripts. nox -s docs runs the warning-as-error build in an isolated environment. CI uses this command. The direct call is faster, so the installation command above includes the docs extra.

python -m tests.prose reports on the reader-facing prose. It does not rewrite anything and it does not fail on what it finds. Read each finding, then either change the sentence or record accepted: <reason> against it in tests/prose-report.md. The fast tier fails only on a finding with no recorded judgment, which keeps the decision with the writer. Sentence length is advisory output and carries no row at all. tests/prose.py says which rules were rejected and why.

The fast tier compiles every Python fence and executes the registered reader-facing guides. It also resolves relative links and checks that generated API source represents the root API. Scientific behavior belongs in ordinary fast tests or named slow statistical studies. Run the relevant checks locally before handoff; a green GitHub Actions CI run is the final merge signal.

CONTRIBUTING.md gives the branch names, the checks each kind of change needs, and the commit style.

Citing

There is no DOI yet. Cite the repository commit used in the analysis and the primary method papers listed in the technical reference and references.

License

MIT License.

The R comparison runners under tests/canonical/ are under the GNU General Public License v3.0. They call reference R packages in the same process, and one package is AGPL-3. No published distribution carries them, so an installed copy of cleverly is MIT alone.

Metadata

Release files for cleverly 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cleverly 0.1.0
File Size Uploaded
cleverly-0.1.0.tar.gz 1.2 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for cleverly 0.1.0
File Interpreter ABI Platform
cleverly-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 1.9 MB

Release files / cleverly-0.1.0.tar.gz

Download URL cleverly-0.1.0.tar.gz
Size 1.2 MB
Tags Source
SHA-256 checksum
How to use checksums
c84add64ac64e544f6b72365c87023f217bd214ba0b8722fd4f57be5b9ff6f74
BLAKE2b-256 checksum
How to use checksums
af2bc1deb8b0267b953f9810172f8be8f03741d7122b87ec07d9f06a1b9fc890
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 1, 2026.

Transparency log

Release files / cleverly-0.1.0-py3-none-any.whl

Download URL cleverly-0.1.0-py3-none-any.whl
Size 681.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6a256299859a01b64c8062482d790e528a7441921a96902c636f2628a0f7b2ae
BLAKE2b-256 checksum
How to use checksums
0bf887f3c037d7b98ff88225ca40ca9dafc03a41803f9b370f80706a6ce827e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 1, 2026.

Transparency log

Release history Release notifications | RSS feed

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

This release

0.1.0 This release

2 release 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