comparison 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
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 (hmm)
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 (not yet supported), else sum-product [0]
11 % filter - if 1, does filtering, else smoothing [0]
12 % oneslice - 1 means only compute marginals on nodes within a single slice [0]
13 %
14 % e.g., engine = enter_evidence(engine, ev, 'maximize', 1)
15
16 maximize = 0;
17 filter = 0;
18 oneslice = 0;
19
20 % parse optional params
21 args = varargin;
22 nargs = length(args);
23 if nargs > 0
24 for i=1:2:nargs
25 switch args{i},
26 case 'maximize', maximize = args{i+1};
27 case 'filter', filter = args{i+1};
28 case 'oneslice', oneslice = args{i+1};
29 otherwise,
30 error(['invalid argument name ' args{i}]);
31 end
32 end
33 end
34
35 [ss T] = size(evidence);
36 engine.maximize = maximize;
37 engine.evidence = evidence;
38 bnet = bnet_from_engine(engine);
39 engine.node_sizes = repmat(bnet.node_sizes_slice(:), [1 T]);
40
41 obs_bitv = ~isemptycell(evidence(:));
42 bitv = reshape(obs_bitv, ss, T);
43 for t=1:T
44 onodes = find(bitv(:,t));
45 if ~isequal(onodes, bnet.observed(:))
46 error(['dbn was created assuming observed nodes per slice were '...
47 num2str(bnet.observed(:)') ' but the evidence in slice ' num2str(t) ...
48 ' has observed nodes ' num2str(onodes(:)')]);
49 end
50 end
51
52 obslik = mk_hmm_obs_lik_matrix(engine, evidence);
53
54 %[alpha, beta, gamma, loglik, xi] = fwdback(engine.startprob, engine.transprob, obslik, ...
55 [alpha, beta, gamma, loglik, xi] = fwdback_twoslice(engine, engine.startprob,...
56 engine.transprob, obslik, ...
57 'maximize', maximize, 'fwd_only', filter, ...
58 'compute_xi', ~oneslice);
59
60 engine.one_slice_marginal = gamma; % gamma(:,t) for t=1:T
61 if ~oneslice
62 Q = size(gamma,1);
63 engine.two_slice_marginal = reshape(xi, [Q*Q T-1]); % xi(:,t) for t=1:T-1
64 end