% Make the following network (from Jensen (1996) p84 fig 4.17)
%    1
%  / | \
% 2  3  4
% |  |  |
% 5  6  7
%  \/ \/
%  8   9
% where all arcs point downwards

N = 9;
dag = zeros(N);
dag(1,2)=1; dag(1,3)=1; dag(1,4)=1;
dag(2,5)=1; dag(3,6)=1; dag(4,7)=1;
dag(5,8)=1; dag(6,8)=1; dag(6,9)=1; dag(7,9) = 1;

node_sizes = twos(1, N);
%node_sizes = [2 3 3 3 2 2 2 2 2]; % makes the brute force checking slower
bnet = mk_bnet(dag, node_sizes);
for i=1:N
  bnet.bnodes{i} = mk_tabular_node(bnet, i);
end
bnet = compile_params(bnet);



% Check correctness of inference by comparing with brute force enumeration

obs_nodes = [1 8];
vals = [2 1];
[bnet, ll1] = enter_evidence(bnet, vals, obs_nodes);
m1 = marginal_family(bnet, 3);
[lik2, m2] = naive_discrete_marginal(bnet, family(bnet.adj_mat, 3));
ll2 = log(lik2);
assert(approxeq(ll1, ll2));
assert(approxeq(m1.T, m2));



% Watch EM increase likelihood

obs_nodes = [2 4 7];
num_train = 5;
training_set = sample_bnet(bnet, num_train);
bnet_hat = randomize_params(bnet, 0);
max_iter = 3;
bnet_hat = learn_params(bnet_hat, training_set(:,obs_nodes), obs_nodes, max_iter);



