Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/bnt/inference/static/@jtree_mnet_inf_engine/find_mpe.m @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e9a9cd732c1e |
---|---|
1 function mpe = find_mpe(engine, evidence, varargin) | |
2 % FIND_MPE Find the most probable explanation of the data (assignment to the hidden nodes) | |
3 % function mpe = find_mpe(engine, evidence,...) | |
4 % | |
5 % evidence{i} = [] if X(i) is hidden, and otherwise contains its observed value (scalar or column vector). | |
6 % | |
7 % The following optional arguments can be specified in the form of name/value pairs: | |
8 % [default value in brackets] | |
9 % | |
10 % soft - a cell array of soft/virtual evidence; | |
11 % soft{i} is a prob. distrib. over i's values, or [] [ cell(1,N) ] | |
12 % | |
13 | |
14 bnet = bnet_from_engine(engine); | |
15 ns = bnet.node_sizes(:); | |
16 N = length(bnet.dag); | |
17 | |
18 engine.evidence = evidence; | |
19 | |
20 % set default params | |
21 exclude = []; | |
22 soft_evidence = cell(1,N); | |
23 | |
24 % parse optional params | |
25 args = varargin; | |
26 nargs = length(args); | |
27 for i=1:2:nargs | |
28 switch args{i}, | |
29 case 'soft', soft_evidence = args{i+1}; | |
30 otherwise, | |
31 error(['invalid argument name ' args{i}]); | |
32 end | |
33 end | |
34 engine.maximize = 1; | |
35 | |
36 onodes = find(~isemptycell(evidence)); | |
37 hnodes = find(isemptycell(evidence)); | |
38 pot_type = determine_pot_type(bnet, onodes); | |
39 if strcmp(pot_type, 'cg') | |
40 check_for_cd_arcs(onodes, bnet.cnodes, bnet.dag); | |
41 end | |
42 | |
43 hard_nodes = 1:N; | |
44 soft_nodes = find(~isemptycell(soft_evidence)); | |
45 S = length(soft_nodes); | |
46 if S > 0 | |
47 assert(pot_type == 'd'); | |
48 assert(mysubset(soft_nodes, bnet.dnodes)); | |
49 end | |
50 | |
51 % Evaluate CPDs with evidence, and convert to potentials | |
52 pot = cell(1, N+S); | |
53 for n=1:N | |
54 fam = family(bnet.dag, n); | |
55 e = bnet.equiv_class(n); | |
56 if isempty(bnet.CPD{e}) | |
57 error(['must define CPD ' num2str(e)]) | |
58 else | |
59 pot{n} = convert_to_pot(bnet.CPD{e}, pot_type, fam(:), evidence); | |
60 end | |
61 end | |
62 | |
63 for i=1:S | |
64 n = soft_nodes(i); | |
65 pot{N+i} = dpot(n, ns(n), soft_evidence{n}); | |
66 end | |
67 clqs = engine.clq_ass_to_node([hard_nodes soft_nodes]); | |
68 | |
69 [clpot, seppot] = init_pot(engine, clqs, pot, pot_type, onodes); | |
70 [clpot, seppot] = collect_evidence(engine, clpot, seppot); | |
71 mpe = find_max_config(engine, clpot, seppot); |