wolffd@0
|
1 function [engine, loglik] = enter_evidence(engine, evidence, varargin)
|
wolffd@0
|
2 % ENTER_EVIDENCE Add the specified evidence to the network (jtree)
|
wolffd@0
|
3 % [engine, loglik] = enter_evidence(engine, evidence, ...)
|
wolffd@0
|
4 %
|
wolffd@0
|
5 % evidence{i} = [] if X(i) is hidden, and otherwise contains its observed value (scalar or column vector).
|
wolffd@0
|
6 %
|
wolffd@0
|
7 % The following optional arguments can be specified in the form of name/value pairs:
|
wolffd@0
|
8 % [default value in brackets]
|
wolffd@0
|
9 %
|
wolffd@0
|
10 % soft - a cell array of soft/virtual evidence;
|
wolffd@0
|
11 % soft{i} is a prob. distrib. over i's values, or [] [ cell(1,N) ]
|
wolffd@0
|
12 %
|
wolffd@0
|
13 % e.g., engine = enter_evidence(engine, ev, 'soft', soft_ev)
|
wolffd@0
|
14
|
wolffd@0
|
15 bnet = bnet_from_engine(engine);
|
wolffd@0
|
16 ns = bnet.node_sizes(:);
|
wolffd@0
|
17 N = length(bnet.dag);
|
wolffd@0
|
18
|
wolffd@0
|
19 engine.evidence = evidence; % store this for marginal_nodes with add_ev option
|
wolffd@0
|
20 engine.maximize = 0;
|
wolffd@0
|
21
|
wolffd@0
|
22 % set default params
|
wolffd@0
|
23 exclude = [];
|
wolffd@0
|
24 soft_evidence = cell(1,N);
|
wolffd@0
|
25
|
wolffd@0
|
26 % parse optional params
|
wolffd@0
|
27 args = varargin;
|
wolffd@0
|
28 nargs = length(args);
|
wolffd@0
|
29 for i=1:2:nargs
|
wolffd@0
|
30 switch args{i},
|
wolffd@0
|
31 case 'soft', soft_evidence = args{i+1};
|
wolffd@0
|
32 case 'maximize', engine.maximize = args{i+1};
|
wolffd@0
|
33 otherwise,
|
wolffd@0
|
34 error(['invalid argument name ' args{i}]);
|
wolffd@0
|
35 end
|
wolffd@0
|
36 end
|
wolffd@0
|
37
|
wolffd@0
|
38 onodes = find(~isemptycell(evidence));
|
wolffd@0
|
39 hnodes = find(isemptycell(evidence));
|
wolffd@0
|
40 pot_type = determine_pot_type(bnet, onodes);
|
wolffd@0
|
41 if strcmp(pot_type, 'cg')
|
wolffd@0
|
42 check_for_cd_arcs(onodes, bnet.cnodes, bnet.dag);
|
wolffd@0
|
43 end
|
wolffd@0
|
44
|
wolffd@0
|
45 if is_mnet(bnet)
|
wolffd@0
|
46 pot = engine.user_pot;
|
wolffd@0
|
47 clqs = engine.nums_ass_to_user_clqs;
|
wolffd@0
|
48 else
|
wolffd@0
|
49 % Evaluate CPDs with evidence, and convert to potentials
|
wolffd@0
|
50 pot = cell(1, N);
|
wolffd@0
|
51 for n=1:N
|
wolffd@0
|
52 fam = family(bnet.dag, n);
|
wolffd@0
|
53 e = bnet.equiv_class(n);
|
wolffd@0
|
54 if isempty(bnet.CPD{e})
|
wolffd@0
|
55 error(['must define CPD ' num2str(e)])
|
wolffd@0
|
56 else
|
wolffd@0
|
57 pot{n} = convert_to_pot(bnet.CPD{e}, pot_type, fam(:), evidence);
|
wolffd@0
|
58 end
|
wolffd@0
|
59 end
|
wolffd@0
|
60 clqs = engine.clq_ass_to_node(1:N);
|
wolffd@0
|
61 end
|
wolffd@0
|
62
|
wolffd@0
|
63 % soft evidence
|
wolffd@0
|
64 soft_nodes = find(~isemptycell(soft_evidence));
|
wolffd@0
|
65 S = length(soft_nodes);
|
wolffd@0
|
66 if S > 0
|
wolffd@0
|
67 assert(pot_type == 'd');
|
wolffd@0
|
68 assert(mysubset(soft_nodes, bnet.dnodes));
|
wolffd@0
|
69 end
|
wolffd@0
|
70 for i=1:S
|
wolffd@0
|
71 n = soft_nodes(i);
|
wolffd@0
|
72 pot{end+1} = dpot(n, ns(n), soft_evidence{n});
|
wolffd@0
|
73 end
|
wolffd@0
|
74 clqs = [clqs engine.clq_ass_to_node(soft_nodes)];
|
wolffd@0
|
75
|
wolffd@0
|
76
|
wolffd@0
|
77 [clpot, seppot] = init_pot(engine, clqs, pot, pot_type, onodes);
|
wolffd@0
|
78 [clpot, seppot] = collect_evidence(engine, clpot, seppot);
|
wolffd@0
|
79 [clpot, seppot] = distribute_evidence(engine, clpot, seppot);
|
wolffd@0
|
80
|
wolffd@0
|
81 C = length(clpot);
|
wolffd@0
|
82 ll = zeros(1, C);
|
wolffd@0
|
83 for i=1:C
|
wolffd@0
|
84 [clpot{i}, ll(i)] = normalize_pot(clpot{i});
|
wolffd@0
|
85 end
|
wolffd@0
|
86 loglik = ll(1); % we can extract the likelihood from any clique
|
wolffd@0
|
87
|
wolffd@0
|
88 engine.clpot = clpot;
|