Skip to main content

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

Project description

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)

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

pcalg_py-0.0.1.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

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

pcalg_py-0.0.1-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file pcalg_py-0.0.1.tar.gz.

File metadata

  • Download URL: pcalg_py-0.0.1.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for pcalg_py-0.0.1.tar.gz
Algorithm Hash digest
SHA256 3181ec07e6d03524c585ed65682d4d14084ef0262752b5737a0f762c9d3118d2
MD5 6a07914c284eada4d9920cad1598a601
BLAKE2b-256 7ef6679b01177318a80aca00e864c75a0374b040b9b12381759a7d47e1fc1ab1

See more details on using hashes here.

File details

Details for the file pcalg_py-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pcalg_py-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for pcalg_py-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b500d7a1e379c5995c92cbe971b1b272ee2ac4538654afa059f96ff05390abf8
MD5 630b5c6cd4e8fb7ba1ea991125a267ce
BLAKE2b-256 f3008f7c4534331ae41bcc04652a5cf93930fc8a864d692b5aa1614a5410a83c

See more details on using hashes here.

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