To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / _FullBNT / BNT / CPDs / @hhmmQ_CPD / Old / update_ess4.m @ 8:b5b38998ef3b

History | View | Annotate | Download (3.04 KB)

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(CPD.self_ndx);
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
  i_fmarg_ndx = 1:Qsz;
33
  eff_oldQsz = Qsz;
34
else
35
  i_counts_ndx = evidence{old_self};
36
  i_fmarg_ndx = 1;
37
  eff_oldQsz = 1;
38
end
39

    
40
if all(hidden_bitv(Qps)) % we assume all are hidden or all are observed
41
  k_counts_ndx = 1:Qpsz;
42
  k_fmarg_ndx = 1:Qpsz;
43
  eff_Qpsz = Qpsz;
44
else
45
  k_counts_ndx = subv2ind(Qpsz, cat(1, evidence{Qps}));
46
  k_fmarg_ndx = 1;
47
  eff_Qpsz = 1;
48
end
49

    
50
if hidden_bitv(self)
51
  j_counts_ndx = 1:Qsz;
52
  j_fmarg_ndx = 1:Qsz;
53
  eff_Qsz = Qsz;
54
else
55
  j_counts_ndx = evidence{self};
56
  j_fmarg_ndx = 1;
57
  eff_Qsz = 1;
58
end
59

    
60
hor_counts = zeros(Qsz, Qpsz, Qsz);
61
ver_counts = zeros(Qpsz, Qsz);
62
    
63
if ~isempty(CPD.Fbelow_ndx)
64
  if ~isempty(CPD.Fself_ndx) % general case
65
    fmarg.T = myreshape(fmarg.T, [eff_oldQsz 2 2 eff_Qpsz eff_Qsz]);
66
    hor_counts(i_counts_ndx, k_counts_ndx, j_counts_ndx) = ...
67
	fmarg.T(:, i_fmarg_ndx, 2, 1, k_fmarg_ndx, j_fmarg_ndx);
68
    ver_counts(k_counts_ndx, j_counts_ndx) = ... % sum over Fbelow and Qold
69
	sum(fmarg.T(:, 1, 2, k_fmarg_ndx, j_fmarg_ndx), 1) + ...
70
	sum(fmarg.T(:, 2, 2, k_fmarg_ndx, j_fmarg_ndx), 1); 
71
  else % no F from self, hence no startprob
72
    fmarg.T = myreshape(fmarg.T, [eff_oldQsz 2 eff_Qpsz eff_Qsz]);
73
    hor_counts(i_counts_ndx, k_counts_ndx, j_counts_ndx) = ...
74
	fmarg.T(i_fmarg_ndx, 2, k_fmarg_ndx, j_fmarg_ndx);
75
  end
76
else % no F signal from below
77
  if ~isempty(CPD.Fself_ndx) % self F
78
    fmarg.T = myreshape(fmarg.T, [eff_oldQsz 2 eff_Qpsz eff_Qsz]);
79
    hor_counts(i_counts_ndx, k_counts_ndx, j_counts_ndx) = ...
80
	fmarg.T(i_fmarg_ndx, 1, k_fmarg_ndx, j_fmarg_ndx);
81
    ver_counts(k_counts_ndx, j_counts_ndx) = ... % sum over Qold
82
	sum(fmarg.T(:, 2, k_fmarg_ndx, j_fmarg_ndx), 1);
83
  else % no F from self
84
    error('An hhmmQ node without any F parents is just a tabular_CPD')
85
  end
86
end
87

    
88

    
89
CPD.sub_CPD_trans = update_ess_simple(CPD.sub_CPD_trans, hor_counts);
90

    
91
if ~isempty(CPD.sub_CPD_start)
92
  CPD.sub_CPD_start = update_ess_simple(CPD.sub_CPD_start, ver_counts);
93
end
94

    
95