comparison toolboxes/FullBNT-1.0.7/bnt/inference/dynamic/@kalman_inf_engine/enter_evidence.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 function [engine, loglik] = enter_evidence(engine, evidence, varargin)
2 % ENTER_EVIDENCE Add the specified evidence to the network (kalman)
3 % [engine, loglik] = enter_evidence(engine, evidence, ...)
4 %
5 % evidence{i,t} = [] if if X(i,t) is hidden, and otherwise contains its observed value (scalar or column vector)
6 %
7 % The following optional arguments can be specified in the form of name/value pairs:
8 % [default value in brackets]
9 %
10 % maximize - if 1, does max-product (same as sum-product for Gaussians!), else sum-product [0]
11 % filter - if 1, do filtering, else smoothing [0]
12 %
13 % e.g., engine = enter_evidence(engine, ev, 'maximize', 1)
14
15 maximize = 0;
16 filter = 0;
17
18 % parse optional params
19 args = varargin;
20 nargs = length(args);
21 if nargs > 0
22 for i=1:2:nargs
23 switch args{i},
24 case 'maximize', maximize = args{i+1};
25 case 'filter', filter = args{i+1};
26 otherwise,
27 error(['invalid argument name ' args{i}]);
28 end
29 end
30 end
31
32 assert(~maximize);
33
34 bnet = bnet_from_engine(engine);
35 n = length(bnet.intra);
36 onodes = bnet.observed;
37 hnodes = mysetdiff(1:n, onodes);
38 T = size(evidence, 2);
39 ns = bnet.node_sizes;
40 O = sum(ns(onodes));
41 data = reshape(cat(1, evidence{onodes,:}), [O T]);
42
43 A = engine.trans_mat;
44 C = engine.obs_mat;
45 Q = engine.trans_cov;
46 R = engine.obs_cov;
47 init_x = engine.init_state;
48 init_V = engine.init_cov;
49
50 if filter
51 [x, V, VV, loglik] = kalman_filter(data, A, C, Q, R, init_x, init_V);
52 else
53 [x, V, VV, loglik] = kalman_smoother(data, A, C, Q, R, init_x, init_V);
54 end
55
56
57 % Wrap the posterior inside a potential, so it can be marginalized easily
58 engine.one_slice_marginal = cell(1,T);
59 engine.two_slice_marginal = cell(1,T);
60 ns(onodes) = 0;
61 ns(onodes+n) = 0;
62 ss = length(bnet.intra);
63 for t=1:T
64 dom = (1:n);
65 engine.one_slice_marginal{t} = mpot(dom+(t-1)*ss, ns(dom), 1, x(:,t), V(:,:,t));
66 end
67 % for t=1:T-1
68 % dom = (1:(2*n));
69 % mu = [x(:,t); x(:,t)];
70 % Sigma = [V(:,:,t) VV(:,:,t+1)';
71 % VV(:,:,t+1) V(:,:,t+1)];
72 % engine.two_slice_marginal{t} = mpot(dom+(t-1)*ss, ns(dom), 1, mu, Sigma);
73 % end
74 for t=2:T
75 %dom = (1:(2*n));
76 current_slice = hnodes;
77 next_slice = hnodes + ss;
78 dom = [current_slice next_slice];
79 mu = [x(:,t-1); x(:,t)];
80 Sigma = [V(:,:,t-1) VV(:,:,t)';
81 VV(:,:,t) V(:,:,t)];
82 engine.two_slice_marginal{t-1} = mpot(dom+(t-2)*ss, ns(dom), 1, mu, Sigma);
83 end