Guides

Getting started#

This guide learns a causal graph from real measurements, checks it against what biologists already know, and then tests how far to trust it. Every output on this page is computed when the site is built. Concepts states the problem formally and what each kind of graph claims.

Install#

Andrey installs from PyPI as andrey-core (the import root is andrey), on Python 3.11 or later. With uv, add it to your project (uv init creates one):

uv add andrey-core            # base (pure NumPy)
uv add "andrey-core[torch]"   # torch backend: GPU primitives and CALM
uv add "andrey-core[viz]"     # Graphviz layouts and matplotlib styling
uv add "andrey-core[all]"     # every extra

With pip:

pip install andrey-core            # base (pure NumPy)
pip install "andrey-core[torch]"   # torch backend: GPU primitives and CALM
pip install "andrey-core[viz]"     # Graphviz layouts and matplotlib styling
pip install "andrey-core[all]"     # every extra

PyTorch for a GPU#

The torch extra needs the PyTorch build that matches your GPU, which an install cannot detect on its own. uv can: it reads the installed driver and fetches the matching build. With pip, install PyTorch from its own index first, choosing the build for your CUDA version or /cpu:

UV_TORCH_BACKEND=auto uv pip install "andrey-core[torch]"

pip install torch --index-url https://download.pytorch.org/whl/cu128
pip install "andrey-core[torch]"

For ROCm, install the matching PyTorch build yourself, then andrey-core. Andrey has no backend for Intel GPUs.

From GitHub#

The main branch on GitHub holds the latest release:

uv add "andrey-core @ git+https://github.com/Abel-ai-lab/Andrey@main"
pip install "andrey-core @ git+https://github.com/Abel-ai-lab/Andrey@main"

The distribution is andrey-core. After the @, name any branch, tag, or commit to install that one.

From source#

Build the wheel and the source distribution yourself, then install the wheel:

git clone https://github.com/Abel-ai-lab/Andrey
cd andrey
uv build                                  # writes both to dist/
uv pip install dist/andrey_core-*.whl     # into the active environment

To work on Andrey itself, uv sync in the clone sets up a development environment instead; see CONTRIBUTING.

The data#

Sachs and colleagues (2005) measured 11 proteins and phospholipids in 853 single human immune cells. Biologists had already mapped how these molecules signal to one another, so a graph learned from the measurements can be checked against a known network of 17 links. load_dataset downloads the measurements on first use and caches them.

import numpy as np
import pandas as pd

import andrey
from andrey.data import load_dataset

sachs = load_dataset("sachs")
sachs.data.head()
raf mek plc pip2 pip3 erk akt pka pkc p38 jnk
0 26.4 13.2 8.82 18.30 58.80 6.61 17.0 414.0 17.00 44.9 40.0
1 35.9 16.5 12.30 16.80 8.13 18.60 32.5 352.0 3.37 16.5 61.5
2 59.4 44.1 14.60 10.20 13.00 14.90 32.5 403.0 11.40 31.9 19.5
3 73.0 82.8 23.10 13.50 1.29 5.83 11.8 528.0 13.70 28.6 23.1
4 33.7 19.8 5.19 9.73 24.80 21.10 46.1 305.0 4.66 25.7 81.3

Each row is one cell and each column one molecule.

Learn a graph#

out = andrey.pc(sachs.data)
print(out)
pc  cpdag  |  11 nodes  |  8 edges (6 undirected, 2 directed)

  raf -- mek
  plc -- pip3
  pip2 -- pip3
  erk -- akt
  erk -- pka
  akt -- pka
  p38 -> pkc
  jnk -> pkc

  indep_test: fisherz
  tie_break: rank

PC starts with every pair of molecules linked. It removes a link when a statistical test finds the two independent, either alone or given some of their other neighbors. The links that survive form the graph’s skeleton.

Directions are harder to learn. To an independence test, raf -> mek and mek -> raf usually look the same, so PC leaves such a link undirected (--). The result is a CPDAG: it stands for every causal graph that independence tests cannot tell apart.

PC orients a link only when a pattern forces it. Here p38 and jnk are not linked, and pkc was not needed to make them independent. Of the graphs that fit, only one explains that: p38 -> pkc <- jnk, a collider.

Use the result#

out.structure holds the graph. oriented_edges() lists each link as (source, target, type), with the column names:

out.structure.oriented_edges()
[('raf', 'mek', 'undirected'),
 ('plc', 'pip3', 'undirected'),
 ('pip2', 'pip3', 'undirected'),
 ('erk', 'akt', 'undirected'),
 ('erk', 'pka', 'undirected'),
 ('akt', 'pka', 'undirected'),
 ('p38', 'pkc', 'directed'),
 ('jnk', 'pkc', 'directed')]

to_networkx() hands the graph to NetworkX, and to_numpy() returns its endpoint marks; see Graph representation.

