% Test noisy-OR
% A -> B

N = 2;
dag = zeros(N);
dag(1,2) = 1;
node_sizes = twos(1, N);
bnet = mk_bnet(dag, node_sizes);
for i=1:N
  bnet.bnodes{i} = mk_noisyor_node(bnet, i);
end
bnet = compile_params(bnet);


% Check correctness of inference

SL = bnet.bnodes{2}.inhibit(1);
SA = bnet.bnodes{2}.inhibit(2);

obs_nodes = [1];
vals = [2];
[bnet, ll1] = enter_evidence(bnet, vals, obs_nodes);
m1 = marginal_nodes(bnet, 2);
% Pr(B=0|L=1,A=1) = prob. both are suppressed
p0 = SL*SA;
assert(approxeq(p0, m1.T(1)));

obs_nodes = [1];
vals = [1];
[bnet, ll1] = enter_evidence(bnet, vals, obs_nodes);
m1 = marginal_nodes(bnet, 2);
% Pr(B=0|L=1,A=1) = prob. leak is suppressed
p0 = SL;
assert(approxeq(p0, m1.T(1)));



% Check "correctness" of learning by watching EM increase likelihood

num_train = 5;
training_set = sample_bnet(bnet, num_train);

bnet_hat = randomize_params(bnet, 0);
%bnet_hat = bnet; % cheat
max_iter = 4;
obs_nodes = 2;
bnet_hat = learn_params(bnet, training_set(:,obs_nodes), obs_nodes, max_iter);


