annotate toolboxes/FullBNT-1.0.7/bnt/general/mk_named_CPT.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 CPT2 = mk_named_CPT(family_names, names, dag, CPT1)
wolffd@0 2 % MK_NAMED_CPT Permute the dimensions of a CPT so they agree with the internal numbering convention
wolffd@0 3 % CPT2 = mk_named_CPT(family_names, names, dag, CPT1)
wolffd@0 4 %
wolffd@0 5 % This is best explained by example.
wolffd@0 6 % Consider the following directed acyclic graph
wolffd@0 7 %
wolffd@0 8 % C
wolffd@0 9 % / \
wolffd@0 10 % R S
wolffd@0 11 % \ /
wolffd@0 12 % W
wolffd@0 13 %
wolffd@0 14 % where all arcs point down.
wolffd@0 15 % When we create the CPT for node W, we consider S as its first parent, and R as its
wolffd@0 16 % second, and hence write
wolffd@0 17 %
wolffd@0 18 % S R W
wolffd@0 19 % CPT1(1,1,:) = [1.0 0.0];
wolffd@0 20 % CPT1(2,1,:) = [0.2 0.8]; % P(W=1 | R=1, S=2) = 0.2
wolffd@0 21 % CPT1(1,2,:) = [0.1 0.9];
wolffd@0 22 % CPT1(2,2,:) = [0.01 0.99];
wolffd@0 23 %
wolffd@0 24 % However, when we create the dag using mk_adj_mat, the nodes get topologically sorted,
wolffd@0 25 % and by chance, node R preceeds node S in this ordering.
wolffd@0 26 % Hence we should have written
wolffd@0 27 %
wolffd@0 28 % R S W
wolffd@0 29 % CPT2(1,1,:) = [1.0 0.0];
wolffd@0 30 % CPT2(2,1,:) = [0.1 0.9];
wolffd@0 31 % CPT2(1,2,:) = [0.2 0.8]; % P(W=1 | R=1, S=2) = 0.2
wolffd@0 32 % CPT2(2,2,:) = [0.01 0.99];
wolffd@0 33 %
wolffd@0 34 % Since we do not know the order of the nodes in advance, we can write
wolffd@0 35 % CPT2 = mk_named_CPT({'S', 'R', 'W'}, names, dag, CPT1)
wolffd@0 36 % where 'S', 'R', 'W' are the order of the dimensions we assumed (the child node must be last in this list),
wolffd@0 37 % and names{i} is the name of the i'th node.
wolffd@0 38
wolffd@0 39 n = length(family_names);
wolffd@0 40 family_nums = zeros(1,n);
wolffd@0 41 for i=1:n
wolffd@0 42 family_nums(i) = stringmatch(family_names{i}, names); % was strmatch
wolffd@0 43 end
wolffd@0 44
wolffd@0 45 fam = family(dag, family_nums(end));
wolffd@0 46 perm = zeros(1,n);
wolffd@0 47 for i=1:n
wolffd@0 48 % perm(i) = find(family_nums(i) == fam);
wolffd@0 49 perm(i) = find(fam(i) == family_nums);
wolffd@0 50 end
wolffd@0 51
wolffd@0 52 CPT2 = permute(CPT1, perm);