wolffd@0: function CPD = hhmmQ_CPD(bnet, self, varargin) wolffd@0: % HHMMQ_CPD Make the CPD for a Q node in a hierarchical HMM wolffd@0: % CPD = hhmmQ_CPD(bnet, self, ...) wolffd@0: % wolffd@0: % Fself(t-1) Qps(t) wolffd@0: % \ | wolffd@0: % \ v wolffd@0: % Qold(t-1) -> Q(t) wolffd@0: % / wolffd@0: % / wolffd@0: % Fbelow(t-1) wolffd@0: % wolffd@0: % Let ss = slice size = num. nodes per slice. wolffd@0: % This node is Q(t), and has mandatory parents Qold(t-1) (assumed to be numbered Q(t)-ss) wolffd@0: % and optional parents Fbelow, Fself, Qps. wolffd@0: % We require parents to be ordered (numbered) as follows: wolffd@0: % Qold, Fbelow, Fself, Qps, Q. wolffd@0: % wolffd@0: % If Fself=2, we use the transition matrix, else we use the prior matrix. wolffd@0: % If Fself node is omitted (eg. top level), we always use the transition matrix. wolffd@0: % If Fbelow=2, we may change state, otherwise we must stay in the same state. wolffd@0: % If Fbelow node is omitted (eg., bottom level), we may change state at every step. wolffd@0: % If Qps (Q parents) are specified, all parameters are conditioned on their joint value. wolffd@0: % We may choose any subset of nodes to condition on, as long as they as numbered lower than self. wolffd@0: % wolffd@0: % optional args [defaults] wolffd@0: % wolffd@0: % Fself - node number <= ss wolffd@0: % Fbelow - node number <= ss wolffd@0: % Qps - node numbers (all <= 2*ss) - uses 2TBN indexing wolffd@0: % transprob - transprob(i,k,j) = prob transition from i to j given Qps = k ['leftright'] wolffd@0: % selfprob - prob of a transition from i to i given Qps=k [0.1] wolffd@0: % startprob - startprob(k,j) = prob start in j given Qps = k ['leftstart'] wolffd@0: % startargs - other args to be passed to the sub tabular_CPD for learning startprob wolffd@0: % transargs - other args will be passed to the sub tabular_CPD for learning transprob wolffd@0: % fullstartprob - 1 means startprob depends on Q(t-1) [0] wolffd@0: % hhmmQ_CPD is a subclass of tabular_CPD so we inherit inference methods like CPD_to_pot, etc. wolffd@0: % wolffd@0: % We create isolated tabular_CPDs with no F parents to learn transprob/startprob wolffd@0: % so we can avail of e.g., entropic or Dirichlet priors. wolffd@0: % In the future, we will be able to represent the transprob using a tree_CPD. wolffd@0: % wolffd@0: % For details, see "Linear-time inference in hierarchical HMMs", Murphy and Paskin, NIPS'01. wolffd@0: wolffd@0: wolffd@0: ss = bnet.nnodes_per_slice; wolffd@0: ns = bnet.node_sizes(:); wolffd@0: wolffd@0: % set default arguments wolffd@0: Fself = []; wolffd@0: Fbelow = []; wolffd@0: Qps = []; wolffd@0: startprob = 'leftstart'; wolffd@0: transprob = 'leftright'; wolffd@0: startargs = {}; wolffd@0: transargs = {}; wolffd@0: selfprob = 0.1; wolffd@0: fullstartprob = 0; wolffd@0: wolffd@0: for i=1:2:length(varargin) wolffd@0: switch varargin{i}, wolffd@0: case 'Fself', Fself = varargin{i+1}; wolffd@0: case 'Fbelow', Fbelow = varargin{i+1}; wolffd@0: case 'Qps', Qps = varargin{i+1}; wolffd@0: case 'transprob', transprob = varargin{i+1}; wolffd@0: case 'selfprob', selfprob = varargin{i+1}; wolffd@0: case 'startprob', startprob = varargin{i+1}; wolffd@0: case 'startargs', startargs = varargin{i+1}; wolffd@0: case 'transargs', transargs = varargin{i+1}; wolffd@0: case 'fullstartprob', fullstartprob = varargin{i+1}; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: CPD.fullstartprob = fullstartprob; wolffd@0: wolffd@0: ps = parents(bnet.dag, self); wolffd@0: ndsz = ns(:)'; wolffd@0: CPD.dom_sz = [ndsz(ps) ns(self)]; wolffd@0: CPD.Fself_ndx = find_equiv_posns(Fself, ps); wolffd@0: CPD.Fbelow_ndx = find_equiv_posns(Fbelow, ps); wolffd@0: %CPD.Qps_ndx = find_equiv_posns(Qps+ss, ps); wolffd@0: CPD.Qps_ndx = find_equiv_posns(Qps, ps); wolffd@0: old_self = self-ss; wolffd@0: CPD.old_self_ndx = find_equiv_posns(old_self, ps); wolffd@0: wolffd@0: Qps = ps(CPD.Qps_ndx); wolffd@0: CPD.Qsz = ns(self); wolffd@0: CPD.Qpsz = prod(ns(Qps)); wolffd@0: CPD.Qpsizes = ns(Qps); wolffd@0: Qsz = CPD.Qsz; wolffd@0: Qpsz = CPD.Qpsz; wolffd@0: wolffd@0: if strcmp(transprob, 'leftright') wolffd@0: LR = mk_leftright_transmat(Qsz, selfprob); wolffd@0: transprob = repmat(reshape(LR, [1 Qsz Qsz]), [Qpsz 1 1]); % transprob(k,i,j) wolffd@0: transprob = permute(transprob, [2 1 3]); % now transprob(i,k,j) wolffd@0: end wolffd@0: transargs{end+1} = 'CPT'; wolffd@0: transargs{end+1} = transprob; wolffd@0: CPD.sub_CPD_trans = mk_isolated_tabular_CPD(ns([old_self Qps self]), transargs); wolffd@0: S = struct(CPD.sub_CPD_trans); wolffd@0: %CPD.transprob = myreshape(S.CPT, [Qsz Qpsz Qsz]); wolffd@0: CPD.transprob = S.CPT; wolffd@0: wolffd@0: wolffd@0: if strcmp(startprob, 'leftstart') wolffd@0: startprob = zeros(Qpsz, Qsz); wolffd@0: startprob(:,1) = 1; wolffd@0: end wolffd@0: if isempty(CPD.Fself_ndx) wolffd@0: CPD.sub_CPD_start = []; wolffd@0: CPD.startprob = []; wolffd@0: else wolffd@0: startargs{end+1} = 'CPT'; wolffd@0: startargs{end+1} = startprob; wolffd@0: if CPD.fullstartprob wolffd@0: CPD.sub_CPD_start = mk_isolated_tabular_CPD(ns([self Qps self]), startargs); wolffd@0: S = struct(CPD.sub_CPD_start); wolffd@0: %CPD.startprob = myreshape(S.CPT, [Qsz Qpsz Qsz]); wolffd@0: CPD.startprob = S.CPT; wolffd@0: else wolffd@0: CPD.sub_CPD_start = mk_isolated_tabular_CPD(ns([Qps self]), startargs); wolffd@0: S = struct(CPD.sub_CPD_start); wolffd@0: %CPD.startprob = myreshape(S.CPT, [CPD.Qpsizes Qsz]); wolffd@0: CPD.startprob = S.CPT; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: CPD = class(CPD, 'hhmmQ_CPD', tabular_CPD(bnet, self)); wolffd@0: wolffd@0: CPD = update_CPT(CPD); wolffd@0: