API reference

andrey.data.sample_mixed#

andrey.data.sample_mixed(*, graph='erdos_renyi', d, n, seed, density=2.0, categorical_share=0.5, categories=(2, 3), concentration=1.0, shuffle=True)[source]#

Sample a conditional linear Gaussian network of categorical and continuous variables.

Parameters:
  • graph (str) – The random graph the DAG is drawn from, as for sample_scm().

  • d (int) – Node count.

  • n (int) – Sample count.

  • seed (int) – Root seed.

  • density (float) – Mean total degree (2 * |E| / d) of the graph.

  • categorical_share (float) – The share of the variables that are categorical, from 0 to 1: the first round(categorical_share * d) in causal order.

  • categories (tuple[int, int]) – The least and the most categories a categorical variable has; each one’s count is drawn uniformly between them, both included. The least is at least 2.

  • concentration (float) – Concentration of the symmetric Dirichlet each table row is drawn from, as for sample_discrete().

  • shuffle (bool) – Shuffle the columns with a permutation from the seed’s own column stream, and relabel the true graph, the categorical columns, and the parameters to match, as for sample_scm(). provenance["column_order"][j] is the index column j has with shuffle=False.

Returns:

data, shape (n, d), holds the category codes 0..k-1 of the categorical variables and the values of the continuous ones; graph is the true DAG. provenance["config"]["categorical"] lists the categorical columns and provenance["config"]["categories"] their numbers of categories, in the same order. params.weights holds each edge’s weight into a continuous variable: the coefficient of a continuous parent, or the largest shift of a categorical one; an edge into a categorical variable has nan, its table being its parameters. params.noise_scales is 1 for a continuous variable and nan for a categorical one.

Return type:

CausalDataset

Raises:

ValueError – If categorical_share is not in [0, 1], categories is not a pair with 2 <= least <= most, concentration is not positive, or graph names no graph.

Notes

A continuous variable x with continuous parents p and categorical parents c is x = sum_p w_p * p + sum_c s_c[category of c] + e, with e standard Gaussian. Each |w_p| is uniform on [0.5, 2] with a random sign. The shifts s_c of a parent with k categories are k evenly spaced values from -m to m, in a random order, with m uniform on [0.5, 2], so every category moves x by a different amount.

Examples

>>> from andrey.data import sample_mixed
>>> ds = sample_mixed(d=6, n=200, seed=0)
>>> ds.data.shape, ds.graph.kind, ds.provenance["config"]["categorical"]
((200, 6), 'dag', [1, 2, 5])