annotate 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
rev   line source
wolffd@0 1 % to test whether scg inference engine can handl dynameic BN
wolffd@0 2 % Make a linear dynamical system
wolffd@0 3 % X1 -> X2
wolffd@0 4 % | |
wolffd@0 5 % v v
wolffd@0 6 % Y1 Y2
wolffd@0 7
wolffd@0 8 intra = zeros(2);
wolffd@0 9 intra(1,2) = 1;
wolffd@0 10 inter = zeros(2);
wolffd@0 11 inter(1,1) = 1;
wolffd@0 12 n = 2;
wolffd@0 13
wolffd@0 14 X = 2; % size of hidden state
wolffd@0 15 Y = 2; % size of observable state
wolffd@0 16
wolffd@0 17 ns = [X Y];
wolffd@0 18 dnodes = [];
wolffd@0 19 onodes = [2];
wolffd@0 20 eclass1 = [1 2];
wolffd@0 21 eclass2 = [3 2];
wolffd@0 22 bnet = mk_dbn(intra, inter, ns, dnodes, eclass1, eclass2);
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);
wolffd@0 32 %bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', zeros(Y,1), 'cov', R0, 'weights', C0, 'full', 'untied', 'clamped_mean');
wolffd@0 33 %bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', zeros(X,1), 'cov', Q0, 'weights', A0, 'full', 'untied', 'clamped_mean');
wolffd@0 34 bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', zeros(Y,1), 'cov', R0, 'weights', C0);
wolffd@0 35 bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', zeros(X,1), 'cov', Q0, 'weights', A0);
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, onodes);
wolffd@0 42 engine{1} = scg_unrolled_dbn_inf_engine(bnet, T, onodes);
wolffd@0 43 engine{2} = jtree_unrolled_dbn_inf_engine(bnet, T);
wolffd@0 44
wolffd@0 45 N = length(engine);
wolffd@0 46
wolffd@0 47 % inference
wolffd@0 48
wolffd@0 49 ev = sample_dbn(bnet, T);
wolffd@0 50 evidence = cell(n,T);
wolffd@0 51 evidence(onodes,:) = ev(onodes, :);
wolffd@0 52
wolffd@0 53 t = 2;
wolffd@0 54 query = [1 3];
wolffd@0 55 m = cell(1, N);
wolffd@0 56 ll = zeros(1, N);
wolffd@0 57
wolffd@0 58 engine{1} = enter_evidence(engine{1}, evidence);
wolffd@0 59 [engine{2}, ll(2)] = enter_evidence(engine{2}, evidence);
wolffd@0 60 m{1} = marginal_nodes(engine{1}, query);
wolffd@0 61 m{2} = marginal_nodes(engine{2}, query, t);
wolffd@0 62
wolffd@0 63
wolffd@0 64 % compare all engines to engine{1}
wolffd@0 65 for i=2:N
wolffd@0 66 assert(approxeq(m{1}.mu, m{i}.mu));
wolffd@0 67 assert(approxeq(m{1}.Sigma, m{i}.Sigma));
wolffd@0 68 % assert(approxeq(ll(1), ll(i)));
wolffd@0 69 end
wolffd@0 70