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