Learning a CLG

One of the main features of this library is the possibility to learn a CLG.

More precisely what can be learned is :
  • The dependency graph of a CLG

  • The parameters of a CLG: the mu and sigma of each variable, the coefficients of the arcs

Learning the graph

To learn the graph of a CLG (ie the dependence between variables) we use a modified PC algorithm based on Colombo and Maathuis [CM14].

The independence test used is based on Simionato and Vandin [SV22].

class pyagrum.clg.learning.CLGLearner(filename, *, n_sample=15, fwer_delta=0.05)

Using Rademacher Average to guarantee FWER(Family Wise Error Rate) in independency test. (see “Bounding the Family-Wise Error Rate in Local Causal Discover using Rademacher Averages”, Dario Simionato, Fabio Vandin, 2022)

Parameters:
  • filename (str)

  • n_sample (int)

  • fwer_delta (float)

This function is the first step of PC-algo: Adjacency Search. Apply indep_test() to the first step of PC-algo for Adjacency Search.

Parameters:
  • order (list[int]) – A particular order of the Nodes.

  • verbose (bool) – Whether to print the process of Adjacency Search.

Return type:

tuple[dict[int, set[int]], dict[tuple[int, int], set[int]]]

Returns:

  • C (dict[int, set[int]]) – The temporary skeleton.

  • sepset (dict[tuple[int, int], set[int]]) – Sepset(which will be used in Step2&3 of PC-Algo).

PC_algorithm(order, verbose=False)

This function is an advanced version of PC-algo. We use Indep_test_Rademacher() to replace indep_test() in PC-algo. And we orient the undirected edges in the skeleton C by comparing the variances of the two nodes.

Parameters:
  • order (list[int]) – A particular order of the Nodes.

  • verbose (bool) – Whether to print the process of the PC algorithm.

Returns:

C – A directed graph DAG representing the causal structure.

Return type:

dict[int, set[int]]

Pearson_coeff(X, Y, Z)

Estimate Pearson’s linear correlation(using linear regression when Z is not empty).

Return type:

None

Parmeters

Xint

id of the first variable tested.

Yint

id of the second variable tested.

Zset[int]

The conditioned variable’s id set.

param X:

type X:

int

param Y:

type Y:

int

param Z:

type Z:

set[int]

RAveL_MB(T)

Find the Markov Boundary of variable T with FWER lower than Delta.

Parameters:

T (int) – The id of the target variable T.

Returns:

MB – The Markov Boundary of variable T with FWER lower than Delta.

Return type:

set[int]

RAveL_PC(T)

Find the Parent-Children of variable T with FWER lower than Delta.

Parameters:

T (int) – The id of the target variable T.

Returns:

The Parent-Children of variable T with FWER lower than Delta.

Return type:

set[int]

Repeat_II(order, C, l, verbose=False)

This function is the second part of the Step1 of PC algorithm.

Parameters:
  • order (list[int]) – The order of the variables.

  • C (dict[int, set[int]]) – The temporary skeleton.

  • l (int) – The size of the sepset

  • verbose (bool) – Whether to print.

Returns:

found_edge – True if a new edge is found, False if not.

Return type:

bool

Step4(C, verbose=False)

This function is the fourth step of PC-algo. Orient the remaining undirected edge by comparing variances of two nodes.

Parameters:
  • C (dict[int, set[int]]) – The temporary skeleton.

  • verbose (bool) – Whether to print the process of Step4.

Return type:

tuple[dict[int, set[int]], bool]

Returns:

  • C (dict[int, set[int]]) – The final skeleton (of Step4).

  • new_oriented (bool) – Whether there is a new edge oriented in the fourth step.

estimate_parameters(C)

This function is used to estimate the parameters of the CLG model.

Parameters:

C (dict[int, set[int]]) – A directed graph DAG representing the causal structure.

Return type:

tuple[dict[int, float], dict[int, float], dict[tuple[int, int], float]]

Returns:

  • id2mu (dict[int, float]) – The estimated mean of each node.

  • id2sigma (dict[int, float]) – The estimated variance of each node.

  • arc2coef (dict[tuple[int, int], float]) – The estimated coefficients of each arc.

fitParameters(clg)

In this function, we fit the parameters of the CLG model.

Parameters:

clg (CLG) – The CLG model to be changed its parameters.

Return type:

None

static generate_XYZ(l)

Find all the possible combinations of X, Y and Z.

Returns:

All the possible combinations of X, Y and Z.

Return type:

list[tuple[set[int], set[int]]]

Parameters:

l (list[int])

static generate_subsets(S)

Generator that iterates on all all the subsets of S (from the smallest to the biggest).

Parameters:

S (set[int]) – The set of variables.

Return type:

Generator[set[int], None, None]

id2samples: dict[int, list]
learnCLG()

First use PC algorithm to learn the skeleton of the CLG model. Then estimate the parameters of the CLG model. Finally create a CLG model and return it.

Returns:

learned_clg – The learned CLG model.

Return type:

CLG

r_XYZ: dict[tuple[frozenset[int], frozenset[int]], list[float]]
sepset: dict[tuple[int, int], set[int]]
supremum_deviation(n_sample, fwer_delta)

Use n-MCERA to get supremum deviation.

Parameters:
  • n_sample (int) – The MC number n in n-MCERA.

  • fwer_delta (float ∈ (0,1]) – Threshold.

Returns:

SD – The supremum deviation.

Return type:

float

test_indep(X, Y, Z)

Perform a standard statistical test and use Bonferroni correction to correct for multiple hypothesis testing.

Parameters:
  • X (int) – The id of the first variable tested.

  • Y (int) – The id of the second variable tested.

  • Z (set[int]) – The conditioned variable’s id set.

Returns:

True if X and Y are indep given Z, False if not indep.

Return type:

bool

three_rules(C, verbose=False)

This function is the third step of PC-algo. Orient as many of the remaining undirected edges as possible by repeatedly application of the three rules.

Parameters:
  • C (dict[int, set[int]]) – The temporary skeleton.

  • verbose (bool) – Whether to print the process of this function.

Returns:

C – The final skeleton (of Step3).

Return type:

dict[int, set[int]]

[CM14]

Diego Colombo and Marloes H. Maathuis. Order-independent constraint-based causal structure learning. Journal of Machine Learning Research, 15(1):3741–3782, 2014. URL: https://jmlr.org/papers/v15/colombo14a.html.

[SV22]

Dario Simionato and Fabio Vandin. Bounding the family-wise error rate in local causal discovery using Rademacher averages. arXiv preprint arXiv:2212.03742, 2022. URL: https://arxiv.org/abs/2212.03742.