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