wolffd@0: function CPT2 = mk_named_CPT(family_names, names, dag, CPT1) wolffd@0: % MK_NAMED_CPT Permute the dimensions of a CPT so they agree with the internal numbering convention wolffd@0: % CPT2 = mk_named_CPT(family_names, names, dag, CPT1) wolffd@0: % wolffd@0: % This is best explained by example. wolffd@0: % Consider the following directed acyclic graph wolffd@0: % wolffd@0: % C wolffd@0: % / \ wolffd@0: % R S wolffd@0: % \ / wolffd@0: % W wolffd@0: % wolffd@0: % where all arcs point down. wolffd@0: % When we create the CPT for node W, we consider S as its first parent, and R as its wolffd@0: % second, and hence write wolffd@0: % wolffd@0: % S R W wolffd@0: % CPT1(1,1,:) = [1.0 0.0]; wolffd@0: % CPT1(2,1,:) = [0.2 0.8]; % P(W=1 | R=1, S=2) = 0.2 wolffd@0: % CPT1(1,2,:) = [0.1 0.9]; wolffd@0: % CPT1(2,2,:) = [0.01 0.99]; wolffd@0: % wolffd@0: % However, when we create the dag using mk_adj_mat, the nodes get topologically sorted, wolffd@0: % and by chance, node R preceeds node S in this ordering. wolffd@0: % Hence we should have written wolffd@0: % wolffd@0: % R S W wolffd@0: % CPT2(1,1,:) = [1.0 0.0]; wolffd@0: % CPT2(2,1,:) = [0.1 0.9]; wolffd@0: % CPT2(1,2,:) = [0.2 0.8]; % P(W=1 | R=1, S=2) = 0.2 wolffd@0: % CPT2(2,2,:) = [0.01 0.99]; wolffd@0: % wolffd@0: % Since we do not know the order of the nodes in advance, we can write wolffd@0: % CPT2 = mk_named_CPT({'S', 'R', 'W'}, names, dag, CPT1) wolffd@0: % where 'S', 'R', 'W' are the order of the dimensions we assumed (the child node must be last in this list), wolffd@0: % and names{i} is the name of the i'th node. wolffd@0: wolffd@0: n = length(family_names); wolffd@0: family_nums = zeros(1,n); wolffd@0: for i=1:n wolffd@0: family_nums(i) = stringmatch(family_names{i}, names); % was strmatch wolffd@0: end wolffd@0: wolffd@0: fam = family(dag, family_nums(end)); wolffd@0: perm = zeros(1,n); wolffd@0: for i=1:n wolffd@0: % perm(i) = find(family_nums(i) == fam); wolffd@0: perm(i) = find(fam(i) == family_nums); wolffd@0: end wolffd@0: wolffd@0: CPT2 = permute(CPT1, perm);