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