Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/bnt/inference/dynamic/@hmm_inf_engine/fwdback_twoslice.m @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e9a9cd732c1e |
---|---|
1 function [alpha, beta, gamma, loglik, xi, gamma2] = fwdback_twoslice(engine, init_state_distrib, transmat, obslik, varargin) | |
2 % FWDBACK Compute the posterior probs. in an HMM using the forwards backwards algo. | |
3 % | |
4 % [alpha, beta, gamma, loglik, xi, gamma2] = fwdback(init_state_distrib, transmat, obslik, ...) | |
5 % | |
6 % Notation: | |
7 % Y(t) = observation, Q(t) = hidden state, M(t) = mixture variable (for MOG outputs) | |
8 % A(t) = discrete input (action) (for POMDP models) | |
9 % | |
10 % INPUT: | |
11 % init_state_distrib(i) = Pr(Q(1) = i) | |
12 % transmat(i,j) = Pr(Q(t) = j | Q(t-1)=i) | |
13 % or transmat{a}(i,j) = Pr(Q(t) = j | Q(t-1)=i, A(t-1)=a) if there are discrete inputs | |
14 % obslik(i,t) = Pr(Y(t)| Q(t)=i) | |
15 % (Compute obslik using eval_pdf_xxx on your data sequence first.) | |
16 % | |
17 % Optional parameters may be passed as 'param_name', param_value pairs. | |
18 % Parameter names are shown below; default values in [] - if none, argument is mandatory. | |
19 % | |
20 % For HMMs with MOG outputs: if you want to compute gamma2, you must specify | |
21 % 'obslik2' - obslik(i,j,t) = Pr(Y(t)| Q(t)=i,M(t)=j) [] | |
22 % 'mixmat' - mixmat(i,j) = Pr(M(t) = j | Q(t)=i) [] | |
23 % | |
24 % For HMMs with discrete inputs: | |
25 % 'act' - act(t) = action performed at step t | |
26 % | |
27 % Optional arguments: | |
28 % 'fwd_only' - if 1, only do a forwards pass and set beta=[], gamma2=[] [0] | |
29 % 'scaled' - if 1, normalize alphas and betas to prevent underflow [1] | |
30 % 'maximize' - if 1, use max-product instead of sum-product [0] | |
31 % | |
32 % OUTPUTS: | |
33 % alpha(i,t) = p(Q(t)=i | y(1:t)) (or p(Q(t)=i, y(1:t)) if scaled=0) | |
34 % beta(i,t) = p(y(t+1:T) | Q(t)=i)*p(y(t+1:T)|y(1:t)) (or p(y(t+1:T) | Q(t)=i) if scaled=0) | |
35 % gamma(i,t) = p(Q(t)=i | y(1:T)) | |
36 % loglik = log p(y(1:T)) | |
37 % xi(i,j,t-1) = p(Q(t-1)=i, Q(t)=j | y(1:T)) | |
38 % gamma2(j,k,t) = p(Q(t)=j, M(t)=k | y(1:T)) (only for MOG outputs) | |
39 % | |
40 % If fwd_only = 1, these become | |
41 % alpha(i,t) = p(Q(t)=i | y(1:t)) | |
42 % beta = [] | |
43 % gamma(i,t) = p(Q(t)=i | y(1:t)) | |
44 % xi(i,j,t-1) = p(Q(t-1)=i, Q(t)=j | y(1:t)) | |
45 % gamma2 = [] | |
46 % | |
47 % Note: we only compute xi if it is requested as a return argument, since it can be very large. | |
48 % Similarly, we only compute gamma2 on request (and if using MOG outputs). | |
49 % | |
50 % Examples: | |
51 % | |
52 % [alpha, beta, gamma, loglik] = fwdback(pi, A, multinomial_prob(sequence, B)); | |
53 % | |
54 % [B, B2] = mixgauss_prob(data, mu, Sigma, mixmat); | |
55 % [alpha, beta, gamma, loglik, xi, gamma2] = fwdback(pi, A, B, 'obslik2', B2, 'mixmat', mixmat); | |
56 | |
57 | |
58 if nargout >= 5, compute_xi = 1; else compute_xi = 0; end | |
59 if nargout >= 6, compute_gamma2 = 1; else compute_gamma2 = 0; end | |
60 | |
61 [obslik2, mixmat, fwd_only, scaled, act, maximize, compute_xi, compute_gamma2] = process_options(varargin, 'obslik2', [], 'mixmat', [], 'fwd_only', 0, 'scaled', 1, 'act', [], 'maximize', 0, 'compute_xi', compute_xi, 'compute_gamma2', compute_gamma2); | |
62 | |
63 | |
64 [Q T] = size(obslik); | |
65 | |
66 if isempty(obslik2) | |
67 compute_gamma2 = 0; | |
68 end | |
69 | |
70 if isempty(act) | |
71 act = ones(1,T); | |
72 transmat = { transmat } ; | |
73 end | |
74 | |
75 scale = ones(1,T); | |
76 | |
77 % scale(t) = Pr(O(t) | O(1:t-1)) = 1/c(t) as defined by Rabiner (1989). | |
78 % Hence prod_t scale(t) = Pr(O(1)) Pr(O(2)|O(1)) Pr(O(3) | O(1:2)) = Pr(O(1), ... ,O(T)) | |
79 % or log P = sum_t log scale(t). | |
80 % Rabiner suggests multiplying beta(t) by scale(t), but we can instead | |
81 % normalise beta(t) - the constants will cancel when we compute gamma. | |
82 | |
83 loglik = 0; | |
84 | |
85 alpha = zeros(Q,T); | |
86 gamma = zeros(Q,T); | |
87 if compute_xi | |
88 xi = zeros(Q,Q,T-1); | |
89 else | |
90 xi = []; | |
91 end | |
92 | |
93 | |
94 %%%%%%%%% Forwards %%%%%%%%%% | |
95 | |
96 t = 1; | |
97 alpha(:,1) = init_state_distrib(:) .* obslik(:,t); | |
98 if scaled | |
99 %[alpha(:,t), scale(t)] = normaliseC(alpha(:,t)); | |
100 [alpha(:,t), scale(t)] = normalise(alpha(:,t)); | |
101 end | |
102 if scaled, assert(approxeq(sum(alpha(:,t)),1)), end | |
103 for t=2:T | |
104 %trans = transmat(:,:,act(t-1))'; | |
105 trans = transmat{act(t-1)}; | |
106 if maximize | |
107 m = max_mult(trans', alpha(:,t-1)); | |
108 %A = repmat(alpha(:,t-1), [1 Q]); | |
109 %m = max(trans .* A, [], 1); | |
110 else | |
111 m = trans' * alpha(:,t-1); | |
112 end | |
113 alpha(:,t) = m(:) .* obslik(:,t); | |
114 if scaled | |
115 %[alpha(:,t), scale(t)] = normaliseC(alpha(:,t)); | |
116 [alpha(:,t), scale(t)] = normalise(alpha(:,t)); | |
117 end | |
118 if compute_xi & fwd_only % useful for online EM | |
119 %xi(:,:,t-1) = normaliseC((alpha(:,t-1) * obslik(:,t)') .* trans); | |
120 xi(:,:,t-1) = normalise((alpha(:,t-1) * obslik(:,t)') .* trans); | |
121 end | |
122 if scaled, assert(approxeq(sum(alpha(:,t)),1)), end | |
123 end | |
124 if scaled | |
125 if any(scale==0) | |
126 loglik = -inf; | |
127 else | |
128 loglik = sum(log(scale)); | |
129 end | |
130 else | |
131 loglik = log(sum(alpha(:,T))); | |
132 end | |
133 | |
134 if fwd_only | |
135 gamma = alpha; | |
136 beta = []; | |
137 gamma2 = []; | |
138 return; | |
139 end | |
140 | |
141 | |
142 %%%%%%%%% Backwards %%%%%%%%%% | |
143 | |
144 beta = zeros(Q,T); | |
145 if compute_gamma2 | |
146 M = size(mixmat, 2); | |
147 gamma2 = zeros(Q,M,T); | |
148 else | |
149 gamma2 = []; | |
150 end | |
151 | |
152 beta(:,T) = ones(Q,1); | |
153 %gamma(:,T) = normaliseC(alpha(:,T) .* beta(:,T)); | |
154 gamma(:,T) = normalise(alpha(:,T) .* beta(:,T)); | |
155 t=T; | |
156 if compute_gamma2 | |
157 denom = obslik(:,t) + (obslik(:,t)==0); % replace 0s with 1s before dividing | |
158 gamma2(:,:,t) = obslik2(:,:,t) .* mixmat .* repmat(gamma(:,t), [1 M]) ./ repmat(denom, [1 M]); | |
159 %gamma2(:,:,t) = normaliseC(obslik2(:,:,t) .* mixmat .* repmat(gamma(:,t), [1 M])); % wrong! | |
160 end | |
161 for t=T-1:-1:1 | |
162 b = beta(:,t+1) .* obslik(:,t+1); | |
163 %trans = transmat(:,:,act(t)); | |
164 trans = transmat{act(t)}; | |
165 if maximize | |
166 B = repmat(b(:)', Q, 1); | |
167 beta(:,t) = max(trans .* B, [], 2); | |
168 else | |
169 beta(:,t) = trans * b; | |
170 end | |
171 if scaled | |
172 %beta(:,t) = normaliseC(beta(:,t)); | |
173 beta(:,t) = normalise(beta(:,t)); | |
174 end | |
175 %gamma(:,t) = normaliseC(alpha(:,t) .* beta(:,t)); | |
176 gamma(:,t) = normalise(alpha(:,t) .* beta(:,t)); | |
177 if compute_xi | |
178 %xi(:,:,t) = normaliseC((trans .* (alpha(:,t) * b'))); | |
179 xi(:,:,t) = normalise((trans .* (alpha(:,t) * b'))); | |
180 %xi(:,:,t) = (trans .* (alpha(:,t) * b')); | |
181 end | |
182 if compute_gamma2 | |
183 denom = obslik(:,t) + (obslik(:,t)==0); % replace 0s with 1s before dividing | |
184 gamma2(:,:,t) = obslik2(:,:,t) .* mixmat .* repmat(gamma(:,t), [1 M]) ./ repmat(denom, [1 M]); | |
185 %gamma2(:,:,t) = normaliseC(obslik2(:,:,t) .* mixmat .* repmat(gamma(:,t), [1 M])); | |
186 end | |
187 end | |
188 | |
189 | |
190 % We now explain the equation for gamma2 | |
191 % Let zt=y(1:t-1,t+1:T) be all observations except y(t) | |
192 % gamma2(Q,M,t) = P(Qt,Mt|yt,zt) = P(yt|Qt,Mt,zt) P(Qt,Mt|zt) / P(yt|zt) | |
193 % = P(yt|Qt,Mt) P(Mt|Qt) P(Qt|zt) / P(yt|zt) | |
194 % Now gamma(Q,t) = P(Qt|yt,zt) = P(yt|Qt) P(Qt|zt) / P(yt|zt) | |
195 % hence | |
196 % P(Qt,Mt|yt,zt) = P(yt|Qt,Mt) P(Mt|Qt) [P(Qt|yt,zt) P(yt|zt) / P(yt|Qt)] / P(yt|zt) | |
197 % = P(yt|Qt,Mt) P(Mt|Qt) P(Qt|yt,zt) / P(yt|Qt) | |
198 % |