The CLG model
A CLG is :
: - A pyagrum.DiGraph to 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)
Section titled “class pyagrum.clg.CLG(clg=None)”- Parameters:
clg (
CLG|None)
add(var)
Section titled “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)
Section titled “addArc(val1, val2, coef=1)”Add an arc val->val2 with a coefficient coef to the CLG.
- Parameters:
- val1 (
int|str) – The name or the int of the parent variable. - val2 (
int|str) – The name or the int of the child variable. - coef (
float|int) – The coefficient of the arc.
- val1 (
- 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()
Section titled “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)
Section titled “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:
BayesNet
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)
Section titled “children(val)”Return the list of children ids from the name or the id of a node.
- Parameters:
val (
int|str) – The name or the int of the variable. - Returns: The set of children nodes’ ids.
- Return type:
set[int]
children_names(val)
Section titled “children_names(val)”Return the list of children names from the name or the id of a node.
- Parameters:
val (
int|str) – The name or the int of the variable. - Returns: The list of val’s children’s names.
- Return type:
list[str]
coefArc(val1, val2)
Section titled “coefArc(val1, val2)”Return the coefficient of the arc val1->val2.
- Parameters:
- val1 (
int|str) – The name or the int of the parent variable. - val2 (
int|str) – The name or the int of the child variable.
- val1 (
- Returns: The coefficient of the arc.
- Return type:
float|int - Raises:
- pyagrum.NotFound – if one of the names is not found in the CLG.
- pyagrum.NotFound – if the arc does not exist.
copy(clg)
Section titled “copy(clg)”- Parameters:
clg (
CLG) - Return type:
None
Return the graph of the CLG (which is a DAG).
- Returns: The graph of the CLG.
- Return type:
DAG
dag2dict()
Section titled “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)
Section titled “eraseArc(val1, val2)”Erase the arc val->val2.
- Parameters:
- val1 (
int) - val2 (
int)
- val1 (
- Return type:
None
existsArc(val1, val2)
Section titled “existsArc(val1, val2)”Check if an arc val->val2 exists.
- Parameters:
- val1 (
int|str) – The name or the int of the parent variable. - val2 (
int|str) – The name or the int of the child variable.
- val1 (
- 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)
Section titled “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)
Section titled “logLikelihood(data)”Return the log-likelihood of the data.
- Parameters:
data (
str) – The data. - Returns: The log-likelihood of the data for the CLG.
- Return type:
float
name(node)
Section titled “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)
Section titled “nameOrId(val)”Return the int from the name or the int.
- Parameters:
val (
int|str) – The name or the int of the variable. - Returns: The int of the variable.
- Return type:
int
names()
Section titled “names()”Return the list of names in the CLG.
- Returns: The list of names in the CLG.
- Return type:
list[str]
nodes()
Section titled “nodes()”Return the list of NodeIds in the CLG.
- Returns: The list of NodeIds in the CLG.
- Return type:
list[int]
parent_names(val)
Section titled “parent_names(val)”Return the list of parents names from the name or the id of a node.
- Parameters:
val (
int|str) – The name or the int of the variable. - Returns: The list of val’s parents’ names.
- Return type:
list[str]
parents(val)
Section titled “parents(val)”Return the list of parent ids from the name or the id of a node.
- Parameters:
val (
int|str) – The name or the int of the variable. - Returns: The set of parent nodes’ ids.
- Return type:
set[int]
setCoef(val1, val2, coef)
Section titled “setCoef(val1, val2, coef)”Set the coefficient of an arc val1->val2.
- Parameters:
- val1 (
int|str) – The name or the int of the parent variable. - val2 (
int|str) – The name or the int of the child variable. - coef (
float|int) – The new coefficient of the arc.
- val1 (
- 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)
Section titled “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.
- node (
- Raises: pyagrum.NotFound – if the node is not found in the CLG.
- Return type:
None
setSigma(node, sigma)
Section titled “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.
- node (
- Raises: pyagrum.NotFound – if the node is not found in the CLG.
- Return type:
None
structuralFScore(other)
Section titled “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()
Section titled “toDot()”topologicalOrder()
Section titled “topologicalOrder()”Return the topological order of the CLG.
- Returns: The list of NodeIds in the topological order.
- Return type:
list[int]
variable(val)
Section titled “variable(val)”Return the variable from the int or from the name.
- Parameters:
val (
int|str) – The name or the int of the variable. - Returns: The variable.
- Return type:
GaussianVariable - Raises: pyagrum.NotFound – if val is not Found in the CLG.
variables()
Section titled “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
Section titled “class pyagrum.clg.SEM”This class is used to parse a SEM into a CLG model or convert a CLG model into a SEM.
sem = SEM(‘’’
hyper parameters
Section titled “hyper parameters”A = 4[5] B = 3[5] C = -2[5]
equations
Section titled “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]*)$‘
Section titled “FIND_FLOAT = ’^([0-9]*\\.?[0-9]*)$‘”FIND_STDDEV = ’^\\[([0-9]*\\.?[0-9]*)\\]$‘
Section titled “FIND_STDDEV = ’^\\[([0-9]*\\.?[0-9]*)\\]$‘”FIND_TERM = ’^([0-9]*\\.?[0-9]*)\\*?([a-zA-Z_]\\w*)$‘
Section titled “FIND_TERM = ’^([0-9]*\\.?[0-9]*)\\*?([a-zA-Z_]\\w*)$‘”FIND_VAR = ’^([a-zA-Z_]\\w*)$‘
Section titled “FIND_VAR = ’^([a-zA-Z_]\\w*)$‘”ID = ‘[a-zA-Z_]\\w*‘
Section titled “ID = ‘[a-zA-Z_]\\w*‘”NUMBER = ‘[0-9]*\\.?[0-9]*‘
Section titled “NUMBER = ‘[0-9]*\\.?[0-9]*‘”static loadCLG(filename)
Section titled “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:
CLG
static saveCLG(clg, filename)
Section titled “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.
- clg (
- Return type:
None
static toclg(sem)
Section titled “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:
CLG
static tosem(clg)
Section titled “static tosem(clg)”This function converts a CLG model into a SEM.
- Parameters:
clg (
CLG) – The CLG model to be converted. - Returns: lines – The SEM corresponding to the CLG model.
- Return type:
str
Other functions for CLG
Section titled “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)
Section titled “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_variables variables.
- Parameters:
- nb_variables (
int) – Number of variables. Must be >= 4. - names (
list[str]) – Names of the variables. Must satisfylen(names) == nb_variables. - max_parents (
int|None) – Maximum number of parents per node.Nonemeans no constraint. - ratio_arc (
float) – Target number of arcs expressed as a multiple ofnb_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.
- nb_variables (
- Returns: A random CLG.
- Return type:
CLG - Raises: ValueError – If any parameter violates its constraint.