diff options
Diffstat (limited to 'sourcecodes/bnt-master/BNT/CPDs/@hhmmF_CPD/Old')
9 files changed, 178 insertions, 0 deletions
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); |
