wolffd@0: function engine = jtree_sparse_2TBN_inf_engine(bnet, varargin) wolffd@0: % JTREE_ONLINE_INF_ENGINE Online Junction tree inference algorithm for DBNs. wolffd@0: % engine = jtree_online_inf_engine(bnet, ...) wolffd@0: % wolffd@0: % The following optional arguments can be specified in the form of name/value pairs: wolffd@0: % [default value in brackets] wolffd@0: % wolffd@0: % clusters - specifies variables that must be grouped in the 1.5 slice DBN wolffd@0: % maximize - 1 means do max-product, 0 means sum-product [0] wolffd@0: % wolffd@0: % The same nodes must be observed in every slice. wolffd@0: wolffd@0: ss = length(bnet.intra); wolffd@0: clusters = {}; wolffd@0: engine.maximize = 0; wolffd@0: wolffd@0: args = varargin; wolffd@0: nargs = length(args); wolffd@0: for i=1:2:length(args) wolffd@0: switch args{i}, wolffd@0: case 'clusters', clusters = args{i+1}; wolffd@0: case 'maximize', engine.maximize = args{i+1}; wolffd@0: otherwise, error(['unrecognized argument ' args{i}]) wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: engine.evidence = []; wolffd@0: engine.node_sizes = []; wolffd@0: wolffd@0: int = []; wolffd@0: % include nodes with any outgoing arcs wolffd@0: for u=1:ss wolffd@0: if any(bnet.inter(u,:)) wolffd@0: int = [int u]; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: engine.interface = int; wolffd@0: engine.nonint = mysetdiff(1:ss, int); wolffd@0: wolffd@0: onodes = bnet.observed; wolffd@0: wolffd@0: % Create a "1.5 slice" jtree, containing the interface nodes of slice 1 wolffd@0: % and all the nodes of slice 2 wolffd@0: % To keep the node numbering the same, we simply disconnect the non-interface nodes wolffd@0: % from slice 1, and set their size to 1. wolffd@0: % We do this to speed things up, and so that the likelihood is computed correctly - we do not need to do wolffd@0: % this if we just want to compute marginals (i.e., we can include nodes whose potentials will wolffd@0: % be left as all 1s). wolffd@0: intra15 = bnet.intra; wolffd@0: for i=engine.nonint(:)' wolffd@0: intra15(:,i) = 0; wolffd@0: intra15(i,:) = 0; wolffd@0: assert(~any(bnet.inter(i,:))) wolffd@0: end wolffd@0: dag15 = [intra15 bnet.inter; wolffd@0: zeros(ss) bnet.intra]; wolffd@0: ns = bnet.node_sizes(:); wolffd@0: ns(engine.nonint) = 1; % disconnected nodes get size 1 wolffd@0: obs_nodes = [onodes(:) onodes(:)+ss]; wolffd@0: bnet15 = mk_bnet(dag15, ns, 'discrete', bnet.dnodes, 'equiv_class', bnet.equiv_class(:), ... wolffd@0: 'observed', obs_nodes(:)); wolffd@0: wolffd@0: % use unconstrained elimination, wolffd@0: % but force there to be a clique containing both interfaces wolffd@0: clusters(end+1:end+2) = {int, int+ss}; wolffd@0: %engine.jtree_engine = jtree_inf_engine(bnet15, 'clusters', clusters, 'root', int+ss); wolffd@0: engine.jtree_engine = jtree_sparse_inf_engine(bnet15, 'clusters', clusters, 'root', int+ss); wolffd@0: jtree_engine = struct(engine.jtree_engine); % violate object privacy wolffd@0: wolffd@0: engine.in_clq = clq_containing_nodes(engine.jtree_engine, int); wolffd@0: engine.out_clq = clq_containing_nodes(engine.jtree_engine, int+ss); wolffd@0: engine.clq_ass_to_node = jtree_engine.clq_ass_to_node; wolffd@0: engine.root = jtree_engine.root_clq; wolffd@0: wolffd@0: % Also create an engine just for slice 1 wolffd@0: bnet1 = mk_bnet(bnet.intra1, bnet.node_sizes_slice, 'discrete', myintersect(bnet.dnodes,1:ss), ... wolffd@0: 'equiv_class', bnet.equiv_class(:,1), 'observed', onodes); wolffd@0: for i=1:max(bnet1.equiv_class) wolffd@0: bnet1.CPD{i} = bnet.CPD{i}; wolffd@0: end wolffd@0: %engine.jtree_engine1 = jtree_inf_engine(bnet1, 'clusters', {int}, 'root', int); wolffd@0: engine.jtree_engine1 = jtree_sparse_inf_engine(bnet1, 'clusters', {int}, 'root', int); wolffd@0: jtree_engine1 = struct(engine.jtree_engine1); % violate object privacy wolffd@0: engine.int_clq1 = clq_containing_nodes(engine.jtree_engine1, int); wolffd@0: engine.clq_ass_to_node1 = jtree_engine1.clq_ass_to_node; wolffd@0: engine.root1 = jtree_engine1.root_clq; wolffd@0: wolffd@0: engine.observed = [onodes onodes+ss]; wolffd@0: engine.observed1 = onodes; wolffd@0: engine.pot_type = determine_pot_type(bnet, onodes); wolffd@0: engine.slice_size = bnet.nnodes_per_slice; wolffd@0: wolffd@0: engine = class(engine, 'jtree_sparse_2TBN_inf_engine', inf_engine(bnet)); wolffd@0: