Causal Model

class pyAgrum.causal.CausalModel(bn, latentVarsDescriptor=None, keepArcs=False)

From an observational BNs and the description of latent variables, this class represent a complet causal model obtained by adding the latent variables specified in latentVarsDescriptor to the Bayesian network bn.

Parameters:
  • bn (pyAgrum.BayesNet) – an observational Bayesian network

  • latentVarsDescriptor (List[(str,List[int])]) – list of couples (<latent variable name>, <list of affected variables’ ids>).

  • keepArcs (bool) – By default, the arcs between variables affected by a common latent variable will be removed but this can be avoided by setting keepArcs to True

addCausalArc(x, y)

Add an arc x->y

Parameters:
  • x (int|str) – the nodeId or the name of the first node

  • y (int|str) – the nodeId or the name of the second node

Return type:

None

addLatentVariable(name, lchild, keepArcs=False)

Add a new latent variable with a name, a tuple of children and replacing (or not) correlations between children.

Parameters:
  • name (str) – the name of the latent variable

  • lchild (Tuple[str,str]) – the tuple of (2) children

  • keepArcs (bool) – do wee keep (or not) the arc between the children ?

Return type:

None

arcs()
Return type:

Set[Tuple[NewType()(NodeId, int), NewType()(NodeId, int)]]

Returns:

the set of arcs

backDoor(cause, effect, withNames=True)

Check if a backdoor exists between cause and effect

Parameters:
  • cause (int|str) – the nodeId or the name of the cause

  • effect (int|str) – the nodeId or the name of the effect

  • withNames (bool) – wether we use ids (int) or names (str)

Returns:

None if no found backdoor. Otherwise return the found backdoors as set of ids or set of names.

Return type:

None|Set[str]|Set[int]

causalBN()
Return type:

BayesNet

Returns:

the causal Bayesian network

Warning:

do not infer any computations in this model. It is strictly a structural model

children(x)

From a NodeId, returns its children (as a set of NodeId)

Parameters:

x (int) – the node

Returns:

the set of children

Return type:

Set[int]

connectedComponents()

Return a map of connected components and their nodes.

Returns:

thedisc of connected components

Return type:

Dict[int,NodeSet]

eraseCausalArc(x, y)

Erase the arc x->y

Parameters:
  • x (int|str) – the nodeId or the name of the first node

  • y (int|str) – the nodeId or the name of the second node

Return type:

None

existsArc(x, y)

Does the arc x->y exist ?

Parameters:
  • x (int|str) – the nodeId or the name of the first node

  • y (int|str) – the nodeId or the name of the second node

Returns:

True if the arc exists.

Return type:

bool

frontDoor(cause, effect, withNames=True)

Check if a frontdoor exists between cause and effet

Parameters:
  • cause (int|str) – the nodeId or the name of the cause

  • effect (int|str) – the nodeId or the name of the effect

  • withNames (bool) – wether we use ids (int) or names (str)

Returns:

None if no found frontdoot. Otherwise return the found frontdoors as set of ids or set of names.

Return type:

None|Set[str]|Set[int]

idFromName(name)
Parameters:

name (str) – the name of the variable

Returns:

the id of the variable

Return type:

int

latentVariablesIds()
Returns:

the set of ids of latent variables in the causal model

Return type:

NodeSet

names()
Returns:

the map NodeId,Name

Return type:

Dict[int,str]

nodes()
Return type:

Set[NewType()(NodeId, int)]

Returns:

the set of nodes

observationalBN()
Return type:

BayesNet

Returns:

the observational Bayesian network

parents(x)

From a NodeId, returns its parent (as a set of NodeId)

Parameters:

x (int) – the node

Returns:

the set of parents

Return type:

Set[int]

toDot()

Create a dot representation of the causal model

Return type:

str

Returns:

the dot representation in a string