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