API reference

andrey.lges#

andrey.lges(data, *, score_func=None, penalty_discount=1.0, ebic_gamma=0.0, equivalent_sample_size=1.0, insertion='safe', schedule='lges', categorical=None)[source]#

Learn a CPDAG from observational data with less greedy equivalence search (LGES).

LGES searches the equivalence classes that GES searches [2], with GES’s moves, the turning move of GIES [3], and a decomposable score, the BIC by default, but adds fewer edges [1]: it leaves out an edge between two variables when the score says that some graph of the current class already makes them independent given the parents of one of them. By default it runs the paper’s main method, LGES; schedule also runs its LGES-0 variant in the phase order of the code published with the paper (see Notes).

Parameters:
  • data (ArrayLike) – Observed data, one row per sample. Coerced to float64. A DataFrame’s column names become the structure’s labels.

  • score_func (str | None) – Local score objective, as for ges(): "bic", the linear-Gaussian BIC, for continuous data. "bdeu" is the BDeu score and "discrete_bic" the multinomial BIC, for categorical data coded by whole numbers. "degen_gauss" (degenerate Gaussian) and "cond_gauss" (conditional Gaussian) are BIC scores for mixed data, continuous and categorical columns. "normal_scores", experimental, is "bic" on each column’s normal scores, for continuous data that are Gaussian only after an unknown increasing transformation of each column, as ges() describes, with its limits. None picks from the declared column types: "bdeu" when every column is categorical, "cond_gauss" when some are, else "bic". A column is declared categorical by categorical or by a DataFrame’s category, bool, or text type, never by its values. metadata["score_func"] names the score used, including the choice for None.

  • penalty_discount (float) – Multiplier on the BIC penalty of log(n_samples) per parameter: per parent for the linear-Gaussian BIC. Larger values give sparser graphs; 1.0 with ebic_gamma=0.0, the defaults, is the standard BIC, the score of the method’s paper [1]. Must be finite and non-negative. BDeu ignores it; for the multinomial BIC it weighs log(n) per free parameter of a variable’s conditional probability table.

  • ebic_gamma (float) – Weight on the extended-BIC term of the linear-Gaussian BIC: each variable’s score adds 2 * ebic_gamma * log C(n_variables - 1, n_parents), which grows with the number of parent sets a variable can choose from. Larger values give sparser graphs; 0.0, the default, is the standard BIC, the score of the method’s paper [1]. Must be from 0 to 1. Only "bic" and "normal_scores" use it: passing it with another score raises ValueError. See Notes.

  • equivalent_sample_size (float) – The equivalent sample size of the BDeu score: how many rows the uniform prior over each variable’s conditional distributions is worth. A larger value weighs the prior more against the data, and the graph can change with it. Must be finite and positive. Only "bdeu" uses it: passing it with another score raises ValueError.

  • insertion (str) – Which pairs of variables an edge addition leaves out (see Notes). "safe" (SafeInsert) leaves out a pair when one graph of the current class scores better without the edge; the paper proves that LGES then recovers the true class as the rows grow (its Corollary 1; Notes cover LGES-0) [1]. "conservative" (ConservativeInsert) also leaves out a pair when any valid addition of the edge worsens the score; it made fewer errors in the paper’s simulations, with no such proof. "safe" favors the proof of recovery; it gives up accuracy. "conservative" favors fewer errors; it gives up that proof. On the paper’s linear-Gaussian simulation (the authors’ generator; 10, 20, 50, and 100 variables at mean degree 1 to 4, and 10, 20, and 50 at degree 6; 1,000 and 10,000 rows; 20 graphs per setting), under schedule="lges", "conservative" had the lower mean error in 31 of 38 settings, the higher in 3 (by 0.1 to 1.15 edges, all at mean degree 4 and 6), and tied in 4; over the 480 graphs of mean degree 1 to 3 it made 6,789 errors against 9,029, and its median fit took 0.78 to 1.03 times as long (mean degree 2, 4, and 6).

  • schedule (str) – The order of the search’s phases (see Notes). "lges" is the paper’s main method, LGES (its Algorithm 1): before each single edge addition it deletes, then reverses, edges until neither improves the score, and it stops when no move improves it. "lges0_released" is the paper’s LGES-0 (its Algorithm 8) in the phase order of the code published with the paper: additions, then deletions, then reversals, each phase run once until no move improves the score. "lges" favors accuracy on denser graphs; it gives up time. "lges0_released" favors time; it gives up accuracy on denser graphs. On the simulation of insertion, counting a schedule better in a setting and rule when its mean error is lower and it is lower on more graphs than higher, LGES was better in 15 of 16 at mean degree 3 and 12 of 16 at mean degree 4, LGES-0 in 1 and 2. At mean degree 1 and 2 the two made about as many errors, 457 against 459 summed over the 32 settings and rules of the mean errors, with LGES-0 better in 12, LGES in 5, and 15 ties. LGES rescans the deletions and reversals after every addition: where both ran in one process (the conservative rule at mean degree 1 and 3; the safe fits there ran first and carry about 0.25 s of start-up), its median fit took 1.0 to 1.33 times LGES-0’s time; at mean degree 2, 4, and 6, timed in separate runs, so small differences are noise, 0.9 to 1.8 times.

  • categorical (Sequence[str | int] | None) – The categorical columns of data, for a mixed-data score: column labels (strings) or 0-based positions. None takes a DataFrame’s category, bool, and text columns, never a column by its values. [] marks none. A categorical score reads every column as categorical, and a continuous score raises ValueError on a declared column, named here or by its type. See Data types in the getting-started guide.

