comparison toolboxes/FullBNT-1.0.7/bnt/general/mk_fgraph_given_ev.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 fg = mk_fgraph_given_ev(G, node_sizes, factors, ev_CPD, evidence, varargin)
2 % MK_FGRAPH_GIVEN_EV Make a factor graph where each node has its own private evidence term
3 % fg = mk_fgraph(G, node_sizes, factors, ev_CPD, evidence, ...)
4 %
5 % G, node_sizes and factors are as in mk_fgraph, but they refer to the hidden nodes.
6 % ev_CPD{i} is a CPD for the i'th hidden node; this will be converted into a factor
7 % for node i using evidence{i}.
8 % We currently assume all hidden nodes are discrete, for simplicity.
9 %
10 % The list below gives optional arguments [default value in brackets].
11 %
12 % equiv_class - equiv_class(i)=j means factor node i gets its params from factors{j} [1:F]
13 % ev_equiv_class - ev_equiv_class(i)=j means evidence node i gets its params from ev_CPD{j} [1:N]
14
15
16 N = length(node_sizes);
17 nfactors = length(factors);
18
19 % default values for parameters
20 eclass = 1:nfactors;
21 ev_eclass = 1:N;
22
23 if nargin >= 6
24 args = varargin;
25 nargs = length(args);
26 for i=1:2:nargs
27 switch args{i},
28 case 'equiv_class', eclass = args{i+1};
29 case 'ev_equiv_class', ev_eclass = args{i+1};
30 otherwise,
31 error(['invalid argument name ' args{i}]);
32 end
33 end
34 end
35
36 pot_type = 'd';
37 for x=1:N
38 ev = cell(1,2); % cell 1 is the hidden parent, cell 2 is the observed child
39 ev(2) = evidence(x);
40 dom = 1:2;
41 F = convert_to_pot(ev_CPD{ev_eclass(x)}, pot_type, dom(:), ev);
42 M = pot_to_marginal(F);
43 %factors{end+1} = tabular_CPD('self', 1, 'ps', [], 'sz', node_sizes(x), 'CPT', M.T);
44 factors{end+1} = mk_isolated_tabular_CPD(node_sizes(x), {'CPT', M.T});
45 end
46
47 E = max(eclass);
48 fg = mk_fgraph([G eye(N)], node_sizes, factors, 'equiv_class', [eclass E+1:E+N]);