Check it against the known network#

andrey.metrics.score compares a learned graph with a reference graph:

from andrey.metrics import score

scores = score(out.structure, sachs.graph)
shown = ("shd", "skeleton_precision", "skeleton_recall")
{k: round(scores[k], 2) for k in shown}
{'shd': 11, 'skeleton_precision': 1.0, 'skeleton_recall': 0.47}
  • Skeleton precision is the share of learned links that are in the known network.

  • Skeleton recall is the share of the known network’s links that were learned.

  • SHD, the structural Hamming distance, counts the links to add, remove, or reorient to reach the known network. Lower is better.

PC finds 8 of the 17 links in the known signaling network, and none outside it.

A score means little on its own. The graph with no links is the baseline: a graph whose SHD is no lower has not improved on it by this measure, even if some of its links are right.

empty = andrey.GraphStructure.from_numpy(
    np.zeros((11, 11), dtype=np.int8),
    kind="cpdag",
    labels=sachs.feature_names,
)
score(empty, sachs.graph)["shd"]
17

The directions need more care. These cells saw no intervention, and independence tests on observational data cannot orient any of the known network’s 17 links. Its CPDAG, the most PC or GES could recover from these cells, leaves every link undirected:

from andrey.metrics import to_cpdag

{kind for _, _, kind in to_cpdag(sachs.graph).oriented_edges()}
{'undirected'}

So PC’s collider at pkc disagrees with the known network. If the known network is right, at least one of PC’s independence decisions was wrong. A direction learned from observational data can rest on a single test. Methods that assume more, such as LiNGAM’s non-Gaussian noise, can orient links that independence tests leave open; Graph types lists the methods that return a fully directed graph.

How far to trust it#

The threshold#

alpha is the significance level of each independence test. PC removes a link when the test cannot reject independence at that level, so a larger alpha removes fewer links:

def summary(graph):
    """Links, precision, recall, and SHD against the known network."""
    s = score(graph, sachs.graph)
    return {
        "links": len(graph.oriented_edges()),
        "precision": s["skeleton_precision"],
        "recall": s["skeleton_recall"],
        "shd": s["shd"],
    }


sweep = {}
for alpha in (0.01, 0.05, 0.1, 0.2):
    sweep[alpha] = summary(andrey.pc(sachs.data, alpha=alpha).structure)
pd.DataFrame.from_dict(sweep, orient="index").rename_axis("alpha").round(2)
links precision recall shd
alpha
0.01 8 1.00 0.47 11
0.05 8 1.00 0.47 11
0.10 12 0.75 0.53 15
0.20 14 0.71 0.59 17

The extra links a larger alpha keeps are partly right and partly wrong: recall rises and precision falls. No setting is correct in general. Pick alpha for the cost of a missed link against the cost of a false one, and report how the graph changes with it.

The test’s assumptions#

PC’s default test, Fisher’s z, assumes each variable is a linear function of its causes plus Gaussian noise. (For effects that are not linear, indep_test also takes nonlinear tests; the API reference lists them.) These measurements are far from Gaussian: most are strongly skewed to the right.

sachs.data.skew().round(1)
raf      4.1
mek      6.7
plc      3.8
pip2     2.7
pip3    12.3
erk     26.9
akt     25.3
pka      2.9
pkc      2.1
p38      2.1
jnk      2.5
dtype: float64

A log transform makes them closer to Gaussian. Learn the graph again from the logged values:

logged = andrey.pc(np.log(sachs.data))
print(logged)
pc  cpdag  |  11 nodes  |  7 edges (5 undirected, 2 directed)

  raf -- mek
  plc -> pip3
  pip2 -> pip3
  erk -- akt
  akt -- pka
  pkc -- p38
  pkc -- jnk

  indep_test: fisherz
  tie_break: rank

PC keeps 7 of the 8 links it found before and adds none. The directions changed: the collider at pkc is gone, and another appears at pip3. The skeleton is the more stable part of the answer. Treat directions learned from observational data as hypotheses to test.

A second method#

GES reaches a graph a different way: it searches for the graph with the best score, instead of testing independence.

ges = andrey.ges(sachs.data)
ges.structure.oriented_edges() == out.structure.oriented_edges()
True

On these data GES returns the same graph as PC. Agreement between methods is reassuring but not proof: GES’s score assumes linear-Gaussian data too. The Examples compare more methods, and draw the learned graph beside the known one.

Resampling#

One run gives one graph, and a different sample of cells could give another. andrey.bootstrap refits PC on subsamples, each a random half of the cells, and counts how often each link appears. seed fixes the subsamples, so a rerun gives the same counts:

