annotate toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/Old/online1.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 % Check that online inference gives same results as filtering for various algorithms
wolffd@0 2
wolffd@0 3 N = 3;
wolffd@0 4 Q = 2;
wolffd@0 5 ss = N*2;
wolffd@0 6
wolffd@0 7 rand('state', 0);
wolffd@0 8 randn('state', 0);
wolffd@0 9
wolffd@0 10
wolffd@0 11 obs_size = 1;
wolffd@0 12 discrete_obs = 0;
wolffd@0 13 bnet = mk_chmm(N, Q, obs_size, discrete_obs);
wolffd@0 14 ns = bnet.node_sizes_slice;
wolffd@0 15
wolffd@0 16 engine = {};
wolffd@0 17 engine{end+1} = hmm_inf_engine(bnet);
wolffd@0 18 E = length(engine);
wolffd@0 19
wolffd@0 20 onodes = (1:N)+N;
wolffd@0 21
wolffd@0 22 T = 4;
wolffd@0 23 ev = cell(ss,T);
wolffd@0 24 ev(onodes,:) = num2cell(randn(N, T));
wolffd@0 25
wolffd@0 26
wolffd@0 27 filter = 1;
wolffd@0 28 loglik2 = zeros(1,E);
wolffd@0 29 for e=1:E
wolffd@0 30 [engine2{e}, loglik2(e)] = enter_evidence(engine{e}, ev, 'filter', filter);
wolffd@0 31 end
wolffd@0 32
wolffd@0 33 loglik = zeros(1,E);
wolffd@0 34 marg1 = cell(E,N,T);
wolffd@0 35 for e=1:E
wolffd@0 36 ll = zeros(1,T);
wolffd@0 37 engine{e} = dbn_init_bel(engine{e});
wolffd@0 38 for t=1:T
wolffd@0 39 [engine{e}, ll(t)] = dbn_update_bel(engine{e}, ev(:,t), t);
wolffd@0 40 for i=1:N
wolffd@0 41 marg1{e,i,t} = dbn_marginal_from_bel(engine{e}, i);
wolffd@0 42 end
wolffd@0 43 end
wolffd@0 44 loglik1(e) = sum(ll);
wolffd@0 45 end
wolffd@0 46
wolffd@0 47 assert(approxeq(loglik1, loglik2))
wolffd@0 48
wolffd@0 49 a = zeros(E,N,T);
wolffd@0 50 for e=1:E
wolffd@0 51 for t=1:T
wolffd@0 52 for i=1:N
wolffd@0 53 marg2{e,i,t} = marginal_nodes(engine2{e}, i, t);
wolffd@0 54 a(e,i,t) = (approxeq(marg2{e,i,t}.T(:), marg1{e,i,t}.T(:)));
wolffd@0 55 end
wolffd@0 56 end
wolffd@0 57 end
wolffd@0 58
wolffd@0 59 assert(all(a(:)==1))