This repository was archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo.jl
351 lines (348 loc) · 11.5 KB
/
demo.jl
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#
# Code released under the MIT license, see corresponding LICENSE file
# (c) 2015, Thibaut Lienart
#
# --------------------------------------------------------------------------------------------------
#
include("lib_support.jl")
include("lib_epbp.jl")
include("lib_lbpd.jl")
include("lib_pbp.jl")
include("lib_ep.jl")
include("lib_doSim.jl")
#
demoNames = ["demoGrid","demoTree","demoImg","demoChain"]
expname = demoNames[2] # choice of demo (reason for this syntax is to show possibilities)
# make directory to store stuff
if ~isdir(expname)
mkdir(expname)
end
#
# ==================================================================================================
if expname == "demoGrid"
#
# SIMULATIONS TO BE RUN
#
RELOAD = false # re-generate everything
LBPD = false # LBP on deterministic grid
EPBP = false # EPBP
FEPBP = false # Fast-EPBP
PBP = true # PBP with MH sampling
EP = false # straight EP
#
# VERBOSITY
NODE_PROGRESS = false
#
# SIMULATION PARAMETERS [!USER!]
#
Nlist = [50] # (list) number of particles per node
Clist = [7] # (list) number of components for FEPBP, need to be of same dim as NLIST
Ninteg = 30 # number of integration points for EP proj
Ngrid = 200 # number of points in the discretization
nloops = 10 # number of loops through scheduling
nEPloops = 25 # number of EP iterations
nruns = 1 # number of time we run the whole thing
#
# EP PROJECTION MODE, default is KL ignoring update if moments not valid
EP_PROJ_MLE = false # use MLE projection instead of KL-EP
#
# Additional parameters for PBP
#
MHIter = 20 # number of MH iterations
MHProposal = Normal(0,1) # form of the MH proposal
PARACHAINS = true
#
# DECLARE GM
#
# > Declare a 5x5 grid
m,n = 5,5
nnodes,nedges,edge_list = gm_grid(m,n)
# > declare scheduling
scheduling = gm_grid_scheduling(m,n)
# > declare edge and node potential
HOMOG_EDGE_POT = true # edge pot is symmetric & same everywhere (eg: image)
# > side functions to define edge/node potential
node_potential = MixtureModel([Normal(-2,1),Gumbel(2,1.3)],[0.6,0.4])
edge_potential = Laplace(0,2)
# > definition of edge/node potential functions
eval_edge_pot(from,to,xfrom,xto) = pdf(edge_potential,xfrom-xto)
eval_node_pot(node,xnode) = pdf(node_potential,obs_values[node]-xnode)
# > (PBP) sampling from MH?
sampleMHP(old) = old+rand(MHProposal,N)'
LMHCHAIN = 1000
ENDCHUNK = 300
sampleMHP2(old) = old+rand(Normal(0,0.1),1)
# > initial values on the graph
orig_values = zeros(nnodes,1) + 2
#
est_range = (-5,15) # > estimated 1D-range for integration
sigma_thresh = 0.01
if RELOAD
# > generate observations
obs_values = orig_values + rand(node_potential,nnodes)
writecsv("$expname/$expname\_orig_values.dat",orig_values)
writecsv("$expname/$expname\_obs_values.dat",obs_values)
#
obs_var = sqrt(var(obs_values))
s_init = 4*obs_var
end
# ==================================================================================================
elseif expname == "demoTree"
#
# SIMULATIONS TO BE RUN
#
RELOAD = true # re-generate everything
LBPD = true # LBP on deterministic grid
EPBP = true # EPBP
FEPBP = false # Fast-EPBP
PBP = true # PBP with MH sampling
EP = false # straight EP
#
# VERBOSITY
NODE_PROGRESS = false
#
# SIMULATION PARAMETERS [!USER!]
#
Nlist = [100] # (list) number of particles per node
Clist = [7] # (list) number of components for FEPBP, need to be of same dim as NLIST
Ninteg = 30 # number of integration points for EP proj
Ngrid = 200 # number of points in the discretization
nloops = 10 # number of loops through scheduling
nEPloops = 25 # number of EP iterations
nruns = 1 # number of time we run the whole thing
#
# EP PROJECTION MODE, default is KL ignoring update if moments not valid
EP_PROJ_MLE = false # use MLE projection instead of KL-EP
#
# Additional parameters for PBP
#
MHIter = 20 # number of MH iterations
MHProposal = Normal(0,1) # form of the MH proposal
PARACHAINS = true
#
# DECLARE GM
#
nnodes,nedges = 8,7
edge_list = [ 1 2; 1 3; # top 1-{2,3}
2 1; 2 4; 2 5; 2 6; 3 1; 3 7; 3 8; # middle 2-{1,4,5,6} and 3-{1,7,8}
4 2; 5 2; 6 2; 7 3; 8 3; ] # leaves 4-2,5-2,6-2,7-3,8-3
sched1 = hcat(1,2,4,5,6,3,7,8)
sched2 = hcat(4,5,6,2,7,8,3,1)
scheduling = vcat(sched1[:],sched2[:])
# > declare edge and node potential
HOMOG_EDGE_POT = true # edge pot is symmetric & same everywhere (eg: image)
# > side functions to define edge/node potential
node_potential = MixtureModel([Normal(-2,1),Gumbel(1,1.5)],[0.7,0.3])
edge_potential = Cauchy(0,1)
# > definition of edge/node potential functions
eval_edge_pot(from,to,xfrom,xto) = pdf(edge_potential,xfrom-xto)
eval_node_pot(node,xnode) = pdf(node_potential,obs_values[node]-xnode)
# > (PBP) sampling from MH?
sampleMHP(old) = old+rand(MHProposal,N)'
LMHCHAIN = 1000
ENDCHUNK = 300
sampleMHP2(old) = old+rand(Normal(0,0.1),1)
# > initial values on the graph
orig_values = zeros(nnodes,1) + 2
#
est_range = (-7,7) # > estimated 1D-range for integration
sigma_thresh = 0.01
if RELOAD
# > generate observations
obs_values = orig_values + rand(node_potential,nnodes)
writecsv("$expname/$expname\_orig_values.dat",orig_values)
writecsv("$expname/$expname\_obs_values.dat",obs_values)
#
obs_var = sqrt(var(obs_values))
s_init = 4*obs_var
end
# ==================================================================================================
elseif expname == "demoImg"
#
# SIMULATIONS TO BE RUN
#
RELOAD = false # re-generate everything
LBPD = false # LBP on deterministic grid
EPBP = false # EPBP
FEPBP = true # Fast-EPBP
PBP = false # PBP with MH sampling
EP = false # straight EP
#
# VERBOSITY
NODE_PROGRESS = false
#
# SIMULATION PARAMETERS [!USER!]
#
Nlist = [70] # (list) number of particles per node
Clist = [5] # (list) number of components for FEPBP, need to be of same dim as NLIST
Ninteg = 50 # number of integration points for EP proj
Ngrid = 200 # number of points in the discretization
nloops = 2 # number of loops through scheduling
nEPloops = 3 # number of EP iterations
nruns = 1 # number of time we run the whole thing
#
# EP PROJECTION MODE, default is KL ignoring update if moments not valid.
EP_PROJ_MLE = false # use MLE projection instead of KL-EP
#
# Additional parameters for PBP
#
MHIter = 20 # number of MH iterations
MHProposal = Normal(0,.1) # form of the MH proposal
PARACHAINS = true
#
# DECLARE GM
#
obs = readdlm("ex_squareNoisy.dat")
obs_values = obs[:]
obs_var = sqrt(var(obs_values))
s_init = 4*obs_var
#
est_range = (-1,1.5)
sigma_thresh = 0.01
#
m,n = 50,50
nnodes,nedges,edge_list = gm_grid(m,n)
# > declare scheduling
scheduling = gm_grid_scheduling(m,n)
#
HOMOG_EDGE_POT = true
node_potential = Normal(0,0.1)
#
function eval_edge_pot(from,to,xfrom,xto)
d = abs(xfrom-xto)
if length(d)==1
r = d
if d>0.25
v = exp(-0.25/0.035)
r = d*0+v
else
r = exp(-d/0.035)
end
else
r=copy(d)
for i =1:length(d)
if d[i]>0.25
v = exp(-0.25/0.035)
r[i] = d[i]*0+v
else
r[i] = exp(-d[i]/0.035)
end
end
end
return r
end
#
function eval_node_pot(node,xnode)
return pdf(node_potential,obs_values[node]-xnode)
end
# ==================================================================================================
elseif expname == "demoChain"
#
# SIMULATIONS TO BE RUN
#
RELOAD = true # re-generate everything
LBPD = true # LBP on deterministic grid
EPBP = true # EPBP
FEPBP = false # Fast-EPBP
PBP = false # PBP with MH sampling
EP = true # straight EP
#
# VERBOSITY
NODE_PROGRESS = false
#
# SIMULATION PARAMETERS [!USER!]
#
Nlist = [100,200] # (list) number of particles per node
Clist = [7,10] # (list) number of components for FEPBP, need to be of same dim as NLIST
Ninteg = 30 # number of integration points for EP proj
Ngrid = 200 # number of points in the discretization
nloops = 10 # number of loops through scheduling
nEPloops = 25 # number of EP iterations
nruns = 1 # number of time we run the whole thing
#
# EP PROJECTION MODE, default is KL ignoring update if moments not valid.
EP_PROJ_MLE = false # use MLE projection instead of KL-EP
#
# Additional parameters for PBP
#
MHIter = 20 # number of MH iterations
MHProposal = Normal(0,.1) # form of the MH proposal
PARACHAINS = true
#
# DECLARE GM
#
T = 5
nnodes,nedges,edge_list = gm_chain(T)
# > declare scheduling
scheduling = gm_chain_scheduling(T,true) # forward only
# > declare edge and node potential
HOMOG_EDGE_POT = false # if edge pot is symmetric & the same everywhere (eg: image)
#
node_noise = Normal(0,2)
node_mult = 0.7
edge_noise = Normal(0,1)
edge_mult = 0.5
#
eval_edge_pot(from,to,xfrom,xto) = pdf(edge_noise,xto-edge_mult*xfrom)
eval_node_pot(node,xnode) = pdf(node_noise,obs_values[node]-node_mult*xnode)
#
# > (PBP) sampling from MH?
sampleMHP(old) = old+rand(MHProposal,N)'
LMHCHAIN = 1000
ENDCHUNK = 300
sampleMHP2(old) = old+rand(Normal(0,0.1),1)
#
est_range = (-10,10) # > estimated 1D-range for integration
sigma_thresh = 0.01
# > generate observations
if RELOAD
orig_values = zeros(nnodes,1)
obs_values = zeros(nnodes,1)
#
orig_values[1] = rand(Normal(0,1))
obs_values[1] = rand(Normal(node_mult*orig_values[1],1))
#
for node=2:T
orig_values[node] = rand(Normal(edge_mult*orig_values[node-1],2))
obs_values[node] = rand(Normal(node_mult*orig_values[node],1))
end
writecsv("$expname/$expname\_orig_values.dat",orig_values)
writecsv("$expname/$expname\_obs_values.dat",obs_values)
#
# > to start
obs_var = sqrt(var(obs_values))
s_init = 4*obs_var
end
end
#
# ======== RUN SIMULATIONS =========================================================================
#
integ_pts = linspace(est_range[1],est_range[2],Ninteg)' # ! leave the transpose
grid = linspace(est_range[1],est_range[2],Ngrid)'
# > generate observations
#
# --------------------------------------------------------------------------------------------------
# (cf. lib_doSim.jl)
#
if LBPD
doLBPD()
end
for N_index in 1:length(Nlist)
global N = Nlist[N_index]
global C = Clist[N_index] # for FEPBP
global R
for R = 1:nruns
if EPBP
doEPBP()
end
if FEPBP
doFEPBP()
end
if PBP
doPBP()
end
end
end
if EP
doEP()
end