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