Translated cliques_to_jtree()

This commit is contained in:
Waldir Leoncio 2022-12-22 14:05:13 +01:00
parent 76d31e593a
commit ac1e73c6fa

View file

@ -1,58 +1,49 @@
cliques_to_jtree <- function(cliques, ns) { cliques_to_jtree <- function(cliques, ns) {
stop("needs translation") # MK_JTREE Make an optimal junction tree.
# function [jtree, root, B, w] = cliques_to_jtree(cliques, ns) # [jtree, root, B, w] = mk_jtree(cliques, ns)
# % MK_JTREE Make an optimal junction tree. # A junction tree is a tree that satisfies the jtree property, which says:
# % [jtree, root, B, w] = mk_jtree(cliques, ns) # for each pair of cliques U, V with intersection S, all cliques on the path between U and V
# % # contain S. (This ensures that local propagation leads to # global consistency.)
# % A junction tree is a tree that satisfies the jtree property, which says: # We can create a junction tree by computing the maximal spanning tree of the junction graph.
# % for each pair of cliques U,V with intersection S, all cliques on the path between U and V # (The junction graph connects all cliques, and the weight of an edge (i, j) is
# % contain S. (This ensures that local propagation leads to global consistency.) # |C(i) intersect C(j)|, where C(i) is the i'th clique.)
# % # The best jtree is the maximal spanning tree which minimizes the sum of the costs on each edge,
# % We can create a junction tree by computing the maximal spanning tree of the junction graph. # where cost[i, j] <- w(C(i)) + w(C(j)), and w(C) is the weight of clique C,
# % (The junction graph connects all cliques, and the weight of an edge (i,j) is # which is the total number of values C can take on.
# % |C(i) intersect C(j)|, where C(i) is the i'th clique.) # For details, see
# % # - Jensen and Jensen, "Optimal Junction Trees", UAI 94.
# % The best jtree is the maximal spanning tree which minimizes the sum of the costs on each edge, # Input:
# % where cost(i,j) = w(C(i)) + w(C(j)), and w(C) is the weight of clique C, # cliques{i} = nodes in clique i
# % which is the total number of values C can take on. # ns[i] <- number of values node i can take on
# % # Output:
# % For details, see # jtree[i, j] <- 1 iff cliques i and j aer connected
# % - Jensen and Jensen, "Optimal Junction Trees", UAI 94. # root <- the clique that should be used as root
# % # B[i, j] <- 1 iff node j occurs in clique i
# % Input: # w[i] <- weight of clique i
# % cliques{i} = nodes in clique i
# % ns(i) = number of values node i can take on
# % Output:
# % jtree(i,j) = 1 iff cliques i and j aer connected
# % root = the clique that should be used as root
# % B(i,j) = 1 iff node j occurs in clique i
# % w(i) = weight of clique i
num_cliques <- length(cliques)
w <- zeros(num_cliques, 1)
B <- zeros(num_cliques, 1)
for (i in 1:num_cliques) {
B[i, cliques[[i]]] <- 1
w[i] <- prod(ns(cliques[[i]]))
}
# C1[i, j] <- length(intersect(cliques{i}, cliques{j}))
# The length of the intersection of two sets is the dot product of their bit vector representation.
C1 <- B %*% t(B)
C1 <- setdiag(C1, 0)
# num_cliques = length(cliques); # C2[i, j] <- w(i) + w(j)
# w = zeros(num_cliques, 1); num_cliques <- length(w)
# B = sparse(num_cliques, 1); W <- repmat(w, c(1, num_cliques))
# for i=1:num_cliques C2 <- W + t(W)
# B(i, cliques{i}) = 1; C2 <- setdiag(C2, 0)
# w(i) = prod(ns(cliques{i}));
# end
jtree <- zeros(minimum_spanning_tree(-C1, C2))# Using - C1 gives * maximum * spanning tree
# % C1(i,j) = length(intersect(cliques{i}, cliques{j})); # The root is arbitrary, but since the first pass is towards the root,
# % The length of the intersection of two sets is the dot product of their bit vector representation. # we would like this to correspond to going forward in time in a DBN.
# C1 = B*B'; root <- num_cliques
# C1 = setdiag(C1, 0); return(list("jtree" = jtree, "root" = root, "B" = B, "w" = w))
# % C2(i,j) = w(i) + w(j)
# num_cliques = length(w);
# W = repmat(w, 1, num_cliques);
# C2 = W + W';
# C2 = setdiag(C2, 0);
# jtree = sparse(minimum_spanning_tree(-C1, C2)); % Using -C1 gives *maximum* spanning tree
# % The root is arbitrary, but since the first pass is towards the root,
# % we would like this to correspond to going forward in time in a DBN.
# root = num_cliques;
} }