wolffd@0: function mpe = find_mpe(engine, evidence, varargin) wolffd@0: % FIND_MPE Find the most probable explanation of the data (assignment to the hidden nodes) wolffd@0: % function mpe = find_mpe(engine, evidence,...) wolffd@0: % wolffd@0: % evidence{i} = [] if X(i) is hidden, and otherwise contains its observed value (scalar or column vector). wolffd@0: % wolffd@0: % The following optional arguments can be specified in the form of name/value pairs: wolffd@0: % [default value in brackets] wolffd@0: % wolffd@0: % soft - a cell array of soft/virtual evidence; wolffd@0: % soft{i} is a prob. distrib. over i's values, or [] [ cell(1,N) ] wolffd@0: % wolffd@0: wolffd@0: bnet = bnet_from_engine(engine); wolffd@0: ns = bnet.node_sizes(:); wolffd@0: N = length(bnet.dag); wolffd@0: wolffd@0: engine.evidence = evidence; wolffd@0: wolffd@0: % set default params wolffd@0: exclude = []; wolffd@0: soft_evidence = cell(1,N); wolffd@0: wolffd@0: % parse optional params wolffd@0: args = varargin; wolffd@0: nargs = length(args); wolffd@0: for i=1:2:nargs wolffd@0: switch args{i}, wolffd@0: case 'soft', soft_evidence = args{i+1}; wolffd@0: otherwise, wolffd@0: error(['invalid argument name ' args{i}]); wolffd@0: end wolffd@0: end wolffd@0: engine.maximize = 1; wolffd@0: wolffd@0: onodes = find(~isemptycell(evidence)); wolffd@0: hnodes = find(isemptycell(evidence)); wolffd@0: pot_type = determine_pot_type(bnet, onodes); wolffd@0: if strcmp(pot_type, 'cg') wolffd@0: check_for_cd_arcs(onodes, bnet.cnodes, bnet.dag); wolffd@0: end wolffd@0: wolffd@0: hard_nodes = 1:N; wolffd@0: soft_nodes = find(~isemptycell(soft_evidence)); wolffd@0: S = length(soft_nodes); wolffd@0: if S > 0 wolffd@0: assert(pot_type == 'd'); wolffd@0: assert(mysubset(soft_nodes, bnet.dnodes)); wolffd@0: end wolffd@0: wolffd@0: % Evaluate CPDs with evidence, and convert to potentials wolffd@0: pot = cell(1, N+S); wolffd@0: for n=1:N wolffd@0: fam = family(bnet.dag, n); wolffd@0: e = bnet.equiv_class(n); wolffd@0: if isempty(bnet.CPD{e}) wolffd@0: error(['must define CPD ' num2str(e)]) wolffd@0: else wolffd@0: pot{n} = convert_to_pot(bnet.CPD{e}, pot_type, fam(:), evidence); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: for i=1:S wolffd@0: n = soft_nodes(i); wolffd@0: pot{N+i} = dpot(n, ns(n), soft_evidence{n}); wolffd@0: end wolffd@0: clqs = engine.clq_ass_to_node([hard_nodes soft_nodes]); wolffd@0: wolffd@0: [clpot, seppot] = init_pot(engine, clqs, pot, pot_type, onodes); wolffd@0: [clpot, seppot] = collect_evidence(engine, clpot, seppot); wolffd@0: mpe = find_max_config(engine, clpot, seppot, evidence); % instead of distribute evidence