wolffd@0: % Check that online inference gives same results as filtering for various algorithms wolffd@0: wolffd@0: N = 3; wolffd@0: Q = 2; wolffd@0: ss = N*2; wolffd@0: wolffd@0: rand('state', 0); wolffd@0: randn('state', 0); wolffd@0: wolffd@0: wolffd@0: obs_size = 1; wolffd@0: discrete_obs = 0; wolffd@0: bnet = mk_chmm(N, Q, obs_size, discrete_obs); wolffd@0: ns = bnet.node_sizes_slice; wolffd@0: wolffd@0: engine = {}; wolffd@0: engine{end+1} = hmm_inf_engine(bnet); wolffd@0: E = length(engine); wolffd@0: wolffd@0: onodes = (1:N)+N; wolffd@0: wolffd@0: T = 4; wolffd@0: ev = cell(ss,T); wolffd@0: ev(onodes,:) = num2cell(randn(N, T)); wolffd@0: wolffd@0: wolffd@0: filter = 1; wolffd@0: loglik2 = zeros(1,E); wolffd@0: for e=1:E wolffd@0: [engine2{e}, loglik2(e)] = enter_evidence(engine{e}, ev, 'filter', filter); wolffd@0: end wolffd@0: wolffd@0: loglik = zeros(1,E); wolffd@0: marg1 = cell(E,N,T); wolffd@0: for e=1:E wolffd@0: ll = zeros(1,T); wolffd@0: engine{e} = dbn_init_bel(engine{e}); wolffd@0: for t=1:T wolffd@0: [engine{e}, ll(t)] = dbn_update_bel(engine{e}, ev(:,t), t); wolffd@0: for i=1:N wolffd@0: marg1{e,i,t} = dbn_marginal_from_bel(engine{e}, i); wolffd@0: end wolffd@0: end wolffd@0: loglik1(e) = sum(ll); wolffd@0: end wolffd@0: wolffd@0: assert(approxeq(loglik1, loglik2)) wolffd@0: wolffd@0: a = zeros(E,N,T); wolffd@0: for e=1:E wolffd@0: for t=1:T wolffd@0: for i=1:N wolffd@0: marg2{e,i,t} = marginal_nodes(engine2{e}, i, t); wolffd@0: a(e,i,t) = (approxeq(marg2{e,i,t}.T(:), marg1{e,i,t}.T(:))); wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: assert(all(a(:)==1))