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