Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/cmp_online_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 [time, engine] = cmp_online_inference(bnet, engine, T, varargin) | |
2 % CMP_ONLINE_INFERENCE Compare several online inference engines on a DBN | |
3 % function [time, engine] = cmp_online_inference(bnet, engine, T, ...) | |
4 % | |
5 % engine{i} is the i'th inference engine. | |
6 % time(e) = elapsed time for doing inference with engine e | |
7 % | |
8 % The list below gives optional arguments [default value in brackets]. | |
9 % | |
10 % exact - specifies which engines do exact inference [ 1:length(engine) ] | |
11 % singletons_only - if 1, we only call marginal_nodes, else this and marginal_family [0] | |
12 % check_ll - 1 means we check that the log-likelihoods are correct [1] | |
13 | |
14 % set default params | |
15 exact = 1:length(engine); | |
16 singletons_only = 0; | |
17 check_ll = 1; | |
18 onodes = bnet.observed; | |
19 | |
20 args = varargin; | |
21 nargs = length(args); | |
22 for i=1:2:nargs | |
23 switch args{i}, | |
24 case 'exact', exact = args{i+1}; | |
25 case 'singletons_only', singletons_only = args{i+1}; | |
26 case 'check_ll', check_ll = args{i+1}; | |
27 case 'observed', onodes = args{i+1}; | |
28 otherwise, | |
29 error(['unrecognized argument ' args{i}]) | |
30 end | |
31 end | |
32 | |
33 E = length(engine); | |
34 ref = exact(1); % reference | |
35 cmp = mysetdiff(exact, ref); | |
36 | |
37 ss = length(bnet.intra); | |
38 hnodes = mysetdiff(1:ss, onodes); | |
39 ev = sample_dbn(bnet, 'length', T); | |
40 evidence = cell(ss,T); | |
41 evidence(onodes,:) = ev(onodes, :); | |
42 | |
43 time = zeros(1,E); | |
44 for t=1:T | |
45 for e=1:E | |
46 tic; | |
47 [engine{e}, ll(e)] = enter_evidence(engine{e}, evidence(:,t), t); | |
48 time(e)= time(e) + toc; | |
49 end | |
50 if check_ll | |
51 for e=cmp(:)' | |
52 if ~approxeq(ll(ref), ll(e)) | |
53 error(['engine ' num2str(e) ' has wrong ll']) | |
54 end | |
55 end | |
56 end | |
57 if ~singletons_only | |
58 check_marginals(engine, hnodes, exact, 0, t); | |
59 end | |
60 check_marginals(engine, hnodes, exact, 1, t); | |
61 end | |
62 | |
63 | |
64 %%%%%%%%%% | |
65 | |
66 function check_marginals(engine, hnodes, exact, singletons, t) | |
67 | |
68 bnet = bnet_from_engine(engine{1}); | |
69 N = length(bnet.intra); | |
70 cnodes_bitv = zeros(1,N); | |
71 cnodes_bitv(bnet.cnodes) = 1; | |
72 ref = exact(1); % reference | |
73 cmp = exact(2:end); | |
74 E = length(engine); | |
75 m = cell(1,E); | |
76 | |
77 for n=1:N | |
78 %for n=hnodes(:)' | |
79 for e=1:E | |
80 if singletons | |
81 m{e} = marginal_nodes(engine{e}, n, t); | |
82 else | |
83 m{e} = marginal_family(engine{e}, n, t); | |
84 end | |
85 end | |
86 for e=cmp(:)' | |
87 assert(isequal(m{e}.domain, m{ref}.domain)); | |
88 if cnodes_bitv(n) & isfield(m{e}, 'mu') & isfield(m{ref}, 'mu') | |
89 wrong = ~approxeq(m{ref}.mu, m{e}.mu) | ~approxeq(m{ref}.Sigma, m{e}.Sigma); | |
90 else | |
91 wrong = ~approxeq(m{ref}.T(:), m{e}.T(:)); | |
92 end | |
93 if wrong | |
94 error(sprintf('engine %d is wrong; n=%d, t=%d, fam=%d', e, n, t, ~singletons)) | |
95 end | |
96 end | |
97 end |