% MIT MAS622j/1.126J Fall 2008, Problem Set 3 
%
% Conditional Dependence, Explaining Away
% (Burglary-->Alarm<---Earthquake example)
%
% 9/20/2008: Adapted from Kevin Murphy's sample script
%            by Hyungil Ahn (hiahn@media.mit.edu)

clear all
N = 3; 
dag = zeros(N,N);
B = 1; E = 2; A = 3; 
dag(B,A) = 1;
dag(E,A) = 1;

false= 1; true= 2; % by convention
ns = 2*ones(1,N); % binary nodes
names = {'B', 'E', 'A'};

% Set up the topology of the Bayes Net
bnet = mk_bnet(dag, ns, 'names', names, 'discrete', 1:N);

% Display the graph: download graph_draw from the link on the class webpage
graph_draw(bnet.dag, 'node_labels', names);

% Set up conditional probability tables for each node 
%
% Refer to http://www.cs.ubc.ca/~murphyk/Software/BNT/usage.html
%
% ### Fill in the row vectors with CPD values ###
bnet.CPD{B} = tabular_CPD(bnet, B, [ ]);
bnet.CPD{E} = tabular_CPD(bnet, E, [ ]);
bnet.CPD{A} = tabular_CPD(bnet, A, [ ]);

% Choose an inference engine
engine = jtree_inf_engine(bnet);

% First, compute P(A)
% (this is a working example for your convenience!)
evidence = cell(1,N);
[engine, ll] = enter_evidence(engine, evidence);

m = marginal_nodes(engine, A);
disp(sprintf('p(A) = %g', m.T(true))); 

% Second, compute P(B|A)
% ### Your code comes here! ###
% Hint:  Enter the observation that A = true.



% Third, compute P(B|E,A)
% ### Your code comes here! ###




% Fourth, compute P(E|A)
% ### Your code comes here! ###



% Fifth, compute P(E|B,A)
% ### Your code comes here! ###



