
Code Request Page Hello,
I am trying to implement a Kalman Filter MLE approach in Matlab. I have written a code, but it gives a lot of errors: function[L] = kalmanFilter1(params) global Z; k=6; T = size(Z,2); n = size(Z,1); F=zeros(k,k); delta=ones(k,1); S=diag(delta); V=zeros( k, k, T); %Vm=zeros(k,k,T); xhat=zeros( k, T); %xhatm=zeros(k,T); %params A matrix beta1=params(1); beta2=params(2); beta3=params(3); beta4=params(4); beta5=params(5); gamma1=params(6); gamma2=params(7); gamma3=params(8); gamma4=params(9); gamma5=params(10); A=[beta1, gamma1, 0, 0, 0, 0;... beta1, gamma1, 0, 0, 0, 0;... beta2, 0, gamma2, 0, 0, 0;... beta2, 0, gamma2, 0, 0, 0;... beta2, 0, gamma2, 0, 0, 0;... beta3, 0, 0, gamma3, 0, 0;... beta3, 0, 0, gamma3, 0, 0;... beta3, 0, 0, gamma3, 0, 0;... beta4, 0, 0, 0, gamma4, 0;... beta4, 0, 0, 0, gamma4, 0;... beta4, 0, 0, 0, gamma4, 0;... beta5, 0, 0, 0, 0, gamma5;... beta5, 0, 0, 0, 0, gamma5;... beta5, 0, 0, 0, 0, gamma5;... beta5, 0, 0, 0, 0, gamma5]; %params a a1=params(11); a2=params(12); a3=params(13); a4=params(14); a5=params(15); a6=params(16); a7=params(17); a8=params(18); a9=params(19); a10=params(20); a11=params(21); a12=params(22); a13=params(23); a14=params(24); a15=params(25); for i=1:T a(1,i)=a1; a(2,i)=a2; a(3,i)=a3; a(4,i)=a4; a(5,i)=a5; a(6,i)=a6; a(7,i)=a7; a(8,i)=a8; a(9,i)=a9; a(10,i)=a10; a(11,i)=a11; a(12,i)=a12; a(13,i)=a13; a(14,i)=a14; a(15,i)=a15; %params rho rho1=params(26); rho2=params(27); rho3=params(28); rho4=params(29); rho5=params(30); rho=[rho1, rho1, rho2, rho2, rho2, rho3, rho3, rho3, rho4, rho4, rho4, rho5, rho5, rho5, rho5]; W=diag(rho);
if n~=size(A,1) error('Dimension error  A & Z must agree.'); end
if (n~=size(W,1)  n~=size(W,2)) error('Dimension error  W is not valid covariance matrix.'); end
if (k~=size(F,2)) error('Dimension error  F not square.'); end
if (k~=size(A,2)) error('Dimension error  F & A do not agree.'); end
if (k~=size(S,1)  k~=size(S,2)) error('Dimension error  S is not valid.'); end
% Initialize loglikelihood L = 0;
for i = 1:T % Time update if i==1 xhatm = F*x0 ; Vm= F*V0*F' + S; else xhatm = F*xhat ; Vm = F*V*F' + S; end % Correction H = A*Vm*A' + W; K = Vm* A'*inv(H); xhat= xhatm+ K*(Z(:,i) A*xhatma); V= (eye(k)  K*A)*Vm'; %xfilt(:,i) = xhat(:,i); %Vfilt = V; %Vmfilt = Vm; % Update log likelihood L = L (n/2)*log(2*pi)  .5 * log( abs(det(H)) ) .5*(( Z(:,i)  a  A*xhatm )'*inv(H)*( Z(:,i)  a  A*xhatm )); end L=L*(1); end
Then I try to minimize the negative log likelihood function by: load DAT2.dat; Z=DAT2; T = size(Z,2); n = size(Z,1); k = 6; xo=ones(k,T); V0=zeros(k,k); F=zeros(k,k);
delta=ones(k,1); S=diag(delta); %params params=[0.0548, 0.0760, 0.0564, 0.0694, 0.0839, 0.0214, 0.0001, 0.0305, 0.0001, 0.0476, 0.0113, 0.0070, 0.0059, 0.0063, 0.0099, 0.0019, 0.0152, 0.0092, 0.0091, 0.0039, 0.0073, 0.0034, 0.0139, 0.0065, 0.0128, 0.0671, 0.0716, 0.0623, 0.0728, 0.1069];
x0=params; L=0
%optimize options=optimset('MaxIter',10000,'MaxFunEvals',10000); [A, a, rho]=fminsearch('kalmanFilter1',x0,options);
The following errors were generated: Error in ==> kalmanFilter1 at 2 global Z;
??? Output argument "L" (and maybe others) not assigned during call to "C:\Documents and Settings\Izabela\My Documents\MATLAB\kalmanFilter1.m (kalmanFilter1)".
Error in ==> fminsearch at 205 fv(:,1) = funfcn(x,varargin{:});
Can you please help? This is really urgent,any suggestion or ideas will be greatly appreciated.
