Skip to main content

C++ port of CMU's Tetrad causal discovery library with Python bindings

Project description

tetrad-port

C++ port of CMU's Tetrad causal inference library, with Python bindings via nanobind.

Algorithms

Algorithm Type Output Latent Confounders
PC Constraint-based (Fisher Z) CPDAG No
FGES Score-based (BIC) CPDAG No
GFCI Hybrid (FGES + FCI rules) PAG Yes
BOSS Permutation-based (BIC) CPDAG No
BOSS-FCI BOSS + FCI rules PAG Yes
GRaSP Permutation-based (tuck DFS) CPDAG No
GRaSP-FCI GRaSP + FCI rules PAG Yes

All algorithms support background knowledge: temporal tiers, forbidden edges, and required edges.

Quick Start

pip install -e ".[dev]"
import pandas as pd
from tetrad_port import TetradPort, Knowledge

tp = TetradPort()
df = pd.read_csv("data.csv")

# Run PC (constraint-based)
results, graph_info = tp.run_pc(df, alpha=0.05)

# Run FGES (score-based, faster for large graphs)
results, graph_info = tp.run_fges(df, penalty_discount=1.0)

# Run GFCI (handles latent confounders)
results, graph_info = tp.run_gfci(df, alpha=0.05)

# Run BOSS (permutation-based, often faster than FGES)
results, graph_info = tp.run_boss(df, penalty_discount=1.0)

# Run GRaSP (permutation-based with DFS tucks)
results, graph_info = tp.run_grasp(df, penalty_discount=1.0)

# Run BOSS-FCI or GRaSP-FCI (latent confounders)
results, graph_info = tp.run_boss_fci(df, alpha=0.05)
results, graph_info = tp.run_grasp_fci(df, alpha=0.05)

# Add background knowledge
k = Knowledge()
k.set_tier(0, ["Age", "Genetics"])    # Cannot be caused by later variables
k.set_tier(1, ["Exercise", "Diet"])
k.set_forbidden("Exercise", "Cholesterol")
k.set_required("Smoking", "BP")
results, graph_info = tp.run_pc(df, alpha=0.05, knowledge=k)

Building from Source

C++ standalone

cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
./build/tetrad_tests        # Run all C++ tests
./build/run_pc              # Example CLI

Python package

pip install -e ".[dev]"
pytest tests/test_python_bindings.py -v

Documentation

Architecture

The C++ core lives in src/ under namespace tetrad:

  • src/graph/ — Node, Edge, Graph (EdgeListGraph with TAIL/ARROW/CIRCLE endpoints)
  • src/data/ — DataSet (Eigen matrix wrapper), Knowledge (tiers, forbidden/required edges)
  • src/search/ — PC, FAS, MeekRules, FGES, FciOrient, GFCI, BOSS, BOSS-FCI, GRaSP, GRaSP-FCI, IndTestFisherZ, SemBicScore, GrowShrinkTree
  • src/util/ — ChoiceGenerator, SublistGenerator

Python bindings (bindings/tetrad_bindings.cpp) expose algorithms via nanobind. The TetradPort facade class (python/tetrad_port/__init__.py) provides a pandas-friendly API with SEM fitting helpers.

Dependencies

C++ (auto-fetched via CMake FetchContent):

  • Eigen 3.4.0 — linear algebra
  • Catch2 v3.5.2 — testing
  • nanobind 2.0+ — Python bindings

Python:

  • numpy, pandas (required)
  • semopy (optional, SEM fitting)

Reference

Ported from Tetrad 7.6.8 (Java). This version contains critical FciOrient correctness fixes reviewed by Peter Spirtes.

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

tetrad_port-0.3.0.tar.gz (169.2 kB view details)

Uploaded Source

Built Distributions

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

tetrad_port-0.3.0-cp312-cp312-win_amd64.whl (2.2 MB view details)

Uploaded CPython 3.12Windows x86-64

tetrad_port-0.3.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

tetrad_port-0.3.0-cp312-cp312-macosx_11_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file tetrad_port-0.3.0.tar.gz.

File metadata

  • Download URL: tetrad_port-0.3.0.tar.gz
  • Upload date:
  • Size: 169.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tetrad_port-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e9c6d751d78c0f1901723f2f0124bb9cd0bdd0fe730727db8e9cc38f4d7d6ed8
MD5 a0541dd1435afdaf928a7a794436845f
BLAKE2b-256 88fe3f50b4b5c5f8d33fc904df287565127f2911d5613ecd57a456d7081f9924

See more details on using hashes here.

Provenance

The following attestation bundles were made for tetrad_port-0.3.0.tar.gz:

Publisher: publish.yml on kelvinlim/tetrad-port

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

File details

Details for the file tetrad_port-0.3.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for tetrad_port-0.3.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 33b84b6354469fb9552246389f518ae9ce7c2c28c3b230923df40ad195a0b9d3
MD5 a454a4e2f7623029afc5d3357dc1131d
BLAKE2b-256 3bf1db9cb2044d135f01fc6508e96c7a8ea87134b25f446e3f4bce6dbb77f261

See more details on using hashes here.

Provenance

The following attestation bundles were made for tetrad_port-0.3.0-cp312-cp312-win_amd64.whl:

Publisher: publish.yml on kelvinlim/tetrad-port

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

File details

Details for the file tetrad_port-0.3.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tetrad_port-0.3.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c0801ae2ef8a0f5d207df7d8ae0bece537860fe8a42b1524570185f9470bba4c
MD5 258305a4e2e1fc2c3588e549c32f772a
BLAKE2b-256 3ff4724139e6e38b3a5bdaad83e782cc185f6cf854b0912018f2eb6618a4668f

See more details on using hashes here.

Provenance

The following attestation bundles were made for tetrad_port-0.3.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on kelvinlim/tetrad-port

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

File details

Details for the file tetrad_port-0.3.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tetrad_port-0.3.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3a346ff4378b670bde4c376b063042f83918b2408e6dc81af27e554cf1b6db2e
MD5 54c5c1afda520256ac13b2f2b51b3c05
BLAKE2b-256 3f1c88de261529e39fb8a1dbb54ba43746661c97a5b56a4cb321e71a623cf926

See more details on using hashes here.

Provenance

The following attestation bundles were made for tetrad_port-0.3.0-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: publish.yml on kelvinlim/tetrad-port

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