andrey.das#
- andrey.das(data, *, alpha=0.001, max_parents=20, knowledge=None)[source]#
Learn a DAG with DAS, SCORE’s causal order with edges screened from the score’s Jacobian.
DAS [1] (Discovery At Scale) assumes the model of
score_matching(): each variable is a nonlinear function of its parents plus Gaussian noise,x_j = f_j(parents) + e_j. It finds SCORE’s causal order [2] and then reads the edges from the same estimate of the score’s Jacobian: for a leafl, the entryd^2 log p(x) / d x_l d x_jis zero at every point exactly whenjis not a parent [1]. Each leaf keeps the variables before it whose entries are clearly above zero, at mostmax_parentsof them, and the smooth-term tests of CAM [3] prune those candidates only, not every variable before it in the order, which makes the pruning fast for many variables. It returns a DAG and a causal order.- Parameters:
data (
ArrayLike) – Observed data, one row per sample. Coerced tofloat64. A DataFrame’s column names become the structure’slabels.alpha (
float) – Significance level of the pruning test, in(0, 1): a candidate parent stays when its smooth term’s p-value is at mostalpha. Neither the causal order nor the edge screen reads it.0.001is the cutoff of the SCORE experiments the paper follows.max_parents (
int|None) – A cap on every node’s parents, at least0: the edge screen keeps at mostmax_parentscandidate parents per variable, required edges included, so no node of the returned DAG has more.20is the paper’s value.Nonesets no cap.knowledge (
Knowledge|None) – Background knowledge, aKnowledge: required and forbidden edges, the pairs allowed to be adjacent, and temporal tiers, all honored. A variable is the next leaf only when no remaining variable is in a later tier or is the effect of a required edge out of it. Forbidden edges, edges into an earlier tier, and pairs outside the allowed set are never candidates, but the edge screen still ranks them and may take one as its reference; the score estimate still reads every remaining variable. A required edge is a candidate that the pruning never removes.
- Returns:
Wraps a
dagGraphStructure;metadata["algorithm"]is"das".orderingis the causal order of the leaf search, as a tuple of column indices, causes first; it is the orderscore_matching()finds on the same data.metadata["tie_break"]is the tie-break rule the fit ran under,andrey.config.tie_break.- Return type:
- 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
knowledgeis neitherNonenor aKnowledge.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
datais not a usable 2-D matrix: not numeric, holdingNaNorinf, with fewer than2 m + 2rows for pruning fits onm = min(max_parents, n_variables - 1)parents, or with a constant column, a column of fewer than 3 distinct values, or a column that repeats another up to scale and shift, each named in the message; or if more than half of the pairs of rows are equal on the remaining variables at a step, which leaves the kernel no bandwidth. Also ifknowledgenames a columndatalacks, contradicts itself, asKnowledgelists, or requires more thanmax_parentsparents of a variable.
- 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 column holds at most 10 distinct whole numbers and so may hold category codes, which the fit reads as numbers. Each message warns once per process.
Notes
When to use. DAS suits i.i.d. continuous observational data whose effects are nonlinear in every parent, with additive Gaussian noise and no unmeasured common causes [1], when there are too many variables for
score_matching()’s pruning: it finds the same causal order and prunes at mostmax_parentscandidates per variable instead of every variable before it. The paper shows it on 10 to 1,000 variables and 1,000 rows, with a DAG close to SCORE’s and a fit several times faster from 20 variables up. With fewer variablesscore_matching()prunes every edge its order allows; with linear effects and Gaussian noise usepc()orges(), and for additive noise that is not Gaussian,nogam()orresit().The steps. The paper’s three phases [1]:
Causal order. The leaf search of
score_matching(): the diagonal of the score’s Jacobian is estimated at every row of the remaining variables by the Stein estimator of [2] (Gaussian kernel at the median distance between the rows), the variable whose entry varies least is the next leaf, and the estimate is made again on the others.Edge screen. At the step that picks a leaf, the variables left are the leaf and those before it in the order, so the leaf has no children among them, and the leaf’s row of the step’s estimate gives
d^2 log p / d x_l d x_jat every row. The absolute median of the entry ranks the variables before the leaf. Themax_parents + 1largest are kept and the smallest of them is the reference, standing for a zero entry; each of the others stays as a candidate parent when a one-sided Welch test of equal means of the absolute entries against the reference’s gives a p-value at most0.01, the paper’s level.Pruning. Each variable is fit on its candidate parents, an additive model of penalized thin plate regression splines with ten basis functions per parent and smoothing chosen by GCV [4], as in
score_matching(), and a candidate stays when its smooth term’s p-value is at mostalpha[3].
The choices the paper leaves open. It states no pruning cutoff and follows the experiments of [2], whose cutoff
0.001is the default. The five largest medians stay without a test, the value of themin_parentsof the code published with the paper: the paper’s screen never keeps the reference, so without them a variable all of whose predecessors are parents would lose the weakest one, and a chain of two variables would have no edge. That code counts the leaf’s own entry among the ranked ones, so it usually keeps four untested; here the ranking holds only the predecessors, and five stay. The ranking follows that code, the absolute median of the entries, where the paper states the mean of their absolute values. That code estimates the Jacobian once on all the variables; DAS here reads each leaf’s row at the step that removes it, as the paper’s Algorithm 1 does. A test whose two samples are both constant keeps no edge. The estimator’s ridge term and bandwidth are those ofscore_matching(), whose Notes state them.The order search reads the columns centered and divided by their standard deviations, as
score_matching()does, so rescaling or shifting a column changes no step; the pruning reads each variable through an affine map to a fixed interval. The paper’s experiments run on the columns as simulated. The fit draws no random numbers and runs on one BLAS thread.Ties. The variances and the medians are compared rounded to 9 decimal places, as
hc()compares its score changes per row, so two values that differ only in their rounding tie. A tie goes to the variable with the lower rank computed from the data, as forhc(); the fit runs on the columns in that rank order, so the result does not depend on the column order unless two columns are identical. The column-order guide says what the rank keeps under a new column order.Missing values. A
NaNindataraisesValueError: every fit reads every row of its variables, so a row with a missing value has no place in it. Drop the incomplete rows first, which suits values missing completely at random, or usepc()orfci(), whose tests delete rows per test.Limitations. DAS is not benchmarked yet: no campaign has compared its graph with the code published with the paper or with other methods. Its causal order is SCORE’s, computed by the same code, so the order’s measured limits, which
score_matching()states in full, are its own. The order search reads standardized columns, where it orders fewer edges of a sparse graph correctly: on ten graphs of 10 variables with as many edges as variables, it put 99% of the true edges in the true direction on the columns as simulated and 80% on standardized columns. Where links have a strong linear part, so that variance builds up along the order, it did no better than a random order (41% to 53% of the true edges on linksz + sin z). The leaf rule is exact only for Gaussian noise: under other noise, such as Gumbel noise, a leaf’s entry changes with its residual, andnogam()andresit()drop that assumption. An edge the order reverses cannot be recovered, and the screen can drop a true parent whose entries are small next to the reference’s, which the pruning cannot add back. Each step factors a matrix with one row and one column per sample, so the order search’s time grows with the cube ofn_samplesand its memory with the square; the screen adds one solve per step. Binary columns, and any column with fewer than 3 distinct values, are not supported: the pruning fits a smooth term of each parent, and such a column raisesValueErrorbefore the fit. 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 >>> from andrey import das >>> rng = np.random.default_rng(0) >>> x = rng.standard_normal(300) >>> y = np.sin(2 * x) + 0.3 * rng.standard_normal(300) >>> out = das(np.column_stack([x, y])) >>> out.structure.kind, out.ordering ('dag', (0, 1))