annotate toolboxes/FullBNT-1.0.7/bnt/general/fgraph_to_bnet.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 bnet = fgraph_to_bnet(fg)
wolffd@0 2 % FGRAPH_TO_BNET Convert a factor graph to a Bayes net
wolffd@0 3 % bnet = fgraph_to_bnet(fg)
wolffd@0 4 %
wolffd@0 5 % We assume all factors are tabular_CPD.
wolffd@0 6 % We create 1 dummy observed node for every factor.
wolffd@0 7
wolffd@0 8 N = fg.nvars + fg.nfactors;
wolffd@0 9 vnodes = 1:fg.nvars;
wolffd@0 10 fnodes = fg.nvars+1:N;
wolffd@0 11 dag = zeros(N);
wolffd@0 12 for x=1:fg.nvars
wolffd@0 13 dag(x, fnodes(fg.dep{x})) = 1;
wolffd@0 14 end
wolffd@0 15 ns = [fg.node_sizes ones(1, fg.nfactors)];
wolffd@0 16 discrete = [fg.dnodes fnodes];
wolffd@0 17 bnet = mk_bnet(dag, ns, 'discrete', discrete);
wolffd@0 18 for x=1:fg.nvars
wolffd@0 19 bnet.CPD{x} = tabular_CPD(bnet, x, 'CPT', 'unif');
wolffd@0 20 end
wolffd@0 21 ev = cell(1, fg.nvars); % no evidence
wolffd@0 22 for i=1:fg.nfactors
wolffd@0 23 f = fnodes(i);
wolffd@0 24 e = fg.equiv_class(i);
wolffd@0 25 pot = convert_to_pot(fg.factors{e}, 'd', fg.dom{i}, ev);
wolffd@0 26 m = pot_to_marginal(pot);
wolffd@0 27 bnet.CPD{f} = tabular_CPD(bnet, f, 'CPT', m.T);
wolffd@0 28 end
wolffd@0 29
wolffd@0 30