-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgbp.py
123 lines (91 loc) · 3.09 KB
/
cgbp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import numpy as np
import pandas as pd
import matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
from functools import reduce
class Graph:
def __init__(self, nodes = [], edges = []):
self.nodes = nodes
self.edges = edges
class MarkovNet:
def __init__(self, graph = Graph(), factors = []):
self.graph = graph
self.factors = factors
class ProbTable:
def __init__(self):
self.variables = variables
i = pd.MultiIndex.from_product([values[v] for v in variables], names = variables)
self.data = pd.DataFrame(0, index = i, columns = ['value']).reset_index()
def get_columns(self, assignment):
column_bools = [self.data[v] == assignment[v] for v in variables if v in assignment.keys()]
return reduce(lambda x,y: x & y, column_bools)
def __getitem__(self, assignment):
return self.data.loc[self.get_columns(assignment), 'value']
def __setitem__(self, assignment, value):
self.data.loc[self.get_columns(assignment), 'value'] = value
def __mul__(self, other):
return self.data * other.data
t = ProbTable()
t[{'A':0, 'B':0}] = 5
t2 = ProbTable()
t2[{'A':0, 'C':0}] = 10
t * t2
class Cluster:
def __init__(self, variables, factors):
self.variables = variables
self.potential =
class ClusterGraph:
def __init__(self, markov_net):
self.markov_net = markov_net
self.clusters = [f.variables for f in self.markov_net.factors]
self.potentials = [f.table for f in self.markov_net.factors]
self.N = len(self.clusters)
self.edges = np.zeros((self.N,) * 2, dtype = bool)
edge_idxs = [(i, j) for i in range(self.N) for j in range(self.N) if self.sepset(i, j) != []]
self.edges[tuple(zip(*edge_idxs))] = True
self.messages = self.edges.astype(int)
def sepset(self, i, j):
return [c for self.clusters[i] if c in self.clusters[j]]
def update_message(self, i, j):
incoming_idxs = [idx for idx in range(self.N) if idx != j and cluster_graph.edges[idx, i]]
incoming_msgs = self.messages[incoming_idxs].prod()
var_idxs_without_sepset = tuple(idx for idx, var in enumerate(self.clusters[i]) if var not in self.sepsep(i, j))
marginal_without_sepset = self.potentials[i].sum(var_idxs_without_sepset)
variables = ['A', 'B', 'C', 'D']
values = {
'A': [0, 1],
'B': [0, 1],
'C': [0, 1],
'D': [0, 1]
}
edges = [
{'A', 'B'},
{'A', 'C'},
{'B', 'D'},
{'C', 'D'}
]
graph = Graph(variables, edges)
phi1 = Factor(['A', 'B'])
phi1.table[0, 0] = 10
phi1.table[0, 1] = 0.1
phi1.table[1, 0] = 0.1
phi1.table[1, 1] = 10
phi2 = Factor(['A', 'C'])
phi2.table[0, 0] = 5
phi2.table[0, 1] = 0.2
phi2.table[1, 0] = 0.2
phi2.table[1, 1] = 5
phi3 = Factor(['B', 'D'])
phi3.table[0, 0] = 5
phi3.table[0, 1] = 0.2
phi3.table[1, 0] = 0.2
phi3.table[1, 1] = 5
phi4 = Factor(['C', 'D'])
phi4.table[0, 0] = 0.5
phi4.table[0, 1] = 1
phi4.table[1, 0] = 20
phi4.table[1, 1] = 2.5
factors = [phi1, phi2, phi3, phi4]
markov_net = MarkovNet(graph, factors)
cluster_graph = ClusterGraph(markov_net)