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