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