comparison toolboxes/FullBNT-1.0.7/bnt/examples/static/cmp_inference_static.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_inference_static(bnet, engine, varargin)
2 % CMP_INFERENCE Compare several inference engines on a BN
3 % function [time, engine] = cmp_inference_static(bnet, engine, ...)
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 % maximize - 1 means we do max-propagation, 0 means sum-propagation [0]
13 % check_ll - 1 means we check that the log-likelihoods are correct [1]
14 % observed - list of the observed ndoes [ bnet.observed ]
15 % check_converged - list of loopy engines that should be checked for convergence [ [] ]
16 % If an engine has converged, it is added to the exact list.
17
18
19 % set default params
20 exact = 1:length(engine);
21 singletons_only = 0;
22 maximize = 0;
23 check_ll = 1;
24 observed = bnet.observed;
25 check_converged = [];
26
27 args = varargin;
28 nargs = length(args);
29 for i=1:2:nargs
30 switch args{i},
31 case 'exact', exact = args{i+1};
32 case 'singletons_only', singletons_only = args{i+1};
33 case 'maximize', maximize = args{i+1};
34 case 'check_ll', check_ll = args{i+1};
35 case 'observed', observed = args{i+1};
36 case 'check_converged', check_converged = args{i+1};
37 otherwise,
38 error(['unrecognized argument ' args{i}])
39 end
40 end
41
42 E = length(engine);
43 ref = exact(1); % reference
44
45 N = length(bnet.dag);
46 ev = sample_bnet(bnet);
47 evidence = cell(1,N);
48 evidence(observed) = ev(observed);
49 %celldisp(evidence(observed))
50
51 for i=1:E
52 tic;
53 if check_ll
54 [engine{i}, ll(i)] = enter_evidence(engine{i}, evidence, 'maximize', maximize);
55 else
56 engine{i} = enter_evidence(engine{i}, evidence, 'maximize', maximize);
57 end
58 time(i)=toc;
59 end
60
61 for i=check_converged(:)'
62 niter = loopy_converged(engine{i});
63 if niter > 0
64 fprintf('loopy engine %d converged in %d iterations\n', i, niter);
65 % exact = myunion(exact, i);
66 else
67 fprintf('loopy engine %d has not converged\n', i);
68 end
69 end
70
71 cmp = exact(2:end);
72 if check_ll
73 for i=cmp(:)'
74 assert(approxeq(ll(ref), ll(i)));
75 end
76 end
77
78 hnodes = mysetdiff(1:N, observed);
79
80 if ~singletons_only
81 get_marginals(engine, hnodes, exact, 0);
82 end
83 get_marginals(engine, hnodes, exact, 1);
84
85 %%%%%%%%%%
86
87 function get_marginals(engine, hnodes, exact, singletons)
88
89 bnet = bnet_from_engine(engine{1});
90 N = length(bnet.dag);
91 cnodes_bitv = zeros(1,N);
92 cnodes_bitv(bnet.cnodes) = 1;
93 ref = exact(1); % reference
94 cmp = exact(2:end);
95 E = length(engine);
96
97 for n=hnodes(:)'
98 for e=1:E
99 if singletons
100 m{e} = marginal_nodes(engine{e}, n);
101 else
102 m{e} = marginal_family(engine{e}, n);
103 end
104 end
105 for e=cmp(:)'
106 if cnodes_bitv(n)
107 assert(approxeq(m{ref}.mu, m{e}.mu))
108 assert(approxeq(m{ref}.Sigma, m{e}.Sigma))
109 else
110 assert(approxeq(m{ref}.T, m{e}.T))
111 end
112 assert(isequal(m{e}.domain, m{ref}.domain));
113 end
114 end