wolffd@0: function [time, engine] = cmp_online_inference(bnet, engine, T, varargin) wolffd@0: % CMP_ONLINE_INFERENCE Compare several online inference engines on a DBN wolffd@0: % function [time, engine] = cmp_online_inference(bnet, engine, T, ...) wolffd@0: % wolffd@0: % engine{i} is the i'th inference engine. wolffd@0: % time(e) = elapsed time for doing inference with engine e wolffd@0: % wolffd@0: % The list below gives optional arguments [default value in brackets]. wolffd@0: % wolffd@0: % exact - specifies which engines do exact inference [ 1:length(engine) ] wolffd@0: % singletons_only - if 1, we only call marginal_nodes, else this and marginal_family [0] wolffd@0: % check_ll - 1 means we check that the log-likelihoods are correct [1] wolffd@0: wolffd@0: % set default params wolffd@0: exact = 1:length(engine); wolffd@0: singletons_only = 0; wolffd@0: check_ll = 1; wolffd@0: onodes = bnet.observed; wolffd@0: wolffd@0: args = varargin; wolffd@0: nargs = length(args); wolffd@0: for i=1:2:nargs wolffd@0: switch args{i}, wolffd@0: case 'exact', exact = args{i+1}; wolffd@0: case 'singletons_only', singletons_only = args{i+1}; wolffd@0: case 'check_ll', check_ll = args{i+1}; wolffd@0: case 'observed', onodes = args{i+1}; wolffd@0: otherwise, wolffd@0: error(['unrecognized argument ' args{i}]) wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: E = length(engine); wolffd@0: ref = exact(1); % reference wolffd@0: cmp = mysetdiff(exact, ref); wolffd@0: wolffd@0: ss = length(bnet.intra); wolffd@0: hnodes = mysetdiff(1:ss, onodes); wolffd@0: ev = sample_dbn(bnet, 'length', T); wolffd@0: evidence = cell(ss,T); wolffd@0: evidence(onodes,:) = ev(onodes, :); wolffd@0: wolffd@0: time = zeros(1,E); wolffd@0: for t=1:T wolffd@0: for e=1:E wolffd@0: tic; wolffd@0: [engine{e}, ll(e)] = enter_evidence(engine{e}, evidence(:,t), t); wolffd@0: time(e)= time(e) + toc; wolffd@0: end wolffd@0: if check_ll wolffd@0: for e=cmp(:)' wolffd@0: if ~approxeq(ll(ref), ll(e)) wolffd@0: error(['engine ' num2str(e) ' has wrong ll']) wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: if ~singletons_only wolffd@0: check_marginals(engine, hnodes, exact, 0, t); wolffd@0: end wolffd@0: check_marginals(engine, hnodes, exact, 1, t); wolffd@0: end wolffd@0: wolffd@0: wolffd@0: %%%%%%%%%% wolffd@0: wolffd@0: function check_marginals(engine, hnodes, exact, singletons, t) wolffd@0: wolffd@0: bnet = bnet_from_engine(engine{1}); wolffd@0: N = length(bnet.intra); wolffd@0: cnodes_bitv = zeros(1,N); wolffd@0: cnodes_bitv(bnet.cnodes) = 1; wolffd@0: ref = exact(1); % reference wolffd@0: cmp = exact(2:end); wolffd@0: E = length(engine); wolffd@0: m = cell(1,E); wolffd@0: wolffd@0: for n=1:N wolffd@0: %for n=hnodes(:)' wolffd@0: for e=1:E wolffd@0: if singletons wolffd@0: m{e} = marginal_nodes(engine{e}, n, t); wolffd@0: else wolffd@0: m{e} = marginal_family(engine{e}, n, t); wolffd@0: end wolffd@0: end wolffd@0: for e=cmp(:)' wolffd@0: assert(isequal(m{e}.domain, m{ref}.domain)); wolffd@0: if cnodes_bitv(n) & isfield(m{e}, 'mu') & isfield(m{ref}, 'mu') wolffd@0: wrong = ~approxeq(m{ref}.mu, m{e}.mu) | ~approxeq(m{ref}.Sigma, m{e}.Sigma); wolffd@0: else wolffd@0: wrong = ~approxeq(m{ref}.T(:), m{e}.T(:)); wolffd@0: end wolffd@0: if wrong wolffd@0: error(sprintf('engine %d is wrong; n=%d, t=%d, fam=%d', e, n, t, ~singletons)) wolffd@0: end wolffd@0: end wolffd@0: end