Daniel@0: function fg = mk_fgraph_given_ev(G, node_sizes, factors, ev_CPD, evidence, varargin) Daniel@0: % MK_FGRAPH_GIVEN_EV Make a factor graph where each node has its own private evidence term Daniel@0: % fg = mk_fgraph(G, node_sizes, factors, ev_CPD, evidence, ...) Daniel@0: % Daniel@0: % G, node_sizes and factors are as in mk_fgraph, but they refer to the hidden nodes. Daniel@0: % ev_CPD{i} is a CPD for the i'th hidden node; this will be converted into a factor Daniel@0: % for node i using evidence{i}. Daniel@0: % We currently assume all hidden nodes are discrete, for simplicity. Daniel@0: % Daniel@0: % The list below gives optional arguments [default value in brackets]. Daniel@0: % Daniel@0: % equiv_class - equiv_class(i)=j means factor node i gets its params from factors{j} [1:F] Daniel@0: % ev_equiv_class - ev_equiv_class(i)=j means evidence node i gets its params from ev_CPD{j} [1:N] Daniel@0: Daniel@0: Daniel@0: N = length(node_sizes); Daniel@0: nfactors = length(factors); Daniel@0: Daniel@0: % default values for parameters Daniel@0: eclass = 1:nfactors; Daniel@0: ev_eclass = 1:N; Daniel@0: Daniel@0: if nargin >= 6 Daniel@0: args = varargin; Daniel@0: nargs = length(args); Daniel@0: for i=1:2:nargs Daniel@0: switch args{i}, Daniel@0: case 'equiv_class', eclass = args{i+1}; Daniel@0: case 'ev_equiv_class', ev_eclass = args{i+1}; Daniel@0: otherwise, Daniel@0: error(['invalid argument name ' args{i}]); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: pot_type = 'd'; Daniel@0: for x=1:N Daniel@0: ev = cell(1,2); % cell 1 is the hidden parent, cell 2 is the observed child Daniel@0: ev(2) = evidence(x); Daniel@0: dom = 1:2; Daniel@0: F = convert_to_pot(ev_CPD{ev_eclass(x)}, pot_type, dom(:), ev); Daniel@0: M = pot_to_marginal(F); Daniel@0: %factors{end+1} = tabular_CPD('self', 1, 'ps', [], 'sz', node_sizes(x), 'CPT', M.T); Daniel@0: factors{end+1} = mk_isolated_tabular_CPD(node_sizes(x), {'CPT', M.T}); Daniel@0: end Daniel@0: Daniel@0: E = max(eclass); Daniel@0: fg = mk_fgraph([G eye(N)], node_sizes, factors, 'equiv_class', [eclass E+1:E+N]);