annotate toolboxes/FullBNT-1.0.7/bnt/general/mk_fgraph.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(G, node_sizes, factors, varargin)
Daniel@0 2 % MK_FGRAPH Make a factor graph
Daniel@0 3 % fg = mk_fgraph(G, node_sizes, factors, ...)
Daniel@0 4 %
Daniel@0 5 % A factor graph is a bipartite graph, with one side containing variables,
Daniel@0 6 % and the other containing functions of (subsets of) these variables.
Daniel@0 7 % For details, see "Factor Graphs and the Sum-Product Algorithm",
Daniel@0 8 % F. Kschischang and B. Frey and H-A. Loeliger,
Daniel@0 9 % IEEE Trans. Info. Theory, 2001
Daniel@0 10 %
Daniel@0 11 % G(i,j) = 1 if there is an arc from variable i to factor j
Daniel@0 12 %
Daniel@0 13 % node_sizes(i) is the number of values node i can take on,
Daniel@0 14 % or the length of node i if i is a continuous-valued vector.
Daniel@0 15 %
Daniel@0 16 % 'factors' is the list of factors (kernel functions)
Daniel@0 17 %
Daniel@0 18 % The list below gives optional arguments [default value in brackets].
Daniel@0 19 %
Daniel@0 20 % equiv_class - equiv_class(i)=j means factor node i gets its params from factors{j} [1:F]
Daniel@0 21 % discrete - the list of nodes which are discrete random variables [1:N]
Daniel@0 22 %
Daniel@0 23 % e.g., fg = mk_fgraph(G, [2 2], {bnet.CPD{1},bnet.CPD{2}}, 'discrete', [1 2])
Daniel@0 24
Daniel@0 25 fg.G = G;
Daniel@0 26 fg.node_sizes = node_sizes;
Daniel@0 27 fg.factors = factors;
Daniel@0 28 [fg.nvars fg.nfactors] = size(G);
Daniel@0 29
Daniel@0 30 % default values for parameters
Daniel@0 31 fg.equiv_class = 1:fg.nfactors;
Daniel@0 32 fg.dnodes = 1:fg.nvars;
Daniel@0 33
Daniel@0 34 if nargin >= 4
Daniel@0 35 args = varargin;
Daniel@0 36 nargs = length(args);
Daniel@0 37 for i=1:2:nargs
Daniel@0 38 switch args{i},
Daniel@0 39 case 'equiv_class', fg.equiv_class = args{i+1};
Daniel@0 40 case 'discrete', fg.dnodes = args{i+1};
Daniel@0 41 otherwise,
Daniel@0 42 error(['invalid argument name ' args{i}]);
Daniel@0 43 end
Daniel@0 44 end
Daniel@0 45 end
Daniel@0 46
Daniel@0 47 % so that determine_pot_type will work...
Daniel@0 48 fg.utility_nodes = [];
Daniel@0 49 %fg.decision_nodes = [];
Daniel@0 50 %fg.chance_nodes = fg.nvars;
Daniel@0 51
Daniel@0 52 fg.dom = cell(1, fg.nfactors);
Daniel@0 53 for f=1:fg.nfactors
Daniel@0 54 fg.dom{f} = find(G(:,f));
Daniel@0 55 end
Daniel@0 56 fg.dep = cell(1, fg.nvars);
Daniel@0 57 for x=1:fg.nvars
Daniel@0 58 fg.dep{x} = find(G(x,:));
Daniel@0 59 end
Daniel@0 60 fg.cnodes = mysetdiff(1:fg.nvars, fg.dnodes);