comparison toolboxes/FullBNT-1.0.7/bnt/examples/static/gibbs_test1.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 function gibbs_test1()
2
3 disp('gibbs test 1')
4
5 rand('state', 0);
6 randn('state', 0);
7
8 %[bnet onodes hnodes qnodes] = gibbs_ex_1;
9 [bnet onodes hnodes qnodes] = gibbs_ex_2;
10
11 je = jtree_inf_engine(bnet);
12 ge = gibbs_sampling_inf_engine (bnet, 'T', 50, 'burnin', 0, ...
13 'order', [2 2 1 2 1]);
14
15 ev = sample_bnet(bnet);
16
17 evidence = cell(length(bnet.dag), 1);
18 evidence(onodes) = ev(onodes);
19 [je lj] = enter_evidence(je, evidence);
20 [ge lg] = enter_evidence(ge, evidence);
21
22
23 mj = marginal_nodes(je, qnodes);
24
25 [mg ge] = marginal_nodes (ge, qnodes);
26 for t = 1:100
27 [mg ge] = marginal_nodes (ge, qnodes, 'reset_counts', 0);
28 diff = mj.T - mg.T;
29 err(t) = norm (diff(:), 1);
30 end
31 clf
32 plot(err);
33 %title('error vs num. Gibbs samples')
34
35
36 %%%%%%%
37
38 function [bnet, onodes, hnodes, qnodes] = gibbs_ex_1
39 % bnet = gibbs_ex_1
40 % a simple network to test the gibbs sampling engine
41 % 1
42 % / | \
43 % 2 3 4
44 % | | |
45 % 5 6 7
46 % \/ \/
47 % 8 9
48 % where all arcs point downwards
49
50 N = 9;
51 dag = zeros(N,N);
52 dag(1,2)=1; dag(1,3)=1; dag(1,4)=1;
53 dag(2,5)=1; dag(3,6)=1; dag(4,7)=1;
54 dag(5,8)=1; dag(6,8)=1; dag(6,9)=1; dag(7,9) = 1;
55
56 onodes = 8:9;
57 hnodes = 1:7;
58 qnodes = [1 2 6];
59 ns = [2 3 4 3 5 2 4 3 2];
60
61 eclass = [1 2 3 2 4 5 6 7 8];
62
63 bnet = mk_bnet (dag, ns, 'equiv_class', eclass);
64
65 for i = 1:3
66 bnet.CPD{i} = tabular_CPD(bnet, i);
67 end
68
69 for i = 4:8
70 bnet.CPD{i} = tabular_CPD(bnet, i+1);
71 end
72
73
74
75 %%%%%%%
76
77 function [bnet, onodes, hnodes, qnodes] = gibbs_ex_2
78 % bnet = gibbs_ex_2
79 % a very simple network
80 %
81 % 1 2
82 % \ /
83 % 3
84
85 N = 3;
86 dag = zeros(N,N);
87 dag(1,3)=1; dag(2,3)=1;
88
89 onodes = 3;
90 hnodes = 1:2;
91 qnodes = 1:2;
92 ns = [2 4 3];
93
94 eclass = [1 2 3];
95
96 bnet = mk_bnet (dag, ns, 'equiv_class', eclass);
97
98 for i = 1:3
99 bnet.CPD{i} = tabular_CPD(bnet, i);
100 end
101
102
103
104
105
106
107