Causal Model
class pyagrum.causal.CausalModel(bn, latentVarsDescriptor=None, keepArcs=False)
Section titled “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 (
, <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
keepArcstoTrue
addCausalArc(x, y)
Section titled “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)
Section titled “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()
Section titled “arcs()”- Return type:
Set[Tuple[NewType(NodeId,int),NewType(NodeId,int)]] - Returns: the set of arcs
backDoor(cause, effect, withNames=True)
Section titled “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()
Section titled “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)
Section titled “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()
Section titled “connectedComponents()”Return a map of connected components and their nodes.
- Returns: thedisc of connected components
- Return type: Dict[int,NodeSet]
eraseCausalArc(x, y)
Section titled “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)
Section titled “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)
Section titled “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)
Section titled “idFromName(name)”- Parameters: name (str) – the name of the variable
- Returns: the id of the variable
- Return type: int
latentVariablesIds()
Section titled “latentVariablesIds()”- Returns: the set of ids of latent variables in the causal model
- Return type: NodeSet
names()
Section titled “names()”- Returns: the map NodeId,Name
- Return type: Dict[int,str]
nodes()
Section titled “nodes()”- Return type:
Set[NewType(NodeId,int)] - Returns: the set of nodes
observationalBN()
Section titled “observationalBN()”- Return type:
BayesNet - Returns: the observational Bayesian network
parents(x)
Section titled “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()
Section titled “toDot()”Create a dot representation of the causal model
- Return type:
str - Returns: the dot representation in a string