annotate toolboxes/FullBNT-1.0.7/bnt/general/Old/calc_mpe_given_inf_engine.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, prob] = calc_mpe_given_inf_engine(engine, evidence)
wolffd@0 2 % CALC_MPE_GIVEN_ENGINE Computes the most probable explanation of the evidence
wolffd@0 3 % [mpe, prob] = calc_mpe_given_inf_engine(engine, evidence)
wolffd@0 4 %
wolffd@0 5 % INPUT
wolffd@0 6 % engine must support max-propagation
wolffd@0 7 % evidence{i} is the obsevred value of node i, or [] if hidden
wolffd@0 8 %
wolffd@0 9 % OUTPUT
wolffd@0 10 % mpe(i) is the most likely value of node i
wolffd@0 11 % prob is the likelihood of the globally best assignment
wolffd@0 12 %
wolffd@0 13 % This currently only works when all nodes are discrete
wolffd@0 14
wolffd@0 15 [engine, ll] = enter_evidence(engine, evidence);
wolffd@0 16
wolffd@0 17 observed = ~isemptycell(evidence);
wolffd@0 18 N = length(evidence);
wolffd@0 19 mpe = zeros(1,N);
wolffd@0 20 for i=1:N
wolffd@0 21 m = marginal_nodes(engine, i);
wolffd@0 22 % discrete observed nodes are all set to 1 inside the inference engine, so we must undo this
wolffd@0 23 if observed(i)
wolffd@0 24 mpe(i) = evidence{i};
wolffd@0 25 else
wolffd@0 26 mpe(i) = argmax(m.T);
wolffd@0 27 end
wolffd@0 28 end
wolffd@0 29
wolffd@0 30 bnet = bnet_from_engine(engine);
wolffd@0 31 ll = log_lik_complete(bnet, num2cell(mpe(:)));
wolffd@0 32 prob = exp(ll);