about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD
diff options
context:
space:
mode:
authorziejd22017-09-28 15:04:40 -0500
committerziejd22017-09-28 15:04:40 -0500
commit8070dc963753142bb86c4ed698d91fd623ed28e7 (patch)
treed0f6dd8fc46a49b819aa55c1a90faa14d8448883 /sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD
parent7cc31810d53176e805532b2789955f4eedbce6bb (diff)
downloadBNW-8070dc963753142bb86c4ed698d91fd623ed28e7.tar.gz
BNW using Octave instead of Matlab.
This version of BNW should perform the same as the original version. The only difference is that it uses Octave instead of Matlab when running BayesNet Toolbox during parameter learning.

I am calling this BNW_1.02. It can be accessed at:
compbio.uthsc.edu/BNW_1.02
Diffstat (limited to 'sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD')
-rw-r--r--sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/CVS/Entries7
-rw-r--r--sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/CVS/Repository1
-rw-r--r--sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/CVS/Root1
-rw-r--r--sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/CVS/Entries7
-rw-r--r--sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/CVS/Repository1
-rw-r--r--sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/CVS/Root1
-rw-r--r--sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/hhmmF_CPD.m76
-rw-r--r--sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/log_prior.m5
-rw-r--r--sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/maximize_params.m9
-rw-r--r--sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/reset_ess.m5
-rw-r--r--sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/update_CPT.m13
-rw-r--r--sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/update_ess.m61
-rw-r--r--sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/hhmmF_CPD.m73
-rw-r--r--sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/log_prior.m5
-rw-r--r--sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/maximize_params.m9
-rw-r--r--sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/reset_ess.m5
-rw-r--r--sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/update_CPT.m13
-rw-r--r--sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/update_ess.m40
18 files changed, 332 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/CVS/Entries b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/CVS/Entries
new file mode 100644
index 00000000..3e0cc360
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/CVS/Entries
@@ -0,0 +1,7 @@
+/hhmmF_CPD.m/1.1.1.1/Mon Jun 24 23:38:24 2002//
+/log_prior.m/1.1.1.1/Wed May 29 15:59:54 2002//
+/maximize_params.m/1.1.1.1/Wed May 29 15:59:54 2002//
+/reset_ess.m/1.1.1.1/Wed May 29 15:59:54 2002//
+/update_CPT.m/1.1.1.1/Mon Jun 24 22:45:04 2002//
+/update_ess.m/1.1.1.1/Mon Jun 24 23:54:30 2002//
+D/Old////
diff --git a/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/CVS/Repository b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/CVS/Repository
new file mode 100644
index 00000000..7c96bc38
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/CVS/Repository
@@ -0,0 +1 @@
+FullBNT/BNT/CPDs/@hhmmF_CPD
diff --git a/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/CVS/Root b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/CVS/Root
new file mode 100644
index 00000000..f3bd14a6
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/CVS/Root
@@ -0,0 +1 @@
+:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt
diff --git a/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/CVS/Entries b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/CVS/Entries
new file mode 100644
index 00000000..3ab747df
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/CVS/Entries
@@ -0,0 +1,7 @@
+/hhmmF_CPD.m/1.1.1.1/Mon Jun 24 22:35:06 2002//
+/log_prior.m/1.1.1.1/Mon Jun 24 22:35:06 2002//
+/maximize_params.m/1.1.1.1/Mon Jun 24 22:35:06 2002//
+/reset_ess.m/1.1.1.1/Mon Jun 24 22:35:06 2002//
+/update_CPT.m/1.1.1.1/Mon Jun 24 22:35:06 2002//
+/update_ess.m/1.1.1.1/Mon Jun 24 22:35:06 2002//
+D
diff --git a/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/CVS/Repository b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/CVS/Repository
new file mode 100644
index 00000000..8981a516
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/CVS/Repository
@@ -0,0 +1 @@
+FullBNT/BNT/CPDs/@hhmmF_CPD/Old
diff --git a/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/CVS/Root b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/CVS/Root
new file mode 100644
index 00000000..f3bd14a6
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/CVS/Root
@@ -0,0 +1 @@
+:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt
diff --git a/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/hhmmF_CPD.m b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/hhmmF_CPD.m
new file mode 100644
index 00000000..4fdd9bc9
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/hhmmF_CPD.m
@@ -0,0 +1,76 @@
+function CPD = hhmmF_CPD(bnet, self, Qnodes, d, D, varargin)
+% HHMMF_CPD Make the CPD for an F node at depth D of a D-level hierarchical HMM
+% CPD = hhmmF_CPD(bnet, self, Qnodes, d, D, ...)
+%
+%    Q(d-1)
+%          \
+%           \
+%           F(d)
+%         /   |
+%        /    |
+%    Q(d)  F(d+1)
+%
+% We assume nodes are ordered (numbered) as follows:
+% Q(1), ... Q(d), F(d+1), F(d)
+%
+% F(d)=2 means level d has finished. The prob this happens depends on Q(d)
+% and optionally on Q(d-1), Q(d=1), ..., Q(1).
+% Also, level d can only finish if the level below has finished
+% (hence the F(d+1) -> F(d) arc).
+%
+% If d=D, there is no F(d+1), so F(d) is just a regular tabular_CPD.
+% If all models always finish in the same state (e.g., their last),
+% we don't need to condition on the state of parent models (Q(d-1), ...)
+%
+% optional args [defaults]
+%
+% termprob - termprob(k,i,2) = prob finishing given Q(d)=i and Q(1:d-1)=k [ finish in last state ]
+%
+% hhmmF_CPD is a subclass of tabular_CPD so we inherit inference methods like CPD_to_pot, etc.
+%
+% We create an isolated tabular_CPD with no F parent to learn termprob
+% so we can avail of e.g., entropic or Dirichlet priors.
+%
+% For details, see "Linear-time inference in hierarchical HMMs", Murphy and Paskin, NIPS'01.
+
+
+ps = parents(bnet.dag, self);
+Qps = myintersect(ps, Qnodes);
+F = mysetdiff(ps, Qps);
+CPD.Q = Qps(end); % Q(d)
+assert(CPD.Q == Qnodes(d));
+CPD.Qps = Qps(1:end-1); % all Q parents except Q(d), i.e., calling context
+
+ns = bnet.node_sizes(:);
+CPD.Qsizes = ns(Qnodes);
+CPD.d = d;
+CPD.D = D;
+
+Qsz = ns(CPD.Q);
+Qpsz = prod(ns(CPD.Qps));
+
+% set default arguments
+p = 0.9;
+%termprob(k,i,t) Might terminate if i=Qsz; will not terminate if i<Qsz
+termprob = zeros(Qpsz, Qsz, 2);
+termprob(:, Qsz, 2) = p; 
+termprob(:, Qsz, 1) = 1-p; 
+termprob(:, 1:(Qsz-1), 1) = 1; 
+    
+for i=1:2:length(varargin)
+  switch varargin{i},
+   case 'termprob', termprob = varargin{i+1}; 
+   otherwise, error(['unrecognized argument ' varargin{i}])
+  end
+end
+
+ps = [CPD.Qps CPD.Q];
+% ns(self) = 2 since this is an F node
+CPD.sub_CPD_term = mk_isolated_tabular_CPD(ps, ns([ps self]), {'CPT', termprob});
+S = struct(CPD.sub_CPD_term);
+CPD.termprob = S.CPT;
+
+CPD = class(CPD, 'hhmmF_CPD', tabular_CPD(bnet, self));
+
+CPD = update_CPT(CPD);
+
diff --git a/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/log_prior.m b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/log_prior.m
new file mode 100644
index 00000000..7561205d
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/log_prior.m
@@ -0,0 +1,5 @@
+function L = log_prior(CPD)
+% LOG_PRIOR Return log P(theta) for a hhmm F CPD 
+% L = log_prior(CPD)
+
+L = log_prior(CPD.sub_CPD_term);
diff --git a/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/maximize_params.m b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/maximize_params.m
new file mode 100644
index 00000000..16e51ddc
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/maximize_params.m
@@ -0,0 +1,9 @@
+function CPD = maximize_params(CPD, temp)
+% MAXIMIZE_PARAMS Set the params of a hhmmF node to their ML/MAP values.
+% CPD = maximize_params(CPD, temperature)
+
+CPD.sub_CPD_term = maximize_params(CPD.sub_CPD_term, temp);
+S = struct(CPD.sub_CPD_term);
+CPD.termprob = S.CPT;
+
+CPD = update_CPT(CPD);
diff --git a/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/reset_ess.m b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/reset_ess.m
new file mode 100644
index 00000000..f4428937
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/reset_ess.m
@@ -0,0 +1,5 @@
+function CPD = reset_ess(CPD)
+% RESET_ESS Reset the Expected Sufficient Statistics of a hhmm F node.
+% CPD = reset_ess(CPD)
+
+CPD.sub_CPD_term = reset_ess(CPD.sub_CPD_term);   
diff --git a/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/update_CPT.m b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/update_CPT.m
new file mode 100644
index 00000000..4ce14d9f
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/update_CPT.m
@@ -0,0 +1,13 @@
+function CPD = update_CPT(CPD)
+% Compute the big CPT for an HHMM F node given internal termprob
+% function CPD = update_CPT(CPD)
+
+Qsz = CPD.Qsizes(CPD.Q);
+Qpsz = prod(CPD.Qsizes(CPD.Qps));
+
+% P(Q(1:d-1), Q(d), F(d+1), F(d))
+CPT = zeros(Qpsz, Qsz, 2, 2);
+CPT(:,:,1,1) = 1; % if F(d+1)=1, then F(d)=1
+CPT(:,:,2,:) = CPD.termprob;
+
+CPD = set_fields(CPD, 'CPT', CPT);          
diff --git a/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/update_ess.m b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/update_ess.m
new file mode 100644
index 00000000..18f7057e
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old/update_ess.m
@@ -0,0 +1,61 @@
+function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, hidden_bitv)
+% UPDATE_ESS Update the Expected Sufficient Statistics of a hhmmF node.
+% function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, hidden_bitv)
+
+% Figure out the node numbers associated with each parent
+% so we extract evidence from the right place
+dom = fmarginal.domain; % Q(1) .. Q(d) F(d+1) F(d)
+Qps = fmarginal.domain(1:end-2);
+Q = Qps(end);
+Qps = Qps(1:end-1);
+
+Qsz = CPD.Qsizes(CPD.Q);
+Qpsz = prod(CPD.Qsizes(CPD.Qps)); % may be 1
+
+% We assume the F node are always hidden, but allow some of the Q nodes
+% to be observed. We do case analysis for speed.
+%We only extract prob from fmarginal.T when F(d+1)=2 i.e., model below has finished.
+% wrong -> % We sum over the possibilities that F(d+1) = 1 or 2
+
+obs_self = ~hidden_bitv(Q);
+if obs_self
+  self_val = evidence{Q};
+end
+
+if isempty(Qps) % independent of parent context
+  counts = zeros(Qsz, 2);
+  %fmarginal.T(Q(d), F(d+1), F(d))
+  if obs_self
+    marg = myreshape(fmarginal.T, [1 2 2]);
+    counts(self_val,:) = marg(1,2,:);
+    %counts(self_val,:) = marg(1,1,:) + marg(1,2,:);
+  else
+    marg = myreshape(fmarginal.T, [Qsz 2 2]);
+    counts = squeeze(marg(:,2,:));
+    %counts = squeeze(marg(:,2,:)) + squeeze(marg(:,1,:));
+  end
+else
+  counts = zeros(Qpsz, Qsz, 2);
+  %fmarginal.T(Q(1:d-1), Q(d), F(d+1), F(d))
+  obs_Qps = ~any(hidden_bitv(Qps));  % we assume that all or none of the Q  parents are observed
+  if obs_Qps
+    Qps_val = subv2ind(Qpsz, cat(1, evidence{Qps}));
+  end
+  if obs_self & obs_Qps
+    marg = myreshape(fmarginal.T, [1 1 2 2]);
+    counts(Qps_val, self_val, :) = squeeze(marg(1,1,2,:));
+    %counts(Qps_val, self_val, :) = squeeze(marg(1,1,2,:)) + squeeze(marg(1,1,1,:));
+  elseif ~obs_self & obs_Qps
+    marg = myreshape(fmarginal.T, [1 Qsz 2 2]);
+    counts(Qps_val, :, :) = squeeze(marg(1,:,2,:));
+    %counts(Qps_val, :, :) = squeeze(marg(1,:,2,:)) + squeeze(marg(1,:,1,:));
+  elseif obs_self & ~obs_Qps
+    error('not yet implemented')
+  else
+    marg = myreshape(fmarginal.T, [Qpsz Qsz 2 2]);
+    counts(:, :, :) = squeeze(marg(:,:,2,:));
+    %counts(:, :, :) = squeeze(marg(:,:,2,:)) + squeeze(marg(:,:,1,:));
+  end    
+end
+
+CPD.sub_CPD_term = update_ess_simple(CPD.sub_CPD_term, counts);
diff --git a/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/hhmmF_CPD.m b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/hhmmF_CPD.m
new file mode 100644
index 00000000..0c15580e
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/hhmmF_CPD.m
@@ -0,0 +1,73 @@
+function CPD = hhmmF_CPD(bnet, self, Qself, Fbelow, varargin)
+% HHMMF_CPD Make the CPD for an F node in a hierarchical HMM
+% CPD = hhmmF_CPD(bnet, self, Qself,  Fbelow, ...)
+%
+%        Qps
+%          \
+%           \
+%           Fself
+%         /   |
+%        /    |
+%       Qself Fbelow
+%
+% We assume nodes are ordered (numbered) as follows: Qps, Q, Fbelow, F
+% All nodes numbers should be from slice 1.
+%
+% If Fbelow if missing, this becomes a regular tabular_CPD.
+% Qps may be omitted.
+%
+% optional args [defaults]
+% 
+% Qps - node numbers.
+% termprob - termprob(k,i,2) = prob finishing given Q(d)=i and Q(1:d-1)=k [ finish in last state wp 0.9]
+%
+% hhmmF_CPD is a subclass of tabular_CPD so we inherit inference methods like CPD_to_pot, etc.
+%
+% We create an isolated tabular_CPD with no F parent to learn termprob
+% so we can avail of e.g., entropic or Dirichlet priors.
+%
+% For details, see "Linear-time inference in hierarchical HMMs", Murphy and Paskin, NIPS'01.
+
+
+
+Qps = [];
+% get parents
+for i=1:2:length(varargin)
+  switch varargin{i},
+   case 'Qps', Qps = varargin{i+1}; 
+  end
+end
+
+ns = bnet.node_sizes(:);
+Qsz = ns(Qself);
+Qpsz = prod(ns(Qps));
+CPD.Qsz = Qsz;
+CPD.Qpsz = Qpsz;
+
+ps = parents(bnet.dag, self);
+CPD.Fbelow_ndx = find_equiv_posns(Fbelow, ps);
+CPD.Qps_ndx = find_equiv_posns(Qps, ps);
+CPD.Qself_ndx = find_equiv_posns(Qself, ps);
+
+% set default arguments
+p = 0.9;
+%termprob(k,i,t) Might terminate if i=Qsz; will not terminate if i<Qsz
+termprob = zeros(Qpsz, Qsz, 2);
+termprob(:, Qsz, 2) = p; 
+termprob(:, Qsz, 1) = 1-p; 
+termprob(:, 1:(Qsz-1), 1) = 1; 
+    
+for i=1:2:length(varargin)
+  switch varargin{i},
+   case 'termprob', termprob = varargin{i+1}; 
+  end
+end
+
+CPD.sub_CPD_term = mk_isolated_tabular_CPD([Qpsz Qsz 2], {'CPT', termprob});
+S = struct(CPD.sub_CPD_term);
+CPD.termprob = S.CPT;
+
+CPD = class(CPD, 'hhmmF_CPD', tabular_CPD(bnet, self));
+
+CPD = update_CPT(CPD);
+
diff --git a/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/log_prior.m b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/log_prior.m
new file mode 100644
index 00000000..7561205d
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/log_prior.m
@@ -0,0 +1,5 @@
+function L = log_prior(CPD)
+% LOG_PRIOR Return log P(theta) for a hhmm F CPD 
+% L = log_prior(CPD)
+
+L = log_prior(CPD.sub_CPD_term);
diff --git a/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/maximize_params.m b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/maximize_params.m
new file mode 100644
index 00000000..16e51ddc
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/maximize_params.m
@@ -0,0 +1,9 @@
+function CPD = maximize_params(CPD, temp)
+% MAXIMIZE_PARAMS Set the params of a hhmmF node to their ML/MAP values.
+% CPD = maximize_params(CPD, temperature)
+
+CPD.sub_CPD_term = maximize_params(CPD.sub_CPD_term, temp);
+S = struct(CPD.sub_CPD_term);
+CPD.termprob = S.CPT;
+
+CPD = update_CPT(CPD);
diff --git a/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/reset_ess.m b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/reset_ess.m
new file mode 100644
index 00000000..f4428937
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/reset_ess.m
@@ -0,0 +1,5 @@
+function CPD = reset_ess(CPD)
+% RESET_ESS Reset the Expected Sufficient Statistics of a hhmm F node.
+% CPD = reset_ess(CPD)
+
+CPD.sub_CPD_term = reset_ess(CPD.sub_CPD_term);   
diff --git a/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/update_CPT.m b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/update_CPT.m
new file mode 100644
index 00000000..1250457e
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/update_CPT.m
@@ -0,0 +1,13 @@
+function CPD = update_CPT(CPD)
+% Compute the big CPT for an HHMM F node given internal termprob
+% function CPD = update_CPT(CPD)
+
+Qsz = CPD.Qsz;
+Qpsz = CPD.Qpsz;
+
+% CPT(Qpsz, Q, Fbelow, Fself)
+CPT = zeros(Qpsz, Qsz, 2, 2);
+CPT(:,:,1,1) = 1; % if Fbelow=1 (off), then Fself=1 (off)
+CPT(:,:,2,:) = CPD.termprob;
+
+CPD = set_fields(CPD, 'CPT', CPT);          
diff --git a/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/update_ess.m b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/update_ess.m
new file mode 100644
index 00000000..cc636520
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/update_ess.m
@@ -0,0 +1,40 @@
+function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, hidden_bitv)
+% UPDATE_ESS Update the Expected Sufficient Statistics of a hhmmF node.
+% function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, hidden_bitv)
+%
+% We assume the F nodes are always hidden
+
+% Figure out the node numbers associated with each parent
+dom = fmarginal.domain;
+%Fself = dom(end); 
+%Fbelow = dom(CPD.Fbelow_ndx);
+Qself = dom(CPD.Qself_ndx);
+Qps = dom(CPD.Qps_ndx);
+
+Qsz = CPD.Qsz;
+Qpsz = CPD.Qpsz;
+
+if all(hidden_bitv(Qps)) % we assume all are hidden or all are observed
+  k_ndx = 1:Qpsz;
+  eff_Qpsz = Qpsz;
+else
+  k_ndx = subv2ind(Qpsz, cat(1, evidence{Qps}));
+  eff_Qpsz = 1;
+end
+
+if hidden_bitv(Qself)
+  j_ndx = 1:Qsz;
+  eff_Qsz = Qsz;
+else
+  j_ndx = evidence{Qself};
+  eff_Qsz = 1;
+end
+
+% Fmarginal(Qps, Q, Fbelow, F)
+fmarg = myreshape(fmarginal.T, [eff_Qpsz eff_Qsz  2 2]);
+
+counts = zeros(Qpsz, Qsz, 2);
+%counts(k_ndx, j_ndx, :) = sum(fmarginal.T(:, :, :, :), 3); % sum over Fbelow
+counts(k_ndx, j_ndx, :) = fmarg(:, :, 2, :); % Fbelow = 2
+
+CPD.sub_CPD_term = update_ess_simple(CPD.sub_CPD_term, counts);