Returns:

Wraps a cpdag GraphStructure; metadata["algorithm"] is "lges" and metadata["score"] the score of the result, computed as for andrey.ges(); lower is better. Directed marks (->) are oriented edges, undirected (--) the ones the equivalence class leaves open. Under a mixed-data setting, metadata["categorical"] lists the categorical columns it read, by name or 0-based position. metadata["tie_break"] is the tie-break rule the fit ran under, andrey.config.tie_break, and metadata also records the penalties its score used, under their parameter names.

Return type:

StructureOutput

Raises:
  • TypeError – If an argument has the wrong type, such as a string for a number; the message names the method, the argument, what it accepts, and the value. Also if categorical is not a list of column names and positions.

  • ValueError – If an argument lies outside its range or is not one of its choices; the message names the method, the argument, what it accepts, and the value. Also if data is not a 2-D numeric matrix with at least two rows and no NaN or inf, or if a column is constant; with a categorical score, if data holds a number that is not whole; or, with a mixed-data score, if categorical names a column data lacks, or if a categorical column holds a number that is not whole. Also if a continuous score meets a column declared categorical, by categorical or by its type. With the BIC score, also if a column repeats another column up to scale and shift given more than two rows or, with more rows than columns, is a linear function of other columns. One message names every group of copies and says to keep one column per group.

Warns:
  • ExperimentalWarning – On the first call in a process: the method is not supported (see Limitations), and its API may change without deprecation.

  • DataTypeWarning – When a mixed-data score finds no declared categorical column and so runs as the linear-Gaussian form, or when a setting reads columns that look like category codes as numbers. Each message warns once per process.

Notes

When to use. LGES makes the assumptions of ges(): i.i.d. observational data, no unmeasured common causes and, with the default BIC score on continuous data, linear relations with Gaussian noise. Its moves need only a score that adds one term per variable and gives equivalent graphs the same score, which each of the scores does. The paper claims LGES and LGES-0, with the standard BIC, for sparse linear-Gaussian graphs of up to 150 variables with 1,000 or 10,000 rows, where they added fewer wrong edges than GES and ran faster [1]; it returns a CPDAG. When unmeasured common causes are plausible, use fci(); when the noise is non-Gaussian and every edge should be oriented, use direct_lingam(); when some rows come from interventions, use gies().

