annotate toolboxes/FullBNT-1.0.7/bnt/general/mk_fgraph_given_ev.m @ 0:cc4b1211e677 tip

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