API#

causal_networkx:

Causal modeling with structural causal models and causal graphs.

This is the application programming interface (API) reference for classes (CamelCase names) and functions (underscore_case names) of Causal-Networkx, grouped thematically by analysis stage.

Most-used classes#

These are the causal classes for Structural Causal Models (SCMs), or various causal graphs encountered in the literature.

StructuralCausalModel(exogenous, endogenous)

Structural Causal Model (SCM) class.

DAG([incoming_graph_data])

Causal directed acyclic graph.

CPDAG([incoming_graph_data, ...])

Completed partially directed acyclic graphs (CPDAG).

ADMG([incoming_graph_data, ...])

Acyclic directed mixed graph (ADMG).

PAG([incoming_graph_data, ...])

Partial ancestral graph (PAG).

To see a breakdown of different inner graph functionalities, see the Graph API page. See

Discovery functions#

These functions compute try to learn the causal graph from data. All these functions work with a Pandas DataFrame object, which is the recommended input to these functions. However, they also should work on numpy array inputs.

PC(ci_estimator[, alpha, init_graph, ...])

Methods

FCI(ci_estimator[, alpha, init_graph, ...])

Methods

RobustPC(ci_estimator[, alpha, init_graph, ...])

Methods

Conditional independence testing functions#

These functions are implementations of common conditional independence (CI) testing functions. These will eventually be ported out of this repository as they are of independent interest.

The general API for CI tests require a data, x, y and sep_set input, where data is the DataFrame containing the data to be analyzed, x and y are single columns of the DataFrame corresponding to a single variable and then sep_set is the conditioning set of variables.

BaseConditionalIndependenceTest()

Abstract class for any conditional independence test.

GSquareCITest([data_type])

Methods

FisherZCITest([correlation_matrix])

Methods

KernelCITest([kernel_x, kernel_y, kernel_z, ...])

Methods

PartialCorrelation([method, ...])

Methods

Oracle(graph)

Oracle conditional independence testing.

ParentChildOracle(graph)

Parent and children oracle for conditional independence testing.

IO for reading/writing causal graphs#

We advocate for using our implemented causal graph classes whenever utilizing various packages. However, we also support transformations to and from popular storage classes, such as numpy arrays, pandas dataframes, pgmpy, DOT and dagitty. Note that not all these are supported for all types of graphs because of inherent limitations in supporting mixed-edge graphs in other formats.

load_from_networkx(G)

Load causal graph from networkx.

load_from_numpy(arr[, type])

Load causal graph from a numpy array.

load_from_pgmpy(pgmpy_dag)

Load causal graph from pgmpy.

to_networkx(causal_graph)

Convert causal graph to networkx class.

to_numpy(causal_graph)

Convert causal graph to a numpy adjacency array.

Utility Algorithms for Causal Graphs#

d_separated(G, x, y[, z])

Check d-separation among 'x' and 'y' given 'z' in graph G.

find_cliques(G[, nodes])

Find all maximal cliques in causal DAG.

is_directed_acyclic_graph(G)

Check if G is a directed acyclic graph (DAG) or not.

topological_sort(G)

Returns a generator of nodes in topologically sorted order.

discriminating_path(graph, u, a, c, ...)

Find the discriminating path for <..., a, u, c>.

possibly_d_sep_sets(graph, node_x[, node_y, ...])

Find all PDS sets between node_x and node_y.

uncovered_pd_path(graph, u, c, max_path_length)

Compute uncovered potentially directed path from u to c.

dag2cpdag(graph)

Convert a DAG to a completed partially directed acyclic graph.

admg2pag(graph)

Convert an ADMG to a PAG.

is_markov_equivalent(graph, other_graph)

Check markov equivalence of two graphs.

compute_v_structures(graph)

Iterate through the graph to compute all v-structures.