The CLG model
- A CLG is :
A
pyagrum.DiGraphto represents dependency between random variables. The model does not allows cycles.A dictionary id2var to map each NodeID to a
pyagrum.clg.GaussianVariablerandom variable.A dictionary name2id to map each variable’s name to its NodeID.
A dictionary arc2coef to map each arc to its coefficient.
A CLG is equivalent to a SEM (Structural Equation Model) with Gaussian variables.
- class pyagrum.clg.CLG(clg=None)
- Parameters:
clg (
Optional[CLG])
- add(var)
Add a new variable to the CLG.
- Parameters:
var (GaussianVariable) – The variable to be added to the CLG.
- Returns:
The id of the added variable.
- Return type:
int
- Raises:
ValueError – if the argument is None.
NameError – if the name of the variable is empty.
NameError – if a variable with the same name already exists in the CLG.
- addArc(val1, val2, coef=1)
Add an arc val->val2 with a coefficient coef to the CLG.
- Parameters:
val1 (str | int) – The name or the int of the parent variable.
val2 (str | int) – The name or the int of the child variable.
coef (float or int) – The coefficient of the arc.
- Returns:
The tuple of the NodeIds of the parent and the child variables.
- Return type:
tuple[int, int]
- Raises:
pyagrum.NotFound – if one of the names is not found in the CLG.
ValueError – if the coefficient is 0.
- arcs()
Return the set of arcs in the CLG.
- Returns:
The set of arcs in the CLG.
- Return type:
set[tuple[int, int]]
- asDiscreteBN(domain=2)
Return a BN with the same structure as the CLG. The variables of the BN are RangeVariable[domain].
- Parameters:
domain (int) – The domain of the variables in the returned BN.
- Returns:
A BN with the same structure as the CLG.
- Return type:
Warning
The returned BN is not a faithful representation of the CLG since the variables in the CLG are Gaussian and the variables in the returned BN are discrete. In particular, the CPTs are not defined in the returned BN.
- children(val)
Return the list of children ids from the name or the id of a node.
- Parameters:
val (str | int) – The name or the int of the variable.
- Returns:
The set of children nodes’ ids.
- Return type:
set[int]
- children_names(val)
Return the list of children names from the name or the id of a node.
- Parameters:
val (str | int) – The name or the int of the variable.
- Returns:
The list of val’s children’s names.
- Return type:
list[str]
- coefArc(val1, val2)
Return the coefficient of the arc val1->val2.
- Parameters:
val1 (str | int) – The name or the int of the parent variable.
val2 (str | int) – The name or the int of the child variable.
- Returns:
The coefficient of the arc.
- Return type:
float
- Raises:
pyagrum.NotFound – if one of the names is not found in the CLG.
pyagrum.NotFound – if the arc does not exist.
- dag()
Return the graph of the CLG (which is a DAG).
- Returns:
The graph of the CLG.
- Return type:
- dag2dict()
Return a dictionary representing the DAG of the CLG.
- Returns:
C – A directed graph DAG representing the causal structure.
- Return type:
dict[int, set[int]]
- eraseArc(val1, val2)
Erase the arc val->val2.
- Parameters:
val1 (
int)val2 (
int)
- Return type:
None
- existsArc(val1, val2)
Check if an arc val->val2 exists.
- Parameters:
val1 (str | int) – The name or the int of the parent variable.
val2 (str | int) – The name or the int of the child variable.
- Returns:
True if the arc exists.
- Return type:
bool
- Raises:
pyagrum.NotFound – if one of the names is not found in the CLG.
- idFromName(name)
Return the int from the name.
- Parameters:
name (str) – The name of the variable.
- Returns:
The int of the variable.
- Return type:
int
- Raises:
pyagrum.NotFound – if the name is not found in the CLG.
- logLikelihood(data)
Return the log-likelihood of the data.
- Parameters:
data (csv file) – The data.
- Returns:
The log-likelihood of the data for the CLG.
- Return type:
float
- name(node)
Return the associated name of the variable.
- Parameters:
node (int) – The id of the variable.
- Returns:
The associated name of the variable.
- Return type:
str
- Raises:
pyagrum.NotFound – if the node is not found in the CLG.
- nameOrId(val)
Return the int from the name or the int.
- Parameters:
val (str | int) – The name or the int of the variable.
- Returns:
The int of the variable.
- Return type:
int
- names()
Return the list of names in the CLG.
- Returns:
The list of names in the CLG.
- Return type:
list[str]
- nodes()
Return the list of NodeIds in the CLG.
- Returns:
The list of NodeIds in the CLG.
- Return type:
list[int]
- parent_names(val)
Return the list of parents names from the name or the id of a node.
- Parameters:
val (str | int) – The name or the int of the variable.
- Returns:
The list of val’s parents’ names.
- Return type:
list[str]
- parents(val)
Return the list of parent ids from the name or the id of a node.
- Parameters:
val (str | int) – The name or the int of the variable.
- Returns:
The set of parent nodes’ ids.
- Return type:
set[int]
- setCoef(val1, val2, coef)
Set the coefficient of an arc val1->val2.
- Parameters:
val1 (str | int) – The name or the int of the parent variable.
val2 (str | int) – The name or the int of the child variable.
coef (float or int) – The new coefficient of the arc.
- Raises:
pyagrum.NotFound – if one of the names is not found in the CLG.
ValueError – if the coefficient is 0.
ValueError – if the arc does not exist.
- Return type:
None
- setMu(node, mu)
Set the mean of a variable.
- Parameters:
node (int) – The id of the variable.
mu (float) – The new mean of the variable.
- Raises:
pyagrum.NotFound – if the node is not found in the CLG.
- Return type:
None
- setSigma(node, sigma)
Set the standard deviation of a variable.
- Parameters:
node (int) – The id of the variable.
sigma (float) – The new standard deviation of the variable.
- Raises:
pyagrum.NotFound – if the node is not found in the CLG.
- Return type:
None
- structuralFScore(other)
Compare the structure of two CLGs using the F-score.
- Parameters:
other (CLG) – The CLG to compare with.
- Returns:
The F-score of the structural comparison (1.0 = identical structure).
- Return type:
float
- toDot()
- topologicalOrder()
Return the topological order of the CLG.
- Returns:
The list of NodeIds in the topological order.
- Return type:
list[int]
- variable(val)
Return the variable from the int or from the name.
- Parameters:
val (str | int) – The name or the int of the variable.
- Returns:
The variable.
- Return type:
- Raises:
pyagrum.NotFound – if val is not Found in the CLG.
- variables()
Return the list of the variables in the CLG.
- Returns:
The list of the variables in the CLG.
- Return type:
list[GaussianVariable]
- class pyagrum.clg.SEM
This class is used to parse a SEM into a CLG model or convert a CLG model into a SEM.
code
sem = SEM(‘’’ # hyper parameters A = 4[5] B = 3[5] C = -2[5]
# equations D = A[.2] # D is a noisy version of A E = 1 + D + 2 B[2] F = E + C + 3.5*B + E[0.001] ‘’’)
- FIND_FLOAT = '^([0-9]*\\.?[0-9]*)$'
- FIND_STDDEV = '^\\[([0-9]*\\.?[0-9]*)\\]$'
- FIND_TERM = '^([0-9]*\\.?[0-9]*)\\*?([a-zA-Z_]\\w*)$'
- FIND_VAR = '^([a-zA-Z_]\\w*)$'
- ID = '[a-zA-Z_]\\w*'
- NUMBER = '[0-9]*\\.?[0-9]*'
- static loadCLG(filename)
Load the CLG from the file containing a SEM.
- Parameters:
filename (str) – The name of the file containing the SEM of CLG.
- Return type:
the loaded CLG
- static saveCLG(clg, filename)
Save the CLG as a SEM to a file.
- Parameters:
clg (CLG) – The CLG model to be saved.
filename (str) – The name of the file containing the SEM of CLG.
- Return type:
None
- static toclg(sem)
This function parses a SEM into a CLG model.
- Parameters:
sem (str) – The SEM to be parsed.
- Returns:
The CLG model corresponding to the SEM.
- Return type:
Other functions for CLG
- pyagrum.clg.randomCLG(nb_variables, names, max_parents=None, ratio_arc=1.2, MuMin=-5, MuMax=5, SigmaMin=1, SigmaMax=10, ArcCoefMin=1, ArcCoefMax=10)
Generate a random CLG with
nb_variablesvariables.- Parameters:
nb_variables (int) – Number of variables. Must be >= 4.
names (list[str]) – Names of the variables. Must satisfy
len(names) == nb_variables.max_parents (int or None) – Maximum number of parents per node.
Nonemeans no constraint.ratio_arc (float) – Target number of arcs expressed as a multiple of
nb_variables(passed topyagrum.randomBN()). Must be > 0.MuMin (float) – Lower bound for the uniform draw of each node’s mean. Must be <=
MuMax.MuMax (float) – Upper bound for the uniform draw of each node’s mean.
SigmaMin (float) – Lower bound for the uniform draw of each node’s std deviation. Must be > 0 and <=
SigmaMax.SigmaMax (float) – Upper bound for the uniform draw of each node’s std deviation.
ArcCoefMin (float) – Minimum absolute value of arc coefficients. Must be > 0 and <=
ArcCoefMax. Coefficients are drawn uniformly from[-ArcCoefMax, -ArcCoefMin] ∪ [ArcCoefMin, ArcCoefMax].ArcCoefMax (float) – Maximum absolute value of arc coefficients.
- Returns:
A random CLG.
- Return type:
- Raises:
ValueError – If any parameter violates its constraint.