API reference

andrey.skewd#

andrey.skewd(data, *, alpha=None, criterion='independence', tuning_iter=60, max_iter=5000, seed=None, missing=None)[source]#

Test the direction of a variable pair with SkewD, the skew-normal location-scale model.

SkewD [1] fits y = f(x) + g(x) n in each direction with skew-normal noise n, which includes the Gaussian, by a penalized likelihood over B-spline features of the cause. Either an HSIC test [2] of the scaled residual against the proposed cause or the two likelihoods determine the edge, as for loci().

Parameters:
  • data (ArrayLike) – The two variables, one per column: column 0 is x and column 1 is y. Coerced to float64. A DataFrame’s column names become the structure’s labels.

  • alpha (float | None) – Under criterion="independence": None chooses the direction with the larger p-value, or with the smaller test statistic when the p-values are equal; a significance level such as 0.05 draws an edge only when exactly one p-value exceeds it. Must be None under criterion="likelihood".

  • criterion (str) – "independence" tests whether each direction’s scaled residual is independent of its proposed cause (SkewD-IT); "likelihood" chooses the direction whose model has the larger likelihood (SkewD-LL). See Notes.

  • tuning_iter (int) – The penalty settings, at least 1, that the Bayesian optimization scores by cross-validation in each direction; the first two thirds, rounded up, are Latin hypercube points. See Notes.

  • max_iter (int) – The most iterations, at least 1, of the expectation conditional maximization that refines each direction’s fit.

  • seed (int | None) – None, the default, is the configured seed, ANDREY_SEED or 0 when it is unset, not a fresh random one, so a fit repeats. Seed of the random draws of the optimization.

  • missing (str | None) – How missing values (NaN) are handled. "testwise" deletes rows per test: each test reads only the rows where all of its variables are observed. None picks the method’s default, "testwise". On data with missing values, metadata["missing"] records the handling used. See Missing values. in Notes.

Returns:

Contains a two-node GraphStructure of kind dag with the chosen edge, or no edge. metadata["algorithm"] is "skewd". metadata["log_likelihoods"] holds the mean log-likelihood of y given x under "forward" and of x given y under "backward". Under criterion="independence", metadata["p_values"] and metadata["statistics"] hold each direction’s HSIC p-value and statistic, divided by its mean under independence, under the same keys. metadata["seed"] is the seed used.

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.

  • 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.

Warns:
  • ExperimentalWarning – On the first call in a process: the method is not benchmarked yet.

  • SearchLimitWarning – When a direction’s refinement stops at max_iter rather than on its own rule.

  • 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. SkewD decides the direction between two continuous variables observed i.i.d., under the location-scale noise model y = f(x) + g(x) n with g > 0, the noise n independent of the cause x, and no hidden common cause of the two [1]. It is designed for pairs whose noise is skewed, where a model with symmetric noise such as loci() can misjudge the scale and the direction; with symmetric noise its model reduces to loci()’s Gaussian one. The paper finds the likelihood criterion the more accurate on skewed location-scale pairs and the independence criterion the more robust across other benchmarks, so the independence criterion is the default.

In each direction, the effect given the cause is skew-normal with location f, scale g, and one shape lambda for every row; f is a cubic B-spline of 12 knots (14 columns) and log g^2 one of 5 knots (7 columns), each extended linearly past the data’s range [1]. The estimate maximizes the log-likelihood less a second-order difference penalty on each spline’s coefficients [3], in three steps [1]: Bayesian optimization chooses the two penalties, each candidate scored by the held-out log-likelihood of an 8-fold cross-validation, tuning_iter candidates with a Gaussian-process model of the scores and the expected improvement; CMA-ES [4], with a population of 100 and fixed learning rates where the paper’s runs adapt them, maximizes the penalized likelihood from least squares and from each fold’s estimate; and expectation conditional maximization refines the best of them until the parameters move less than 1e-6 over 25 iterations, or for max_iter iterations. The defaults, 60 candidates and 5,000 iterations, are those of the paper’s runs. Each column is standardized first, as in [1], so the model does not depend on the units of either variable; the Gaussian marginals of the two standardized causes are then equal, so the likelihoods compare the two conditional models. A change of units by a power of two gives the identical result, and any other factor changes the standardized values in their last digits (see Limitations).

With criterion="independence" (SkewD-IT), each direction’s scaled residual (y - f(x)) / g(x) is tested against the proposed cause with the HSIC test of loci(): Gaussian kernels, each bandwidth the median distance between the rows, and a gamma approximation of the null distribution [2]. The larger p-value decides, as the paper says, and alpha acts as for loci(). With criterion="likelihood" (SkewD-LL), the direction with the larger mean log-likelihood decides, and equal ones give no edge. Both directions draw their random numbers from the same seed, so swapping the columns swaps the fits.

Missing values. A NaN in data marks a missing value. Both fits read only the rows where both columns are observed, which for a pair are the rows test-wise deletion keeps; metadata["rows_tested"] gives their count as both min and median. When values are missing completely at random, those rows are a random sample of all rows. Fewer than 8 such rows (one for each cross-validation fold), or a column constant on them, raises ValueError.

Limitations. Not benchmarked yet. The paper measures it on skewed location-scale pairs of 1,000 rows and on established pair benchmarks [1]. A fit at the defaults runs about 490 CMA-ES searches in each direction, 8 folds for each of the tuning_iter penalty settings and 9 final starts, and takes tens of minutes per pair; tuning_iter sets most of that cost. Each HSIC test builds matrices with one row and one column per sample. The skew-normal’s skewness lies within about -1 and 1, so noise more skewed than that is misspecified, and the likelihood criterion assumes Gaussian marginals of the standardized variables [1]. A change of units by a factor that is not a power of two changed no edge in 120 refits of 20 pairs (80 to 150 rows, tuning_iter=3) and moved 2 of the 240 likelihoods by more than 1e-6 (both by 0.014, on a pair with no direction), so an edge can change only where the two directions already lie within what another seed changes. 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 scipy.stats import skewnorm
>>> from andrey import skewd
>>> rng = np.random.default_rng(0)
>>> x = rng.standard_normal(300)
>>> noise = skewnorm.rvs(5, size=300, random_state=rng)
>>> y = np.tanh(2 * x) + x + (0.4 + 0.2 * np.tanh(x)) * noise
>>> data = np.column_stack([x, y])
>>> out = skewd(data, criterion="likelihood", tuning_iter=2, max_iter=50, seed=0)
>>> out.structure.oriented_edges()
[(0, 1, 'directed')]