annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@hhmmQ_CPD/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, varargin)
wolffd@0 2 % HHMMQ_CPD Make the CPD for a Q node in a hierarchical HMM
wolffd@0 3 % CPD = hhmmQ_CPD(bnet, self, ...)
wolffd@0 4 %
wolffd@0 5 % Fself(t-1) Qps(t)
wolffd@0 6 % \ |
wolffd@0 7 % \ v
wolffd@0 8 % Qold(t-1) -> Q(t)
wolffd@0 9 % /
wolffd@0 10 % /
wolffd@0 11 % Fbelow(t-1)
wolffd@0 12 %
wolffd@0 13 % Let ss = slice size = num. nodes per slice.
wolffd@0 14 % This node is Q(t), and has mandatory parents Qold(t-1) (assumed to be numbered Q(t)-ss)
wolffd@0 15 % and optional parents Fbelow, Fself, Qps.
wolffd@0 16 % We require parents to be ordered (numbered) as follows:
wolffd@0 17 % Qold, Fbelow, Fself, Qps, Q.
wolffd@0 18 %
wolffd@0 19 % If Fself=2, we use the transition matrix, else we use the prior matrix.
wolffd@0 20 % If Fself node is omitted (eg. top level), we always use the transition matrix.
wolffd@0 21 % If Fbelow=2, we may change state, otherwise we must stay in the same state.
wolffd@0 22 % If Fbelow node is omitted (eg., bottom level), we may change state at every step.
wolffd@0 23 % If Qps (Q parents) are specified, all parameters are conditioned on their joint value.
wolffd@0 24 % We may choose any subset of nodes to condition on, as long as they as numbered lower than self.
wolffd@0 25 %
wolffd@0 26 % optional args [defaults]
wolffd@0 27 %
wolffd@0 28 % Fself - node number <= ss
wolffd@0 29 % Fbelow - node number <= ss
wolffd@0 30 % Qps - node numbers (all <= 2*ss) - uses 2TBN indexing
wolffd@0 31 % transprob - transprob(i,k,j) = prob transition from i to j given Qps = k ['leftright']
wolffd@0 32 % selfprob - prob of a transition from i to i given Qps=k [0.1]
wolffd@0 33 % startprob - startprob(k,j) = prob start in j given Qps = k ['leftstart']
wolffd@0 34 % startargs - other args to be passed to the sub tabular_CPD for learning startprob
wolffd@0 35 % transargs - other args will be passed to the sub tabular_CPD for learning transprob
wolffd@0 36 % fullstartprob - 1 means startprob depends on Q(t-1) [0]
wolffd@0 37 % hhmmQ_CPD is a subclass of tabular_CPD so we inherit inference methods like CPD_to_pot, etc.
wolffd@0 38 %
wolffd@0 39 % We create isolated tabular_CPDs with no F parents to learn transprob/startprob
wolffd@0 40 % so we can avail of e.g., entropic or Dirichlet priors.
wolffd@0 41 % In the future, we will be able to represent the transprob using a tree_CPD.
wolffd@0 42 %
wolffd@0 43 % For details, see "Linear-time inference in hierarchical HMMs", Murphy and Paskin, NIPS'01.
wolffd@0 44
wolffd@0 45
wolffd@0 46 ss = bnet.nnodes_per_slice;
wolffd@0 47 ns = bnet.node_sizes(:);
wolffd@0 48
wolffd@0 49 % set default arguments
wolffd@0 50 Fself = [];
wolffd@0 51 Fbelow = [];
wolffd@0 52 Qps = [];
wolffd@0 53 startprob = 'leftstart';
wolffd@0 54 transprob = 'leftright';
wolffd@0 55 startargs = {};
wolffd@0 56 transargs = {};
wolffd@0 57 selfprob = 0.1;
wolffd@0 58 fullstartprob = 0;
wolffd@0 59
wolffd@0 60 for i=1:2:length(varargin)
wolffd@0 61 switch varargin{i},
wolffd@0 62 case 'Fself', Fself = varargin{i+1};
wolffd@0 63 case 'Fbelow', Fbelow = varargin{i+1};
wolffd@0 64 case 'Qps', Qps = varargin{i+1};
wolffd@0 65 case 'transprob', transprob = varargin{i+1};
wolffd@0 66 case 'selfprob', selfprob = varargin{i+1};
wolffd@0 67 case 'startprob', startprob = varargin{i+1};
wolffd@0 68 case 'startargs', startargs = varargin{i+1};
wolffd@0 69 case 'transargs', transargs = varargin{i+1};
wolffd@0 70 case 'fullstartprob', fullstartprob = varargin{i+1};
wolffd@0 71 end
wolffd@0 72 end
wolffd@0 73
wolffd@0 74 CPD.fullstartprob = fullstartprob;
wolffd@0 75
wolffd@0 76 ps = parents(bnet.dag, self);
wolffd@0 77 ndsz = ns(:)';
wolffd@0 78 CPD.dom_sz = [ndsz(ps) ns(self)];
wolffd@0 79 CPD.Fself_ndx = find_equiv_posns(Fself, ps);
wolffd@0 80 CPD.Fbelow_ndx = find_equiv_posns(Fbelow, ps);
wolffd@0 81 %CPD.Qps_ndx = find_equiv_posns(Qps+ss, ps);
wolffd@0 82 CPD.Qps_ndx = find_equiv_posns(Qps, ps);
wolffd@0 83 old_self = self-ss;
wolffd@0 84 CPD.old_self_ndx = find_equiv_posns(old_self, ps);
wolffd@0 85
wolffd@0 86 Qps = ps(CPD.Qps_ndx);
wolffd@0 87 CPD.Qsz = ns(self);
wolffd@0 88 CPD.Qpsz = prod(ns(Qps));
wolffd@0 89 CPD.Qpsizes = ns(Qps);
wolffd@0 90 Qsz = CPD.Qsz;
wolffd@0 91 Qpsz = CPD.Qpsz;
wolffd@0 92
wolffd@0 93 if strcmp(transprob, 'leftright')
wolffd@0 94 LR = mk_leftright_transmat(Qsz, selfprob);
wolffd@0 95 transprob = repmat(reshape(LR, [1 Qsz Qsz]), [Qpsz 1 1]); % transprob(k,i,j)
wolffd@0 96 transprob = permute(transprob, [2 1 3]); % now transprob(i,k,j)
wolffd@0 97 end
wolffd@0 98 transargs{end+1} = 'CPT';
wolffd@0 99 transargs{end+1} = transprob;
wolffd@0 100 CPD.sub_CPD_trans = mk_isolated_tabular_CPD(ns([old_self Qps self]), transargs);
wolffd@0 101 S = struct(CPD.sub_CPD_trans);
wolffd@0 102 %CPD.transprob = myreshape(S.CPT, [Qsz Qpsz Qsz]);
wolffd@0 103 CPD.transprob = S.CPT;
wolffd@0 104
wolffd@0 105
wolffd@0 106 if strcmp(startprob, 'leftstart')
wolffd@0 107 startprob = zeros(Qpsz, Qsz);
wolffd@0 108 startprob(:,1) = 1;
wolffd@0 109 end
wolffd@0 110 if isempty(CPD.Fself_ndx)
wolffd@0 111 CPD.sub_CPD_start = [];
wolffd@0 112 CPD.startprob = [];
wolffd@0 113 else
wolffd@0 114 startargs{end+1} = 'CPT';
wolffd@0 115 startargs{end+1} = startprob;
wolffd@0 116 if CPD.fullstartprob
wolffd@0 117 CPD.sub_CPD_start = mk_isolated_tabular_CPD(ns([self Qps self]), startargs);
wolffd@0 118 S = struct(CPD.sub_CPD_start);
wolffd@0 119 %CPD.startprob = myreshape(S.CPT, [Qsz Qpsz Qsz]);
wolffd@0 120 CPD.startprob = S.CPT;
wolffd@0 121 else
wolffd@0 122 CPD.sub_CPD_start = mk_isolated_tabular_CPD(ns([Qps self]), startargs);
wolffd@0 123 S = struct(CPD.sub_CPD_start);
wolffd@0 124 %CPD.startprob = myreshape(S.CPT, [CPD.Qpsizes Qsz]);
wolffd@0 125 CPD.startprob = S.CPT;
wolffd@0 126 end
wolffd@0 127 end
wolffd@0 128
wolffd@0 129 CPD = class(CPD, 'hhmmQ_CPD', tabular_CPD(bnet, self));
wolffd@0 130
wolffd@0 131 CPD = update_CPT(CPD);
wolffd@0 132