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