annotate toolboxes/FullBNT-1.0.7/bnt/inference/static/@jtree_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 (assignment to the hidden nodes)
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 % 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 % soft - a cell array of soft/virtual evidence;
wolffd@0 11 % soft{i} is a prob. distrib. over i's values, or [] [ cell(1,N) ]
wolffd@0 12 %
wolffd@0 13
wolffd@0 14 bnet = bnet_from_engine(engine);
wolffd@0 15 ns = bnet.node_sizes(:);
wolffd@0 16 N = length(bnet.dag);
wolffd@0 17
wolffd@0 18 engine.evidence = evidence;
wolffd@0 19
wolffd@0 20 % set default params
wolffd@0 21 exclude = [];
wolffd@0 22 soft_evidence = cell(1,N);
wolffd@0 23
wolffd@0 24 % parse optional params
wolffd@0 25 args = varargin;
wolffd@0 26 nargs = length(args);
wolffd@0 27 for i=1:2:nargs
wolffd@0 28 switch args{i},
wolffd@0 29 case 'soft', soft_evidence = args{i+1};
wolffd@0 30 otherwise,
wolffd@0 31 error(['invalid argument name ' args{i}]);
wolffd@0 32 end
wolffd@0 33 end
wolffd@0 34 engine.maximize = 1;
wolffd@0 35
wolffd@0 36 onodes = find(~isemptycell(evidence));
wolffd@0 37 hnodes = find(isemptycell(evidence));
wolffd@0 38 pot_type = determine_pot_type(bnet, onodes);
wolffd@0 39 if strcmp(pot_type, 'cg')
wolffd@0 40 check_for_cd_arcs(onodes, bnet.cnodes, bnet.dag);
wolffd@0 41 end
wolffd@0 42
wolffd@0 43 hard_nodes = 1:N;
wolffd@0 44 soft_nodes = find(~isemptycell(soft_evidence));
wolffd@0 45 S = length(soft_nodes);
wolffd@0 46 if S > 0
wolffd@0 47 assert(pot_type == 'd');
wolffd@0 48 assert(mysubset(soft_nodes, bnet.dnodes));
wolffd@0 49 end
wolffd@0 50
wolffd@0 51 % Evaluate CPDs with evidence, and convert to potentials
wolffd@0 52 pot = cell(1, N+S);
wolffd@0 53 for n=1:N
wolffd@0 54 fam = family(bnet.dag, n);
wolffd@0 55 e = bnet.equiv_class(n);
wolffd@0 56 if isempty(bnet.CPD{e})
wolffd@0 57 error(['must define CPD ' num2str(e)])
wolffd@0 58 else
wolffd@0 59 pot{n} = convert_to_pot(bnet.CPD{e}, pot_type, fam(:), evidence);
wolffd@0 60 end
wolffd@0 61 end
wolffd@0 62
wolffd@0 63 for i=1:S
wolffd@0 64 n = soft_nodes(i);
wolffd@0 65 pot{N+i} = dpot(n, ns(n), soft_evidence{n});
wolffd@0 66 end
wolffd@0 67 clqs = engine.clq_ass_to_node([hard_nodes soft_nodes]);
wolffd@0 68
wolffd@0 69 [clpot, seppot] = init_pot(engine, clqs, pot, pot_type, onodes);
wolffd@0 70 [clpot, seppot] = collect_evidence(engine, clpot, seppot);
wolffd@0 71 mpe = find_max_config(engine, clpot, seppot, evidence); % instead of distribute evidence