annotate toolboxes/FullBNT-1.0.7/bnt/inference/static/@belprop_fg_inf_engine/find_mpe.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 mpe = find_mpe(engine, evidence, varargin)
wolffd@0 2 % FIND_MPE Find the most probable explanation of the data (belprop_fg)
wolffd@0 3 % function mpe = find_mpe(engine, evidence,...)
wolffd@0 4 %
wolffd@0 5 % evidence{i} = [] if X(i) is hidden, and otherwise contains its observed value (scalar or column vector).
wolffd@0 6 %
wolffd@0 7 % This finds the marginally most likely value for each hidden node,
wolffd@0 8 % and may give the wrong results even if the graph is acyclic,
wolffd@0 9 % unless you set break_ties = 1.
wolffd@0 10 %
wolffd@0 11 % The following optional arguments can be specified in the form of name/value pairs:
wolffd@0 12 % [default value in brackets]
wolffd@0 13 %
wolffd@0 14 % break_ties is optional. If 1, we will force ties to be broken consistently
wolffd@0 15 % by calling enter_evidence N times. (see Jensen96, p106) Default = 1.
wolffd@0 16
wolffd@0 17 break_ties = 1;
wolffd@0 18
wolffd@0 19 % parse optional params
wolffd@0 20 args = varargin;
wolffd@0 21 nargs = length(args);
wolffd@0 22 for i=1:2:nargs
wolffd@0 23 switch args{i},
wolffd@0 24 case 'break_ties', break_ties = args{i+1};
wolffd@0 25 otherwise,
wolffd@0 26 error(['invalid argument name ' args{i}]);
wolffd@0 27 end
wolffd@0 28 end
wolffd@0 29
wolffd@0 30 engine = enter_evidence(engine, evidence, 'maximize', 1);
wolffd@0 31
wolffd@0 32 observed = ~isemptycell(evidence);
wolffd@0 33 evidence = evidence(:); % hack to handle unrolled DBNs
wolffd@0 34 N = length(evidence);
wolffd@0 35 mpe = cell(1,N);
wolffd@0 36 for i=1:N
wolffd@0 37 m = marginal_nodes(engine, i);
wolffd@0 38 % observed nodes are all set to 1 inside the inference engine, so we must undo this
wolffd@0 39 if observed(i)
wolffd@0 40 mpe{i} = evidence{i};
wolffd@0 41 else
wolffd@0 42 mpe{i} = argmax(m.T);
wolffd@0 43 if break_ties
wolffd@0 44 evidence{i} = mpe{i};
wolffd@0 45 [engine, ll] = enter_evidence(engine, evidence, 'maximize', 1);
wolffd@0 46 end
wolffd@0 47 end
wolffd@0 48 end
wolffd@0 49