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 networkbn
.- Parameters
bn (
BayesNet
) – a observational Bayesian networklatentVarsDescriptor (
Optional
[List
[Tuple
[str
,Tuple
[str
,str
]]]]) – 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 settingkeepArcs
toTrue
- addCausalArc(x, y)¶
Add an arc x->y
- Parameters
x (
Union
[NewType()
(NodeId
,int
),str
]) – the nodeId or the name of the first nodey (
Union
[NewType()
(NodeId
,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 variablelchild (
Tuple
[str
,str
]) – the tuple of (2) childrenkeepArcs (
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 effet
- Parameters
cause (
Union
[NewType()
(NodeId
,int
),str
]) – the nodeId or the name of the causeeffect (
Union
[NewType()
(NodeId
,int
),str
]) – the nodeId or the name of the effectwithNames (
bool
) – does the function return the set of NodeId or the set of name ?
- Return type
Union
[None
,Set
[str
],Set
[NewType()
(NodeId
,int
)]]- Returns
None if no backdoor has been found. Otherwise the set of NodeId or names of the backdoor.
- causalBN()¶
- Return type
- Returns
the causal Bayesian network
- Warning
do not infer any computations in this model. It is strictly a structural model
- children(x)¶
- Parameters
x (
Union
[NewType()
(NodeId
,int
),str
]) – the node- Return type
Set
[NewType()
(NodeId
,int
)]- Returns
- eraseCausalArc(x, y)¶
Erase the arc x->y
- Parameters
x (
Union
[NewType()
(NodeId
,int
),str
]) – the nodeId or the name of the first nodey (
Union
[NewType()
(NodeId
,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 (
Union
[NewType()
(NodeId
,int
),str
]) – the nodeId or the name of the first nodey (
Union
[NewType()
(NodeId
,int
),str
]) – the nodeId or the name of the second node
- Return type
bool
- Returns
True if the arc exists.
- frontDoor(cause, effect, withNames=True)¶
Check if a frontdoor exists between cause and effet
- Parameters
cause (
Union
[NewType()
(NodeId
,int
),str
]) – the nodeId or the name of the causeeffect (
Union
[NewType()
(NodeId
,int
),str
]) – the nodeId or the name of the effectwithNames (
bool
) – does the function return the set of NodeId or the set of name ?
- Return type
Union
[None
,Set
[str
],Set
[NewType()
(NodeId
,int
)]]- Returns
None if no frontdoor has been found. Otherwise the set of NodeId or names of the frontdoor.
- idFromName(name)¶
- Parameters
name (
str
) – the name of the variable- Return type
NewType()
(NodeId
,int
)- Returns
the id of the variable
- latentVariablesIds()¶
- Return type
Set
[NewType()
(NodeId
,int
)]- Returns
the set of ids of latent variables in the causal model
- names()¶
- Return type
Dict
[NewType()
(NodeId
,int
),str
]- Returns
the map NodeId,Name
- nodes()¶
- Return type
Set
[NewType()
(NodeId
,int
)]- Returns
the set of nodes
- parents(x)¶
From a NodeId, returns its parent (as a set of NodeId)
- Parameters
x (
Union
[NewType()
(NodeId
,int
),str
]) – the node- Return type
Set
[NewType()
(NodeId
,int
)]- Returns
- toDot()¶
Create a dot representation of the causal model
- Return type
str
- Returns
the dot representation in a string