Advertisement
Jeren

Untitled

Nov 20th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.07 KB | None | 0 0
  1. function [x_T, P_T, P_Tm2] = smoother(G,F,Q,R,mu0,Sigma,y)
  2.     % G is T*p
  3.     p = size(mu0,1); % mu0 is p*1
  4.     T = size(y,1); % y is T*1
  5.     J = zeros(p,p,T);
  6.     K = zeros(p,T); % gain matrix
  7.     x = zeros(p,T);
  8.     x(:,1) = mu0;
  9.     x_m1 = zeros(p,T);
  10.     x_T = zeros(p,T); % x values when we know all the data
  11.     % Notation : x = xt given t ; x_m1 = xt given t-1 (m1 stands for minus
  12.     % one)
  13.     P = zeros(p,p,T);% array of cov(xt|y1...yt), eq (6) in Shumway & Stoffer 1982
  14.     P(:,:,1) = Sigma;
  15.     P_m1 = zeros(p,p,T); % Same notation ; = cov(xt, xt-1|y1...yt) , eq (7)
  16.     P_T = zeros(p,p,T);
  17.     P_Tm2 = zeros(p,p,T); % cov(xT, xT-1|y1...yT)
  18.    
  19.     for t = 2:T %starts at t = 2 because at each time t we need info about t-1
  20.         x_m1(:,t) = F*x(:,t-1); % eq A3 ; pxp * px1 = px1
  21.         P_m1(:,:,t) = (F*P(:,:,t-1)*F') + Q; % A4 ; pxp * pxp = pxp
  22.        
  23.         if nnz(isnan(P_m1(:,:,t)))
  24.             error('NaNs in P_m1 at time t = %d',t);
  25.         end
  26.         if nnz(isinf(P_m1(:,:,t)))
  27.             error('Infs in P_m1 at time t = %d',t);
  28.         end
  29.        
  30.         K(:,t) = P_m1(:,:,t)*G(t,:)'*pinv((G(t,:)*P_m1(:,:,t)*G(t,:)') + R); %A5 ; pxp * px1 * 1*1 = p*1
  31.         %K(:,t) = P_m1(:,:,t)*G(t,:)'/((G(t,:)*P_m1(:,:,t)*G(t,:)') + R); %A5 ; pxp * px1 * 1*1 = p*1
  32.              
  33.         % The matrix inversion seems to generate NaN values which quickly
  34.         % contaminate all the other matrices. There is no warning about
  35.         % (close to) singular matrices or whatever. The use of pinv()
  36.         % instead of inv() seems to solve the problem... but I don't think
  37.         % it's the appropriate way to deal with it, there must be something
  38.         % wrong elsewhere
  39.        
  40.         if nnz(isnan(K(:,t)))
  41.             error('NaNs in K at time t = %d',t);
  42.         end
  43.      
  44.        
  45.         x(:,t) = x_m1(:,t) + (K(:,t)*(y(t)-(G(t,:)*x_m1(:,t)))); %A6
  46.         P(:,:,t) = P_m1(:,:,t) - (K(:,t)*G(t,:)*P_m1(:,:,t)); %A7
  47.     end
  48.  
  49.     x_T(:,T) = x(:,T);
  50.     P_T(:,:,T) = P(:,:,T);
  51.  
  52.     for t = T:-1:2 % we stop at 2 since we need to use t-1
  53.         %P_m1 seem to get really huge (x10^22...), might lead to "Inf"
  54.         %values which in turn might screw pinv()
  55.  
  56.         %% inv() caused NaN value to appear, pinv seems to solve the issue
  57.        
  58.         J(:,:,t-1) = P(:,:,t-1)*F'*pinv(P_m1(:,:,t)); % A8 pxp * pxp * pxp  
  59.         %J(:,:,t-1) = P(:,:,t-1)*F'/(P_m1(:,:,t)); % A8 pxp * pxp * pxp  
  60.         x_T(:,t-1) = x(:,t-1) + J(:,:,t-1)*(x_T(:,t)-(F*x(:,t-1))); %A9  % Becomes NaN during 8th iteration!
  61.         P_T(:,:,t-1) = P(:,:,t-1) + J(:,:,t-1)*(P_T(:,:,t)-P_m1(:,:,t))*J(:,:,t-1)'; %A10
  62.        
  63.         nans = [nnz(isnan(J)) nnz(isnan(P_m1)) nnz(isnan(F)) nnz(isnan(x_T)) nnz(isnan(x_m1))];  
  64.         if nnz(nans)
  65.             error('NaN invasion at time t = %d',t);
  66.         end
  67.     end
  68.        
  69.     P_Tm2(:,:,T) = (eye(p) - K(:,T)*G(T,:))*F*P(:,:,T-1); % %A12
  70.    
  71.     for t = T:-1:3 % stop at 3 because use of t-2
  72.        P_Tm2(:,:,t-1) = P_m1(:,:,t-1)*J(:,:,t-2)' + J(:,:,t-1)*(P_Tm2(:,:,t)-F*P(:,:,t-1))*J(:,:,t-2)'; % A11
  73.     end
  74. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement