In [1]:
import numpy as np
from pybbn.lg.graph import Dag, Parameters, Bbn
# create the directed acylic graph
dag = Dag()
dag.add_node(0)
dag.add_node(1)
dag.add_edge(0, 1)
# create parameters
means = np.array([0, 25])
cov = np.array([
[1.09, 1.95],
[1.95, 4.52]
])
params = Parameters(means, cov)
# create the bayesian belief network
bbn = Bbn(dag, params)
# do the inference
M, S = bbn.do_inference()
print(M)
# set the evidence on node 0 to a value of 1
bbn.set_evidence(0, 1)
M, S = bbn.do_inference()
print(M)
bbn.clear_evidences()
# set the evidence on node 1 to a value of 20
bbn.set_evidence(1, 20)
M, S = bbn.do_inference()
print(M)
bbn.clear_evidences()
This example simulates data and then computes the means and covariances to be used as the parameter input. The structure is given as follows.
The data is sampled as follows.
In [2]:
import numpy as np
from numpy.random import normal
import pandas as pd
In [3]:
np.random.seed(37)
N = 1000
x1 = normal(0, 1, N)
x2 = normal(2, 2, N)
x3 = normal(1.0 + 0.8 * x1 + 0.2 * x2, 1, N)
x4 = normal(0.9 * x3, 1, N)
df = pd.DataFrame({
'x1': x1,
'x2': x2,
'x3': x3,
'x4': x4
})
In [4]:
dag = Dag()
dag.add_node(0)
dag.add_node(1)
dag.add_node(2)
dag.add_node(3)
dag.add_edge(0, 2)
dag.add_edge(1, 2)
dag.add_edge(2, 3)
# create parameters
means = np.array(list(df.mean()))
cov = df.cov().values
params = Parameters(means, cov)
print('means: {}'.format(means))
print('cov: {}'.format(cov))
# create the bayesian belief network
bbn = Bbn(dag, params)
# do the inference
print('doing inference with and without evidence')
M, C = bbn.do_inference()
print('>')
print('means: {}'.format(M))
print('cov: {}'.format(C))
bbn.set_evidence(2, 1.0)
M, C = bbn.do_inference()
print('>')
print('means: {}'.format(M))
print('cov: {}'.format(C))
bbn.clear_evidences()
bbn.set_evidence(3, 0.5)
M, C = bbn.do_inference()
print('>')
print('means: {}'.format(M))
print('cov: {}'.format(C))
bbn.clear_evidences()
bbn.set_evidence(0, 0)
bbn.set_evidence(2, 0.5)
M, C = bbn.do_inference()
print('>')
print('means: {}'.format(M))
print('cov: {}'.format(C))
bbn.clear_evidences()
This is a simple example of learning the parameters (as before above) and structure using a simple maximum-weight spanning tree algorithm. Note that arc-orientation is simply the order given to it.
True Model
Learned Model: arc commission error with $X_1 \rightarrow X_2$, arc omission error with $X_2 \rightarrow X_3$
In [5]:
def to_edge_list(df):
data = []
corr_matrix = df.corr().values
num_vars = corr_matrix.shape[0]
for i in range(num_vars):
for j in range(num_vars):
if j > i:
t = (i, j, abs(corr_matrix[i, j]))
data.append(t)
return data
def learn_structure(num_vars, edge_list):
# add all nodes
dag = Dag()
for i in range(num_vars):
dag.add_node(i)
# add edges using maximum weight spanning tree algorithm
for edge in edge_list:
try:
dag.add_edge(edge[0], edge[1])
except ValueError:
pass
if len(dag.g.edges()) == num_vars - 1:
break
return dag
In [6]:
edge_list = sorted(to_edge_list(df), key=lambda tup: tup[2], reverse=True)
print(edge_list)
In [7]:
dag = learn_structure(df.shape[1], edge_list)
In [8]:
dag.g.nodes()
Out[8]:
In [9]:
dag.g.edges()
Out[9]:
In [10]:
# create the bayesian belief network
bbn = Bbn(dag, params)
# do the inference
print('inferences')
M, C = bbn.do_inference()
print('means: {}'.format(M))
print('cov: {}'.format(C))
bbn.set_evidence(2, 1.0)
M, C = bbn.do_inference()
print('>')
print('means: {}'.format(M))
print('cov: {}'.format(C))
bbn.clear_evidences()
bbn.set_evidence(3, 0.5)
M, C = bbn.do_inference()
print('>')
print('means: {}'.format(M))
print('cov: {}'.format(C))
bbn.clear_evidences()
bbn.set_evidence(0, 0)
bbn.set_evidence(2, 0.5)
M, C = bbn.do_inference()
print('>')
print('means: {}'.format(M))
print('cov: {}'.format(C))
bbn.clear_evidences()
In [ ]: