% test Greedy Equivalence Search-Expectation Maximization Algorithm using
% Asia network
%cpdag
%draw_graph(resulting_bnet.dag);

clear all;
%rand('state', 0);
%randn('state', 0);

bnet=mk_asia_bnet ;
dag = bnet.dag;
N = length(dag);
ns = bnet.node_sizes;

nsamples = 1000
samplesM = cell(N, nsamples);
for i=1:nsamples
  samplesM(:,i) = sample_bnet(bnet);
end

hide = rand(N, nsamples) > 0.9;
[I,J]=find(hide);
for k=1:length(I)
  samplesM{I(k), J(k)} = [];
end

initial_dag = zeros(N,N);
initial_bnet = mk_bnet(initial_dag, ns);
%initial random parameters
for i=1:N
  initial_bnet.CPD{i} = tabular_CPD(initial_bnet, i, 'prior_type', 'dirichlet', 'dirichlet_weight', 0);
end

fprintf('\t- GES-EM algorithm : Greedy Equivalence Search-Expectation Maximization algorithm\n\n');

max_loop=30;
tmp=cputime;
[resulting_bnet, cpdag, BIC_score, nloop] = learn_struct_ges_EM(initial_bnet, samplesM, max_loop, 3);
%[resulting_bnet, order, BIC_score, nloop] = learn_struct_EM(initial_bnet, samplesM, max_loop)
tmp=cputime-tmp;

fprintf('\t- Expected BIC score of learned network: %8.3f \n',BIC_score);
fprintf('\t- Execution time : %3.2f seconds\n',tmp);
fprintf('\t- Iteration number : %d \n',nloop);
