annotate toolboxes/FullBNT-1.0.7/bnt/general/mk_named_CPT.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function CPT2 = mk_named_CPT(family_names, names, dag, CPT1)
Daniel@0 2 % MK_NAMED_CPT Permute the dimensions of a CPT so they agree with the internal numbering convention
Daniel@0 3 % CPT2 = mk_named_CPT(family_names, names, dag, CPT1)
Daniel@0 4 %
Daniel@0 5 % This is best explained by example.
Daniel@0 6 % Consider the following directed acyclic graph
Daniel@0 7 %
Daniel@0 8 % C
Daniel@0 9 % / \
Daniel@0 10 % R S
Daniel@0 11 % \ /
Daniel@0 12 % W
Daniel@0 13 %
Daniel@0 14 % where all arcs point down.
Daniel@0 15 % When we create the CPT for node W, we consider S as its first parent, and R as its
Daniel@0 16 % second, and hence write
Daniel@0 17 %
Daniel@0 18 % S R W
Daniel@0 19 % CPT1(1,1,:) = [1.0 0.0];
Daniel@0 20 % CPT1(2,1,:) = [0.2 0.8]; % P(W=1 | R=1, S=2) = 0.2
Daniel@0 21 % CPT1(1,2,:) = [0.1 0.9];
Daniel@0 22 % CPT1(2,2,:) = [0.01 0.99];
Daniel@0 23 %
Daniel@0 24 % However, when we create the dag using mk_adj_mat, the nodes get topologically sorted,
Daniel@0 25 % and by chance, node R preceeds node S in this ordering.
Daniel@0 26 % Hence we should have written
Daniel@0 27 %
Daniel@0 28 % R S W
Daniel@0 29 % CPT2(1,1,:) = [1.0 0.0];
Daniel@0 30 % CPT2(2,1,:) = [0.1 0.9];
Daniel@0 31 % CPT2(1,2,:) = [0.2 0.8]; % P(W=1 | R=1, S=2) = 0.2
Daniel@0 32 % CPT2(2,2,:) = [0.01 0.99];
Daniel@0 33 %
Daniel@0 34 % Since we do not know the order of the nodes in advance, we can write
Daniel@0 35 % CPT2 = mk_named_CPT({'S', 'R', 'W'}, names, dag, CPT1)
Daniel@0 36 % where 'S', 'R', 'W' are the order of the dimensions we assumed (the child node must be last in this list),
Daniel@0 37 % and names{i} is the name of the i'th node.
Daniel@0 38
Daniel@0 39 n = length(family_names);
Daniel@0 40 family_nums = zeros(1,n);
Daniel@0 41 for i=1:n
Daniel@0 42 family_nums(i) = stringmatch(family_names{i}, names); % was strmatch
Daniel@0 43 end
Daniel@0 44
Daniel@0 45 fam = family(dag, family_nums(end));
Daniel@0 46 perm = zeros(1,n);
Daniel@0 47 for i=1:n
Daniel@0 48 % perm(i) = find(family_nums(i) == fam);
Daniel@0 49 perm(i) = find(fam(i) == family_nums);
Daniel@0 50 end
Daniel@0 51
Daniel@0 52 CPT2 = permute(CPT1, perm);