Skip to main content

pcalg-py

A Python implementation of the PC algorithm for causal structure learning, ported from the R package pcalg.

This package provides a strict re-implementation of the PC algorithm (Spirtes et al., 2000) and related functions (Skeleton estimation, separation sets, etc.), verified to match the reference R implementation exactly.

Features

  • PC Algorithm: Estimate the equivalence class of a DAG (Patterns/PDAG) from observational data.
  • Skeleton Estimation: Infer the undirected skeleton graph.
  • Separation Sets: Record condition sets that d-separate variables.
  • Parsimonious: Strict port of the stable method from pcalg ensuring order-independence.
  • Verified: Numerically comparable to R output (see COMPARISON_REPORT.md).

Installation

You can install the package via pip (after building or if published):

pip install pcalg-py

Or install from source:

git clone https://github.com/yourusername/pcalg-py.git
cd pcalg-py
pip install .

Usage

import numpy as np
import pandas as pd
from pcalg_py.pc import pc
from pcalg_py.indep_test import gaussCItest, SuffStat

# 1. Prepare Data (Correlation matrix and sample size)
# Example: Generate random data
np.random.seed(42)
data = np.random.randn(100, 5)
C = np.corrcoef(data.T)
n = 100
labels = [str(i) for i in range(5)]

# 2. Create Sufficient Statistics object
suffStat = SuffStat(C=C, n=n)

# 3. Run PC Algorithm
# alpha is the significance level for the independence test
res = pc(suffStat, gaussCItest, alpha=0.01, labels=labels, verbose=True)

# 4. Access Results
graph = res["graph"] # NetworkX DiGraph (CPDAG)
pdag_matrix = res["pdag_matrix"]
sepset = res["skel"]["sepset"]

print("Edges:", graph.edges())

Structure

  • src/pcalg_py/: Source code.
  • tests/: Verification scripts comparing R vs Python.

License

GPLv2 (Derived from pcalg R package)

Metadata

Release files for pcalg-py 0.0.1

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

Source distribution (sdist)

Source distribution for pcalg-py 0.0.1
File Size Uploaded
pcalg_py-0.0.1.tar.gz 18.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pcalg-py 0.0.1
File Interpreter ABI Platform
pcalg_py-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 33.8 kB

Release files / pcalg_py-0.0.1.tar.gz

Download URL pcalg_py-0.0.1.tar.gz
Size 18.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3181ec07e6d03524c585ed65682d4d14084ef0262752b5737a0f762c9d3118d2
BLAKE2b-256 checksum
How to use checksums
7ef6679b01177318a80aca00e864c75a0374b040b9b12381759a7d47e1fc1ab1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / pcalg_py-0.0.1-py3-none-any.whl

Download URL pcalg_py-0.0.1-py3-none-any.whl
Size 15.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b500d7a1e379c5995c92cbe971b1b272ee2ac4538654afa059f96ff05390abf8
BLAKE2b-256 checksum
How to use checksums
f3008f7c4534331ae41bcc04652a5cf93930fc8a864d692b5aa1614a5410a83c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.0.1 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