To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / _FullBNT / BNT / CPDs / @hhmmQ_CPD / Old / hhmmQ_CPD.m @ 8:b5b38998ef3b

History | View | Annotate | Download (3.78 KB)

1
function CPD = hhmmQ_CPD(bnet, self, Qnodes, d, D, varargin)
2
% HHMMQ_CPD Make the CPD for a Q node at depth D of a D-level hierarchical HMM
3
% CPD = hhmmQ_CPD(bnet, self, Qnodes, d, D, ...)
4
%
5
%  Fd(t-1) \   Q1:d-1(t)
6
%           \  |
7
%            \ v
8
%  Qd(t-1) -> Qd(t)
9
%            /
10
%           /
11
%  Fd+1(t-1) 
12
%
13
% We assume parents are ordered (numbered) as follows:
14
% Qd(t-1), Fd+1(t-1), Fd(t-1), Q1(t), ..., Qd(t)
15
%
16
% The parents of Qd(t) can either be just Qd-1(t) or the whole stack Q1:d-1(t) (allQ)
17
% In either case, we will call them Qps.
18
% If d=1, Qps does not exist. Also, the F1(t-1) -> Q1(t) arc is optional.
19
% If the arc is missing, startprob does not need to be specified,
20
% since the toplevel is assumed to never reset (F1 does not exist).
21
% If d=D, Fd+1(t-1) does not exist (there is no signal from below).
22
%
23
% optional args [defaults]
24
%
25
% transprob - transprob(i,k,j) = prob transition from i to j given Qps = k ['leftright']
26
% selfprob  - prob of a transition from i to i given Qps=k [0.1]
27
% startprob - startprob(k,j) = prob start in j given Qps = k ['leftstart']
28
% startargs - other args to be passed to the sub tabular_CPD for learning startprob
29
% transargs - other args will be passed to the sub tabular_CPD for learning transprob
30
% allQ      - 1 means use all Q nodes above d as parents, 0 means just level d-1 [0]
31
% F1toQ1    - 1 means add F1(t-1) -> Q1(t) arc, 0 means level 1 never resets [0]
32
%
33
% For d=1, startprob(1,j) is only needed if F1toQ1=1
34
% Also, transprob(i,j) can be used instead of transprob(i,1,j).
35
%
36
% hhmmQ_CPD is a subclass of tabular_CPD so we inherit inference methods like CPD_to_pot, etc.
37
%
38
% We create isolated tabular_CPDs with no F parents to learn transprob/startprob
39
% so we can avail of e.g., entropic or Dirichlet priors.
40
% In the future, we will be able to represent the transprob using a tree_CPD.
41
%
42
% For details, see "Linear-time inference in hierarchical HMMs", Murphy and Paskin, NIPS'01.
43

    
44

    
45
ss = bnet.nnodes_per_slice;
46
%assert(self == Qnodes(d)+ss);
47
ns = bnet.node_sizes(:);
48
CPD.Qsizes = ns(Qnodes);
49
CPD.d = d;
50
CPD.D = D;
51
allQ = 0;
52

    
53
% find out which parents to use, to get right size
54
for i=1:2:length(varargin)
55
  switch varargin{i},
56
   case 'allQ', allQ = varargin{i+1}; 
57
  end
58
end
59

    
60
if d==1
61
  CPD.Qps = [];
62
else
63
  if allQ
64
    CPD.Qps = Qnodes(1:d-1);
65
  else
66
    CPD.Qps = Qnodes(d-1);
67
  end
68
end
69

    
70
Qsz = ns(self);
71
Qpsz = prod(ns(CPD.Qps));
72

    
73
% set default arguments
74
startprob = 'leftstart';
75
transprob = 'leftright';
76
startargs = {};
77
transargs = {};
78
CPD.F1toQ1 = 0;
79
selfprob = 0.1;
80

    
81
for i=1:2:length(varargin)
82
  switch varargin{i},
83
   case 'transprob', transprob = varargin{i+1}; 
84
   case 'selfprob',  selfprob = varargin{i+1}; 
85
   case 'startprob', startprob = varargin{i+1}; 
86
   case 'startargs', startargs = varargin{i+1}; 
87
   case 'transargs', transargs = varargin{i+1}; 
88
   case 'F1toQ1',    CPD.F1toQ1 = varargin{i+1}; 
89
  end
90
end
91

    
92
Qps = CPD.Qps + ss;
93
old_self = self-ss;
94

    
95
if strcmp(transprob, 'leftright')
96
  LR = mk_leftright_transmat(Qsz, selfprob);
97
  transprob = repmat(reshape(LR, [1 Qsz Qsz]), [Qpsz 1 1]); % transprob(k,i,j)
98
  transprob = permute(transprob, [2 1 3]); % now transprob(i,k,j)
99
end
100
transargs{end+1} = 'CPT';
101
transargs{end+1} = transprob;
102
CPD.sub_CPD_trans = mk_isolated_tabular_CPD([old_self Qps], ns([old_self Qps self]), transargs);
103
S = struct(CPD.sub_CPD_trans);
104
CPD.transprob = myreshape(S.CPT, [Qsz Qpsz Qsz]);
105

    
106

    
107
if strcmp(startprob, 'leftstart')
108
  startprob = zeros(Qpsz, Qsz);
109
  startprob(:,1) = 1;
110
end
111

    
112
if (d==1) & ~CPD.F1toQ1
113
  CPD.sub_CPD_start = [];
114
  CPD.startprob = [];
115
else
116
  startargs{end+1} = 'CPT';
117
  startargs{end+1} = startprob;
118
  CPD.sub_CPD_start = mk_isolated_tabular_CPD(Qps, ns([Qps self]), startargs);
119
  S = struct(CPD.sub_CPD_start);
120
  CPD.startprob = myreshape(S.CPT, [Qpsz Qsz]);
121
end
122

    
123
CPD = class(CPD, 'hhmmQ_CPD', tabular_CPD(bnet, self));
124

    
125
CPD = update_CPT(CPD);
126