annotate toolboxes/FullBNT-1.0.7/bnt/inference/static/@jtree_inf_engine/find_mpe.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function mpe = find_mpe(engine, evidence, varargin)
Daniel@0 2 % FIND_MPE Find the most probable explanation of the data (assignment to the hidden nodes)
Daniel@0 3 % function mpe = find_mpe(engine, evidence,...)
Daniel@0 4 %
Daniel@0 5 % evidence{i} = [] if X(i) is hidden, and otherwise contains its observed value (scalar or column vector).
Daniel@0 6 %
Daniel@0 7 % The following optional arguments can be specified in the form of name/value pairs:
Daniel@0 8 % [default value in brackets]
Daniel@0 9 %
Daniel@0 10 % soft - a cell array of soft/virtual evidence;
Daniel@0 11 % soft{i} is a prob. distrib. over i's values, or [] [ cell(1,N) ]
Daniel@0 12 %
Daniel@0 13
Daniel@0 14 bnet = bnet_from_engine(engine);
Daniel@0 15 ns = bnet.node_sizes(:);
Daniel@0 16 N = length(bnet.dag);
Daniel@0 17
Daniel@0 18 engine.evidence = evidence;
Daniel@0 19
Daniel@0 20 % set default params
Daniel@0 21 exclude = [];
Daniel@0 22 soft_evidence = cell(1,N);
Daniel@0 23
Daniel@0 24 % parse optional params
Daniel@0 25 args = varargin;
Daniel@0 26 nargs = length(args);
Daniel@0 27 for i=1:2:nargs
Daniel@0 28 switch args{i},
Daniel@0 29 case 'soft', soft_evidence = args{i+1};
Daniel@0 30 otherwise,
Daniel@0 31 error(['invalid argument name ' args{i}]);
Daniel@0 32 end
Daniel@0 33 end
Daniel@0 34 engine.maximize = 1;
Daniel@0 35
Daniel@0 36 onodes = find(~isemptycell(evidence));
Daniel@0 37 hnodes = find(isemptycell(evidence));
Daniel@0 38 pot_type = determine_pot_type(bnet, onodes);
Daniel@0 39 if strcmp(pot_type, 'cg')
Daniel@0 40 check_for_cd_arcs(onodes, bnet.cnodes, bnet.dag);
Daniel@0 41 end
Daniel@0 42
Daniel@0 43 hard_nodes = 1:N;
Daniel@0 44 soft_nodes = find(~isemptycell(soft_evidence));
Daniel@0 45 S = length(soft_nodes);
Daniel@0 46 if S > 0
Daniel@0 47 assert(pot_type == 'd');
Daniel@0 48 assert(mysubset(soft_nodes, bnet.dnodes));
Daniel@0 49 end
Daniel@0 50
Daniel@0 51 % Evaluate CPDs with evidence, and convert to potentials
Daniel@0 52 pot = cell(1, N+S);
Daniel@0 53 for n=1:N
Daniel@0 54 fam = family(bnet.dag, n);
Daniel@0 55 e = bnet.equiv_class(n);
Daniel@0 56 if isempty(bnet.CPD{e})
Daniel@0 57 error(['must define CPD ' num2str(e)])
Daniel@0 58 else
Daniel@0 59 pot{n} = convert_to_pot(bnet.CPD{e}, pot_type, fam(:), evidence);
Daniel@0 60 end
Daniel@0 61 end
Daniel@0 62
Daniel@0 63 for i=1:S
Daniel@0 64 n = soft_nodes(i);
Daniel@0 65 pot{N+i} = dpot(n, ns(n), soft_evidence{n});
Daniel@0 66 end
Daniel@0 67 clqs = engine.clq_ass_to_node([hard_nodes soft_nodes]);
Daniel@0 68
Daniel@0 69 [clpot, seppot] = init_pot(engine, clqs, pot, pot_type, onodes);
Daniel@0 70 [clpot, seppot] = collect_evidence(engine, clpot, seppot);
Daniel@0 71 mpe = find_max_config(engine, clpot, seppot, evidence); % instead of distribute evidence