comparison toolboxes/FullBNT-1.0.7/bnt/inference/static/@jtree_inf_engine/Old/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 % maximize - if 1, does max-product instead of sum-product [0]
11 % soft - a cell array of soft/virtual evidence;
12 % soft{i} is a prob. distrib. over i's values, or [] [ cell(1,N) ]
13 %
14 % e.g., engine = enter_evidence(engine, ev, 'soft', soft_ev)
15 %
16 % For backwards compatibility with BNT2, you can also specify the parameters in the following order
17 % engine = enter_evidence(engine, ev, soft_ev)
18
19 bnet = bnet_from_engine(engine);
20 ns = bnet.node_sizes(:);
21 N = length(bnet.dag);
22
23 % set default params
24 exclude = [];
25 soft_evidence = cell(1,N);
26 maximize = 0;
27
28 % parse optional params
29 args = varargin;
30 nargs = length(args);
31 if nargs > 0
32 if iscell(args{1})
33 soft_evidence = args{1};
34 else
35 for i=1:2:nargs
36 switch args{i},
37 case 'soft', soft_evidence = args{i+1};
38 case 'maximize', maximize = args{i+1};
39 otherwise,
40 error(['invalid argument name ' args{i}]);
41 end
42 end
43 end
44 end
45
46 engine.maximize = maximize;
47
48 onodes = find(~isemptycell(evidence));
49 hnodes = find(isemptycell(evidence));
50 pot_type = determine_pot_type(bnet, onodes);
51 if strcmp(pot_type, 'cg')
52 check_for_cd_arcs(onodes, bnet.cnodes, bnet.dag);
53 end
54
55
56 hard_nodes = 1:N;
57 soft_nodes = find(~isemptycell(soft_evidence));
58 S = length(soft_nodes);
59 if S > 0
60 assert(pot_type == 'd');
61 assert(mysubset(soft_nodes, bnet.dnodes));
62 end
63
64 % Evaluate CPDs with evidence, and convert to potentials
65 pot = cell(1, N+S);
66 for n=1:N
67 fam = family(bnet.dag, n);
68 e = bnet.equiv_class(n);
69 pot{n} = convert_to_pot(bnet.CPD{e}, pot_type, fam(:), evidence);
70 end
71
72 for i=1:S
73 n = soft_nodes(i);
74 pot{N+i} = dpot(n, ns(n), soft_evidence{n});
75 end
76
77 %clqs = engine.clq_ass_to_node([hard_nodes soft_nodes]);
78 %[clpot, loglik] = enter_soft_evidence(engine, clqs, pot, onodes, pot_type);
79 %engine.clpot = clpot; % save the results for marginal_nodes
80
81
82 clique = engine.clq_ass_to_node([hard_nodes soft_nodes]);
83 potential = pot;
84
85
86 % Set the clique potentials to all 1s
87 C = length(engine.cliques);
88 for i=1:C
89 engine.clpot{i} = mk_initial_pot(pot_type, engine.cliques{i}, ns, bnet.cnodes, onodes);
90 end
91
92 % Multiply on specified potentials
93 for i=1:length(clique)
94 c = clique(i);
95 engine.clpot{c} = multiply_by_pot(engine.clpot{c}, potential{i});
96 end
97
98 root = 1; % arbitrary
99 engine = collect_evidence(engine, root);
100 engine = distribute_evidence(engine, root);
101
102 ll = zeros(1, C);
103 for i=1:C
104 [engine.clpot{i}, ll(i)] = normalize_pot(engine.clpot{i});
105 end
106 loglik = ll(1); % we can extract the likelihood from any clique
107