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