annotate toolboxes/FullBNT-1.0.7/bnt/examples/static/StructLearn/cooper_yoo.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 % Do the example in Cooper and Yoo, "Causal discovery from a mixture of experimental and
wolffd@0 2 % observational data", UAI 99, p120
wolffd@0 3
wolffd@0 4 N = 2;
wolffd@0 5 dag = zeros(N);
wolffd@0 6 A = 1; B = 2;
wolffd@0 7 dag(A,B) = 1;
wolffd@0 8 ns = 2*ones(1,N);
wolffd@0 9
wolffd@0 10 bnet0 = mk_bnet(dag, ns);
wolffd@0 11 %bnet0.CPD{A} = tabular_CPD(bnet0, A, 'unif', 1);
wolffd@0 12 bnet0.CPD{A} = tabular_CPD(bnet0, A, 'CPT', 'unif', 'prior_type', 'dirichlet');
wolffd@0 13 bnet0.CPD{B} = tabular_CPD(bnet0, B, 'CPT', 'unif', 'prior_type', 'dirichlet');
wolffd@0 14
wolffd@0 15 samples = [2 2;
wolffd@0 16 2 1;
wolffd@0 17 2 2;
wolffd@0 18 1 1;
wolffd@0 19 1 2;
wolffd@0 20 2 2;
wolffd@0 21 1 1;
wolffd@0 22 2 2;
wolffd@0 23 1 2;
wolffd@0 24 2 1;
wolffd@0 25 1 1];
wolffd@0 26
wolffd@0 27 clamped = [0 0;
wolffd@0 28 0 0;
wolffd@0 29 0 0;
wolffd@0 30 0 0;
wolffd@0 31 0 0;
wolffd@0 32 1 0;
wolffd@0 33 1 0;
wolffd@0 34 0 1;
wolffd@0 35 0 1;
wolffd@0 36 0 1;
wolffd@0 37 0 1];
wolffd@0 38
wolffd@0 39 nsamples = size(samples, 1);
wolffd@0 40
wolffd@0 41 % sequential version
wolffd@0 42 LL = 0;
wolffd@0 43 bnet = bnet0;
wolffd@0 44 for l=1:nsamples
wolffd@0 45 ev = num2cell(samples(l,:)');
wolffd@0 46 manip = find(clamped(l,:)');
wolffd@0 47 LL = LL + log_marg_lik_complete(bnet, ev, manip);
wolffd@0 48 bnet = bayes_update_params(bnet, ev, manip);
wolffd@0 49 end
wolffd@0 50 assert(approxeq(exp(LL), 5.97e-7)) % compare with result from UAI paper
wolffd@0 51
wolffd@0 52
wolffd@0 53 % batch version
wolffd@0 54 cases = num2cell(samples');
wolffd@0 55 LL2 = log_marg_lik_complete(bnet0, cases, clamped');
wolffd@0 56 bnet2 = bayes_update_params(bnet0, cases, clamped');
wolffd@0 57
wolffd@0 58 assert(approxeq(LL, LL2))
wolffd@0 59
wolffd@0 60 for j=1:N
wolffd@0 61 s1 = struct(bnet.CPD{j}); % violate object privacy
wolffd@0 62 s2 = struct(bnet2.CPD{j});
wolffd@0 63 assert(approxeq(s1.CPT, s2.CPT))
wolffd@0 64 end
wolffd@0 65