wolffd@0
|
1 function [err, time, engine] = cmp_inference(bnet, engine, exact, T, filter, singletons, maximize)
|
wolffd@0
|
2 % CMP_INFERENCE Compare several inference engines on a DBN
|
wolffd@0
|
3 % [err, time, engine] = cmp_inference(bnet, engine, exact, T, filter, singletons, maximize)
|
wolffd@0
|
4 %
|
wolffd@0
|
5 % engine{i} is the i'th inference engine.
|
wolffd@0
|
6 % 'exact' specifies which engines do exact inference -
|
wolffd@0
|
7 % we check that these all give the same results.
|
wolffd@0
|
8 % 'T' is the length of the random sequence we generate.
|
wolffd@0
|
9 % If filter=1, we do filtering, else smoothing (default: smoothing)
|
wolffd@0
|
10 % If singletons=1, we compare marginal_nodes, else marginal_family (default: family)
|
wolffd@0
|
11 %
|
wolffd@0
|
12 % err(e,n,t) = sum_i | Pr_exact(X(n,t)=i) - Pr_e(X(n,t)=i) |
|
wolffd@0
|
13 % where Pr_e = prob. according to engine e
|
wolffd@0
|
14 % time(e) = elapsed time for doing inference with engine e
|
wolffd@0
|
15
|
wolffd@0
|
16 err = [];
|
wolffd@0
|
17
|
wolffd@0
|
18 if nargin < 5, filter = 0; end
|
wolffd@0
|
19 if nargin < 6, singletons = 0; end
|
wolffd@0
|
20 if nargin < 7, maximize = 0; end
|
wolffd@0
|
21
|
wolffd@0
|
22 check_ll = 1;
|
wolffd@0
|
23
|
wolffd@0
|
24 assert(~maximize);
|
wolffd@0
|
25
|
wolffd@0
|
26 E = length(engine);
|
wolffd@0
|
27 ref = exact(1); % reference
|
wolffd@0
|
28
|
wolffd@0
|
29 ss = length(bnet.intra);
|
wolffd@0
|
30 ev = sample_dbn(bnet, 'length', T);
|
wolffd@0
|
31 evidence = cell(ss,T);
|
wolffd@0
|
32 onodes = bnet.observed;
|
wolffd@0
|
33 evidence(onodes,:) = ev(onodes, :);
|
wolffd@0
|
34
|
wolffd@0
|
35 assert(~filter);
|
wolffd@0
|
36 for i=1:E
|
wolffd@0
|
37 tic;
|
wolffd@0
|
38 %[engine{i}, ll(i)] = enter_evidence(engine{i}, evidence, 'maximize', maximize);
|
wolffd@0
|
39 [engine{i}, ll(i)] = enter_evidence(engine{i}, evidence);
|
wolffd@0
|
40 time(i)=toc;
|
wolffd@0
|
41 fprintf('engine %d took %6.4f seconds\n', i, time(i));
|
wolffd@0
|
42 end
|
wolffd@0
|
43
|
wolffd@0
|
44 cmp = mysetdiff(exact, ref);
|
wolffd@0
|
45 if check_ll
|
wolffd@0
|
46 for i=cmp(:)'
|
wolffd@0
|
47 if ~approxeq(ll(ref), ll(i))
|
wolffd@0
|
48 error(['engine ' num2str(i) ' has wrong ll'])
|
wolffd@0
|
49 end
|
wolffd@0
|
50 end
|
wolffd@0
|
51 end
|
wolffd@0
|
52 ll
|
wolffd@0
|
53
|
wolffd@0
|
54 hnodes = mysetdiff(1:ss, onodes);
|
wolffd@0
|
55 m = cell(1,E);
|
wolffd@0
|
56 for t=1:T
|
wolffd@0
|
57 for n=hnodes(:)'
|
wolffd@0
|
58 for e=1:E
|
wolffd@0
|
59 if singletons
|
wolffd@0
|
60 m{e} = marginal_nodes(engine{e}, n, t);
|
wolffd@0
|
61 else
|
wolffd@0
|
62 m{e} = marginal_family(engine{e}, n, t);
|
wolffd@0
|
63 end
|
wolffd@0
|
64 end
|
wolffd@0
|
65 for e=1:E
|
wolffd@0
|
66 assert(isequal(m{e}.domain, m{ref}.domain));
|
wolffd@0
|
67 end
|
wolffd@0
|
68 for e=cmp(:)'
|
wolffd@0
|
69 if ~approxeq(m{ref}.T(:), m{e}.T(:))
|
wolffd@0
|
70 str= sprintf('engine %d is wrong; n=%d, t=%d', e, n, t);
|
wolffd@0
|
71 error(str)
|
wolffd@0
|
72 end
|
wolffd@0
|
73 end
|
wolffd@0
|
74 end
|
wolffd@0
|
75 end
|