wolffd@0: function bnet = mk_bnet(dag, node_sizes, varargin) wolffd@0: % MK_BNET Make a Bayesian network. wolffd@0: % wolffd@0: % BNET = MK_BNET(DAG, NODE_SIZES, ...) makes a graphical model with an arc from i to j iff DAG(i,j) = 1. wolffd@0: % Thus DAG is the adjacency matrix for a directed acyclic graph. wolffd@0: % The nodes are assumed to be in topological order. Use TOPOLOGICAL_SORT if necessary. wolffd@0: % wolffd@0: % node_sizes(i) is the number of values node i can take on, wolffd@0: % or the length of node i if i is a continuous-valued vector. wolffd@0: % node_sizes(i) = 1 if i is a utility node. wolffd@0: % wolffd@0: % Below are the names of optional arguments [and their default value in brackets]. wolffd@0: % Pass as 'PropertyName1', PropertyValue1, 'PropertyName2', PropertyValue2, ... wolffd@0: % wolffd@0: % discrete - the list of nodes which are discrete random variables [1:N] wolffd@0: % equiv_class - equiv_class(i)=j means node i gets its params from CPD{j} [1:N] wolffd@0: % observed - the list of nodes which will definitely be observed in every case [ [] ] wolffd@0: % 'names' - a cell array of strings to be associated with nodes 1:n [{}] wolffd@0: % This creates an associative array, so you write e.g. wolffd@0: % 'evidence(bnet.names{'bar'}) = 42' instead of 'evidence(2} = 42' wolffd@0: % assuming names = { 'foo', 'bar', ...}. wolffd@0: % wolffd@0: % e.g., bnet = mk_bnet(dag, ns, 'discrete', [1 3]) wolffd@0: % wolffd@0: % For backwards compatibility with BNT2, you can also specify the parameters in the following order wolffd@0: % bnet = mk_bnet(dag, node_sizes, discrete_nodes, equiv_class) wolffd@0: wolffd@0: n = length(dag); wolffd@0: wolffd@0: % default values for parameters wolffd@0: bnet.equiv_class = 1:n; wolffd@0: bnet.dnodes = 1:n; % discrete wolffd@0: bnet.observed = []; wolffd@0: bnet.names = {}; wolffd@0: wolffd@0: if nargin >= 3 wolffd@0: args = varargin; wolffd@0: nargs = length(args); wolffd@0: if ~isstr(args{1}) wolffd@0: if nargs >= 1, bnet.dnodes = args{1}; end wolffd@0: if nargs >= 2, bnet.equiv_class = args{2}; end wolffd@0: else wolffd@0: for i=1:2:nargs wolffd@0: switch args{i}, wolffd@0: case 'equiv_class', bnet.equiv_class = args{i+1}; wolffd@0: case 'discrete', bnet.dnodes = args{i+1}; wolffd@0: case 'observed', bnet.observed = args{i+1}; wolffd@0: case 'names', bnet.names = assocarray(args{i+1}, num2cell(1:n)); wolffd@0: otherwise, wolffd@0: error(['invalid argument name ' args{i}]); wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: bnet.observed = sort(bnet.observed); % for comparing sets wolffd@0: bnet.hidden = mysetdiff(1:n, bnet.observed(:)'); wolffd@0: bnet.hidden_bitv = zeros(1,n); wolffd@0: bnet.hidden_bitv(bnet.hidden) = 1; wolffd@0: bnet.dag = dag; wolffd@0: bnet.node_sizes = node_sizes(:)'; wolffd@0: wolffd@0: bnet.cnodes = mysetdiff(1:n, bnet.dnodes); wolffd@0: % too many functions refer to cnodes to rename it to cts_nodes - wolffd@0: % We hope it won't be confused with chance nodes! wolffd@0: wolffd@0: bnet.parents = cell(1,n); wolffd@0: for i=1:n wolffd@0: bnet.parents{i} = parents(dag, i); wolffd@0: end wolffd@0: wolffd@0: E = max(bnet.equiv_class); wolffd@0: mem = cell(1,E); wolffd@0: for i=1:n wolffd@0: e = bnet.equiv_class(i); wolffd@0: mem{e} = [mem{e} i]; wolffd@0: end wolffd@0: bnet.members_of_equiv_class = mem; wolffd@0: wolffd@0: bnet.CPD = cell(1, E); wolffd@0: wolffd@0: bnet.rep_of_eclass = zeros(1,E); wolffd@0: for e=1:E wolffd@0: mems = bnet.members_of_equiv_class{e}; wolffd@0: bnet.rep_of_eclass(e) = mems(1); wolffd@0: end wolffd@0: wolffd@0: directed = 1; wolffd@0: if ~acyclic(dag,directed) wolffd@0: error('graph must be acyclic') wolffd@0: end wolffd@0: wolffd@0: bnet.order = topological_sort(bnet.dag);