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