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.GaussianVariable random 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 (CLG | None)

Scientific notes

A CLG of pyAgrum is a linear Gaussian Bayesian network: all its variables are continuous and each variable \(X\) is a linear function of its parents plus an independent Gaussian noise (a linear structural equation),

\[X = \mu_X + \sum_{Y \in \mathrm{Pa}(X)} \beta_{YX}\, Y + \varepsilon_X, \qquad \varepsilon_X \sim \mathcal{N}(0, \sigma_X^2),\]

so that the conditional density of \(X\) is \(\mathcal{N}(\mu_X + \sum_Y \beta_{YX}\, y, \sigma_X^2)\). This is the representation of the Gaussian influence diagrams of [1] (a regression coefficient per arc, a conditional variance per variable), up to the parametrization of the mean: [1] attach to each variable its unconditional mean, while \(\mu_X\) here is the intercept of its equation. The joint distribution is multivariate normal: with \(B = (\beta_{YX})\) the matrix of the arc coefficients and \(D = \mathrm{diag}(\sigma_X^2)\), the vector of the variables is \((I - B^\top)^{-1}(\mu + \varepsilon)\), hence the mean \(m = (I - B^\top)^{-1}\mu\) and the covariance \(\Sigma = (I - B^\top)^{-1} D\, (I - B^\top)^{-\top}\).

Conventions of pyAgrum.

  • The parameter mu of a variable is the intercept \(\mu_X\) of its equation: it is the mean of \(X\) only when \(X\) has no parent (the means of the other variables are given by pyagrum.clg.CLGVariableElimination).

  • The parameter sigma is the standard deviation \(\sigma_X\) of the noise (the conditional standard deviation of \(X\) given its parents), not its variance.

  • The coefficient of an arc cannot be 0: a variable without influence on another one is not its parent.

  • The log-likelihood of a database is \(\sum_{i} \sum_X \ln \varphi(x_i;\, \mu_X + \sum_Y \beta_{YX} y_i, \sigma_X^2)\) (natural logarithm, \(\varphi\) the normal density), the maximized quantity of the parameter estimation of pyagrum.clg.learning.CLGLearner.

  • A CLG is written as text with the syntax of pyagrum.clg.SEM.

Implementation limit. The standard deviation must be positive, as the densities and the exact inference divide by \(\sigma_X^2\): a deterministic variable cannot be represented, and a variable added with \(\sigma_X = 0\), or a database with a constant column given to pyagrum.clg.learning.CLGLearner, is refused.

Methods

add()

Add a new variable to the CLG.

addArc()

Add an arc val->val2 with a coefficient coef to the CLG.

arcs()

Return the set of arcs in the CLG.

asDiscreteBN()

Return a BN with the same structure as the CLG.

children()

Return the list of children ids from the name or the id of a node.

children_names()

Return the list of children names from the name or the id of a node.

coefArc()

Return the coefficient of the arc val1->val2.

copy()

dag()

Return the graph of the CLG (which is a DAG).

dag2dict()

Return a dictionary representing the DAG of the CLG.

eraseArc()

Erase the arc val->val2.

existsArc()

Check if an arc val->val2 exists.

idFromName()

Return the int from the name.

logLikelihood()

Return the log-likelihood of the data.

name()

Return the associated name of the variable.

nameOrId()

Return the int from the name or the int.

names()

Return the list of names in the CLG.

nodes()

Return the list of NodeIds in the CLG.

parent_names()

Return the list of parents names from the name or the id of a node.

parents()

Return the list of parent ids from the name or the id of a node.

setCoef()

Set the coefficient of an arc val1->val2.

setMu()

Set the mean of a variable, or the intercept of its equation when the variable has parents.

setSigma()

Set the standard deviation of a variable.

structuralFScore()

Compare the structure of two CLGs using the F-score.

toDot()

Gives the representation of the CLG in the DOT language: one node per variable with its mean and its standard deviation, and one arc per dependency labelled with its coefficient.

topologicalOrder()

Return the topological order of the CLG.

variable()

Return the variable from the int or from the name.

variables()

Return the list of the variables in the CLG.

class pyagrum.clg.SEM

This class is used to parse a SEM into a CLG model or convert a CLG model into a SEM.

Each equation reads name = terms [stddev]: the terms are a constant, a variable or a coefficient times a variable (2.5*A, 2.5 A or 2.5A), separated by + or -, and the standard deviation of the noise is written between brackets at the end. The spaces separate the tokens. The numbers may use the exponent notation (1e-05), the exponent being glued to its number: 2e3A is 2000 times A, 2 e3 or 2*e3 is 2 times a variable e3. # starts a comment. A line without = (outside its comment) continues the previous equation and must start with +, - or [.

Examples

>>> sem = SEM.toclg('''
... # 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]  # a line without equal sign continues the equation
... F = E + C + 3.5*B + E[0.001]  # the terms of a same variable are summed: 2*E
... ''')
>>> print(SEM.tosem(sem))
C=-2.0[5.0]
B=3.0[5.0]
A=4.0[5.0]
D=A[0.2]
E=1.0+2.0*B+D[2.0]
F=3.5*B+C+2.0*E[0.001]

Scientific notes

SEM reads and writes a pyagrum.clg.CLG as a text of linear structural equations [2], one per variable: X = c + b1*Y1 + b2*Y2 [s] stands for \(X = c + b_1 Y_1 + b_2 Y_2 + \varepsilon_X\) with \(\varepsilon_X \sim \mathcal{N}(0, s^2)\), and X = c[s] for a variable without parent.

Conventions.

  • The constant \(c\) is the intercept mu of the variable (0 when it is absent), each term \(b\,Y\) is an arc \(Y \to X\) of coefficient \(b\), and the number in brackets is the standard deviation of the noise, not its variance.

  • The * is optional; several constants, or several terms of the same parent, are summed (F = E + 2E [1] gives a coefficient 3); a parent whose coefficients sum to 0 is refused, as is any null coefficient of a CLG.

  • # starts a comment; a line without = continues the previous equation and must start with +, - or [.

  • The spaces separate the tokens. Numbers may use the exponent notation (1e-05), the exponent being glued to its number: 2e3A is \(2000\,A\), while 2 e3 and 2*e3 are 2 times a variable e3.

  • A parent without equation, or a part of a line that is not a term, raises a SyntaxError.

  • When writing, the equations follow a topological order, a coefficient 1 or -1 is written as a sign alone and the others b*Y, so that the text is read back as the same model.

Methods

loadCLG()

Load the CLG from the file containing a SEM.

saveCLG()

Save the CLG as a SEM to a file.

toclg()

This function parses a SEM into a CLG model.

tosem()

This function converts a CLG model into a SEM.

Attributes

FIND_FLOAT

str(object=’’) -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

FIND_STDDEV

str(object=’’) -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

FIND_TERM

str(object=’’) -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

FIND_VAR

str(object=’’) -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

ID

str(object=’’) -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

NUMBER

str(object=’’) -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Other functions for CLG

randomCLG()

Generate a random CLG with nb_variables variables.