annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@hhmmQ_CPD/Old/hhmmQ_CPD.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function CPD = hhmmQ_CPD(bnet, self, Qnodes, d, D, varargin)
wolffd@0 2 % HHMMQ_CPD Make the CPD for a Q node at depth D of a D-level hierarchical HMM
wolffd@0 3 % CPD = hhmmQ_CPD(bnet, self, Qnodes, d, D, ...)
wolffd@0 4 %
wolffd@0 5 % Fd(t-1) \ Q1:d-1(t)
wolffd@0 6 % \ |
wolffd@0 7 % \ v
wolffd@0 8 % Qd(t-1) -> Qd(t)
wolffd@0 9 % /
wolffd@0 10 % /
wolffd@0 11 % Fd+1(t-1)
wolffd@0 12 %
wolffd@0 13 % We assume parents are ordered (numbered) as follows:
wolffd@0 14 % Qd(t-1), Fd+1(t-1), Fd(t-1), Q1(t), ..., Qd(t)
wolffd@0 15 %
wolffd@0 16 % The parents of Qd(t) can either be just Qd-1(t) or the whole stack Q1:d-1(t) (allQ)
wolffd@0 17 % In either case, we will call them Qps.
wolffd@0 18 % If d=1, Qps does not exist. Also, the F1(t-1) -> Q1(t) arc is optional.
wolffd@0 19 % If the arc is missing, startprob does not need to be specified,
wolffd@0 20 % since the toplevel is assumed to never reset (F1 does not exist).
wolffd@0 21 % If d=D, Fd+1(t-1) does not exist (there is no signal from below).
wolffd@0 22 %
wolffd@0 23 % optional args [defaults]
wolffd@0 24 %
wolffd@0 25 % transprob - transprob(i,k,j) = prob transition from i to j given Qps = k ['leftright']
wolffd@0 26 % selfprob - prob of a transition from i to i given Qps=k [0.1]
wolffd@0 27 % startprob - startprob(k,j) = prob start in j given Qps = k ['leftstart']
wolffd@0 28 % startargs - other args to be passed to the sub tabular_CPD for learning startprob
wolffd@0 29 % transargs - other args will be passed to the sub tabular_CPD for learning transprob
wolffd@0 30 % allQ - 1 means use all Q nodes above d as parents, 0 means just level d-1 [0]
wolffd@0 31 % F1toQ1 - 1 means add F1(t-1) -> Q1(t) arc, 0 means level 1 never resets [0]
wolffd@0 32 %
wolffd@0 33 % For d=1, startprob(1,j) is only needed if F1toQ1=1
wolffd@0 34 % Also, transprob(i,j) can be used instead of transprob(i,1,j).
wolffd@0 35 %
wolffd@0 36 % hhmmQ_CPD is a subclass of tabular_CPD so we inherit inference methods like CPD_to_pot, etc.
wolffd@0 37 %
wolffd@0 38 % We create isolated tabular_CPDs with no F parents to learn transprob/startprob
wolffd@0 39 % so we can avail of e.g., entropic or Dirichlet priors.
wolffd@0 40 % In the future, we will be able to represent the transprob using a tree_CPD.
wolffd@0 41 %
wolffd@0 42 % For details, see "Linear-time inference in hierarchical HMMs", Murphy and Paskin, NIPS'01.
wolffd@0 43
wolffd@0 44
wolffd@0 45 ss = bnet.nnodes_per_slice;
wolffd@0 46 %assert(self == Qnodes(d)+ss);
wolffd@0 47 ns = bnet.node_sizes(:);
wolffd@0 48 CPD.Qsizes = ns(Qnodes);
wolffd@0 49 CPD.d = d;
wolffd@0 50 CPD.D = D;
wolffd@0 51 allQ = 0;
wolffd@0 52
wolffd@0 53 % find out which parents to use, to get right size
wolffd@0 54 for i=1:2:length(varargin)
wolffd@0 55 switch varargin{i},
wolffd@0 56 case 'allQ', allQ = varargin{i+1};
wolffd@0 57 end
wolffd@0 58 end
wolffd@0 59
wolffd@0 60 if d==1
wolffd@0 61 CPD.Qps = [];
wolffd@0 62 else
wolffd@0 63 if allQ
wolffd@0 64 CPD.Qps = Qnodes(1:d-1);
wolffd@0 65 else
wolffd@0 66 CPD.Qps = Qnodes(d-1);
wolffd@0 67 end
wolffd@0 68 end
wolffd@0 69
wolffd@0 70 Qsz = ns(self);
wolffd@0 71 Qpsz = prod(ns(CPD.Qps));
wolffd@0 72
wolffd@0 73 % set default arguments
wolffd@0 74 startprob = 'leftstart';
wolffd@0 75 transprob = 'leftright';
wolffd@0 76 startargs = {};
wolffd@0 77 transargs = {};
wolffd@0 78 CPD.F1toQ1 = 0;
wolffd@0 79 selfprob = 0.1;
wolffd@0 80
wolffd@0 81 for i=1:2:length(varargin)
wolffd@0 82 switch varargin{i},
wolffd@0 83 case 'transprob', transprob = varargin{i+1};
wolffd@0 84 case 'selfprob', selfprob = varargin{i+1};
wolffd@0 85 case 'startprob', startprob = varargin{i+1};
wolffd@0 86 case 'startargs', startargs = varargin{i+1};
wolffd@0 87 case 'transargs', transargs = varargin{i+1};
wolffd@0 88 case 'F1toQ1', CPD.F1toQ1 = varargin{i+1};
wolffd@0 89 end
wolffd@0 90 end
wolffd@0 91
wolffd@0 92 Qps = CPD.Qps + ss;
wolffd@0 93 old_self = self-ss;
wolffd@0 94
wolffd@0 95 if strcmp(transprob, 'leftright')
wolffd@0 96 LR = mk_leftright_transmat(Qsz, selfprob);
wolffd@0 97 transprob = repmat(reshape(LR, [1 Qsz Qsz]), [Qpsz 1 1]); % transprob(k,i,j)
wolffd@0 98 transprob = permute(transprob, [2 1 3]); % now transprob(i,k,j)
wolffd@0 99 end
wolffd@0 100 transargs{end+1} = 'CPT';
wolffd@0 101 transargs{end+1} = transprob;
wolffd@0 102 CPD.sub_CPD_trans = mk_isolated_tabular_CPD([old_self Qps], ns([old_self Qps self]), transargs);
wolffd@0 103 S = struct(CPD.sub_CPD_trans);
wolffd@0 104 CPD.transprob = myreshape(S.CPT, [Qsz Qpsz Qsz]);
wolffd@0 105
wolffd@0 106
wolffd@0 107 if strcmp(startprob, 'leftstart')
wolffd@0 108 startprob = zeros(Qpsz, Qsz);
wolffd@0 109 startprob(:,1) = 1;
wolffd@0 110 end
wolffd@0 111
wolffd@0 112 if (d==1) & ~CPD.F1toQ1
wolffd@0 113 CPD.sub_CPD_start = [];
wolffd@0 114 CPD.startprob = [];
wolffd@0 115 else
wolffd@0 116 startargs{end+1} = 'CPT';
wolffd@0 117 startargs{end+1} = startprob;
wolffd@0 118 CPD.sub_CPD_start = mk_isolated_tabular_CPD(Qps, ns([Qps self]), startargs);
wolffd@0 119 S = struct(CPD.sub_CPD_start);
wolffd@0 120 CPD.startprob = myreshape(S.CPT, [Qpsz Qsz]);
wolffd@0 121 end
wolffd@0 122
wolffd@0 123 CPD = class(CPD, 'hhmmQ_CPD', tabular_CPD(bnet, self));
wolffd@0 124
wolffd@0 125 CPD = update_CPT(CPD);
wolffd@0 126