Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/kalman1.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 % Make a linear dynamical system | |
2 % X1 -> X2 | |
3 % | | | |
4 % v v | |
5 % Y1 Y2 | |
6 | |
7 intra = zeros(2); | |
8 intra(1,2) = 1; | |
9 inter = zeros(2); | |
10 inter(1,1) = 1; | |
11 n = 2; | |
12 | |
13 X = 2; % size of hidden state | |
14 Y = 2; % size of observable state | |
15 ns = [X Y]; | |
16 bnet = mk_dbn(intra, inter, ns, 'discrete', [], 'observed', 2); | |
17 | |
18 x0 = rand(X,1); | |
19 V0 = eye(X); | |
20 C0 = rand(Y,X); | |
21 R0 = eye(Y); | |
22 A0 = rand(X,X); | |
23 Q0 = eye(X); | |
24 | |
25 bnet.CPD{1} = gaussian_CPD(bnet, 1, 'mean', x0, 'cov', V0, 'cov_prior_weight', 0); | |
26 bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', zeros(Y,1), 'cov', R0, 'weights', C0, ... | |
27 'clamp_mean', 1, 'cov_prior_weight', 0); | |
28 bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', zeros(X,1), 'cov', Q0, 'weights', A0, ... | |
29 'clamp_mean', 1, 'cov_prior_weight', 0); | |
30 | |
31 | |
32 T = 5; % fixed length sequences | |
33 | |
34 clear engine; | |
35 engine{1} = kalman_inf_engine(bnet); | |
36 engine{2} = jtree_unrolled_dbn_inf_engine(bnet, T); | |
37 engine{3} = jtree_dbn_inf_engine(bnet); | |
38 engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet)); | |
39 N = length(engine); | |
40 | |
41 | |
42 inf_time = cmp_inference_dbn(bnet, engine, T); | |
43 | |
44 ncases = 2; | |
45 max_iter = 2; | |
46 [learning_time, CPD, LL, cases] = cmp_learning_dbn(bnet, engine, T, 'ncases', ncases, 'max_iter', max_iter); | |
47 | |
48 | |
49 % Compare to KF toolbox | |
50 | |
51 data = zeros(Y, T, ncases); | |
52 for i=1:ncases | |
53 data(:,:,i) = cell2num(cases{i}(onodes, :)); | |
54 end | |
55 [A2, C2, Q2, R2, x2, V2, LL2trace] = learn_kalman(data, A0, C0, Q0, R0, x0, V0, max_iter); | |
56 | |
57 | |
58 e = 1; | |
59 assert(approxeq(x2, CPD{e,1}.mean)) | |
60 assert(approxeq(V2, CPD{e,1}.cov)) | |
61 assert(approxeq(C2, CPD{e,2}.weights)) | |
62 assert(approxeq(R2, CPD{e,2}.cov)); | |
63 assert(approxeq(A2, CPD{e,3}.weights)) | |
64 assert(approxeq(Q2, CPD{e,3}.cov)); | |
65 assert(approxeq(LL2trace, LL{1})) | |
66 |