clear
cd FullBNT-1.0.4
addpath(genpathKPM(pwd))
cd ..

%% Init BayesNet
clear

N = 5;
dag = zeros(N,N);
B=1; E=2; A=3; JC=4; MC=5;

dag(B,A) = 1;
dag(E,A) = 1;
dag(A,JC) = 1;
dag(A,MC) = 1;

discrete_nodes = 1:N;
node_sizes=2*ones(1,N);
bnet = mk_bnet(dag,node_sizes);

%% Define CPT:s
% 1 - False, 2 - True

% Burglary
CPT_B(1) = 1-0.001;
CPT_B(2) = 0.001;
bnet.CPD{B} = tabular_CPD(bnet, B, 'CPT', CPT_B );

% Earthquake
CPT_E(1) = 1-0.002;
CPT_E(2) = 0.002;
bnet.CPD{E} = tabular_CPD(bnet, E, 'CPT', CPT_E );

% Alarm
% Node order: B, E, A
CPT_A(1,1,1) = 1-0.001;
CPT_A(1,1,2) = 0.001;
CPT_A(1,2,1) = 1-0.29;
CPT_A(1,2,2) = 0.29;
CPT_A(2,1,1) = 1-0.94;
CPT_A(2,1,2) = 0.94;
CPT_A(2,2,1) = 1-0.95;
CPT_A(2,2,2) = 0.95;
bnet.CPD{A} = tabular_CPD(bnet, A, 'CPT', CPT_A );

% JohnCalls
% Node order: A, JC
CPT_JC(1,1) = 1-0.05;
CPT_JC(1,2) = 0.05;
CPT_JC(2,1) = 1-0.90;
CPT_JC(2,2) = 0.90;
bnet.CPD{JC} = tabular_CPD(bnet, JC, 'CPT', CPT_JC );

% MaryCalls
% Node order: A, MC
CPT_MC(1,1) = 1-0.01;
CPT_MC(1,2) = 0.01;
CPT_MC(2,1) = 1-0.70;
CPT_MC(2,2) = 0.70;
bnet.CPD{MC} = tabular_CPD(bnet, MC, 'CPT', CPT_MC );


%% Compute stuff
engine = jtree_inf_engine(bnet);

% Compute P(B|JC=True and MC=True) (example from page 505 in Russel Norvig)
ev = cell(1,N);
ev{JC} = 2; 
ev{MC} = 2; 
%ev{A} = []; % empty evidence, can be used when no evidence should be provided
[engine, loglik] = enter_evidence(engine, ev);

marg = marginal_nodes(engine, B); marg.T

