% Check most probable assignment

[intra, inter] = mk_fhmm_topology(1); % regular HMM
ss = 2;
node_sizes = [2 2];
obs_nodes = [2];
discrete_nodes = 1:ss;
equiv_class = [1 2 3 2];
bnet = mk_dbn(intra, inter, node_sizes, discrete_nodes, equiv_class, obs_nodes, 'jtree');
for i=1:length(bnet.bnodes)
  bnet.bnodes{i} = mk_tabular_node(bnet, i);
end
%bnet.bnodes{1}.CPT(:) = [1.0 0.0];
%bnet.bnodes{2}.CPT = [0.4 0.6; 0.6 0.4];
%bnet.bnodes{3}.CPT = [0.4 0.6; 0.6 0.4];
bnet = randomize_params(bnet, 2);
bnet = compile_params(bnet);
[HMM_prior, transmat, obsmat, obsmat1] = dbn_to_hmm(bnet);

T = 10;
data = sample_dhmm(HMM_prior, transmat, obsmat, 1, T);

this_obsmat = mk_dhmm_obs_mat(data, obsmat);

path = viterbi_path(HMM_prior, transmat, this_obsmat);

bnet.maximize = 1;
bnet = enter_evidence(bnet, data, obs_nodes);
bnet.maximize = 0;
path2 = bnet.mpa([1:2:(2*T)]);

assert(isequal(path, path2));

ss = bnet.slice_size;
[bnet, llj] = enter_evidence(bnet, reshape(bnet.mpa(:), [ss T]), 1:ss);
llv = prob_path(HMM_prior, transmat, this_obsmat, path);

assert(approxeq(llj, llv));

path
