Lightweight PC algorithm for causal skeleton discovery — pip + CLI + graph viz from a CSV in ~30 seconds.
纯 Python PC 算法(Peter–Clark):从观测数据画因果骨架。比 R
pcalg更轻,比causal-learn更易读。
pip install -e .
pcalg demo
# or: pcalg run your_data.csv --alpha 0.05 -o skeleton.pngUseful? Star ⭐ so others can find a small causal-discovery CLI.
| py-pcalg | causal-learn | R pcalg | |
|---|---|---|---|
| Install | pip + CLI |
heavier deps | R runtime |
| Code size | ~1k LOC, readable | full toolkit | reference impl |
| Use case | quick skeleton + viz | research pipeline | production stats |
pip install -e . # from source
# after PyPI publish:
# pip install py-pcalgDependencies: numpy, pandas, matplotlib, networkx.
# bundled demo (test_data.csv)
pcalg demo
# your CSV (numeric columns)
pcalg run your_data.csv --alpha 0.05 -o skeleton.png
# headless CI
pcalg run your_data.csv --no-plotimport pandas as pd
from pcalg import skeleton, independence_test, visualize_graph
data = pd.read_csv("your_data.csv")
corr = data.corr()
labels = list(range(data.shape[1]))
label_dict = {i: str(c) for i, c in enumerate(data.columns)}
graph = skeleton(
suff_stat=[corr.to_numpy(), len(data)],
indep_test=independence_test,
alpha=0.05,
labels=labels,
)
visualize_graph(graph, label_dict, save_path="skeleton.png")
print(graph.M)Legacy imports still work: from pc import skeleton, from utils import Matrix.
- Start from a complete undirected graph.
- Skeleton discovery: for each edge (X, Y), search conditioning sets S; remove X–Y if X ⊥ Y | S (Fisher-Z test, α = 0.05 by default).
- Orientation (partial): V-structures and Meek rules — full CPDAG orientation is a roadmap item.
Independence test: Fisher's Z on partial correlations (see pcalg.utils.independence_test).
py_pcalg/
├── pcalg/ # installable package
│ ├── pc.py # skeleton(), pc()
│ ├── utils.py # Matrix, Fisher-Z tests
│ ├── graph.py # NetworkX + matplotlib viz
│ └── cli.py # pcalg command
├── tests/ # pytest
├── test_data.csv # demo dataset
└── pyproject.toml
pip install -e ".[dev]"
pytest- causal-learn — CMU toolkit (PC, FCI, GES, …)
- gCastle — Huawei causal structure learning
- pcalg (R) — original reference implementation
References: Spirtes, Glymour & Scheines (2000); Kalisch & Bühlmann (2007).
- Causal Markov + faithfulness; no latent confounders (use FCI for that).
- Needs sufficient sample size for stable partial-correlation tests.
- Dense graphs can be slow —
m_maxcaps conditioning-set size.
MIT — see repository root.
