annotate toolboxes/FullBNT-1.0.7/bnt/examples/static/StructLearn/model_select1.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 % Bayesian model selection demo.
Daniel@0 2
Daniel@0 3 % We generate data from the model A->B
Daniel@0 4 % and compute the posterior prob of all 3 dags on 2 nodes:
Daniel@0 5 % (1) A B, (2) A <- B , (3) A -> B
Daniel@0 6 % Models 2 and 3 are Markov equivalent, and therefore indistinguishable from
Daniel@0 7 % observational data alone.
Daniel@0 8 % Using the "difficult" params, the true model only gets a higher posterior after 2000 trials!
Daniel@0 9 % However, using the noisy NOT gate, the true model wins after 12 trials.
Daniel@0 10
Daniel@0 11 % ground truth
Daniel@0 12 N = 2;
Daniel@0 13 dag = zeros(N);
Daniel@0 14 A = 1; B = 2;
Daniel@0 15 dag(A,B) = 1;
Daniel@0 16
Daniel@0 17 difficult = 0;
Daniel@0 18 if difficult
Daniel@0 19 ntrials = 2000;
Daniel@0 20 ns = 3*ones(1,N);
Daniel@0 21 true_bnet = mk_bnet(dag, ns);
Daniel@0 22 rand('state', 0);
Daniel@0 23 temp = 5;
Daniel@0 24 for i=1:N
Daniel@0 25 %true_bnet.CPD{i} = tabular_CPD(true_bnet, i, temp);
Daniel@0 26 true_bnet.CPD{i} = tabular_CPD(true_bnet, i);
Daniel@0 27 end
Daniel@0 28 else
Daniel@0 29 ntrials = 25;
Daniel@0 30 ns = 2*ones(1,N);
Daniel@0 31 true_bnet = mk_bnet(dag, ns);
Daniel@0 32 true_bnet.CPD{1} = tabular_CPD(true_bnet, 1, [0.5 0.5]);
Daniel@0 33 pfail = 0.1;
Daniel@0 34 psucc = 1-pfail;
Daniel@0 35 true_bnet.CPD{2} = tabular_CPD(true_bnet, 2, [pfail psucc; psucc pfail]); % NOT gate
Daniel@0 36 end
Daniel@0 37
Daniel@0 38 G = mk_all_dags(N);
Daniel@0 39 nhyp = length(G);
Daniel@0 40 hyp_bnet = cell(1, nhyp);
Daniel@0 41 for h=1:nhyp
Daniel@0 42 hyp_bnet{h} = mk_bnet(G{h}, ns);
Daniel@0 43 for i=1:N
Daniel@0 44 % We must set the CPTs to the mean of the prior for sequential log_marg_lik to be correct
Daniel@0 45 % The BDeu prior is score equivalent, so models 2,3 will be indistinguishable.
Daniel@0 46 % The uniform Dirichlet prior is not score equivalent...
Daniel@0 47 fam = family(G{h}, i);
Daniel@0 48 hyp_bnet{h}.CPD{i}= tabular_CPD(hyp_bnet{h}, i, 'prior_type', 'dirichlet', ...
Daniel@0 49 'CPT', 'unif');
Daniel@0 50 end
Daniel@0 51 end
Daniel@0 52 prior = normalise(ones(1, nhyp));
Daniel@0 53
Daniel@0 54 % save results before doing sequential updating
Daniel@0 55 init_hyp_bnet = hyp_bnet;
Daniel@0 56 init_prior = prior;
Daniel@0 57
Daniel@0 58
Daniel@0 59 rand('state', 0);
Daniel@0 60 hyp_w = zeros(ntrials+1, nhyp);
Daniel@0 61 hyp_w(1,:) = prior(:)';
Daniel@0 62
Daniel@0 63 data = zeros(N, ntrials);
Daniel@0 64
Daniel@0 65 % First we compute the posteriors sequentially
Daniel@0 66
Daniel@0 67 LL = zeros(1, nhyp);
Daniel@0 68 ll = zeros(1, nhyp);
Daniel@0 69 for t=1:ntrials
Daniel@0 70 ev = cell2num(sample_bnet(true_bnet));
Daniel@0 71 data(:,t) = ev;
Daniel@0 72 for i=1:nhyp
Daniel@0 73 ll(i) = log_marg_lik_complete(hyp_bnet{i}, ev);
Daniel@0 74 hyp_bnet{i} = bayes_update_params(hyp_bnet{i}, ev);
Daniel@0 75 end
Daniel@0 76 prior = normalise(prior .* exp(ll));
Daniel@0 77 LL = LL + ll;
Daniel@0 78 hyp_w(t+1,:) = prior;
Daniel@0 79 end
Daniel@0 80
Daniel@0 81 % Plot posterior model probabilities
Daniel@0 82 % Red = model 1 (no arcs), blue/green = models 2/3 (1 arc)
Daniel@0 83 % Blue = model 2 (2->1)
Daniel@0 84 % Green = model 3 (1->2, "ground truth")
Daniel@0 85
Daniel@0 86 if 1
Daniel@0 87 figure;
Daniel@0 88 m = size(hyp_w, 1);
Daniel@0 89 h=plot(1:m, hyp_w(:,1), 'r-', 1:m, hyp_w(:,2), 'b-.', 1:m, hyp_w(:,3), 'g:');
Daniel@0 90 axis([0 m 0 1])
Daniel@0 91 title('model posterior vs. time')
Daniel@0 92 %previewfig(gcf, 'format', 'png', 'height', 2, 'color', 'rgb')
Daniel@0 93 %exportfig(gcf, '/home/cs/murphyk/public_html/Bayes/Figures/model_select.png',...
Daniel@0 94 %'format', 'png', 'height', 2, 'color', 'rgb')
Daniel@0 95 drawnow
Daniel@0 96 end
Daniel@0 97
Daniel@0 98
Daniel@0 99 % Now check that batch updating gives same result
Daniel@0 100 hyp_bnet2 = init_hyp_bnet;
Daniel@0 101 prior2 = init_prior;
Daniel@0 102
Daniel@0 103 cases = num2cell(data);
Daniel@0 104 LL2 = zeros(1, nhyp);
Daniel@0 105 for i=1:nhyp
Daniel@0 106 LL2(i) = log_marg_lik_complete(hyp_bnet2{i}, cases);
Daniel@0 107 hyp_bnet2{i} = bayes_update_params(hyp_bnet2{i}, cases);
Daniel@0 108 end
Daniel@0 109
Daniel@0 110
Daniel@0 111 assert(approxeq(LL, LL2))
Daniel@0 112 LL
Daniel@0 113
Daniel@0 114 for i=1:nhyp
Daniel@0 115 for j=1:N
Daniel@0 116 s1 = struct(hyp_bnet{i}.CPD{j});
Daniel@0 117 s2 = struct(hyp_bnet2{i}.CPD{j});
Daniel@0 118 assert(approxeq(s1.CPT, s2.CPT))
Daniel@0 119 end
Daniel@0 120 end
Daniel@0 121