Daniel@0
|
1 function marginal = marginal_nodes(engine, nodes, t, fam)
|
Daniel@0
|
2 % MARGINAL_NODES Compute the marginal on the specified query nodes (bk)
|
Daniel@0
|
3 %
|
Daniel@0
|
4 % marginal = marginal_nodes(engine, i, t)
|
Daniel@0
|
5 % returns Pr(X(i,t) | Y(1:T)), where X(i,t) is the i'th node in the t'th slice.
|
Daniel@0
|
6 % If enter_evidence used filtering instead of smoothing, this will return Pr(X(i,t) | Y(1:t)).
|
Daniel@0
|
7 %
|
Daniel@0
|
8 % marginal = marginal_nodes(engine, query, t)
|
Daniel@0
|
9 % returns Pr(X(query(1),t), ... X(query(end),t) | Y(1:T)),
|
Daniel@0
|
10 % where X(q,t) is the q'th node in the t'th slice. If q > ss (slice size), this is equal
|
Daniel@0
|
11 % to X(q mod ss, t+1). That is, 't' specifies the time slice of the earliest node.
|
Daniel@0
|
12 % 'query' cannot span more than 2 time slices.
|
Daniel@0
|
13 % Example:
|
Daniel@0
|
14 % Consider a DBN with 2 nodes per slice.
|
Daniel@0
|
15 % Then t=2, nodes=[1 3] refers to node 1 in slice 2 and node 1 in slice 3.
|
Daniel@0
|
16
|
Daniel@0
|
17 if nargin < 3, t = 1; end
|
Daniel@0
|
18 if nargin < 4, fam = 0; else fam = 1; end
|
Daniel@0
|
19
|
Daniel@0
|
20
|
Daniel@0
|
21 % clpot{t} contains slice t-1 and t
|
Daniel@0
|
22 % Example
|
Daniel@0
|
23 % clpot #: 1 2 3
|
Daniel@0
|
24 % slices: 1 1,2 2,3
|
Daniel@0
|
25 % For filtering, we must take care not to take future evidence into account.
|
Daniel@0
|
26 % For smoothing, clpot{1} does not exist.
|
Daniel@0
|
27
|
Daniel@0
|
28 bnet = bnet_from_engine(engine);
|
Daniel@0
|
29 ss = length(bnet.intra);
|
Daniel@0
|
30
|
Daniel@0
|
31 nodes2 = nodes;
|
Daniel@0
|
32 if ~engine.filter
|
Daniel@0
|
33 if t < engine.T
|
Daniel@0
|
34 slice = t+1;
|
Daniel@0
|
35 else % earliest t is T, so all nodes fit in one slice
|
Daniel@0
|
36 slice = engine.T;
|
Daniel@0
|
37 nodes2 = nodes + ss;
|
Daniel@0
|
38 end
|
Daniel@0
|
39 else
|
Daniel@0
|
40 if t == 1
|
Daniel@0
|
41 slice = 1;
|
Daniel@0
|
42 else
|
Daniel@0
|
43 if all(nodes<=ss)
|
Daniel@0
|
44 slice = t;
|
Daniel@0
|
45 nodes2 = nodes + ss;
|
Daniel@0
|
46 elseif t == engine.T
|
Daniel@0
|
47 slice = t;
|
Daniel@0
|
48 else
|
Daniel@0
|
49 slice = t + 1;
|
Daniel@0
|
50 end
|
Daniel@0
|
51 end
|
Daniel@0
|
52 end
|
Daniel@0
|
53
|
Daniel@0
|
54 if engine.filter & t==1
|
Daniel@0
|
55 c = clq_containing_nodes(engine.sub_engine1, nodes2, fam);
|
Daniel@0
|
56 else
|
Daniel@0
|
57 c = clq_containing_nodes(engine.sub_engine, nodes2, fam);
|
Daniel@0
|
58 end
|
Daniel@0
|
59 assert(c >= 1);
|
Daniel@0
|
60 bigpot = engine.clpot{c, slice};
|
Daniel@0
|
61
|
Daniel@0
|
62 pot = marginalize_pot(bigpot, nodes2);
|
Daniel@0
|
63 marginal = pot_to_marginal(pot);
|
Daniel@0
|
64
|
Daniel@0
|
65 % we convert the domain to the unrolled numbering system
|
Daniel@0
|
66 % so that update_ess extracts the right evidence.
|
Daniel@0
|
67 marginal.domain = nodes+(t-1)*ss;
|