Skip to content

dseparation

Signature/Parameters

def dseparation(self, var1, var2)

Retrieve the list of d-separations involving two variables.

Parameters:

Name Type Description Default
var1 str

Name of the first variable.

required
var2 str

Name of the second variable.

required

Returns:

Type Description
list[list[str]] or None

Conditioning sets that d-separate var1 and var2. Each inner list contains the conditioning variables as strings. Returns None when no separating set is found.

Examples:

>>> G = DAG(graph="X -> Z -> Y")
>>> G.dseparation("X", "Y")
[['Z']]
Source code in causalinf/gcm.py
def dseparation(self, var1, var2):
    """
    Retrieve the list of d-separations involving two variables.

    Parameters
    ----------
    var1 : str
        Name of the first variable.
    var2 : str
        Name of the second variable.

    Returns
    -------
    list[list[str]] or None
        Conditioning sets that d-separate ``var1`` and ``var2``. Each inner
        list contains the conditioning variables as strings. Returns
        ``None`` when no separating set is found.

    Examples
    --------
    >>> G = DAG(graph="X -> Z -> Y")
    >>> G.dseparation("X", "Y")
    [['Z']]
    """
    assert var1 and isinstance(var1, str), "'var1' (a str) must be provided."
    assert var2 and isinstance(var2, str), "'var2' (a str) must be provided."

    res = self.local_independencies()
    if res.nrow>0:
        res = (
            res
            .separate('term', into=['var1', 'var2|conditional'], sep='_||_', remove=False)
            .separate('var2|conditional', into=['var2', 'conditional'], sep=' | ', remove=True)  # 
            .mutate(var1 = tp.str_trim('var1'),
                    var2 = tp.str_trim('var2'),
                    conditional = tp.str_trim('conditional'),
                    )
            .replace_null({'conditional':''})
            .filter(((tp.col("var1")==var1) & (tp.col('var2')==var2)) |
                    ((tp.col("var2")==var1) & (tp.col('var1')==var2))
                    )
        )
        res = res.pull('conditional')
        res = [s.split(',') for s in res]
        res = [[string.strip() for string in inner_list] for inner_list in res]
    else:
        print(f'Not possible to d-separate {var1} and {var2} in the graph.')
        res = None
    return res