comparison toolboxes/FullBNT-1.0.7/bnt/CPDs/@hhmmQ_CPD/Old/update_ess2.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_ess2(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 % Figure out the node numbers associated with each parent
6 dom = fmarginal.domain;
7 self = dom(end); % by assumption
8 old_self = dom(CPD.old_self_ndx);
9 Fself = dom(CPD.Fself_ndx);
10 Fbelow = dom(CPD.Fbelow_ndx);
11 Qps = dom(CPD.Qps_ndx);
12
13 Qsz = CPD.Qsz;
14 Qpsz = CPD.Qpsz;
15
16
17 fmarg = add_ev_to_dmarginal(fmarginal, evidence, ns);
18
19
20
21 % hor_counts(old_self, Qps, self),
22 % fmarginal(old_self, Fbelow, Fself, Qps, self)
23 % hor_counts(i,k,j) = fmarginal(i,2,1,k,j) % below has finished, self has not
24 % ver_counts(i,k,j) = fmarginal(i,2,2,k,j) % below has finished, and so has self (reset)
25 % Since any of i,j,k may be observed, we write
26 % hor_counts(counts_ndx{:}) = fmarginal(fmarg_ndx{:})
27 % where e.g., counts_ndx = {1, ':', 2} if Qps is hidden but we observe old_self=1, self=2.
28 % To create this counts_ndx, we write counts_ndx = mk_multi_ndx(3, obs_dim, obs_val)
29 % where counts_obs_dim = [1 3], counts_obs_val = [1 2] specifies the values of dimensions 1 and 3.
30
31 counts_obs_dim = [];
32 fmarg_obs_dim = [];
33 obs_val = [];
34 if hidden_bitv(self)
35 effQsz = Qsz;
36 else
37 effQsz = 1;
38 counts_obs_dim = [counts_obs_dim 3];
39 fmarg_obs_dim = [fmarg_obs_dim 5];
40 obs_val = [obs_val evidence{self}];
41 end
42
43 % e.g., D=4, d=3, Qps = all Qs above, so dom = [Q3(t-1) F4(t-1) F3(t-1) Q1(t) Q2(t) Q3(t)].
44 % so self = Q3(t), old_self = Q3(t-1), CPD.Qps = [1 2], Qps = [Q1(t) Q2(t)]
45 dom = fmarginal.domain;
46 self = dom(end);
47 old_self = dom(1);
48 Qps = dom(length(dom)-length(CPD.Qps):end-1);
49
50 Qsz = CPD.Qsizes(CPD.d);
51 Qpsz = prod(CPD.Qsizes(CPD.Qps));
52
53 % If some of the Q nodes are observed (which happens during supervised training)
54 % the counts will only be non-zero in positions
55 % consistent with the evidence. We put the computed marginal responsibilities
56 % into the appropriate slots of the big counts array.
57 % (Recall that observed discrete nodes only have a single effective value.)
58 % (A more general, but much slower, way is to call add_evidence_to_dmarginal.)
59 % We assume the F nodes are never observed.
60
61 obs_self = ~hidden_bitv(self);
62 obs_Qps = (~isempty(Qps)) & (~any(hidden_bitv(Qps))); % we assume that all or none of the Q parents are observed
63
64 if obs_self
65 self_val = evidence{self};
66 oldself_val = evidence{old_self};
67 end
68
69 if obs_Qps
70 Qps_val = subv2ind(Qpsz, cat(1, evidence{Qps}));
71 if Qps_val == 0
72 keyboard
73 end
74 end
75
76 if CPD.d==1 % no Qps from above
77 if ~CPD.F1toQ1 % no F from self
78 % marg(Q1(t-1), F2(t-1), Q1(t))
79 % F2(t-1) P(Q1(t)=j | Q1(t-1)=i)
80 % 1 delta(i,j)
81 % 2 transprob(i,j)
82 if obs_self
83 hor_counts = zeros(Qsz, Qsz);
84 hor_counts(oldself_val, self_val) = fmarginal.T(2);
85 else
86 marg = reshape(fmarginal.T, [Qsz 2 Qsz]);
87 hor_counts = squeeze(marg(:,2,:));
88 end
89 else
90 % marg(Q1(t-1), F2(t-1), F1(t-1), Q1(t))
91 % F2(t-1) F1(t-1) P(Qd(t)=j| Qd(t-1)=i)
92 % ------------------------------------------------------
93 % 1 1 delta(i,j)
94 % 2 1 transprob(i,j)
95 % 1 2 impossible
96 % 2 2 startprob(j)
97 if obs_self
98 marg = myreshape(fmarginal.T, [1 2 2 1]);
99 hor_counts = zeros(Qsz, Qsz);
100 hor_counts(oldself_val, self_val) = marg(1,2,1,1);
101 ver_counts = zeros(Qsz, 1);
102 %ver_counts(self_val) = marg(1,2,2,1);
103 ver_counts(self_val) = marg(1,2,2,1) + marg(1,1,2,1);
104 else
105 marg = reshape(fmarginal.T, [Qsz 2 2 Qsz]);
106 hor_counts = squeeze(marg(:,2,1,:));
107 %ver_counts = squeeze(sum(marg(:,2,2,:),1)); % sum over i
108 ver_counts = squeeze(sum(marg(:,2,2,:),1)) + squeeze(sum(marg(:,1,2,:),1)); % sum i,b
109 end
110 end % F1toQ1
111 else % d ~= 1
112 if CPD.d < CPD.D % general case
113 % marg(Qd(t-1), Fd+1(t-1), Fd(t-1), Qps(t), Qd(t))
114 % Fd+1(t-1) Fd(t-1) P(Qd(t)=j| Qd(t-1)=i, Qps(t)=k)
115 % ------------------------------------------------------
116 % 1 1 delta(i,j)
117 % 2 1 transprob(i,k,j)
118 % 1 2 impossible
119 % 2 2 startprob(k,j)
120 if obs_Qps & obs_self
121 marg = myreshape(fmarginal.T, [1 2 2 1 1]);
122 k = 1;
123 hor_counts = zeros(Qsz, Qpsz, Qsz);
124 hor_counts(oldself_val, Qps_val, self_val) = marg(1, 2,1, k,1);
125 ver_counts = zeros(Qpsz, Qsz);
126 %ver_counts(Qps_val, self_val) = marg(1, 2,2, k,1);
127 ver_counts(Qps_val, self_val) = marg(1, 2,2, k,1) + marg(1, 1,2, k,1);
128 elseif obs_Qps & ~obs_self
129 marg = myreshape(fmarginal.T, [Qsz 2 2 1 Qsz]);
130 k = 1;
131 hor_counts = zeros(Qsz, Qpsz, Qsz);
132 hor_counts(:, Qps_val, :) = marg(:, 2,1, k,:);
133 ver_counts = zeros(Qpsz, Qsz);
134 %ver_counts(Qps_val, :) = sum(marg(:, 2,2, k,:), 1);
135 ver_counts(Qps_val, :) = sum(marg(:, 2,2, k,:), 1) + sum(marg(:, 1,2, k,:), 1);
136 elseif ~obs_Qps & obs_self
137 error('not yet implemented')
138 else % everything is hidden
139 marg = reshape(fmarginal.T, [Qsz 2 2 Qpsz Qsz]);
140 hor_counts = squeeze(marg(:,2,1,:,:)); % i,k,j
141 %ver_counts = squeeze(sum(marg(:,2,2,:,:),1)); % sum over i
142 ver_counts = squeeze(sum(marg(:,2,2,:,:),1)) + squeeze(sum(marg(:,1,2,:,:),1)); % sum over i,b
143 end
144 else % d == D, so no F from below
145 % marg(QD(t-1), FD(t-1), Qps(t), QD(t))
146 % FD(t-1) P(QD(t)=j | QD(t-1)=i, Qps(t)=k)
147 % 1 transprob(i,k,j)
148 % 2 startprob(k,j)
149 if obs_Qps & obs_self
150 marg = myreshape(fmarginal.T, [1 2 1 1]);
151 k = 1;
152 hor_counts = zeros(Qsz, Qpsz, Qsz);
153 hor_counts(oldself_val, Qps_val, self_val) = marg(1, 1, k,1);
154 ver_counts = zeros(Qpsz, Qsz);
155 ver_counts(Qps_val, self_val) = marg(1, 2, k,1);
156 elseif obs_Qps & ~obs_self
157 marg = myreshape(fmarginal.T, [Qsz 2 1 Qsz]);
158 k = 1;
159 hor_counts = zeros(Qsz, Qpsz, Qsz);
160 hor_counts(:, Qps_val, :) = marg(:, 1, k,:);
161 ver_counts = zeros(Qpsz, Qsz);
162 ver_counts(Qps_val, :) = sum(marg(:, 2, k, :), 1);
163 elseif ~obs_Qps & obs_self
164 error('not yet implemented')
165 else % everything is hidden
166 marg = reshape(fmarginal.T, [Qsz 2 Qpsz Qsz]);
167 hor_counts = squeeze(marg(:,1,:,:));
168 ver_counts = squeeze(sum(marg(:,2,:,:),1)); % sum over i
169 end
170 end
171 end
172
173 CPD.sub_CPD_trans = update_ess_simple(CPD.sub_CPD_trans, hor_counts);
174
175 if ~isempty(CPD.sub_CPD_start)
176 CPD.sub_CPD_start = update_ess_simple(CPD.sub_CPD_start, ver_counts);
177 end
178