-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate_proposals.py
150 lines (113 loc) · 4.71 KB
/
update_proposals.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from misc import weight_average, string_to_dict, char_fun
from scipy import log, exp
def update_proposal_cpt(proposal, samples, weights, index, graph,
evidence_parents, eta_rate):
"""
Updates current proposal given the new data.
Arguments
=========
samples: the current samples to use.
index: the current index (used to pick the weight)
"""
# Initialize weighted estimator
wei_est = weight_average(samples, weights)
# estimate CPT table from samples
for node in evidence_parents:
if node is None:
continue
elif proposal.is_root_node(node):
# root node
def f(sample):
res1 = char_fun(sample, {node: 0})
return res1
p, _ = wei_est.eval(f)
proposal.cpt[node][0] += eta_rate(index) * (
p - proposal.cpt[node][0])
proposal.cpt[node][1] += eta_rate(index) * (
1 - p - proposal.cpt[node][1])
else:
# rest of the nodes
for key in proposal.cpt[node]:
parent_dict = string_to_dict(key)
def f(sample):
res1 = char_fun(sample, {node: 0})
res2 = char_fun(sample, parent_dict)
return res1 * res2
def g(sample):
res2 = char_fun(sample, parent_dict)
return res2
p, _ = wei_est.eval(f)
q, _ = wei_est.eval(g)
if abs(p - q) < 1e-10:
ratio = 1
else:
ratio = p / q
proposal.cpt[node][key][0] += eta_rate(index) * (
ratio - proposal.cpt[node][key][0])
proposal.cpt[node][key][1] += eta_rate(index) * (
1 - ratio - proposal.cpt[node][key][1])
return proposal
def update_proposal_lambdas(proposal, samples, weights, index, graph,
evidence_parents, eta_rate):
"""
Updates current proposal given the new data.
Arguments
=========
samples: the current samples to use.
index: the current index (used to pick the weight)
"""
# Initialize weighted estimator
wei_est = weight_average(samples, weights)
# estimate CPT table from samples
for child in evidence_parents:
if child is None:
continue
elif proposal.is_root_node(child):
# root child -- update priors using current samples
def f(sample):
res1 = char_fun(sample, {child: 1})
return res1
p, _ = wei_est.eval(f)
proposal.prior_dict[child][0] += eta_rate(index) * (
1 - p - proposal.prior_dict[child][0])
proposal.prior_dict[child][1] += eta_rate(index) * (
p - proposal.prior_dict[child][1])
else:
# rest of the childs -- lambdas
parents = [ident for ident in graph[child]]
for parent in proposal.lambdas[child]:
state_vec = {p: False for p in parents}
if parent == "leak_node":
def f(sample):
return char_fun(sample, state_vec)
q, _ = wei_est.eval(f)
state_vec[child] = False
def f(sample):
return char_fun(sample, state_vec)
p, _ = wei_est.eval(f)
if abs(p) < 1e-16 or abs(q) < 1e-16:
# Do not update if probabilities are
# too small
continue
ratio = exp(log(p) - log(q))
proposal.lambdas[child]["leak_node"] += eta_rate(index) * (
ratio - proposal.lambdas[child]["leak_node"])
else:
# TODO: something is wrong with this part
# and it doesn't estimate correctly
state_vec[parent] = True
def f(sample):
return char_fun(sample, state_vec)
q, _ = wei_est.eval(f)
state_vec[child] = False
def f(sample):
return char_fun(sample, state_vec)
p, _ = wei_est.eval(f)
if abs(p) < 1e-16 or abs(q) < 1e-16:
# Do not update if probabilities are
# too small
continue
ratio = exp(log(p) - log(q))
proposal.lambdas[child][parent] += eta_rate(index) * (
ratio - proposal.lambdas[child][parent])
return proposal