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