annotate toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/skf_data_assoc_gmux.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 % We consider a switching Kalman filter of the kind studied
Daniel@0 2 % by Zoubin Ghahramani, i.e., where the switch node determines
Daniel@0 3 % which of the hidden chains we get to observe (data association).
Daniel@0 4 % e.g., for n=2 chains
Daniel@0 5 %
Daniel@0 6 % X1 -> X1
Daniel@0 7 % | X2 -> X2
Daniel@0 8 % \ |
Daniel@0 9 % v
Daniel@0 10 % Y
Daniel@0 11 % ^
Daniel@0 12 % |
Daniel@0 13 % S
Daniel@0 14 %
Daniel@0 15 % Y is a gmux (multiplexer) node, where S switches in one of the parents.
Daniel@0 16 % We differ from Zoubin by not connecting the S nodes over time (which
Daniel@0 17 % doesn't make sense for data association).
Daniel@0 18 % Indeed, we assume the S nodes are always observed.
Daniel@0 19 %
Daniel@0 20 %
Daniel@0 21 % We will track 2 objects (points) moving in the plane, as in BNT/Kalman/tracking_demo.
Daniel@0 22 % We will alternate between observing them.
Daniel@0 23
Daniel@0 24 nobj = 2;
Daniel@0 25 N = nobj+2;
Daniel@0 26 Xs = 1:nobj;
Daniel@0 27 S = nobj+1;
Daniel@0 28 Y = nobj+2;
Daniel@0 29
Daniel@0 30 intra = zeros(N,N);
Daniel@0 31 inter = zeros(N,N);
Daniel@0 32 intra([Xs S], Y) =1;
Daniel@0 33 for i=1:nobj
Daniel@0 34 inter(Xs(i), Xs(i))=1;
Daniel@0 35 end
Daniel@0 36
Daniel@0 37 Xsz = 4; % state space = (x y xdot ydot)
Daniel@0 38 Ysz = 2;
Daniel@0 39 ns = zeros(1,N);
Daniel@0 40 ns(Xs) = Xsz;
Daniel@0 41 ns(Y) = Ysz;
Daniel@0 42 ns(S) = n;
Daniel@0 43
Daniel@0 44 bnet = mk_dbn(intra, inter, ns, 'discrete', S, 'observed', [S Y]);
Daniel@0 45
Daniel@0 46 % For each object, we have
Daniel@0 47 % X(t+1) = F X(t) + noise(Q)
Daniel@0 48 % Y(t) = H X(t) + noise(R)
Daniel@0 49 F = [1 0 1 0; 0 1 0 1; 0 0 1 0; 0 0 0 1];
Daniel@0 50 H = [1 0 0 0; 0 1 0 0];
Daniel@0 51 Q = 1e-3*eye(Xsz);
Daniel@0 52 %R = 1e-3*eye(Ysz);
Daniel@0 53 R = eye(Ysz);
Daniel@0 54
Daniel@0 55 % We initialise object 1 moving to the right, and object 2 moving to the left
Daniel@0 56 % (Here, we assume nobj=2)
Daniel@0 57 init_state{1} = [10 10 1 0]';
Daniel@0 58 init_state{2} = [10 -10 -1 0]';
Daniel@0 59
Daniel@0 60 for i=1:nobj
Daniel@0 61 bnet.CPD{Xs(i)} = gaussian_CPD(bnet, Xs(i), 'mean', init_state{i}, 'cov', 1e-4*eye(Xsz));
Daniel@0 62 end
Daniel@0 63 bnet.CPD{S} = root_CPD(bnet, S); % always observed
Daniel@0 64 bnet.CPD{Y} = gmux_CPD(bnet, Y, 'cov', repmat(R, [1 1 nobj]), 'weights', repmat(H, [1 1 nobj]));
Daniel@0 65 % slice 2
Daniel@0 66 eclass = bnet.equiv_class;
Daniel@0 67 for i=1:nobj
Daniel@0 68 bnet.CPD{eclass(Xs(i), 2)} = gaussian_CPD(bnet, Xs(i)+N, 'mean', zeros(Xsz,1), 'cov', Q, 'weights', F);
Daniel@0 69 end
Daniel@0 70
Daniel@0 71 % Observe objects at random
Daniel@0 72 T = 10;
Daniel@0 73 evidence = cell(N, T);
Daniel@0 74 data_assoc = sample_discrete(normalise(ones(1,nobj)), 1, T);
Daniel@0 75 evidence(S,:) = num2cell(data_assoc);
Daniel@0 76 evidence = sample_dbn(bnet, 'evidence', evidence);
Daniel@0 77
Daniel@0 78 % plot the data
Daniel@0 79 true_state = cell(1,nobj);
Daniel@0 80 for i=1:nobj
Daniel@0 81 true_state{i} = cell2num(evidence(Xs(i), :)); % true_state{i}(:,t) = [x y xdot ydot]'
Daniel@0 82 end
Daniel@0 83 obs_pos = cell2num(evidence(Y,:));
Daniel@0 84 figure(1)
Daniel@0 85 clf
Daniel@0 86 hold on
Daniel@0 87 styles = {'rx', 'go', 'b+', 'k*'};
Daniel@0 88 for i=1:nobj
Daniel@0 89 plot(true_state{i}(1,:), true_state{i}(2,:), styles{i});
Daniel@0 90 end
Daniel@0 91 for t=1:T
Daniel@0 92 text(obs_pos(1,t), obs_pos(2,t), sprintf('%d', t));
Daniel@0 93 end
Daniel@0 94 hold off
Daniel@0 95 relax_axes(0.1)
Daniel@0 96
Daniel@0 97
Daniel@0 98 % Inference
Daniel@0 99 ev = cell(N,T);
Daniel@0 100 ev(bnet.observed,:) = evidence(bnet.observed, :);
Daniel@0 101
Daniel@0 102 engines = {};
Daniel@0 103 engines{end+1} = jtree_dbn_inf_engine(bnet);
Daniel@0 104 %engines{end+1} = scg_unrolled_dbn_inf_engine(bnet, T);
Daniel@0 105 engines{end+1} = pearl_unrolled_dbn_inf_engine(bnet);
Daniel@0 106 E = length(engines);
Daniel@0 107
Daniel@0 108 inferred_state = cell(nobj,E); % inferred_state{i,e}(:,t)
Daniel@0 109 for e=1:E
Daniel@0 110 engines{e} = enter_evidence(engines{e}, ev);
Daniel@0 111 for i=1:nobj
Daniel@0 112 inferred_state{i,e} = zeros(4, T);
Daniel@0 113 for t=1:T
Daniel@0 114 m = marginal_nodes(engines{e}, Xs(i), t);
Daniel@0 115 inferred_state{i,e}(:,t) = m.mu;
Daniel@0 116 end
Daniel@0 117 end
Daniel@0 118 end
Daniel@0 119 inferred_state{1,1}
Daniel@0 120 inferred_state{1,2}
Daniel@0 121
Daniel@0 122 % Plot results
Daniel@0 123 figure(2)
Daniel@0 124 clf
Daniel@0 125 hold on
Daniel@0 126 styles = {'rx', 'go', 'b+', 'k*'};
Daniel@0 127 nstyles = length(styles);
Daniel@0 128 c = 1;
Daniel@0 129 for e=1:E
Daniel@0 130 for i=1:nobj
Daniel@0 131 plot(inferred_state{i,e}(1,:), inferred_state{i,e}(2,:), styles{mod(c-1,nstyles)+1});
Daniel@0 132 c = c + 1;
Daniel@0 133 end
Daniel@0 134 end
Daniel@0 135 for t=1:T
Daniel@0 136 text(obs_pos(1,t), obs_pos(2,t), sprintf('%d', t));
Daniel@0 137 end
Daniel@0 138 hold off
Daniel@0 139 relax_axes(0.1)