To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / _FullBNT / BNT / general / Old / calc_mpe_given_inf_engine.m @ 8:b5b38998ef3b
History | View | Annotate | Download (966 Bytes)
| 1 |
function [mpe, prob] = calc_mpe_given_inf_engine(engine, evidence) |
|---|---|
| 2 |
% CALC_MPE_GIVEN_ENGINE Computes the most probable explanation of the evidence |
| 3 |
% [mpe, prob] = calc_mpe_given_inf_engine(engine, evidence) |
| 4 |
% |
| 5 |
% INPUT |
| 6 |
% engine must support max-propagation |
| 7 |
% evidence{i} is the obsevred value of node i, or [] if hidden
|
| 8 |
% |
| 9 |
% OUTPUT |
| 10 |
% mpe(i) is the most likely value of node i |
| 11 |
% prob is the likelihood of the globally best assignment |
| 12 |
% |
| 13 |
% This currently only works when all nodes are discrete |
| 14 |
|
| 15 |
[engine, ll] = enter_evidence(engine, evidence); |
| 16 |
|
| 17 |
observed = ~isemptycell(evidence); |
| 18 |
N = length(evidence); |
| 19 |
mpe = zeros(1,N); |
| 20 |
for i=1:N |
| 21 |
m = marginal_nodes(engine, i); |
| 22 |
% discrete observed nodes are all set to 1 inside the inference engine, so we must undo this |
| 23 |
if observed(i) |
| 24 |
mpe(i) = evidence{i};
|
| 25 |
else |
| 26 |
mpe(i) = argmax(m.T); |
| 27 |
end |
| 28 |
end |
| 29 |
|
| 30 |
bnet = bnet_from_engine(engine); |
| 31 |
ll = log_lik_complete(bnet, num2cell(mpe(:))); |
| 32 |
prob = exp(ll); |