Causal-Learn (Causal Inference Library)

0.1.4.5 · active · verified Fri Apr 17

Causal-learn is a Python library for causal inference, providing various algorithms for causal discovery (identifying causal relationships from observational data) and causal effect estimation. It is part of the Py-Why project and aims to be a comprehensive toolkit for causality. The current version is 0.1.4.5, with frequent patch releases addressing bug fixes and minor enhancements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to generate synthetic data, apply the PC (Peter and Clark) algorithm for causal discovery using the chi-squared conditional independence test, and print the resulting adjacency matrix representing the causal graph. The PC algorithm is a constraint-based method widely used for learning causal structures from observational data.

import numpy as np
from causallearn.search.ConstraintBased.PC import pc
from causallearn.utils.cit import chisq

# 1. Generate synthetic data: X0 -> X1 <- X2, X0 -> X2
# X0 influences X2, and both X0 and X2 influence X1
np.random.seed(42)
N = 1000
X0 = np.random.normal(0, 1, N)
X2 = X0 * 0.5 + np.random.normal(0, 1, N)
X1 = X0 * 0.3 + X2 * 0.7 + np.random.normal(0, 1, N)
data = np.array([X0, X1, X2]).T # P-dimensional data (N samples, P variables)

# 2. Run PC algorithm for causal discovery
# data: input data matrix (N_samples, N_variables)
# alpha: significance level for conditional independence test (e.g., 0.05)
# ci_test: conditional independence test function (e.g., chisq for continuous data)
# verbose: set to True for detailed progress output
# num_cores: number of cores to use for parallel computation (-1 for all available)
causal_graph = pc(data, alpha=0.05, ci_test=chisq, verbose=False, num_cores=-1)

# 3. Print the adjacency matrix of the discovered causal graph
# An entry (i, j) == 1 indicates an edge from i to j, 0 otherwise.
print("\nDiscovered Causal Graph Adjacency Matrix:")
print(causal_graph.G.graph)

view raw JSON →