annotate toolboxes/FullBNT-1.0.7/HMM/fixed_lag_smoother.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [alpha, obslik, gamma, xi] = fixed_lag_smoother(d, alpha, obslik, obsvec, transmat, act)
Daniel@0 2 % FIXED_LAG_SMOOTHER Computed smoothed posterior estimates within a window given previous filtered window.
Daniel@0 3 % [alpha, obslik, gamma, xi] = fixed_lag_smoother(d, alpha, obslik, obsvec, transmat, act)
Daniel@0 4 %
Daniel@0 5 % d >= 2 is the desired window width.
Daniel@0 6 % Actually, we use d=min(d, t0), where t0 is the current time.
Daniel@0 7 %
Daniel@0 8 % alpha(:, t0-d:t0-1) - length d window, excluding t0 (Columns indexed 1..d)
Daniel@0 9 % obslik(:, t0-d:t0-1) - length d window
Daniel@0 10 % obsvec - likelihood vector for current observation
Daniel@0 11 % transmat - transition matrix
Daniel@0 12 % If we specify the optional 'act' argument, transmat{a} should be a cell array, and
Daniel@0 13 % act(t0-d:t0) - length d window, last column = current action
Daniel@0 14 %
Daniel@0 15 % Output:
Daniel@0 16 % alpha(:, t0-d+1:t0) - last column = new filtered estimate
Daniel@0 17 % obslik(:, t0-d+1:t0) - last column = obsvec
Daniel@0 18 % xi(:, :, t0-d+1:t0-1) - 2 slice smoothed window
Daniel@0 19 % gamma(:, t0-d+1:t0) - smoothed window
Daniel@0 20 %
Daniel@0 21 % As usual, we define (using T=d)
Daniel@0 22 % alpha(i,t) = Pr(Q(t)=i | Y(1:t))
Daniel@0 23 % gamma(i,t) = Pr(Q(t)=i | Y(1:T))
Daniel@0 24 % xi(i,j,t) = Pr(Q(t)=i, Q(t+1)=j | Y(1:T))
Daniel@0 25 %
Daniel@0 26 % obslik(i,t) = Pr(Y(t) | Q(t)=i)
Daniel@0 27 % transmat{a}(i,j) = Pr(Q(t)=j | Q(t-1)=i, A(t)=a)
Daniel@0 28
Daniel@0 29 [S n] = size(alpha);
Daniel@0 30 d = min(d, n+1);
Daniel@0 31 if d < 2
Daniel@0 32 error('must keep a window of length at least 2');
Daniel@0 33 end
Daniel@0 34
Daniel@0 35 if ~exist('act')
Daniel@0 36 act = ones(1, n+1);
Daniel@0 37 transmat = { transmat };
Daniel@0 38 end
Daniel@0 39
Daniel@0 40 % pluck out last d-1 components from the history
Daniel@0 41 alpha = alpha(:, n-d+2:n);
Daniel@0 42 obslik = obslik(:, n-d+2:n);
Daniel@0 43
Daniel@0 44 % Extend window by 1
Daniel@0 45 t = d;
Daniel@0 46 obslik(:,t) = obsvec;
Daniel@0 47 xi = zeros(S, S, d-1);
Daniel@0 48 xi(:,:,t-1) = normalise((alpha(:,t-1) * obslik(:,t)') .* transmat{act(t)});
Daniel@0 49 alpha(:,t) = sum(xi(:,:,t-1), 1)';
Daniel@0 50
Daniel@0 51 % Now smooth backwards inside the window
Daniel@0 52 beta = ones(S, d);
Daniel@0 53 T = d;
Daniel@0 54 %fprintf('smooth from %d to 1, i.e., %d to %d\n', d, t0, t0-d+1);
Daniel@0 55 gamma(:,T) = alpha(:,T);
Daniel@0 56 for t=T-1:-1:1
Daniel@0 57 b = beta(:,t+1) .* obslik(:,t+1);
Daniel@0 58 beta(:,t) = normalise(transmat{act(t)} * b);
Daniel@0 59 gamma(:,t) = normalise(alpha(:,t) .* beta(:,t));
Daniel@0 60 xi(:,:,t) = normalise((transmat{act(t)} .* (alpha(:,t) * b')));
Daniel@0 61 end
Daniel@0 62
Daniel@0 63
Daniel@0 64
Daniel@0 65