comparison toolboxes/FullBNT-1.0.7/Kalman/kalman_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 [xsmooth, Vsmooth, VVsmooth, loglik] = kalman_smoother(y, A, C, Q, R, init_x, init_V, varargin)
2 % Kalman/RTS smoother.
3 % [xsmooth, Vsmooth, VVsmooth, loglik] = kalman_smoother(y, A, C, Q, R, init_x, init_V, ...)
4 %
5 % The inputs are the same as for kalman_filter.
6 % The outputs are almost the same, except we condition on y(:, 1:T) (and u(:, 1:T) if specified),
7 % instead of on y(:, 1:t).
8
9 [os T] = size(y);
10 ss = length(A);
11
12 % set default params
13 model = ones(1,T);
14 u = [];
15 B = [];
16
17 args = varargin;
18 nargs = length(args);
19 for i=1:2:nargs
20 switch args{i}
21 case 'model', model = args{i+1};
22 case 'u', u = args{i+1};
23 case 'B', B = args{i+1};
24 otherwise, error(['unrecognized argument ' args{i}])
25 end
26 end
27
28 xsmooth = zeros(ss, T);
29 Vsmooth = zeros(ss, ss, T);
30 VVsmooth = zeros(ss, ss, T);
31
32 % Forward pass
33 [xfilt, Vfilt, VVfilt, loglik] = kalman_filter(y, A, C, Q, R, init_x, init_V, ...
34 'model', model, 'u', u, 'B', B);
35
36 % Backward pass
37 xsmooth(:,T) = xfilt(:,T);
38 Vsmooth(:,:,T) = Vfilt(:,:,T);
39 %VVsmooth(:,:,T) = VVfilt(:,:,T);
40
41 for t=T-1:-1:1
42 m = model(t+1);
43 if isempty(B)
44 [xsmooth(:,t), Vsmooth(:,:,t), VVsmooth(:,:,t+1)] = ...
45 smooth_update(xsmooth(:,t+1), Vsmooth(:,:,t+1), xfilt(:,t), Vfilt(:,:,t), ...
46 Vfilt(:,:,t+1), VVfilt(:,:,t+1), A(:,:,m), Q(:,:,m), [], []);
47 else
48 [xsmooth(:,t), Vsmooth(:,:,t), VVsmooth(:,:,t+1)] = ...
49 smooth_update(xsmooth(:,t+1), Vsmooth(:,:,t+1), xfilt(:,t), Vfilt(:,:,t), ...
50 Vfilt(:,:,t+1), VVfilt(:,:,t+1), A(:,:,m), Q(:,:,m), B(:,:,m), u(:,t+1));
51 end
52 end
53
54 VVsmooth(:,:,1) = zeros(ss,ss);
55