Skip to main content

Python implementation of the GES algorithm for causal discovery

Project description

Greedy Equivalence Search (GES) Algorithm for Causal Discovery

Visit github.com/juangamella/ges for the full documentation and source code.

This is a python implementation of the GES algorithm from the paper "Optimal Structure Identification With Greedy Search" by David Maxwell Chickering. It includes the additional turning phase described in Hauser & Bühlmann (2012).

Installation

You can clone this repo or install the python package via pip:

pip install ges

The only dependency outside the Python Standard Library is numpy>=1.15.0. See requirements.txt for more details.

When you should (and shouldn't) use this implementation

To the best of my knowledge, the only other public implementation of GES is in the R package pcalg. It can be called from Python through an easy-to-use wrapper in the Causal Discovery Toolbox, but given its scope, this library contains many additional dependencies (including PyTorch) and still requires you to have R.

Thus, this implementation might be for you if:

  • you want a dependency-light implementation (the only dependency outside the Python Standard Library is numpy), or
  • you want to rewrite parts of GES for your own research, but you'd rather do it in Python. The code has been written with an emphasis on readability, and everything is thoroughly documented and referenced back to the GES/GIES papers.

You should not use this implementation if:

  • you have no interest in modifying GES itself, and
  • you care about speed, as the pcalg implementation is highly optimized and is very fast.

Running the algorithm

Using the Gaussian BIC score: ges.fit_bic

GES comes ready to use with the Gaussian BIC score, i.e. the l0-penalized Gaussian likelihood score. This is the variant which is commonly found in the literature, and the one which was implemented in the original paper. It is made available under the function ges.fit_bic.

ges.fit_bic(data, A0 = None, phases = ['forward', 'backward', 'turning'], debug = 0)

Parameters

  • data (np.array): the matrix containing the observations of each variable (each column corresponds to a variable).
  • A0 (np.array, optional): the initial CPDAG on which GES will run, where where A0[i,j] != 0 implies i -> j and A[i,j] != 0 & A[j,i] != 0 implies i - j. Defaults to the empty graph.
  • phases ([{'forward', 'backward', 'turning'}*], optional): this controls which phases of the GES procedure are run, and in which order. Defaults to ['forward', 'backward', 'turning']. The turning phase was found by Hauser & Bühlmann (2012) to improve estimation performace, and is implemented here too.
  • debug (int, optional): if larger than 0, debug are traces printed. Higher values correspond to increased verbosity.

Returns

  • estimate (np.array): the adjacency matrix of the estimated CPDAG.
  • total_score (float): the score of the estimate.

Example

Here sempler is used to generate an observational sample from a Gaussian SCM, but this is not a dependency.

import ges
import sempler
import numpy as np

# Generate observational data from a Gaussian SCM using sempler
A = np.array([[0, 0, 1, 0, 0],
              [0, 0, 1, 0, 0],
              [0, 0, 0, 1, 1],
              [0, 0, 0, 0, 1],
              [0, 0, 0, 0, 0]])
W = A * np.random.uniform(1, 2, A.shape) # sample weights
data = sempler.LGANM(W,(1,2), (1,2)).sample(n=5000)

# Run GES with the Gaussian BIC score
estimate, score = ges.fit_bic(data)

print(estimate, score)

# Output
# [[0 0 1 0 0]
#  [0 0 1 0 0]
#  [0 0 0 1 1]
#  [0 0 0 0 1]
#  [0 0 0 1 0]] 21511.315220683457

Using a custom score: ges.fit

While Chickering (2002) chose the BIC score, any score-equivalent and locally decomposable function is adequate. To run with another score of your choice, you can use

ges.fit(score_class, completion_algorithm = None, A0 = None, phases = ['forward', 'backward', 'turning'], debug = 0)

where score_class is an instance of the class which implements your score. It should inherit from ges.scores.DecomposableScore, or define a local_score function and a few attributes (see decomposable_score.py for more details).

You may additionally also use a custom completion algorithm , i.e. the algorithm to go from PDAG to CPDAG after the application of each operator.

Parameters

  • score_class (ges.scores.DecomposableScore): an instance of a class implementing a locally decomposable score, which inherits from ges.scores.DecomposableScore. See decomposable_score.py for more details.
  • completion_algorithm (function, optional): the used to go from PDAG to CPDAG after the application of each operator. Must be a function which takes and returns a PDAG adjacency. If None, the algorithm used in the original GES paper is used.
  • A0 (np.array, optional): the initial CPDAG on which GES will run, where where A0[i,j] != 0 implies i -> j and A[i,j] != 0 & A[j,i] != 0 implies i - j. Defaults to the empty graph.
  • phases ([{'forward', 'backward', 'turning'}*], optional): this controls which phases of the GES procedure are run, and in which order. Defaults to ['forward', 'backward', 'turning']. The turning phase was found by Hauser & Bühlmann (2012) to improve estimation performace, and is implemented here too.
  • debug (int, optional): if larger than 0, debug are traces printed. Higher values correspond to increased verbosity.

Returns

  • estimate (np.array): the adjacency matrix of the estimated CPDAG.
  • total_score (float): the score of the estimate.

Example

Running GES on a custom defined score (in this case the same Gaussian BIC score implemented in ges.scores.GaussObsL0Pen).

import ges
import ges.scores
import sempler
import numpy as np

# Generate observational data from a Gaussian SCM using sempler
A = np.array([[0, 0, 1, 0, 0],
              [0, 0, 1, 0, 0],
              [0, 0, 0, 1, 1],
              [0, 0, 0, 0, 1],
              [0, 0, 0, 0, 0]])
W = A * np.random.uniform(1, 2, A.shape) # sample weights
data = sempler.LGANM(W,(1,2), (1,2)).sample(n=5000)

# Define the score class
score_class = ges.scores.GaussObsL0Pen(data)

# Run GES with the Gaussian BIC score
estimate, score = ges.fit(score_class)

print(estimate, score)

# Output
# [[0 0 1 0 0]
#  [0 0 1 0 0]
#  [0 0 0 1 1]
#  [0 0 0 0 1]
#  [0 0 0 1 0]] 24002.112921580803

Code Structure

All the modules can be found inside the ges/ directory. These include:

  • ges.ges which is the main module with the calls to start GES, and contains the implementation of the insert, delete and turn operators.
  • ges.utils contains auxiliary functions and the logic to transform a PDAG into a CPDAG, used after each application of an operator.
  • ges.scores contains the modules with the score classes:
    • ges.scores.decomposable_score contains the base class for decomposable score classes (see that module for more details).
    • ges.scores.gauss_obs_l0_pen contains an implementation of the cached Gaussian BIC score, as used in the original GES paper.
  • ges.test contains the modules with the unit tests and tests comparing against the algorithm's implementation in the 'pcalg' package.

Tests

All components come with unit tests to match, and some property-based tests. The output of the overall procedure has been checked against that of the pcalg implementation over tens of thousands of random graphs. Of course, this doesn't mean there are no bugs, but hopefully it means they are less likely :)

The tests can be run with make test. You can add SUITE=<module_name> to run a particular module only. There are, however, additional dependencies to run the tests. You can find these in requirements_tests.txt and R_requirements_tests.txt.

Test modules

They are in the sub package ges.test, in the directory ges/test/:

  • test_decomposable_score.py: tests for the decomposable score base class.
  • test_gauss_bic.py: tests for the Gaussian bic score.
  • test_operators.py: tests for the insert, delete and turn operators.
  • test_pdag_to_cpdag.py: tests the conversion from PDAG to CPDAG, which is applied after each application of an operator.
  • test_utils.py: tests the other auxiliary functions.
  • ges.test.test_vs_pcalg: compares the output of the algorithm vs. that of pcalg for randomly generated graphs.

Feedback

I hope you find this useful! Feedback and (constructive) criticism is always welcome, just shoot me an email :)

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

ges-1.1.0.tar.gz (41.0 kB view details)

Uploaded Source

Built Distribution

ges-1.1.0-py3-none-any.whl (47.2 kB view details)

Uploaded Python 3

File details

Details for the file ges-1.1.0.tar.gz.

File metadata

  • Download URL: ges-1.1.0.tar.gz
  • Upload date:
  • Size: 41.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.8

File hashes

Hashes for ges-1.1.0.tar.gz
Algorithm Hash digest
SHA256 3409acc4230e37eaa79aa243f17d2a5bbfd468acc02f986520174bf0fb275ec1
MD5 8a5fd77f60176b38e276db15e1e90934
BLAKE2b-256 5e511541ea8ba1d1f9944a297ae32e7cf10edcbb44031e82822b5be70122ce76

See more details on using hashes here.

File details

Details for the file ges-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: ges-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 47.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.8

File hashes

Hashes for ges-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 17432c788111e0c3bc7f7d535c24c0d4ffab30c788aa682b57d6582674ee4fd5
MD5 998d536d08ce6cf7c09b1259f37533f6
BLAKE2b-256 b47042f6e76c7ff3e4eab6552b15d290aec56bc0f2673d3d28629644f733e7c7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page