Learn a structure from discrete data#

GES learns the Markov equivalence class of a causal structure from discrete observations. It takes a DataFrame, an array or a DiscreteDataset, and every distinct value of a column is one level.

from causalbase import GES
from causalbase.datasets import load_asia
from causalbase.metrics import edge_scores, shd

bundle = load_asia()
frame = bundle.data.to_pandas()
frame.head()
lung smoke bronc xray tub asia dysp either
0 no no no no no no no no
1 no yes yes no no no yes no
2 no no no no no no no no
3 no no no no no no no no
4 no yes no no no no no no


Fitting follows scikit-learn: hyperparameters go to the constructor, and the results end in an underscore. cpdag_ is the equivalence class; adjacency_ is the same graph with the variable names.

lung smoke bronc xray tub asia dysp either
lung 0 1 0 0 0 0 0 1
smoke 1 0 1 0 0 0 0 0
bronc 0 1 0 0 0 0 1 0
xray 0 0 0 0 0 0 0 0
tub 0 0 0 0 0 0 0 1
asia 0 0 0 0 0 0 0 0
dysp 0 0 0 0 0 0 0 0
either 0 0 0 1 0 0 1 0


A directed edge is one every DAG of the class agrees on. An undirected edge is one the data cannot orient.

names = model.feature_names_in_
for u, v in model.cpdag_.directed_edges():
    print(f"{names[u]} -> {names[v]}")
for u, v in model.cpdag_.undirected_edges():
    print(f"{names[u]} -- {names[v]}")
lung -> either
bronc -> dysp
tub -> either
either -> xray
either -> dysp
lung -- smoke
smoke -- bronc

dag_ is one member of the class, which is what the metrics compare.

scores = edge_scores(bundle.truth, model.dag_)
print(f"SHD {shd(bundle.truth, model.dag_)}")
print(f"Skeleton precision {scores.precision_skel:.2f}, recall {scores.recall_skel:.2f}")
SHD 2
Skeleton precision 1.00, recall 0.88

Total running time of the script: (0 minutes 9.435 seconds)