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

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