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