Daniel@0: function engine = cbk_inf_engine(bnet, varargin) Daniel@0: % Just the same as bk_inf_engine, but you can specify overlapping clusters. Daniel@0: Daniel@0: ss = length(bnet.intra); Daniel@0: % set default params Daniel@0: clusters = 'exact'; Daniel@0: Daniel@0: if nargin >= 2 Daniel@0: args = varargin; Daniel@0: nargs = length(args); Daniel@0: for i=1:2:nargs Daniel@0: switch args{i}, Daniel@0: case 'clusters', clusters = args{i+1}; Daniel@0: otherwise, error(['unrecognized argument ' args{i}]) Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: if strcmp(clusters, 'exact') Daniel@0: %clusters = { compute_interface_nodes(bnet.intra, bnet.inter) }; Daniel@0: clusters = { 1:ss }; Daniel@0: elseif strcmp(clusters, 'ff') Daniel@0: clusters = num2cell(1:ss); Daniel@0: end Daniel@0: Daniel@0: Daniel@0: % We need to insert the prior on the clusters in slice 1, Daniel@0: % and extract the posterior on the clusters in slice 2. Daniel@0: % We don't need to care about the separators, b/c they're subsets of the clusters. Daniel@0: C = length(clusters); Daniel@0: clusters2 = cell(1,2*C); Daniel@0: clusters2(1:C) = clusters; Daniel@0: for c=1:C Daniel@0: clusters2{c+C} = clusters{c} + ss; Daniel@0: end Daniel@0: Daniel@0: onodes = bnet.observed; Daniel@0: obs_nodes = [onodes(:) onodes(:)+ss]; Daniel@0: engine.sub_engine = jtree_inf_engine(bnet, 'clusters', clusters2); Daniel@0: Daniel@0: %FH >>> Daniel@0: %Compute separators. Daniel@0: ns = bnet.node_sizes(:,1); Daniel@0: ns(onodes) = 1; Daniel@0: [clusters, separators] = build_jt(clusters, 1:length(ns), ns); Daniel@0: S = length(separators); Daniel@0: engine.separators = separators; Daniel@0: Daniel@0: %Compute size of clusters. Daniel@0: cl_sizes = zeros(1,C); Daniel@0: for c=1:C Daniel@0: cl_sizes(c) = prod(ns(clusters{c})); Daniel@0: end Daniel@0: Daniel@0: %Assign separators to the smallest cluster subsuming them. Daniel@0: engine.cluster_ass_to_separator = zeros(S, 1); Daniel@0: for s=1:S Daniel@0: subsuming_clusters = []; Daniel@0: %find smaunk Daniel@0: Daniel@0: for c=1:C Daniel@0: if mysubset(separators{s}, clusters{c}) Daniel@0: subsuming_clusters(end+1) = c; Daniel@0: end Daniel@0: end Daniel@0: c = argmin(cl_sizes(subsuming_clusters)); Daniel@0: engine.cluster_ass_to_separator(s) = subsuming_clusters(c); Daniel@0: end Daniel@0: Daniel@0: %<<< FH Daniel@0: Daniel@0: engine.clq_ass_to_cluster = zeros(C, 2); Daniel@0: for c=1:C Daniel@0: engine.clq_ass_to_cluster(c,1) = clq_containing_nodes(engine.sub_engine, clusters{c}); Daniel@0: engine.clq_ass_to_cluster(c,2) = clq_containing_nodes(engine.sub_engine, clusters{c}+ss); Daniel@0: end Daniel@0: engine.clusters = clusters; Daniel@0: Daniel@0: engine.clq_ass_to_node = zeros(ss, 2); Daniel@0: for i=1:ss Daniel@0: engine.clq_ass_to_node(i, 1) = clq_containing_nodes(engine.sub_engine, i); Daniel@0: engine.clq_ass_to_node(i, 2) = clq_containing_nodes(engine.sub_engine, i+ss); Daniel@0: end Daniel@0: Daniel@0: Daniel@0: Daniel@0: % Also create an engine just for slice 1 Daniel@0: bnet1 = mk_bnet(bnet.intra1, bnet.node_sizes_slice, 'discrete', myintersect(bnet.dnodes, 1:ss), ... Daniel@0: 'equiv_class', bnet.equiv_class(:,1), 'observed', onodes); Daniel@0: for i=1:max(bnet1.equiv_class) Daniel@0: bnet1.CPD{i} = bnet.CPD{i}; Daniel@0: end Daniel@0: Daniel@0: engine.sub_engine1 = jtree_inf_engine(bnet1, 'clusters', clusters); Daniel@0: Daniel@0: engine.clq_ass_to_cluster1 = zeros(1,C); Daniel@0: for c=1:C Daniel@0: engine.clq_ass_to_cluster1(c) = clq_containing_nodes(engine.sub_engine1, clusters{c}); Daniel@0: end Daniel@0: Daniel@0: engine.clq_ass_to_node1 = zeros(1, ss); Daniel@0: for i=1:ss Daniel@0: engine.clq_ass_to_node1(i) = clq_containing_nodes(engine.sub_engine1, i); Daniel@0: end Daniel@0: Daniel@0: engine.clpot = []; % this is where we store the results between enter_evidence and marginal_nodes Daniel@0: engine.filter = []; Daniel@0: engine.maximize = []; Daniel@0: engine.T = []; Daniel@0: Daniel@0: engine.bel = []; Daniel@0: engine.bel_clpot = []; Daniel@0: engine.slice1 = []; Daniel@0: %engine.pot_type = 'cg'; Daniel@0: % hack for online inference so we can cope with hidden Gaussians and discrete Daniel@0: % it will not affect the pot type used in enter_evidence Daniel@0: engine.pot_type = determine_pot_type(bnet, onodes); Daniel@0: Daniel@0: engine = class(engine, 'cbk_inf_engine', inf_engine(bnet)); Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: function [cliques, seps, jt_size] = build_jt(cliques, vars, ns) Daniel@0: % BUILD_JT connects the cliques into a jtree, computes the respective Daniel@0: % separators and the size of the resulting jtree. Daniel@0: % Daniel@0: % [cliques, seps, jt_size] = build_jt(cliques, vars, ns) Daniel@0: % ns(i) has to hold the size of vars(i) Daniel@0: % vars has to be a superset of the union of cliques. Daniel@0: Daniel@0: %======== Compute the jtree with tool from BNT. This wants the vars to be 1:N. Daniel@0: %==== Map from nodes to their indices. Daniel@0: %disp('Computing jtree for cliques with vars and ns:'); Daniel@0: %cliques Daniel@0: %vars Daniel@0: %ns' Daniel@0: Daniel@0: inv_nodes = sparse(1,max(vars)); Daniel@0: N = length(vars); Daniel@0: for i=1:N Daniel@0: inv_nodes(vars(i)) = i; Daniel@0: end Daniel@0: Daniel@0: tmp_cliques = cell(1,length(cliques)); Daniel@0: %==== Temporarily map clique vars to their indices. Daniel@0: for i=1:length(cliques) Daniel@0: tmp_cliques{i} = inv_nodes(cliques{i}); Daniel@0: end Daniel@0: Daniel@0: %=== Compute the jtree, using BNT. Daniel@0: [jtree, root, B, w] = cliques_to_jtree(tmp_cliques, ns); Daniel@0: Daniel@0: Daniel@0: %======== Now, compute the separators between connected cliques and their weights. Daniel@0: seps = {}; Daniel@0: s_w = []; Daniel@0: [is,js] = find(jtree > 0); Daniel@0: for k=1:length(is) Daniel@0: i = is(k); j = js(k); Daniel@0: sep = vars(find(B(i,:) & B(j,:))); % intersect(cliques{i}, cliques{j}); Daniel@0: if i>j | length(sep) == 0, continue; end; Daniel@0: seps{end+1} = sep; Daniel@0: s_w(end+1) = prod(ns(inv_nodes(seps{end}))); Daniel@0: end Daniel@0: Daniel@0: cl_w = sum(w); Daniel@0: sep_w = sum(s_w); Daniel@0: assert(cl_w > sep_w, 'Weight of cliques must be bigger than weight of separators'); Daniel@0: Daniel@0: jt_size = cl_w + sep_w; Daniel@0: % jt.cliques = cliques; Daniel@0: % jt.seps = seps; Daniel@0: % jt.size = jt_size; Daniel@0: % jt.ns = ns'; Daniel@0: % jt;