comparison toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/Old/cmp_inference.m @ 0:e9a9cd732c1e tip

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