Greedy Interventional Equivalence Search (GIES) Algorithm for Causal Discovery
This is a python implementation of the GIES algorithm from the paper "Characterization and Greedy Learning of Interventional Markov Equivalence Classes of Directed Acyclic Graphs" by Alain Hauser and Peter Bühlmann.
The implementation is an extension of the Python implementation of the GES algorithm, and is the work of Olga Kolotuhina and Juan L. Gamella.
Installation
You can clone this repo or install the python package via pip:
pip install gies
The only dependency outside the Python Standard Library is numpy>=1.15.0. See requirements.txt for more details.
When you should use this implementation
To the best of our knowledge, the only other public implementation of GIES 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 in Python (the only dependency outside the Python Standard Library is numpy).
Running the algorithm
Using the Gaussian BIC score: gies.fit_bic
GIES 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 gies.fit_bic.
gies.fit_bic(data, I, A0 = None, phases = ['forward', 'backward', 'turning'], iterate = True, debug = 0)
Parameters
- data (np.array): the matrix containing the observations of each variable (each column corresponds to a variable).
- I (list of lists of ints): the family of intervention targets, with each list being the targets in the corresponding environment.
- A0 (np.array, optional): the initial CPDAG on which GIES will run, where where
A0[i,j] != 0impliesi -> jandA[i,j] != 0 & A[j,i] != 0impliesi - j. Defaults to the empty graph. - phases (
[{'forward', 'backward', 'turning'}*], optional): this controls which phases of the GIES procedure are run, and in which order. Defaults to['forward', 'backward', 'turning']. - iterate (boolean, default=True): Indicates whether the algorithm should repeat the given phases more than once, until the score is not improved.
- 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 gies
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
scm = sempler.LGANM(W, (1, 2), (1, 2))
data = [scm.sample(n=5000), scm.sample(n=5000, do_interventions={2: (0, 5)})]
# Run GIES with the gaussian BIC score
interventions = [[], [2]]
estimate, score = gies.fit_bic(data, interventions)
print(estimate, score)
# Output
# [[0. 0. 1. 0. 0.]
# [0. 0. 1. 0. 0.]
# [0. 0. 0. 0. 1.]
# [0. 0. 0. 0. 0.]
# [0. 0. 0. 1. 0.]] -29209.33670496673
Using a custom score: gies.fit
While Hauser and Bühlmann (2012) chose the BIC score, any score-equivalent and locally decomposable function is adequate. To run with another score of your choice, you can use
gies.fit(score_class, A0 = None, phases = ['forward', 'backward', 'turning'], iterate = True, debug = 0)
where score_class is an instance of the class which implements your score. It should inherit from gies.scores.DecomposableScore, or define a local_score function and a few attributes (see decomposable_score.py for more details).
Parameters
- score_class (gies.scores.DecomposableScore): an instance of a class implementing a locally decomposable score, which inherits from
gies.scores.DecomposableScore. See decomposable_score.py for more details. - A0 (np.array, optional): the initial CPDAG on which GIES will run, where where
A0[i,j] != 0impliesi -> jandA[i,j] != 0 & A[j,i] != 0impliesi - j. Defaults to the empty graph. - phases (
[{'forward', 'backward', 'turning'}*], optional): this controls which phases of the GIES procedure are run, and in which order. Defaults to['forward', 'backward', 'turning']. - iterate (boolean, default=True): Indicates whether the algorithm should repeat the given phases more than once, until the score is not improved.
- 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 GIES on a custom defined score (in this case the same Gaussian BIC score implemented in gies.scores.GaussIntL0Pen).
import gies
import gies.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
scm = sempler.LGANM(W, (1, 2), (1, 2))
data = [scm.sample(n=5000), scm.sample(n=5000, do_interventions={2: (0, 5)})]
# Define the score class
interventions = [[], [2]]
score_class = gies.scores.GaussIntL0Pen(data, interventions)
# Run GIES with the gaussian BIC score
estimate, score = gies.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.]] -30310.806829328416
Code Structure
All the modules can be found inside the gies/ directory. These include:
gies.mainwhich is the main module with the calls to start GIES, and contains the implementation of the insert, delete and turn operators.gies.utilscontains auxiliary functions and the logic to transform a PDAG into a CPDAG, used after each application of an operator.gies.scorescontains the modules with the score classes:gies.scores.decomposable_scorecontains the base class for decomposable score classes (see that module for more details).gies.scores.gauss_int_l0_pencontains an implementation of the cached Gaussian BIC score.
gies.testcontains the modules with the unit tests and tests comparing against the algorithm's R 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 gies.test, in the directory ges/test/:
test_decomposable_score.py: tests for the decomposable score base class.test_int_score.py: tests for the Gaussian bic score.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.test_vs_R: compares the output of the algorithm to that of the R implementation in thepcalgpackage.
Feedback
I hope you find this useful! Feedback and (constructive) criticism is always welcome, just shoot me an email :)
Metadata
Release files for gies 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| gies-0.0.3.tar.gz | 48.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| gies-0.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 119.5 kB
Release files / gies-0.0.3.tar.gz
| Download URL | gies-0.0.3.tar.gz |
|---|---|
| Size | 48.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
35b72d4ebb92066b630dea6a885bbe22ff3cb087c645ca00eab4e784e1309879
|
|
BLAKE2b-256 checksum How to use checksums |
846c1133ce40b4ded357808e848f737ab33c16e414e0ff29409f1c17fcd0d24a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.16
|
Release files / gies-0.0.3-py3-none-any.whl
| Download URL | gies-0.0.3-py3-none-any.whl |
|---|---|
| Size | 70.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4599d97d420efb0c4affaeeb52cf5db98cce11ffceef8edb941f967bbc621074
|
|
BLAKE2b-256 checksum How to use checksums |
240c6428fb34aa83858b9404149920fed39ea1bac0085d74ce8b0eabbcc85232
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.16
|
Security for gies 0.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.