Skip to main content

A Python implementation of the original PC algorithm.

Project description

PyPCAlg

This repository contains a Python implementation of the original PC algorithm, as described in Causation, Prediction, and Search by P. Spirtes, C. Glymour and R. Scheines (2nd edition, MIT Press, 2000).

Installation

The package is available from PyPi : PyPCAlg.

To install, run

pip install PyPCAlg

Note on the structure of the package

Folder examples contains examples of small dimensional graphs (i.e. with a low number of nodes) to test the PC algorithm on.

The exhaustive lists of the (conditional) independence relationships satisfied by these examples (assuming both the causal Markov condition and causal Faithfulness) have been worked out. They are contained in files :

  • examples/true_independence_relationships_graph_1.csv,
  • examples/true_independence_relationships_graph_2.csv,
  • examples/true_independence_relationships_graph_3.csv, and
  • examples/true_independence_relationships_graph_4.csv.

In practice, the results of the PC algorithm depend on the statistical tests of (conditional) independence that we use. Considering the high number of statistical (conditional) independence tests carried out by the PC algorithm (even on graphs of moderate sizes), it is inevitable that some of these statistical tests will be erroneous (that is the whole problem of Multiple Hypothesis Testing).

By providing the lists of (conditional) independence relationships satisfied by the examples, we make it possible to check whether the implementation of the PC algorithm itself is correct (indeed, things are as if we had at our disposal statistical tests of unconditional/conditional dependence that always return a correct result : no type I error, no type II error).

Example of use

from PyPCAlg.pc_algorithm import run_pc_algorithm, field_pc_cpdag, \
    field_separation_sets
from PyPCAlg.examples.graph_4 import generate_data
from PyPCAlg.examples.graph_4 import oracle_indep_test
from PyPCAlg.examples.graph_4 import oracle_cond_indep_test
from PyPCAlg.examples.graph_4 import get_adjacency_matrix


df = generate_data(sample_size=10)
independence_test_func = oracle_indep_test()
conditional_independence_test_func = oracle_cond_indep_test()

dic = run_pc_algorithm(
    data=df,
    indep_test_func=independence_test_func,
    cond_indep_test_func=conditional_independence_test_func,
    level=0.05
)
cpdag = dic[field_pc_cpdag]
separation_sets = dic[field_separation_sets]

print(f'The true causal graph is \n{get_adjacency_matrix()}')
print(f'\nThe CPDAG retrieved by PC is \n{cpdag}')

The example above demonstrates the use of the PC algorithm on one of the examples provided, using oracle independence and conditional independence tests. The user can provide their own tests of independence / conditional independence ; they need only have the following signatures :

def user_provided_independence_test(data: pandas.DataFrame, x: int, y: int, 
    level: float) -> bool:
    """
    Tests whether the variables X and Y with respective observations 
    data.iloc[:, x] and data.iloc[:, y] are statistically independent at 
    the level considered
    """
    # code for the independence test provided by the user goes here...

def user_provided_conditional_independence_test(data: pandas.DataFrame, x: int,
    y: int, z: list[int], level: float) -> bool:
    """
    Tests whether the variables X and Y with respective observations 
    data.iloc[:, x] and data.iloc[:, y] are statistically independent 
    conditionally on the variables z with observations data.iloc[:, z] at 
    the level considered.
    """
    # code for the conditional independence test provided by the user goes here...

References

  • Causation, Prediction, and Search P. Spirtes, C. Glymour and R. Scheines (2nd edition, MIT Press, 2000)
@book{SpirtesGlymourScheines2000,
	author = {Spirtes, Peter and Glymour, Clark N and Scheines, Richard},
	title = {{Causation, Prediction, and Search}},
	publisher = {MIT press},
	year = {2000},
	edition = {2nd},
	series = {Adaptive Computation and Machine Learning}
}

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

PyPCAlg-1.0.3.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

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

PyPCAlg-1.0.3-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file PyPCAlg-1.0.3.tar.gz.

File metadata

  • Download URL: PyPCAlg-1.0.3.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.0

File hashes

Hashes for PyPCAlg-1.0.3.tar.gz
Algorithm Hash digest
SHA256 2601533829007854fa361b17aa9f3a758da3e2bb086fe9d4f78c89494970bb61
MD5 4f0e9a30d03dec0b5c245a60aa82a294
BLAKE2b-256 958d6255e5d17bf85d9292596cef69a55a213b5b38ab76852270549becad4683

See more details on using hashes here.

File details

Details for the file PyPCAlg-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: PyPCAlg-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 24.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.0

File hashes

Hashes for PyPCAlg-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9cc3980ca7f6fef66b183448b8b381e5dc027d9a5333d2c599cc89a870ea1948
MD5 bc634f61de3cb69f55a27b195a7d4d6a
BLAKE2b-256 861e0df3100abbb3bf1e0af9f5eb8e558b24aa523e4df3fb0f0ceef7ddf266b4

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