boot = andrey.bootstrap("pc", sachs.data, n_resamples=100, seed=0)
pd.DataFrame(boot.table()[:8]).round(2)
source target type count frequency
0 erk akt undirected 100 1.00
1 akt pka undirected 98 0.98
2 erk pka undirected 92 0.92
3 raf mek undirected 91 0.91
4 p38 pkc directed 86 0.86
5 jnk pkc directed 86 0.86
6 pip2 pip3 directed 49 0.49
7 pip2 pip3 undirected 44 0.44

table() lists every link that appeared in some resample, with the direction it had there, most frequent first. The four undirected links appear in 91 to 100 of the 100 resamples. Directions are less stable: each arrow of the collider at pkc appears in 86 resamples, and plc -> pip3 in 31.

boot.structure is the averaged graph. Each pair takes the state it has most often, with no edge counted as one of the states, and the result is then made a valid CPDAG. It holds seven of out’s eight links: plc and pip3 are adjacent in 54 of the 100 resamples, but no one edge between them is as frequent as no edge (plc -> pip3 in 31, no edge in 46). A stricter threshold asks a link to be adjacent in more of the resamples, without refitting:

print(boot.average(0.95))
cpdag  |  11 nodes  |  6 edges (4 undirected, 2 directed)

  raf -- mek
  pip2 -- pip3
  erk -- akt
  akt -- pka
  p38 -> pkc
  jnk -> pkc

That drops the link between erk and pka, which is in 93 of the 100 resamples. Treat the counts as a ranking of the links, not as probabilities. resampling="bootstrap" draws all the cells with replacement instead; a resample then repeats some cells, so PC’s tests see more evidence than it holds and keep weak links more often than they do on the data.

Checking a result against the data#

A graph implies independences: PC’s graph has no link between p38 and jnk, so given the right variables the two must look independent in the data. andrey.markov_check tests the ordered local Markov independences, each variable and each earlier non-parent given its parents, and asks whether the p-values are uniform, as they are when all of them hold. The cells that learned the graph would flatter it, so learn on a random half of the cells and check on the other half:

