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 (BayesNet) – a observational Bayesian network

  • latentVarsDescriptor (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 setting keepArcs to True

addCausalArc(x, y)

Add an arc x->y

Parameters
  • x (Union[NewType()(NodeId, int), str]) – the nodeId or the name of the first node

  • y (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 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 effet

Parameters
  • cause (Union[NewType()(NodeId, int), str]) – the nodeId or the name of the cause

  • effect (Union[NewType()(NodeId, int), str]) – the nodeId or the name of the effect

  • withNames (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

BayesNet

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 node

  • y (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 node

  • y (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 cause

  • effect (Union[NewType()(NodeId, int), str]) – the nodeId or the name of the effect

  • withNames (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

observationalBN()
Return type

BayesNet

Returns

the observational Bayesian network

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