comparison toolboxes/FullBNT-1.0.7/bnt/CPDs/@hhmmQ_CPD/update_ess.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 CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, hidden_bitv)
2 % UPDATE_ESS Update the Expected Sufficient Statistics of a hhmm Q node.
3 % function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, idden_bitv)
4 %
5 % we assume if one of the Qps is observed, all of them are
6 % We assume the F nodes are already hidden
7
8 % Figure out the node numbers associated with each parent
9 dom = fmarginal.domain;
10 self = dom(end);
11 old_self = dom(CPD.old_self_ndx);
12 %Fself = dom(CPD.Fself_ndx);
13 %Fbelow = dom(CPD.Fbelow_ndx);
14 Qps = dom(CPD.Qps_ndx);
15
16 Qsz = CPD.Qsz;
17 Qpsz = CPD.Qpsz;
18
19
20 % hor_counts(old_self, Qps, self),
21 % fmarginal(old_self, Fbelow, Fself, Qps, self)
22 % hor_counts(i,k,j) = fmarginal(i,2,1,k,j) % below has finished, self has not
23 % ver_counts(i,k,j) = fmarginal(i,2,2,k,j) % below has finished, and so has self (reset)
24 % Since any of i,j,k may be observed, we write
25 % hor_counts(i_counts_ndx, kndx, jndx) = fmarginal(i_fmarg_ndx...)
26 % where i_fmarg_ndx = 1 and i_counts_ndx = i if old_self is observed to have value i,
27 % i_fmarg_ndx = 1:Qsz and i_counts_ndx = 1:Qsz if old_self is hidden, etc.
28
29
30 if hidden_bitv(old_self)
31 i_counts_ndx = 1:Qsz;
32 eff_oldQsz = Qsz;
33 else
34 i_counts_ndx = evidence{old_self};
35 eff_oldQsz = 1;
36 end
37
38 if all(hidden_bitv(Qps)) % we assume all are hidden or all are observed
39 k_counts_ndx = 1:Qpsz;
40 eff_Qpsz = Qpsz;
41 else
42 k_counts_ndx = subv2ind(Qpsz, cat(1, evidence{Qps}));
43 eff_Qpsz = 1;
44 end
45
46 if hidden_bitv(self)
47 j_counts_ndx = 1:Qsz;
48 eff_Qsz = Qsz;
49 else
50 j_counts_ndx = evidence{self};
51 eff_Qsz = 1;
52 end
53
54 hor_counts = zeros(Qsz, Qpsz, Qsz);
55 ver_counts = zeros(Qpsz, Qsz);
56
57 if ~isempty(CPD.Fbelow_ndx)
58 if ~isempty(CPD.Fself_ndx) % general case
59 fmarg = myreshape(fmarginal.T, [eff_oldQsz 2 2 eff_Qpsz eff_Qsz]);
60 hor_counts(i_counts_ndx, k_counts_ndx, j_counts_ndx) = fmarg(:, 2, 1, :, :);
61 ver_counts(k_counts_ndx, j_counts_ndx) = ... % sum over Fbelow and Qold
62 sumv(fmarg(:, :, 2, :, :), [1 2]); % require Fself=2
63 else % no F from self, hence no startprob
64 fmarg = myreshape(fmarginal.T, [eff_oldQsz 2 eff_Qpsz eff_Qsz]);
65 hor_counts(i_counts_ndx, k_counts_ndx, j_counts_ndx) = ...
66 fmarg(:, 2, :, :); % require Fbelow = 2
67 end
68 else % no F signal from below
69 if ~isempty(CPD.Fself_ndx) % self F
70 fmarg = myreshape(fmarginal.T, [eff_oldQsz 2 eff_Qpsz eff_Qsz]);
71 hor_counts(i_counts_ndx, k_counts_ndx, j_counts_ndx) = fmarg(:, 1, :, :);
72 ver_counts(k_counts_ndx, j_counts_ndx) = ... % sum over Qold
73 squeeze(sum(fmarg(:, 2, :, :), 1)); % Fself=2
74 else % no F from self
75 error('An hhmmQ node without any F parents is just a tabular_CPD')
76 end
77 end
78
79
80 CPD.sub_CPD_trans = update_ess_simple(CPD.sub_CPD_trans, hor_counts);
81
82 if ~isempty(CPD.sub_CPD_start)
83 CPD.sub_CPD_start = update_ess_simple(CPD.sub_CPD_start, ver_counts);
84 end
85
86