Gaussian Inference

Inference on a Gaussian Bayesian Network (GBN) is accomplished through updating the means and covariance matrix incrementally [CGH97]. The following GBN comes from [Cow98].

digraph { node [fixedsize=true, width=0.3, shape=circle, fontname="Helvetica-Outline", color=crimson, style=filled] Y -> X X -> Z }

Cowell GBN structure.

The variables come from the following Gaussian distributions.

  • \(Y = \mathcal{N}(0, 1)\)

  • \(X = \mathcal{N}(Y, 1)\)

  • \(Z = \mathcal{N}(Z, 1)\)

Below is a code sample of how we can perform inference on this GBN.

 1import numpy as np
 2
 3from pybbn.gaussian.inference import GaussianInference
 4
 5
 6def get_cowell_data():
 7    """
 8    Gets Cowell data.
 9
10    :return: Data and headers.
11    """
12    n = 10000
13    Y = np.random.normal(0, 1, n)
14    X = np.random.normal(Y, 1, n)
15    Z = np.random.normal(X, 1, n)
16
17    D = np.vstack([Y, X, Z]).T
18    return D, ['Y', 'X', 'Z']
19
20
21# assume we have data and headers (variable names per column)
22# X is the data (rows are observations, columns are variables)
23# H is just a list of variable names
24X, H = get_cowell_data()
25
26# then we can compute the means and covariance matrix easily
27M = X.mean(axis=0)
28E = np.cov(X.T)
29
30# the means and covariance matrix are all we need for gaussian inference
31# notice how we keep `g` around?
32# we'll use `g` over and over to do inference with evidence/observations
33g = GaussianInference(H, M, E)
34# {'Y': (0.00967, 0.98414), 'X': (0.01836, 2.02482), 'Z': (0.02373, 3.00646)}
35print(g.P)
36
37# we can make a single observation with do_inference()
38g1 = g.do_inference('X', 1.5)
39# {'X': (1.5, 0), 'Y': (0.76331, 0.49519), 'Z': (1.51893, 1.00406)}
40print(g1.P)
41
42# we can make multiple observations with do_inferences()
43g2 = g.do_inferences([('Z', 1.5), ('X', 2.0)])
44# {'Z': (1.5, 0), 'X': (2.0, 0), 'Y': (1.00770, 0.49509)}
45print(g2.P)