To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / _FullBNT / BNT / general / fgraph_to_bnet.m @ 8:b5b38998ef3b
History | View | Annotate | Download (798 Bytes)
| 1 |
function bnet = fgraph_to_bnet(fg) |
|---|---|
| 2 |
% FGRAPH_TO_BNET Convert a factor graph to a Bayes net |
| 3 |
% bnet = fgraph_to_bnet(fg) |
| 4 |
% |
| 5 |
% We assume all factors are tabular_CPD. |
| 6 |
% We create 1 dummy observed node for every factor. |
| 7 |
|
| 8 |
N = fg.nvars + fg.nfactors; |
| 9 |
vnodes = 1:fg.nvars; |
| 10 |
fnodes = fg.nvars+1:N; |
| 11 |
dag = zeros(N); |
| 12 |
for x=1:fg.nvars |
| 13 |
dag(x, fnodes(fg.dep{x})) = 1;
|
| 14 |
end |
| 15 |
ns = [fg.node_sizes ones(1, fg.nfactors)]; |
| 16 |
discrete = [fg.dnodes fnodes]; |
| 17 |
bnet = mk_bnet(dag, ns, 'discrete', discrete); |
| 18 |
for x=1:fg.nvars |
| 19 |
bnet.CPD{x} = tabular_CPD(bnet, x, 'CPT', 'unif');
|
| 20 |
end |
| 21 |
ev = cell(1, fg.nvars); % no evidence |
| 22 |
for i=1:fg.nfactors |
| 23 |
f = fnodes(i); |
| 24 |
e = fg.equiv_class(i); |
| 25 |
pot = convert_to_pot(fg.factors{e}, 'd', fg.dom{i}, ev);
|
| 26 |
m = pot_to_marginal(pot); |
| 27 |
bnet.CPD{f} = tabular_CPD(bnet, f, 'CPT', m.T);
|
| 28 |
end |
| 29 |
|
| 30 |
|