wolffd@0
|
1 function [mpe, ll] = calc_mpe(engine, evidence, break_ties)
|
wolffd@0
|
2 % CALC_MPE Computes the most probable explanation of the evidence
|
wolffd@0
|
3 % [mpe, ll] = calc_mpe_given_inf_engine(engine, evidence, break_ties)
|
wolffd@0
|
4 %
|
wolffd@0
|
5 % INPUT
|
wolffd@0
|
6 % engine must support max-propagation
|
wolffd@0
|
7 % evidence{i} is the observed value of node i, or [] if hidden
|
wolffd@0
|
8 % break_ties is optional. If 1, we will force ties to be broken consistently
|
wolffd@0
|
9 % by calling enter_evidence N times.
|
wolffd@0
|
10 %
|
wolffd@0
|
11 % OUTPUT
|
wolffd@0
|
12 % mpe{i} is the most likely value of node i (cell array!)
|
wolffd@0
|
13 % ll is the log-likelihood of the globally best assignment
|
wolffd@0
|
14 %
|
wolffd@0
|
15 % This currently only works when all hidden nodes are discrete
|
wolffd@0
|
16
|
wolffd@0
|
17 if nargin < 3, break_ties = 0; end
|
wolffd@0
|
18
|
wolffd@0
|
19
|
wolffd@0
|
20 [engine, ll] = enter_evidence(engine, evidence, 'maximize', 1);
|
wolffd@0
|
21
|
wolffd@0
|
22 observed = ~isemptycell(evidence);
|
wolffd@0
|
23
|
wolffd@0
|
24 if 0 % fgraphs don't support bnet_from_engine
|
wolffd@0
|
25 onodes = find(observed);
|
wolffd@0
|
26 bnet = bnet_from_engine(engine);
|
wolffd@0
|
27 pot_type = determine_pot_type(bnet, onodes);
|
wolffd@0
|
28 assert(pot_type == 'd');
|
wolffd@0
|
29 end
|
wolffd@0
|
30
|
wolffd@0
|
31 scalar = 1;
|
wolffd@0
|
32 evidence = evidence(:); % hack to handle unrolled DBNs
|
wolffd@0
|
33 N = length(evidence);
|
wolffd@0
|
34 mpe = cell(1,N);
|
wolffd@0
|
35 for i=1:N
|
wolffd@0
|
36 m = marginal_nodes(engine, i);
|
wolffd@0
|
37 % observed nodes are all set to 1 inside the inference engine, so we must undo this
|
wolffd@0
|
38 if observed(i)
|
wolffd@0
|
39 mpe{i} = evidence{i};
|
wolffd@0
|
40 else
|
wolffd@0
|
41 mpe{i} = argmax(m.T);
|
wolffd@0
|
42 % Bug fix by Ron Zohar, 8/15/01
|
wolffd@0
|
43 % If there are ties, we must break them as follows (see Jensen96, p106)
|
wolffd@0
|
44 if break_ties
|
wolffd@0
|
45 evidence{i} = mpe{i};
|
wolffd@0
|
46 [engine, ll] = enter_evidence(engine, evidence, 'maximize', 1);
|
wolffd@0
|
47 end
|
wolffd@0
|
48 end
|
wolffd@0
|
49 if length(mpe{i}) > 1, scalar = 0; end
|
wolffd@0
|
50 end
|
wolffd@0
|
51
|
wolffd@0
|
52 if nargout >= 2
|
wolffd@0
|
53 bnet = bnet_from_engine(engine);
|
wolffd@0
|
54 ll = log_lik_complete(bnet, mpe(:));
|
wolffd@0
|
55 end
|
wolffd@0
|
56 if 0 % scalar
|
wolffd@0
|
57 mpe = cell2num(mpe);
|
wolffd@0
|
58 end
|