annotate toolboxes/FullBNT-1.0.7/bnt/general/mk_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 = mk_bnet(dag, node_sizes, varargin)
wolffd@0 2 % MK_BNET Make a Bayesian network.
wolffd@0 3 %
wolffd@0 4 % BNET = MK_BNET(DAG, NODE_SIZES, ...) makes a graphical model with an arc from i to j iff DAG(i,j) = 1.
wolffd@0 5 % Thus DAG is the adjacency matrix for a directed acyclic graph.
wolffd@0 6 % The nodes are assumed to be in topological order. Use TOPOLOGICAL_SORT if necessary.
wolffd@0 7 %
wolffd@0 8 % node_sizes(i) is the number of values node i can take on,
wolffd@0 9 % or the length of node i if i is a continuous-valued vector.
wolffd@0 10 % node_sizes(i) = 1 if i is a utility node.
wolffd@0 11 %
wolffd@0 12 % Below are the names of optional arguments [and their default value in brackets].
wolffd@0 13 % Pass as 'PropertyName1', PropertyValue1, 'PropertyName2', PropertyValue2, ...
wolffd@0 14 %
wolffd@0 15 % discrete - the list of nodes which are discrete random variables [1:N]
wolffd@0 16 % equiv_class - equiv_class(i)=j means node i gets its params from CPD{j} [1:N]
wolffd@0 17 % observed - the list of nodes which will definitely be observed in every case [ [] ]
wolffd@0 18 % 'names' - a cell array of strings to be associated with nodes 1:n [{}]
wolffd@0 19 % This creates an associative array, so you write e.g.
wolffd@0 20 % 'evidence(bnet.names{'bar'}) = 42' instead of 'evidence(2} = 42'
wolffd@0 21 % assuming names = { 'foo', 'bar', ...}.
wolffd@0 22 %
wolffd@0 23 % e.g., bnet = mk_bnet(dag, ns, 'discrete', [1 3])
wolffd@0 24 %
wolffd@0 25 % For backwards compatibility with BNT2, you can also specify the parameters in the following order
wolffd@0 26 % bnet = mk_bnet(dag, node_sizes, discrete_nodes, equiv_class)
wolffd@0 27
wolffd@0 28 n = length(dag);
wolffd@0 29
wolffd@0 30 % default values for parameters
wolffd@0 31 bnet.equiv_class = 1:n;
wolffd@0 32 bnet.dnodes = 1:n; % discrete
wolffd@0 33 bnet.observed = [];
wolffd@0 34 bnet.names = {};
wolffd@0 35
wolffd@0 36 if nargin >= 3
wolffd@0 37 args = varargin;
wolffd@0 38 nargs = length(args);
wolffd@0 39 if ~isstr(args{1})
wolffd@0 40 if nargs >= 1, bnet.dnodes = args{1}; end
wolffd@0 41 if nargs >= 2, bnet.equiv_class = args{2}; end
wolffd@0 42 else
wolffd@0 43 for i=1:2:nargs
wolffd@0 44 switch args{i},
wolffd@0 45 case 'equiv_class', bnet.equiv_class = args{i+1};
wolffd@0 46 case 'discrete', bnet.dnodes = args{i+1};
wolffd@0 47 case 'observed', bnet.observed = args{i+1};
wolffd@0 48 case 'names', bnet.names = assocarray(args{i+1}, num2cell(1:n));
wolffd@0 49 otherwise,
wolffd@0 50 error(['invalid argument name ' args{i}]);
wolffd@0 51 end
wolffd@0 52 end
wolffd@0 53 end
wolffd@0 54 end
wolffd@0 55
wolffd@0 56 bnet.observed = sort(bnet.observed); % for comparing sets
wolffd@0 57 bnet.hidden = mysetdiff(1:n, bnet.observed(:)');
wolffd@0 58 bnet.hidden_bitv = zeros(1,n);
wolffd@0 59 bnet.hidden_bitv(bnet.hidden) = 1;
wolffd@0 60 bnet.dag = dag;
wolffd@0 61 bnet.node_sizes = node_sizes(:)';
wolffd@0 62
wolffd@0 63 bnet.cnodes = mysetdiff(1:n, bnet.dnodes);
wolffd@0 64 % too many functions refer to cnodes to rename it to cts_nodes -
wolffd@0 65 % We hope it won't be confused with chance nodes!
wolffd@0 66
wolffd@0 67 bnet.parents = cell(1,n);
wolffd@0 68 for i=1:n
wolffd@0 69 bnet.parents{i} = parents(dag, i);
wolffd@0 70 end
wolffd@0 71
wolffd@0 72 E = max(bnet.equiv_class);
wolffd@0 73 mem = cell(1,E);
wolffd@0 74 for i=1:n
wolffd@0 75 e = bnet.equiv_class(i);
wolffd@0 76 mem{e} = [mem{e} i];
wolffd@0 77 end
wolffd@0 78 bnet.members_of_equiv_class = mem;
wolffd@0 79
wolffd@0 80 bnet.CPD = cell(1, E);
wolffd@0 81
wolffd@0 82 bnet.rep_of_eclass = zeros(1,E);
wolffd@0 83 for e=1:E
wolffd@0 84 mems = bnet.members_of_equiv_class{e};
wolffd@0 85 bnet.rep_of_eclass(e) = mems(1);
wolffd@0 86 end
wolffd@0 87
wolffd@0 88 directed = 1;
wolffd@0 89 if ~acyclic(dag,directed)
wolffd@0 90 error('graph must be acyclic')
wolffd@0 91 end
wolffd@0 92
wolffd@0 93 bnet.order = topological_sort(bnet.dag);