comparison toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/Old/scg_dbn.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 % to test whether scg inference engine can handl dynameic BN
2 % Make a linear dynamical system
3 % X1 -> X2
4 % | |
5 % v v
6 % Y1 Y2
7
8 intra = zeros(2);
9 intra(1,2) = 1;
10 inter = zeros(2);
11 inter(1,1) = 1;
12 n = 2;
13
14 X = 2; % size of hidden state
15 Y = 2; % size of observable state
16
17 ns = [X Y];
18 dnodes = [];
19 onodes = [2];
20 eclass1 = [1 2];
21 eclass2 = [3 2];
22 bnet = mk_dbn(intra, inter, ns, dnodes, eclass1, eclass2);
23
24 x0 = rand(X,1);
25 V0 = eye(X);
26 C0 = rand(Y,X);
27 R0 = eye(Y);
28 A0 = rand(X,X);
29 Q0 = eye(X);
30
31 bnet.CPD{1} = gaussian_CPD(bnet, 1, 'mean', x0, 'cov', V0);
32 %bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', zeros(Y,1), 'cov', R0, 'weights', C0, 'full', 'untied', 'clamped_mean');
33 %bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', zeros(X,1), 'cov', Q0, 'weights', A0, 'full', 'untied', 'clamped_mean');
34 bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', zeros(Y,1), 'cov', R0, 'weights', C0);
35 bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', zeros(X,1), 'cov', Q0, 'weights', A0);
36
37
38 T = 5; % fixed length sequences
39
40 clear engine;
41 %engine{1} = kalman_inf_engine(bnet, onodes);
42 engine{1} = scg_unrolled_dbn_inf_engine(bnet, T, onodes);
43 engine{2} = jtree_unrolled_dbn_inf_engine(bnet, T);
44
45 N = length(engine);
46
47 % inference
48
49 ev = sample_dbn(bnet, T);
50 evidence = cell(n,T);
51 evidence(onodes,:) = ev(onodes, :);
52
53 t = 2;
54 query = [1 3];
55 m = cell(1, N);
56 ll = zeros(1, N);
57
58 engine{1} = enter_evidence(engine{1}, evidence);
59 [engine{2}, ll(2)] = enter_evidence(engine{2}, evidence);
60 m{1} = marginal_nodes(engine{1}, query);
61 m{2} = marginal_nodes(engine{2}, query, t);
62
63
64 % compare all engines to engine{1}
65 for i=2:N
66 assert(approxeq(m{1}.mu, m{i}.mu));
67 assert(approxeq(m{1}.Sigma, m{i}.Sigma));
68 % assert(approxeq(ll(1), ll(i)));
69 end
70