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.m @ 8:b5b38998ef3b
History | View | Annotate | Download (1.66 KB)
| 1 |
function [mpe, ll] = calc_mpe(engine, evidence, break_ties) |
|---|---|
| 2 |
% CALC_MPE Computes the most probable explanation of the evidence |
| 3 |
% [mpe, ll] = calc_mpe_given_inf_engine(engine, evidence, break_ties) |
| 4 |
% |
| 5 |
% INPUT |
| 6 |
% engine must support max-propagation |
| 7 |
% evidence{i} is the observed value of node i, or [] if hidden
|
| 8 |
% break_ties is optional. If 1, we will force ties to be broken consistently |
| 9 |
% by calling enter_evidence N times. |
| 10 |
% |
| 11 |
% OUTPUT |
| 12 |
% mpe{i} is the most likely value of node i (cell array!)
|
| 13 |
% ll is the log-likelihood of the globally best assignment |
| 14 |
% |
| 15 |
% This currently only works when all hidden nodes are discrete |
| 16 |
|
| 17 |
if nargin < 3, break_ties = 0; end |
| 18 |
|
| 19 |
|
| 20 |
[engine, ll] = enter_evidence(engine, evidence, 'maximize', 1); |
| 21 |
|
| 22 |
observed = ~isemptycell(evidence); |
| 23 |
|
| 24 |
if 0 % fgraphs don't support bnet_from_engine |
| 25 |
onodes = find(observed); |
| 26 |
bnet = bnet_from_engine(engine); |
| 27 |
pot_type = determine_pot_type(bnet, onodes); |
| 28 |
assert(pot_type == 'd'); |
| 29 |
end |
| 30 |
|
| 31 |
scalar = 1; |
| 32 |
evidence = evidence(:); % hack to handle unrolled DBNs |
| 33 |
N = length(evidence); |
| 34 |
mpe = cell(1,N); |
| 35 |
for i=1:N |
| 36 |
m = marginal_nodes(engine, i); |
| 37 |
% observed nodes are all set to 1 inside the inference engine, so we must undo this |
| 38 |
if observed(i) |
| 39 |
mpe{i} = evidence{i};
|
| 40 |
else |
| 41 |
mpe{i} = argmax(m.T);
|
| 42 |
% Bug fix by Ron Zohar, 8/15/01 |
| 43 |
% If there are ties, we must break them as follows (see Jensen96, p106) |
| 44 |
if break_ties |
| 45 |
evidence{i} = mpe{i};
|
| 46 |
[engine, ll] = enter_evidence(engine, evidence, 'maximize', 1); |
| 47 |
end |
| 48 |
end |
| 49 |
if length(mpe{i}) > 1, scalar = 0; end
|
| 50 |
end |
| 51 |
|
| 52 |
if nargout >= 2 |
| 53 |
bnet = bnet_from_engine(engine); |
| 54 |
ll = log_lik_complete(bnet, mpe(:)); |
| 55 |
end |
| 56 |
if 0 % scalar |
| 57 |
mpe = cell2num(mpe); |
| 58 |
end |