wolffd@0
|
1 % Make a linear dynamical system
|
wolffd@0
|
2 % X1 -> X2
|
wolffd@0
|
3 % | |
|
wolffd@0
|
4 % v v
|
wolffd@0
|
5 % Y1 Y2
|
wolffd@0
|
6
|
wolffd@0
|
7 intra = zeros(2);
|
wolffd@0
|
8 intra(1,2) = 1;
|
wolffd@0
|
9 inter = zeros(2);
|
wolffd@0
|
10 inter(1,1) = 1;
|
wolffd@0
|
11 n = 2;
|
wolffd@0
|
12
|
wolffd@0
|
13 X = 2; % size of hidden state
|
wolffd@0
|
14 Y = 2; % size of observable state
|
wolffd@0
|
15
|
wolffd@0
|
16 ns = [X Y];
|
wolffd@0
|
17 dnodes = [];
|
wolffd@0
|
18 onodes = [2];
|
wolffd@0
|
19 eclass1 = [1 2];
|
wolffd@0
|
20 eclass2 = [3 2];
|
wolffd@0
|
21 bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'eclass1', eclass1, 'eclass2', eclass2, ...
|
wolffd@0
|
22 'observed', onodes);
|
wolffd@0
|
23
|
wolffd@0
|
24 x0 = rand(X,1);
|
wolffd@0
|
25 V0 = eye(X);
|
wolffd@0
|
26 C0 = rand(Y,X);
|
wolffd@0
|
27 R0 = eye(Y);
|
wolffd@0
|
28 A0 = rand(X,X);
|
wolffd@0
|
29 Q0 = eye(X);
|
wolffd@0
|
30
|
wolffd@0
|
31 bnet.CPD{1} = gaussian_CPD(bnet, 1, 'mean', x0, 'cov', V0, 'cov_prior_weight', 0);
|
wolffd@0
|
32 bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', zeros(Y,1), 'cov', R0, 'weights', C0, ...
|
wolffd@0
|
33 'clamp_mean', 1, 'cov_prior_weight', 0);
|
wolffd@0
|
34 bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', zeros(X,1), 'cov', Q0, 'weights', A0, ...
|
wolffd@0
|
35 'clamp_mean', 1, 'cov_prior_weight', 0);
|
wolffd@0
|
36
|
wolffd@0
|
37
|
wolffd@0
|
38 T = 5; % fixed length sequences
|
wolffd@0
|
39
|
wolffd@0
|
40 clear engine;
|
wolffd@0
|
41 engine{1} = kalman_inf_engine(bnet);
|
wolffd@0
|
42 engine{2} = jtree_unrolled_dbn_inf_engine(bnet, T);
|
wolffd@0
|
43 engine{3} = jtree_dbn_inf_engine(bnet);
|
wolffd@0
|
44 N = length(engine);
|
wolffd@0
|
45
|
wolffd@0
|
46 % inference
|
wolffd@0
|
47
|
wolffd@0
|
48 ev = sample_dbn(bnet, T);
|
wolffd@0
|
49 evidence = cell(n,T);
|
wolffd@0
|
50 evidence(onodes,:) = ev(onodes, :);
|
wolffd@0
|
51
|
wolffd@0
|
52 t = 1;
|
wolffd@0
|
53 query = [1 3];
|
wolffd@0
|
54 m = cell(1, N);
|
wolffd@0
|
55 ll = zeros(1, N);
|
wolffd@0
|
56 for i=1:N
|
wolffd@0
|
57 [engine{i}, ll(i)] = enter_evidence(engine{i}, evidence);
|
wolffd@0
|
58 m{i} = marginal_nodes(engine{i}, query, t);
|
wolffd@0
|
59 end
|
wolffd@0
|
60
|
wolffd@0
|
61 % compare all engines to engine{1}
|
wolffd@0
|
62 for i=2:N
|
wolffd@0
|
63 assert(approxeq(m{1}.mu, m{i}.mu));
|
wolffd@0
|
64 assert(approxeq(m{1}.Sigma, m{i}.Sigma));
|
wolffd@0
|
65 assert(approxeq(ll(1), ll(i)));
|
wolffd@0
|
66 end
|
wolffd@0
|
67
|
wolffd@0
|
68 if 0
|
wolffd@0
|
69 for i=2:N
|
wolffd@0
|
70 approxeq(m{1}.mu, m{i}.mu)
|
wolffd@0
|
71 approxeq(m{1}.Sigma, m{i}.Sigma)
|
wolffd@0
|
72 approxeq(ll(1), ll(i))
|
wolffd@0
|
73 end
|
wolffd@0
|
74 end
|
wolffd@0
|
75
|
wolffd@0
|
76 % learning
|
wolffd@0
|
77
|
wolffd@0
|
78 ncases = 5;
|
wolffd@0
|
79 cases = cell(1, ncases);
|
wolffd@0
|
80 for i=1:ncases
|
wolffd@0
|
81 ev = sample_dbn(bnet, T);
|
wolffd@0
|
82 cases{i} = cell(n,T);
|
wolffd@0
|
83 cases{i}(onodes,:) = ev(onodes, :);
|
wolffd@0
|
84 end
|
wolffd@0
|
85
|
wolffd@0
|
86 max_iter = 2;
|
wolffd@0
|
87 bnet2 = cell(1,N);
|
wolffd@0
|
88 LLtrace = cell(1,N);
|
wolffd@0
|
89 for i=1:N
|
wolffd@0
|
90 [bnet2{i}, LLtrace{i}] = learn_params_dbn_em(engine{i}, cases, 'max_iter', max_iter);
|
wolffd@0
|
91 end
|
wolffd@0
|
92
|
wolffd@0
|
93 for i=1:N
|
wolffd@0
|
94 temp = bnet2{i};
|
wolffd@0
|
95 for e=1:3
|
wolffd@0
|
96 CPD{i,e} = struct(temp.CPD{e});
|
wolffd@0
|
97 end
|
wolffd@0
|
98 end
|
wolffd@0
|
99
|
wolffd@0
|
100 for i=2:N
|
wolffd@0
|
101 assert(approxeq(LLtrace{i}, LLtrace{1}));
|
wolffd@0
|
102 for e=1:3
|
wolffd@0
|
103 assert(approxeq(CPD{i,e}.mean, CPD{1,e}.mean));
|
wolffd@0
|
104 assert(approxeq(CPD{i,e}.cov, CPD{1,e}.cov));
|
wolffd@0
|
105 assert(approxeq(CPD{i,e}.weights, CPD{1,e}.weights));
|
wolffd@0
|
106 end
|
wolffd@0
|
107 end
|
wolffd@0
|
108
|
wolffd@0
|
109
|
wolffd@0
|
110 % Compare to KF toolbox
|
wolffd@0
|
111
|
wolffd@0
|
112 data = zeros(Y, T, ncases);
|
wolffd@0
|
113 for i=1:ncases
|
wolffd@0
|
114 data(:,:,i) = cell2num(cases{i}(onodes, :));
|
wolffd@0
|
115 end
|
wolffd@0
|
116 [A2, C2, Q2, R2, x2, V2, LL2trace] = learn_kalman(data, A0, C0, Q0, R0, x0, V0, max_iter);
|
wolffd@0
|
117
|
wolffd@0
|
118
|
wolffd@0
|
119 e = 1;
|
wolffd@0
|
120 assert(approxeq(x2, CPD{e,1}.mean))
|
wolffd@0
|
121 assert(approxeq(V2, CPD{e,1}.cov))
|
wolffd@0
|
122 assert(approxeq(C2, CPD{e,2}.weights))
|
wolffd@0
|
123 assert(approxeq(R2, CPD{e,2}.cov));
|
wolffd@0
|
124 assert(approxeq(A2, CPD{e,3}.weights))
|
wolffd@0
|
125 assert(approxeq(Q2, CPD{e,3}.cov));
|
wolffd@0
|
126 assert(approxeq(LL2trace, LLtrace{1}))
|
wolffd@0
|
127
|