annotate toolboxes/FullBNT-1.0.7/bnt/inference/dynamic/@hmm_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
rev   line source
wolffd@0 1 function [engine, loglik] = enter_evidence(engine, evidence, varargin)
wolffd@0 2 % ENTER_EVIDENCE Add the specified evidence to the network (hmm)
wolffd@0 3 % [engine, loglik] = enter_evidence(engine, evidence, ...)
wolffd@0 4 %
wolffd@0 5 % evidence{i,t} = [] if if X(i,t) is hidden, and otherwise contains its observed value (scalar or column vector)
wolffd@0 6 %
wolffd@0 7 % The following optional arguments can be specified in the form of name/value pairs:
wolffd@0 8 % [default value in brackets]
wolffd@0 9 %
wolffd@0 10 % maximize - if 1, does max-product (not yet supported), else sum-product [0]
wolffd@0 11 % filter - if 1, does filtering, else smoothing [0]
wolffd@0 12 % oneslice - 1 means only compute marginals on nodes within a single slice [0]
wolffd@0 13 %
wolffd@0 14 % e.g., engine = enter_evidence(engine, ev, 'maximize', 1)
wolffd@0 15
wolffd@0 16 maximize = 0;
wolffd@0 17 filter = 0;
wolffd@0 18 oneslice = 0;
wolffd@0 19
wolffd@0 20 % parse optional params
wolffd@0 21 args = varargin;
wolffd@0 22 nargs = length(args);
wolffd@0 23 if nargs > 0
wolffd@0 24 for i=1:2:nargs
wolffd@0 25 switch args{i},
wolffd@0 26 case 'maximize', maximize = args{i+1};
wolffd@0 27 case 'filter', filter = args{i+1};
wolffd@0 28 case 'oneslice', oneslice = args{i+1};
wolffd@0 29 otherwise,
wolffd@0 30 error(['invalid argument name ' args{i}]);
wolffd@0 31 end
wolffd@0 32 end
wolffd@0 33 end
wolffd@0 34
wolffd@0 35 [ss T] = size(evidence);
wolffd@0 36 engine.maximize = maximize;
wolffd@0 37 engine.evidence = evidence;
wolffd@0 38 bnet = bnet_from_engine(engine);
wolffd@0 39 engine.node_sizes = repmat(bnet.node_sizes_slice(:), [1 T]);
wolffd@0 40
wolffd@0 41 obs_bitv = ~isemptycell(evidence(:));
wolffd@0 42 bitv = reshape(obs_bitv, ss, T);
wolffd@0 43 for t=1:T
wolffd@0 44 onodes = find(bitv(:,t));
wolffd@0 45 if ~isequal(onodes, bnet.observed(:))
wolffd@0 46 error(['dbn was created assuming observed nodes per slice were '...
wolffd@0 47 num2str(bnet.observed(:)') ' but the evidence in slice ' num2str(t) ...
wolffd@0 48 ' has observed nodes ' num2str(onodes(:)')]);
wolffd@0 49 end
wolffd@0 50 end
wolffd@0 51
wolffd@0 52 obslik = mk_hmm_obs_lik_matrix(engine, evidence);
wolffd@0 53
wolffd@0 54 %[alpha, beta, gamma, loglik, xi] = fwdback(engine.startprob, engine.transprob, obslik, ...
wolffd@0 55 [alpha, beta, gamma, loglik, xi] = fwdback_twoslice(engine, engine.startprob,...
wolffd@0 56 engine.transprob, obslik, ...
wolffd@0 57 'maximize', maximize, 'fwd_only', filter, ...
wolffd@0 58 'compute_xi', ~oneslice);
wolffd@0 59
wolffd@0 60 engine.one_slice_marginal = gamma; % gamma(:,t) for t=1:T
wolffd@0 61 if ~oneslice
wolffd@0 62 Q = size(gamma,1);
wolffd@0 63 engine.two_slice_marginal = reshape(xi, [Q*Q T-1]); % xi(:,t) for t=1:T-1
wolffd@0 64 end