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