Daniel@0
|
1 function [engine, loglik] = enter_evidence(engine, evidence, varargin)
|
Daniel@0
|
2 % ENTER_EVIDENCE Add the specified evidence to the network (ff)
|
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
|
Daniel@0
|
6 % column vector)
|
Daniel@0
|
7 %
|
Daniel@0
|
8 % The following optional arguments can be specified in the form of name/value pairs:
|
Daniel@0
|
9 % [default value in brackets]
|
Daniel@0
|
10 %
|
Daniel@0
|
11 % maximize - if 1, does max-product (not yet supported), else sum-product [0]
|
Daniel@0
|
12 % filter - if 1, do filtering, else smoothing [0]
|
Daniel@0
|
13 %
|
Daniel@0
|
14 % e.g., engine = enter_evidence(engine, ev, 'maximize', 1)
|
Daniel@0
|
15
|
Daniel@0
|
16 maximize = 0;
|
Daniel@0
|
17 filter = 0;
|
Daniel@0
|
18
|
Daniel@0
|
19 % parse optional params
|
Daniel@0
|
20 args = varargin;
|
Daniel@0
|
21 nargs = length(args);
|
Daniel@0
|
22 if nargs > 0
|
Daniel@0
|
23 for i=1:2:nargs
|
Daniel@0
|
24 switch args{i},
|
Daniel@0
|
25 case 'maximize', maximize = args{i+1};
|
Daniel@0
|
26 case 'filter', filter = args{i+1};
|
Daniel@0
|
27 otherwise,
|
Daniel@0
|
28 error(['invalid argument name ' args{i}]);
|
Daniel@0
|
29 end
|
Daniel@0
|
30 end
|
Daniel@0
|
31 end
|
Daniel@0
|
32
|
Daniel@0
|
33 assert(~maximize);
|
Daniel@0
|
34
|
Daniel@0
|
35
|
Daniel@0
|
36 [ss T] = size(evidence);
|
Daniel@0
|
37 observed = ~isemptycell(evidence);
|
Daniel@0
|
38 bnet = bnet_from_engine(engine);
|
Daniel@0
|
39 %pot_type = determine_pot_type(find(observed(:,1)), bnet.cnodes_slice, bnet.intra);
|
Daniel@0
|
40 pot_type = determine_pot_type(bnet, observed);
|
Daniel@0
|
41 % we assume we can use the same pot_type in all slices
|
Daniel@0
|
42
|
Daniel@0
|
43 CPDpot = convert_dbn_CPDs_to_pots(bnet, evidence, pot_type);
|
Daniel@0
|
44
|
Daniel@0
|
45 % Now convert CPDs on observed nodes to be potentials just on their parents
|
Daniel@0
|
46 assert(pot_type == 'd');
|
Daniel@0
|
47 onodes = bnet.observed(:);
|
Daniel@0
|
48 ns = bnet.node_sizes_slice;
|
Daniel@0
|
49 ns(onodes) = 1;
|
Daniel@0
|
50 for t=1:T
|
Daniel@0
|
51 for i=onodes
|
Daniel@0
|
52 p = parents(bnet.dag, i);
|
Daniel@0
|
53 %CPDpot{i,t} = set_domain_pot(CPDpot{i,t}, p); % leaves size too long
|
Daniel@0
|
54 temp = pot_to_marginal(CPDpot{i,t});
|
Daniel@0
|
55 CPDpot{i,t} = dpot(p, ns(p), temp.T); % assumes pot_type = d
|
Daniel@0
|
56 end
|
Daniel@0
|
57 end
|
Daniel@0
|
58
|
Daniel@0
|
59 [engine.marginals, engine.fwd, engine.back, loglik] = enter_soft_evidence(engine, CPDpot, observed, pot_type, filter);
|
Daniel@0
|
60
|
Daniel@0
|
61 engine.CPDpot = CPDpot;
|
Daniel@0
|
62 engine.filter = filter;
|