% Compare particle filtering with other algos.

[intra, inter, node_sizes, onodes] = mk_water_topology;
%[intra, inter, node_sizes, onodes] = mk_bat_topology;
%[intra, inter, node_sizes, onodes] = mk_cancer_topology(5, 0);
ss = length(intra);
dnodes = 1:ss;
eclass = 1:(2*ss);
C = 4;
bnet = cell(1,C);
bnet{1} = mk_dbn(intra, inter, node_sizes, dnodes, eclass, onodes, 'bk', 'exact');
bnet{1}.filter_only = 1;

bnet{2} = mk_dbn(intra, inter, node_sizes, dnodes, eclass, onodes, 'bk', 'ff');
bnet{2}.filter_only = 1;

bnet{3} = mk_dbn(intra, inter, node_sizes, dnodes, eclass, onodes, 'sof');
bnet{3}.sof.num_samples = 50;
bnet{3}.sof.active = 0;
bnet{3}.useC = 1;

bnet{4} = mk_dbn(intra, inter, node_sizes, dnodes, eclass, onodes, 'sof');
bnet{4}.sof.num_samples = 2;
bnet{4}.sof.active = 0;
bnet{4}.useC = 1;

for c=1:C
  for i=1:length(bnet{1}.bnodes)
    bnet{c}.bnodes{i} = mk_tabular_node(bnet{c}, i);
  end
end

for c=1:C
  seed = 0;
  bnet{c} = randomize_params(bnet{c}, seed);
end
compile_sof(bnet{3}); % make C version


T = 5;
O = length(bnet{1}.observed_slice);
rand('seed',0);
%vals = sample_discrete([0.5 0.5], O, T);
vals = sample_dbn(bnet{1}, T);

for c=1:C
  rand('state',0);
  tic; bnet{c} = enter_evidence(bnet{c}, vals(onodes,:)); toc
end

i = 4;
p = zeros(C, T);
for  c=1:C
  for t=1:T
    marg = marginal_nodes(bnet{c}, i, t);
    p(c,t) = marg.T(2);
  end
end
p



m = cell(1, C);
i = 4;
t = 2;
for  c=1:C
  marg = marginal_family(bnet{c}, i, t);
  m{c} = marg.T(:)';
end
celldisp(m)
