annotate toolboxes/FullBNT-1.0.7/bnt/inference/online/@jtree_sparse_2TBN_inf_engine/jtree_sparse_2TBN_inf_engine.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function engine = jtree_sparse_2TBN_inf_engine(bnet, varargin)
wolffd@0 2 % JTREE_ONLINE_INF_ENGINE Online Junction tree inference algorithm for DBNs.
wolffd@0 3 % engine = jtree_online_inf_engine(bnet, ...)
wolffd@0 4 %
wolffd@0 5 % The following optional arguments can be specified in the form of name/value pairs:
wolffd@0 6 % [default value in brackets]
wolffd@0 7 %
wolffd@0 8 % clusters - specifies variables that must be grouped in the 1.5 slice DBN
wolffd@0 9 % maximize - 1 means do max-product, 0 means sum-product [0]
wolffd@0 10 %
wolffd@0 11 % The same nodes must be observed in every slice.
wolffd@0 12
wolffd@0 13 ss = length(bnet.intra);
wolffd@0 14 clusters = {};
wolffd@0 15 engine.maximize = 0;
wolffd@0 16
wolffd@0 17 args = varargin;
wolffd@0 18 nargs = length(args);
wolffd@0 19 for i=1:2:length(args)
wolffd@0 20 switch args{i},
wolffd@0 21 case 'clusters', clusters = args{i+1};
wolffd@0 22 case 'maximize', engine.maximize = args{i+1};
wolffd@0 23 otherwise, error(['unrecognized argument ' args{i}])
wolffd@0 24 end
wolffd@0 25 end
wolffd@0 26
wolffd@0 27 engine.evidence = [];
wolffd@0 28 engine.node_sizes = [];
wolffd@0 29
wolffd@0 30 int = [];
wolffd@0 31 % include nodes with any outgoing arcs
wolffd@0 32 for u=1:ss
wolffd@0 33 if any(bnet.inter(u,:))
wolffd@0 34 int = [int u];
wolffd@0 35 end
wolffd@0 36 end
wolffd@0 37
wolffd@0 38 engine.interface = int;
wolffd@0 39 engine.nonint = mysetdiff(1:ss, int);
wolffd@0 40
wolffd@0 41 onodes = bnet.observed;
wolffd@0 42
wolffd@0 43 % Create a "1.5 slice" jtree, containing the interface nodes of slice 1
wolffd@0 44 % and all the nodes of slice 2
wolffd@0 45 % To keep the node numbering the same, we simply disconnect the non-interface nodes
wolffd@0 46 % from slice 1, and set their size to 1.
wolffd@0 47 % We do this to speed things up, and so that the likelihood is computed correctly - we do not need to do
wolffd@0 48 % this if we just want to compute marginals (i.e., we can include nodes whose potentials will
wolffd@0 49 % be left as all 1s).
wolffd@0 50 intra15 = bnet.intra;
wolffd@0 51 for i=engine.nonint(:)'
wolffd@0 52 intra15(:,i) = 0;
wolffd@0 53 intra15(i,:) = 0;
wolffd@0 54 assert(~any(bnet.inter(i,:)))
wolffd@0 55 end
wolffd@0 56 dag15 = [intra15 bnet.inter;
wolffd@0 57 zeros(ss) bnet.intra];
wolffd@0 58 ns = bnet.node_sizes(:);
wolffd@0 59 ns(engine.nonint) = 1; % disconnected nodes get size 1
wolffd@0 60 obs_nodes = [onodes(:) onodes(:)+ss];
wolffd@0 61 bnet15 = mk_bnet(dag15, ns, 'discrete', bnet.dnodes, 'equiv_class', bnet.equiv_class(:), ...
wolffd@0 62 'observed', obs_nodes(:));
wolffd@0 63
wolffd@0 64 % use unconstrained elimination,
wolffd@0 65 % but force there to be a clique containing both interfaces
wolffd@0 66 clusters(end+1:end+2) = {int, int+ss};
wolffd@0 67 %engine.jtree_engine = jtree_inf_engine(bnet15, 'clusters', clusters, 'root', int+ss);
wolffd@0 68 engine.jtree_engine = jtree_sparse_inf_engine(bnet15, 'clusters', clusters, 'root', int+ss);
wolffd@0 69 jtree_engine = struct(engine.jtree_engine); % violate object privacy
wolffd@0 70
wolffd@0 71 engine.in_clq = clq_containing_nodes(engine.jtree_engine, int);
wolffd@0 72 engine.out_clq = clq_containing_nodes(engine.jtree_engine, int+ss);
wolffd@0 73 engine.clq_ass_to_node = jtree_engine.clq_ass_to_node;
wolffd@0 74 engine.root = jtree_engine.root_clq;
wolffd@0 75
wolffd@0 76 % Also create an engine just for slice 1
wolffd@0 77 bnet1 = mk_bnet(bnet.intra1, bnet.node_sizes_slice, 'discrete', myintersect(bnet.dnodes,1:ss), ...
wolffd@0 78 'equiv_class', bnet.equiv_class(:,1), 'observed', onodes);
wolffd@0 79 for i=1:max(bnet1.equiv_class)
wolffd@0 80 bnet1.CPD{i} = bnet.CPD{i};
wolffd@0 81 end
wolffd@0 82 %engine.jtree_engine1 = jtree_inf_engine(bnet1, 'clusters', {int}, 'root', int);
wolffd@0 83 engine.jtree_engine1 = jtree_sparse_inf_engine(bnet1, 'clusters', {int}, 'root', int);
wolffd@0 84 jtree_engine1 = struct(engine.jtree_engine1); % violate object privacy
wolffd@0 85 engine.int_clq1 = clq_containing_nodes(engine.jtree_engine1, int);
wolffd@0 86 engine.clq_ass_to_node1 = jtree_engine1.clq_ass_to_node;
wolffd@0 87 engine.root1 = jtree_engine1.root_clq;
wolffd@0 88
wolffd@0 89 engine.observed = [onodes onodes+ss];
wolffd@0 90 engine.observed1 = onodes;
wolffd@0 91 engine.pot_type = determine_pot_type(bnet, onodes);
wolffd@0 92 engine.slice_size = bnet.nnodes_per_slice;
wolffd@0 93
wolffd@0 94 engine = class(engine, 'jtree_sparse_2TBN_inf_engine', inf_engine(bnet));
wolffd@0 95