cells = np.random.default_rng(0).permutation(len(sachs.data))
learn, held = sachs.data.iloc[cells[: len(cells) // 2]], sachs.data.iloc[cells[len(cells) // 2 :]]
check = andrey.markov_check(andrey.pc(learn), held, seed=0)
print(check)
Not rejected: the p-values of the 47 independences the graph implies are consistent with uniform (Anderson-Darling p = 0.729 > 0.05); 6.4% have p < 0.05, about 5% expected. A pass does not show the edges are right: a graph with extra edges also passes.
  47 tests | Anderson-Darling A^2 = 0.517 | each test on 214 rows | indep_test fisherz

The data do not reject the 47 independences of PC’s graph. That does not show the graph is right: a graph with extra links implies fewer independences and passes too, so read a pass with the number of links. A rejection is the informative result, because it means a link is missing or an arrow is wrong. check.table() lists each independence with its p-value, check.dag is the DAG whose independences were tested, and andrey markov-check runs the same from the shell. The Markov check section gives the measured error rates.

Your own data#

Pass a pandas DataFrame, whose column names become the node names, or a NumPy array with one row per sample and one column per variable. Most methods take that one argument and return the same kind of result; multi_group_direct_lingam, longitudinal_lingam, and jpcmciplus take a list of datasets, and the methods under Environments and interventions also take each row’s environment. andrey list names the methods, and Graph types explains which graph each one returns.

Environments and interventions#

boss, boss_fci, cdnod, fci_jci, ges, gfci, gies, gnies, grasp, grasp_fci, hc, icp, igsp, tabu, and ut_igsp take env, each row’s environment, with one meaning: one label per row, text or finite numbers, or the name of a column of a DataFrame data, which is then not a variable, or, with env left out, the data given as one table per environment (see below). andrey run reads it from --env, a column of --data or a file. A label that is missing or infinite raises a ValueError. The methods read env in one of two ways. gies, gnies, igsp, and ut_igsp read it as interventions: each distinct label is one environment, which changes the mechanisms of its targets, known or learned, and leaves the others as they are. icp reads it as interventions on any variables, as long as they leave the mechanism of its target alone. cdnod, fci_jci, boss, grasp, ges, hc, tabu, boss_fci, grasp_fci, and gfci read it as a context variable that no data variable causes, the environment index, which joins the search as one more variable; a numeric label, such as a time step, stays a number. ges, hc, tabu, boss, grasp, gfci, boss_fci, and grasp_fci also fit without env, and icp also takes target, the variable whose parents it finds.

gies, igsp, and ut_igsp take targets as a mapping from each label of env to a list of its variables, by name or position, with [] for an observational environment; as the name of a column whose cells give each row’s variables, from whose distinct values env follows when you leave it out; or as a 0/1 matrix shaped like data. A key that is not a label of env raises a ValueError. gies and igsp need every environment’s targets, so env without targets, and an environment the mapping leaves out or gives None, raise one too; ut_igsp reads an environment the mapping leaves out or gives None as one whose targets it learns, and still needs an observational one. gnies takes the same forms of targets, but for the targets it knows: an environment the mapping leaves out or gives None has unknown targets, which it learns, and targets=None, the default, learns them all. When the labels of env are numbers, a text key names the label it spells, so "0" names the label 0 and a mapping read back from JSON passes as it is. icp takes targets in the same forms but reads only whether an environment intervened on its target: it leaves such an environment’s rows out, and keeps an environment whose targets are unknown. andrey run takes a mapping as targets=, a JSON object inline or in a file, or as targets in --config, and reads an --env of whole numbers as integers.

out = andrey.gies(data, env, targets={"observed": [], "knockout": ["x"]})
out = andrey.gies(data, targets="target")  # a column naming each row's targets, "" if none
out = andrey.cdnod(data, "site")  # a column of labels; numbers such as a time step stay numbers
out.metadata["changing_variables"]  # the variables whose mechanism changes across the sites
out = andrey.boss(data, "site")  # BOSS with the same index; boss(data) fits without env
out = andrey.gnies(data, "site")  # targets unknown: learned, in metadata["changing_variables"]
out = andrey.icp(data, env, target="y")  # the parents of y; its p-values in out.metadata

ut_igsp calls the rows of one environment a setting. Map an observational environment to [], a setting to the targets you know, and a setting whose targets you do not know to None:

from andrey import ut_igsp

result = ut_igsp(data, "condition", targets={"control": [], "drug": ["dose"], "knockout": None})
result.metadata["targets"]  # each environment's targets, known and learned, by its label

In Python, every method that takes env also takes each environment’s rows apart as data, in place of one table and env: a list of DataFrames or 2-D arrays, each labeled by its position 0, 1, ..., or a dict from each label to its table. env is then left out, and targets is a mapping from those labels, or None. DataFrames need the same columns, in any order, which take the first one’s order, and a column of the category type in each keeps it, with every DataFrame’s categories; arrays need the same number of columns. The rows are stacked in the order of the list or dict, so the fit is the one on the stacked table with env naming each row’s label.

out = andrey.gies([observed, knockout], targets={0: [], 1: ["x"]})
out = andrey.ut_igsp({"control": control, "drug": drug}, targets={"control": [], "drug": None})

Background knowledge#

What you already know about the system narrows the search. Pass it as an andrey.Knowledge, naming each column by its label, a string, or by its 0-based position, an integer. The background knowledge notebook shows tiers orienting a graph that the data alone leave undirected.

from andrey import Knowledge, pc

knowledge = Knowledge(
    required=[("smoking", "tar")],          # edges the graph must have
    forbidden=[("cancer", "smoking")],      # edges it must not have
    tiers=[["age", "sex"], ["smoking"], ["tar", "cancer"]],  # no edge into an earlier tier
)
result = pc(data, knowledge=knowledge)

allowed lists the only pairs that may be adjacent; listing a pair permits an edge but does not require one. A short list narrows the search, which can make it faster. PC, FCI, and CD-NOD start their adjacency search from the allowed pairs, and GFCI’s GES phase searches only inside them, so you can screen first and then search inside the pairs that survived: Knowledge.from_skeleton(pc(data, max_depth=1)). max_depth is the maximum conditioning-set size of those four methods’ tests, the cap pcalg names m.max and causal-learn’s FCI names depth.

The result honors every part: a required edge is there and directed, a forbidden one is not, and a pair outside allowed is not adjacent. In a CPDAG, the edges the knowledge decides are directed and Meek’s rules direct the edges that follow, which gives the maximally oriented PDAG (MPDAG): the DAGs of the equivalence class that satisfy the knowledge. IAMB, Inter-IAMB, and MMPC return undirected graphs with a target, with structure="skeleton", the default of mmpc, and with structure="moral", a choice of iamb and inter_iamb: a required edge is there without a direction, and tiers and edges forbidden one way, which constrain orientation, do not change them. With structure="cpdag", the default of iamb and inter_iamb, they direct the edges the knowledge decides, as in any CPDAG. In a PAG, a forbidden a -> b puts an arrowhead at a and can leave a circle at b. Knowledge that contradicts itself, such as a required edge into an earlier tier, raises a ValueError before the search runs. In PC, CD-NOD, and FCI, knowledge whose directions close a cycle over the edges the search kept raises one after the adjacency search. Labels stay attached to their columns, so the same knowledge fits the table in any column order. A label is the column name as text: name a column called 1 with "1", as the integer 1 is the second column. On the command line, give a JSON object inline or as a file: andrey run pc --data data.csv knowledge=knowledge.json.

Knowledge can also state causal paths rather than edges. required_paths lists the pairs (a, b) in which a causes b, directly or through other variables, and forbidden_paths the pairs in which it does not:

knowledge = Knowledge(
    required_paths=[("smoking", "cancer")],  # smoking causes cancer, directly or not
    forbidden_paths=[("tar", "age")],        # tar causes age by no path
)
result = pc(data, knowledge=knowledge)
result.metadata["ancestor_clauses"]

The methods whose result is a CPDAG (PC, CD-NOD, GES, XGES, HC, Tabu, MMHC, RSMAX2, BOSS, GRaSP, exact search, and IAMB, Inter-IAMB, and MMPC with structure="cpdag" and no target) honor them: of the DAGs the graph they found stands for, they keep those that meet every claim and return their MPDAG (Fang, Zhao, Liu, and He, 2025), so an edge is directed when all of those DAGs direct it alike. With an undirected result, which the claims cannot restrict, IAMB, Inter-IAMB, and MMPC raise NotImplementedError. Some claims cannot be shown by directing edges. If smoking causes cancer exactly when it is a direct cause of tar or of gene, and the data leave both edges undirected, metadata["ancestor_clauses"] lists the claim as ["smoking", ["gene", "tar"]]. The result is the class the search found, restricted, not a new search for the best class that meets the claims. A claim directs edges of the graph the search found, and where that graph is wrong, a true claim can direct an edge the wrong way: in our measurements, 1 to 11% of the edges true claims directed.

When hidden common causes are possible, confounded lists the pairs that share one, a variable that is not in the data; a common cause that is in the data does not make the pair adjacent. The PAG methods (FCI, FCI-JCI, GFCI, BOSS-FCI, GRaSP-FCI, and LPCMCI over lagged links) honor all three parts:

from andrey import fci

knowledge = Knowledge(
    required_paths=[("smoking", "cancer")],
    confounded=[("diet", "exercise")],  # a hidden common cause
    forbidden_paths=[("diet", "exercise"), ("exercise", "diet")],  # neither causes the other
)
result = fci(data, knowledge=knowledge)  # diet <-> exercise

On a pair the tests keep, a required path makes the edge smoking -> cancer and a forbidden path puts an arrowhead at its cause; no test removes a confounded pair. These marks and checks assume no selection bias: the samples were not kept or dropped by the value of a variable the measured ones cause. DirectLiNGAM and multi-group DirectLiNGAM read required_paths and forbidden_paths to narrow the causal order, as their paper describes (Shimizu et al., 2011). Every other method raises NotImplementedError for a part it does not honor, naming the methods that do. The parts are supported wherever true claims measured no worse than the same fit without them and than a reference package given the same knowledge, which is every method that honors them except six. Required paths are experimental in Tabu search, MMHC, and RSMAX2, where a reference package given the edges those paths imply made fewer errors. Forbidden paths are experimental in IAMB, Inter-IAMB, and MMPC, where the graph found at 50 variables often has no consistent extension: a claim there acts only on an edge, the fits we measured dropped about 96% of the forbidden paths on such a graph, and with forbidden paths alone the CPDAG was 0.65 to 1.95 edges farther from the class the claims define. A path claim says less than an edge and so directs less: a required path cannot restore an edge the search left out. When you know the edge itself, state it as required.

Knowledge that no graph meets, such as required_paths that lead in a cycle, raises a ValueError before the search. A true claim can still be one the graph a fit finds does not meet: on finite data the tests can remove an edge on the only path from smoking to cancer. The fit then keeps its graph, drops the claim, warns with KnowledgeWarning, and lists it:

result.metadata["unmet_knowledge"]  # [] when every claim is met
# [["required_paths", "smoking", "cancer"]]

When only some claims hold together, a CPDAG method keeps them in order, required_paths and then forbidden_paths, each by column position.

On finite data the tests of PC, CD-NOD, IAMB, Inter-IAMB, and MMPC can contradict each other and leave a graph with no consistent extension, one that stands for no DAG. A claim then acts on one edge: between two variables joined by an edge, smoking causes tar exactly when the edge is smoking -> tar (Fang and He, 2020). A claim on a pair joined by an undirected edge directs it, Meek’s rules follow, and metadata["edge_claims"] lists the claims applied this way. A claim on a pair that is not adjacent, a claim against an edge the data directed, and claims that conflict with each other are dropped and listed in metadata["unmet_knowledge"]. The result is not guaranteed to stand for a DAG, as the graph found did not.

The time-series methods read the knowledge as links between lagged variables. A third entry gives an edge its lag: ("x", "y", 1) is the link from x at time t - 1 to y at time t, and lag 0 is the same step, except in PCMCI and Granger, which learn no same-step links. A forbidden pair without a lag holds at every lag, and a required link names its lag. Tiers and allowed hold at every lag, and a variable’s links to its own past stay allowed:

from andrey import Knowledge, pcmciplus

knowledge = Knowledge(
    forbidden=[("y", "x", 1)],     # no link from y at t - 1 to x at t
    required=[("x", "y", 0)],      # x drives y within a step
    tiers=[["temperature"], ["x", "y"]],
)
result = pcmciplus(series, tau_max=2, knowledge=knowledge)

Data types#

Each test and score reads the columns as one kind of data:

Data

indep_test

score_func

Continuous

fisherz (the default when no column is categorical), student_t, normal_scores (monotone effects, any marginals); for nonlinear effects, kci, rcot, cmiknn, and gpdc

bic (the default when no column is categorical), normal_scores (experimental: monotone transformations of Gaussian columns)

Categorical

chisq, gsq (the default when every column is categorical)

bdeu (the default when every column is categorical), discrete_bic

Mixed

degen_gauss, cond_gauss (the default when some columns are categorical)

cond_gauss (the default when some columns are categorical), degen_gauss

pc, fci, gfci, cdnod, fci_jci, iamb, inter_iamb, mmpc, mmhc, rsmax2, boss_fci, grasp_fci, igsp, ut_igsp, skewd_mv, pcmci, pcmciplus, jpcmciplus, and lpcmci take the tests, except normal_scores in cdnod, fci_jci, igsp, ut_igsp, and jpcmciplus, whose tests read several environments at once; gfci, ges, gies, xges, hc, tabu, exact_search, mmhc, rsmax2, boss, grasp, boss_fci, and grasp_fci take every score, except normal_scores in gies, which scores each variable on the rows without an intervention on it. The ranks of normal_scores model one distribution, so neither option takes an env. gfci, boss_fci, grasp_fci, mmhc, and rsmax2 take a score and a test that read the same kind of data, such as andrey.gfci(data, score_func="bdeu", indep_test="chisq"); with score_func="normal_scores" and indep_test="normal_scores", both read each column’s normal scores, so a strictly increasing transformation of any column leaves the graph unchanged.

Categorical data#

Each distinct value of a column is one category. A column may hold whole numbers, which need not start at 0, or, in a DataFrame, text, booleans, or a pandas category.

Default test and score#

indep_test=None and score_func=None, the defaults, pick from the column types you declare: a column is categorical when categorical=[...] names it, by label or 0-based position, or when it is a DataFrame’s category, bool, or text column. When every column is categorical, the tests default to gsq and the scores to bdeu; when some are, both default to cond_gauss, the conditional Gaussian; otherwise to fisherz and bic. Values never decide: a column of counts or ratings stays numbers until you declare it. An indep_test or score_func you pass is always the one used. The result’s metadata["indep_test"] and metadata["score_func"] name the test and the score the fit used, the one picked for None included.

Mixed data#

With continuous and categorical columns, choose a mixed-data test or score, such as andrey.pc(data, indep_test="cond_gauss") or andrey.ges(data, score_func="cond_gauss"). The degenerate Gaussian (degen_gauss) turns each categorical column into indicator columns and treats every column as Gaussian, so it suits effects that are linear and add up across categories. The conditional Gaussian (cond_gauss) gives each combination of categories its own Gaussian, so a category can change a variable’s spread as well as its mean; it needs several rows for each combination of categories it conditions on. Both are experimental. With either score, hc and tabu, and so the score phase of mmhc and rsmax2, give no categorical column a continuous parent.

Marking the categorical columns#

Name them with categorical, by label (the column name as text) or 0-based position: andrey.pc(data, indep_test="cond_gauss", categorical=["smoker", "region"]). Without categorical, a mixed-data setting reads a DataFrame’s category, bool, and text columns as categorical, since their type declares them. Values never declare a column: a column of whole numbers that you have not declared is continuous, so andrey.pc(df, indep_test="cond_gauss") reads the same columns as andrey.pc(df) does when it picks cond_gauss. Pass categorical=[] for no categorical column. The result’s metadata["categorical"] lists the categorical columns it read.

A declared column needs a test or score that reads categories. A test or score for continuous data, such as fisherz or bic, raises ValueError on a column you declare categorical, whether categorical names it or its type does, since it would read the categories as numbers. Leave the setting as None to pick a categorical or mixed-data one, or pass categorical=[] to read every column as numbers. A categorical test or score, such as chisq or bdeu, reads every column as categorical, declared or not.

A mixed-data setting that finds no categorical column, or gets categorical=[], warns and runs as its linear-Gaussian form: both tests become the likelihood-ratio form of the partial-correlation test, close to fisherz, and the degenerate Gaussian score becomes the BIC.

Integer-coded columns#

Every method that reads columns as numbers, the default test and the linear-Gaussian BIC included, reads a column of codes such as 0, 1, 2 as numbers, which assumes the categories are ordered and evenly spaced. When columns of at most 10 distinct whole numbers reach such a method, or the continuous part of a mixed-data setting, a one-time DataTypeWarning names those columns; if they are categories, declare them with categorical=[...] or a pandas category, and the default test or score switches to a categorical or mixed-data one.

From the command line#

The same settings are key=value arguments, such as andrey run pc --data data.csv indep_test=cond_gauss 'categorical=[smoker,region]'. The data file holds numbers only, so code each category as a whole number and name those columns in categorical. The JSON result lists the categorical columns under metadata.categorical and the warnings under run.warnings.

Missing values#

Mark a missing value with NaN or pandas’ NA, or, for andrey run, leave its field empty or write NA or N/A in the data file. PC, FCI, CD-NOD, FCI-JCI, IAMB, Inter-IAMB, MMPC, PCMCI, PCMCI+, LPCMCI, ANM, PNL, LOCI, and SkewD accept missing values: each conditional-independence test reads only the rows where all of its variables are observed (test-wise deletion). This keeps rows that would be dropped if every incomplete row were deleted first. The pair tests of ANM and PNL read the rows where both columns are observed, which for a pair is the same deletion. Here a tenth of the Sachs measurements go missing at random:

gappy = sachs.data.mask(np.random.default_rng(0).random(sachs.data.shape) < 0.1)
out = andrey.pc(gappy)
out.metadata["rows_tested"]
{'min': 601, 'median': 683.0}

metadata["rows_tested"] gives the minimum (min) and median number of rows read by a test. metadata["missing"] names the handling the fit used; a fit on complete data has neither entry. The argument missing names the handling. missing="testwise" is test-wise deletion. None, the default, picks the method’s default handling, which is "testwise" for every method that takes the argument. A value a method does not list raises a ValueError that names the values it takes:

andrey.pc(gappy, missing="testwise").structure == out.structure
True

With fisherz and student_t, each test computes its correlations directly from its rows, for many tests at once. The other tests are built anew on each test’s rows, which costs more. A time series keeps every step: each row of the lagged design that reads a missing value holds it. Each test of PCMCI, PCMCI+, or LPCMCI reads the design rows where all of its lagged variables are observed.

Deleting rows per test is sound when values are missing completely at random: the rows a test keeps are then a random sample of all rows. When whether a value is missing depends on the variables, as when high incomes go unreported, the rows a test keeps are a selected sample. PC’s tests can then find dependence where the population has none and keep extra edges (Tu et al., 2019); FCI allows for selection and stays sound when no missingness mechanism causally affects another (Strobl, Visweswaran, and Spirtes, 2018).

andrey.pc(data, missing="mvpc") corrects PC for those extra edges with Missing-Value PC (Tu et al., 2019). It finds the variables each missingness indicator depends on, then tests again the edges such an indicator could have kept. These tests use virtual data rebuilt from the rows where those variables are observed. It takes fisherz or student_t; metadata["missingness_parents"] names the parents it found and metadata["corrected_edges"] the edges it tested again and removed. A corrected test reads the rows where its pair, its conditioning set, and those parents are all observed. When too few rows are available, it uses the test-wise test instead. The pair then keeps its test-wise result for that conditioning set, and metadata["corrected_edges"]["uncorrected"] lists the pair. On complete data, MVPC gives the test-wise result.

The correction holds when no indicator causes a variable, every independence in the observed rows also holds in the missing ones, no indicator is a deterministic function of others, and no value causes its own missingness. The data cannot check these conditions. The permutation correction also needs linear-Gaussian variables and the paper’s two conditions: given W, the parents of the indicators involved, those indicators are independent of the tested variables, and conditioning on the indicators of W leaves the tested relation unchanged. The second requires the rows used to rebuild the data to preserve the independence of each pair tested. When a variable used to rebuild the data goes missing because of a common effect of that pair, the extra edge can stay. This occurs in the paper’s Figure 1c and when two effects of the same causes each cause the other’s missingness. The paper’s density-ratio correction for those cases is not implemented. andrey.data.missingness simulates data whose missingness graph you know, to see where the correction helps. The missing values notebook runs that comparison on one simulated dataset.

On simulated data in the paper’s setting, MVPC made fewer errors than test-wise deletion in 10 of 16 settings where values go missing for a reason and tied in the rest. It also tied where values are missing completely at random. At 50 variables and 10,000 rows it made 5.1 errors against 14.3. It does not help when every missingness indicator depends weakly on many variables. In that case, its search for those variables can take a long time; max_depth bounds it. On larger graphs, 50 or 100 variables with 20 or 30% of the values missing, it made 0.7 to 3.6 more errors than test-wise deletion in 12 settings, on two sets of datasets each. The 0.16.0 release card has the numbers.

A test needs enough rows where its variables are observed: |S| + 3 for fisherz and student_t, with |S| the size of its conditioning set, and 2 for the other tests. A test with fewer rows raises a ValueError that says so. Drop the columns with the most missing values, or cap the conditioning sets with max_depth (max_p_global in LPCMCI and the same-step phases of PCMCI+). A column with no observed value, or with one value throughout, raises before the fit, and so does infinity, which is not a missing value. CD-NOD needs a complete env.

The searches whose fit reads the data only through their covariance, GES, XGES, hill climbing, tabu search, exact search, CALM, NOTEARS, DAGMA, BOSS, and GRaSP, cannot delete rows per test. They refuse missing values by default, with a message that names missing="em". They take missing="em" instead: structural EM (Friedman, 1997; Gao et al., 2022) alternates two steps. Under a linear-Gaussian model of the current graph, it replaces each row’s missing values by their expected values given the row’s observed ones and adds their uncertainty. This gives the expected covariance of the data. The search then runs on that covariance over every row, and the model is fit again to the graph it returns. It runs 10 rounds. Before each search after the first, 5 more steps refit the model on the graph it has, so the search reads a covariance that graph’s fit has settled on (Friedman’s alternating scheme). metadata["em"] records whether each round changed the graph (graph_changed; when the last round still did, the fit logs a warning) and the observed-data log-likelihood after each round:

em = andrey.ges(gappy, missing="em")
em.metadata["em"]["log_likelihood"][-1]
-42081.37674839807

EM is sound when values are missing completely at random or at random given observed values. When whether a value is missing depends on the missing value itself, it has no guarantee. Each fit runs the search 10 times, and missing="em" takes only score_func="bic" and no env.

missing="em" is supported in ges, hc, tabu, boss, grasp, and notears, where no setting we measured was less accurate than a reference implementation on the first datasets and again on fresh ones. With about 100 rows, hc and tabu are often no better than the empty graph, both with EM and on complete data. From 500 rows on they were better in every setting. We measured ges, boss, and grasp at ebic_gamma=0.0, up to 50 variables, against a reference that estimates one covariance by EM before its search, a simpler method than structural EM, at two settings of the sample size its score counts. boss and grasp were better than the empty graph in 102 and 103 of 108 settings. On denser graphs (mean degree 4) ges at that penalty was rarely better than the empty graph, with EM and on complete data alike.

EM does not always settle within its 10 rounds. In our benchmark the last round still changed the graph in 2 to 6% of the fits, and in 20% of grasp’s, most often with 50 variables and 100 rows. Such a fit logs a warning, and more rows help. EM also costs time: a fit runs the search 10 times and the E-step 56 times, and it took 2 to 72 times as long as the same search on complete data. The missing-data comparison has every number. In xges, exact_search, dagma, and calm, missing="em" is experimental: no reference implementation exists to compare them with, so the release card reports them beside test-wise deletion, dropping rows, and mean imputation.

Every other method raises a ValueError that names it: its score or loss reads every variable over the same rows, so it cannot delete rows per test. Drop the incomplete rows yourself, with data.dropna() for a DataFrame, which suits values missing completely at random, or use one of the methods above. A time series cannot lose a step without joining the steps around it, so for VARMA-LiNGAM, Granger, DYNOTEARS, and J-PCMCI+, fill in the missing steps first.

Backends and parallelism#

Andrey reads its numeric backend and its worker count from andrey.config, or from the ANDREY_BACKEND and ANDREY_N_JOBS environment variables; Configuration lists every setting. With more than one worker, GES, GFCI, the skeleton search of PC and the methods built on it, and the possible-d-separation step of FCI, FCI-JCI, and GFCI run their larger passes in worker processes and return the same graph as with one worker; Parallel search lists the methods and tests that use them.

Starting the worker processes re-imports the script that started them. A script that runs with more than one worker (ANDREY_N_JOBS above 1, or -1 for every core) must put its entry point under the __main__ guard:

import andrey
from andrey.data import load_dataset


def main():
    X = load_dataset("sachs").data
    andrey.ges(X)


if __name__ == "__main__":
    main()

Without the guard the re-imported script starts the search again, the workers fail to start, and the fit stops with RuntimeError: parallel-search workers failed to start.

Known limits#

On the large sparse benchmark graphs, GES with the standard BIC (ebic_gamma=0.0) kept many false edges. The default extended BIC, ebic_gamma=1.0, adds a penalty that grows with the number of variables, as the ges() page describes. On linear-Gaussian data from Erdos-Renyi graphs with mean degree 2, PC’s median errors stayed below the empty graph’s through 400 variables with 2,000 rows, and its fits finished within 1 h on 4 cores through 1,600; with 10 rows per variable, FCI’s stayed below the empty graph’s through 200 variables. Each method’s API page states the data and sizes it was measured on, the largest sizes where its errors stayed below the empty graph’s, and what ended each measurement. See Known limits.

References#