Inference : Variable Elimination

Exact inference in Conditional Linear Gaussian

Exact inference in a Conditional Linear Gaussian (CLG) model is performed by variable elimination, implemented in the class pyagrum.clg.CLGVariableElimination. The densities of the model are written as canonical forms, and a junction tree generator gives the elimination order; the elimination multiplies canonical forms and integrates the eliminated variables out.

Evidence (observed values of variables) can be added, updated, tested and erased; it is integrated into the canonical forms before the elimination.

The posterior density of one or several variables given the current evidence is returned as a canonical form (canonicalPosterior), or as a pyagrum.clg.GaussianVariable for one variable (posterior).

class pyagrum.clg.CLGVariableElimination(clg)
Parameters:

clg (CLG)

Scientific notes

CLGVariableElimination computes exact posterior densities in a pyagrum.clg.CLG by variable elimination (product and integration) on Gaussian potentials written in canonical form, the canonical characteristics \((g, h, K)\) of [1]:

\[\phi(x) = \exp\left(g + h^\top x - \tfrac{1}{2}\, x^\top K x\right).\]

From the model to the potentials. The conditional density of \(X\) with intercept \(\mu\), coefficients \(\beta\) (a column vector, one entry per parent) and noise variance \(\gamma = \sigma^2\) is the canonical form on \((X, \mathrm{Pa}(X))\)

\[\begin{split}g = -\frac{1}{2}\left(\frac{\mu^2}{\gamma} + \ln(2\pi\gamma)\right), \qquad h = \frac{\mu}{\gamma}\begin{pmatrix} 1 \\ -\beta \end{pmatrix}, \qquad K = \frac{1}{\gamma}\begin{pmatrix} 1 & -\beta^\top \\ -\beta & \beta\beta^\top \end{pmatrix}.\end{split}\]

Operations. A product adds the \((g, h, K)\) of the factors (extended by zeros to the union of their variables). Observing \(Y = y\) turns \((g, h, K)\) on \((X, Y)\) into \(g + h_Y^\top y - \frac{1}{2} y^\top K_{YY}\, y\), \(h_X - K_{XY}\, y\) and \(K_{XX}\). Integrating \(Y\) out, which needs \(K_{YY}\) positive definite (otherwise the integral diverges), gives

\[g + \tfrac{1}{2}\left(|Y| \ln(2\pi) - \ln\det K_{YY} + h_Y^\top K_{YY}^{-1} h_Y\right), \qquad h_X - K_{XY} K_{YY}^{-1} h_Y, \qquad K_{XX} - K_{XY} K_{YY}^{-1} K_{YX}.\]

The algorithm. All the potentials are first reduced by the evidence; the other non-target variables are then eliminated (product of the potentials that contain the variable, then integration) in the elimination order given by pyagrum.JunctionTreeGenerator for the graph of the model; the remaining potentials are multiplied and the result is normalized by its integral. The posterior of the targets is the Gaussian of covariance \(K^{-1}\) and mean \(K^{-1} h\): it is exact, not an approximation. With \(\sigma > 0\) for every variable, which pyagrum.clg.CLG ensures, each \(K_{YY}\) met is the one of the joint density of the remaining variables, positive definite: every integration is well defined.

Conventions. Evidence is hard: one observed value per variable. The posterior of an observed variable is returned as a Gaussian centred on the observed value, with a null standard deviation; the canonical posterior of an observed variable is refused.

Implementation limit. No junction tree is kept: each query runs a whole elimination.

Methods

canonicalPosterior()

Returns the posterior density as a canonical form.

eraseAllEvidence()

Remove all the evidences.

eraseEvidence()

Remove the evidence corresponding to the variable name.

hasEvidence()

Check if a variable has an evidence or, without argument, if at least one variable has.

nbrEvidence()

Returns the number of evidence.

posterior()

Returns the posterior density as a Gaussian variable.

updateEvidence()

Update evidences.

Forward sampling

Forward sampling draws samples from the joint distribution of a CLG model, without evidence: each variable is drawn given its parents, in a topological order. The samples can be used to generate synthetic data, or to estimate means, variances and covariances of the variables.

class pyagrum.clg.ForwardSampling(model)
Parameters:

model (CLG)

Scientific notes

ForwardSampling draws independent samples of the joint distribution of a pyagrum.clg.CLG by ancestral sampling, the continuous counterpart of the probabilistic logic sampling of [2]: the variables are drawn in a topological order, each one from its conditional density \(\mathcal{N}(\mu_X + \sum_Y \beta_{YX}\, y, \sigma_X^2)\) given the values just drawn for its parents. The samples are exact draws of the model, without weights, and follow the random generator of aGrUM (pyagrum.initRandom). There is no evidence: the samples are those of the prior joint distribution (the exact posteriors are given by pyagrum.clg.CLGVariableElimination).

Conventions of the statistics. variance_sample, stddev_sample and covariance_sample divide by \(N-1\) (unbiased estimates), so that the variances are the diagonal of the covariance matrix.

Methods

covariance_sample()

Computes the covariance between variables in <vals> (unbiased estimate, divided by N-1 for N samples).

makeSample()

Make N samples using forward sampling.

mean_sample()

Figure out the mean of variable <val> in the samples

stddev_sample()

Figure out the standard deviation of variable <val> in the samples (square root of variance_sample).

toarray()

Return the samples of variable <val>.

tocsv()

Convert the samples to csv.

topandas()

Convert the samples to pandas dataframe.

variance_sample()

Figure out the variance of variable <val> in the samples (unbiased estimate, divided by N-1 for N samples, as covariance_sample).