all_vars = set(self.nodes())
for node in clique_trees.nodes():
marginalised_nodes = all_vars - set(node)
factor_copy = factor.marginalize(list(marginalised_nodes),
inplace=False)
clique_trees.add_factors(factor_copy)
After Change
clique_trees = JunctionTree(nx.minimum_spanning_tree(complete_graph).edges())
// Check whether the factors are defined for all the random variables or not
all_vars = itertools.chain(*[factor.scope() for factor in self.factors])
if set(all_vars) != set(self.nodes()):
ValueError("Factor for all the random variables not specified")