How it works. The search starts from the empty graph and uses three moves: edge additions, GES deletions, and GIES reversals (the turning move) [3]. Each addition takes one DAG G of the current class and, for each pair (x, y) of non-adjacent variables, compares the score of y given its parents in G with the score given those parents and x. When the edge worsens the score, the score holds y independent of x given those parents, and the step adds no edge between x and y (SafeInsert). With insertion="conservative" it also adds no edge between them when any valid addition x -> y of GES worsens the score (ConservativeInsert). Of the additions left, the step takes the one that improves the score most, as GES does. G is the DAG that orients the class by removing, while one is left, the first sink whose neighbors are adjacent to each other, in the order of the columns’ ranks (column-order guide), so the result does not depend on the column order; moves whose score changes are equal after rounding are taken in that order too.

The two schedules order these moves differently. LGES (schedule="lges", the paper’s Algorithm 1) repeats: deletions until none improves the score, reversals until none improves it, then one addition; it stops when no move improves the score. LGES-0 (schedule="lges0_released") runs additions until none improves the score, then deletions, then reversals, once, as the code published with the paper does; the paper’s Algorithm 8 itself runs the reversals before the deletions, and the two orders can return different classes. With insertion="safe", the paper proves that LGES recovers the true class as the rows grow (its Corollary 1), and LGES-0 in Algorithm 8’s order (its Corollary C.1). The same argument (its Theorem 1) covers the published code’s order: its reversals run once the additions and deletions have reached the true class in that limit. LGES+, which retries each deletion, is not implemented. The paper’s pseudocode also excludes (x, y) when x is a descendant of y in G, where adding x -> y to G would close a cycle; the published code compares the two scores for every pair, and so does this search. Testing every pair keeps more pairs than the pseudocode, never fewer, so the paper’s recovery result for SafeInsert still holds; only the path of the search can differ. A move counts as improving when it lowers the score by more than 1e-9, the rule of every greedy search here; the published code applies a move when it raises its score, -1/2 of this one up to a constant, by more than 1e-6. The paper’s prior knowledge, which orders the additions by required and forbidden edges and lets the data overrule them, and its orientation of the result with interventional data are not implemented.

Choosing the penalty. The score charges a variable penalty_discount * log(n_samples) per parent plus the extended-BIC term 2 * ebic_gamma * log C(n_variables - 1, n_parents) [4], as for ges(). The defaults, penalty_discount=1.0 and ebic_gamma=0.0, give the standard BIC of the paper; ges() defaults to ebic_gamma=1.0, so the two searches score the same data differently unless it is set.

Missing values. A NaN in data raises ValueError. The score sums one term per variable over every row; terms over different rows do not sum to one dataset’s score, so the search cannot delete rows per test as conditional-independence searches do. Drop the incomplete rows yourself, which suits values missing completely at random, or use pc() or fci(), whose tests delete rows per test.

Limitations. Not benchmarked yet. Its tests run it on up to 20 variables with 5,000 rows. With the linear-Gaussian BIC, "lges0_released" was compared with the authors’ code on the paper’s simulation up to 50 variables and 10,000 rows, and "lges" with the authors’ step functions run in Algorithm 1’s order up to 35 variables; "lges0_released" was also compared on the paper’s Sachs data, continuous and discretized, the latter with the multinomial BIC. BDeu and the mixed-data scores were not compared: the package’s checks run them on small tables only. The paper proves the recovery of the true class as the rows grow only for insertion="safe". Each addition step scores every pair of non-adjacent variables, and every valid addition for each pair it keeps, in one process; "lges" also rescans the deletions and reversals after every addition. The numbers are a reference, not a hard limit: they change with the data generator, the density, the method’s settings, and the machine.

References

Examples

>>> import numpy as np
>>> import pandas as pd
>>> from andrey import lges
>>> rng = np.random.default_rng(0)
>>> x = rng.standard_normal(500)
>>> y = rng.standard_normal(500)
>>> z = x + y + 0.5 * rng.standard_normal(500)
>>> lges(pd.DataFrame({"x": x, "y": y, "z": z})).structure.oriented_edges()
[('x', 'z', 'directed'), ('y', 'z', 'directed')]