Skip to content
Snippets Groups Projects
Commit 976c8c16 authored by MichelJuillard's avatar MichelJuillard
Browse files

making univariate Kalman filter code simpler and more efficient

parent edd95a94
Branches
Tags
No related merge requests found
...@@ -132,27 +132,25 @@ while notsteady && t<=last ...@@ -132,27 +132,25 @@ while notsteady && t<=last
for i=1:rows(z) for i=1:rows(z)
if Zflag if Zflag
prediction_error = Y(d_index(i),t) - z(i,:)*a; prediction_error = Y(d_index(i),t) - z(i,:)*a;
Fi = z(i,:)*P*z(i,:)' + H(d_index(i)); PZ = P*z(i,:)';
Fi = z(i,:)*PZ + H(d_index(i));
else else
prediction_error = Y(d_index(i),t) - a(z(i)); prediction_error = Y(d_index(i),t) - a(z(i));
Fi = P(z(i),z(i)) + H(d_index(i)); PZ = P(:,z(i));
Fi = PZ(z(i)) + H(d_index(i));
end end
if Fi>kalman_tol if Fi>kalman_tol
if Zflag Ki = PZ/Fi;
Ki = (P*z(i,:)')/Fi;
else
Ki = P(:,z(i))/Fi;
end
if t>no_more_missing_observations if t>no_more_missing_observations
K(:,i) = Ki; K(:,i) = Ki;
end end
a = a + Ki*prediction_error; a = a + Ki*prediction_error;
P = P - (Fi*Ki)*Ki'; P = P - PZ*Ki';
lik(s) = lik(s) + log(Fi) + prediction_error*prediction_error/Fi + l2pi; lik(s) = lik(s) + log(Fi) + prediction_error*prediction_error/Fi + l2pi;
end end
end end
a = T*a; a = T*a;
P = T*P*transpose(T) + QQ; P = T*P*T' + QQ;
if t>=no_more_missing_observations if t>=no_more_missing_observations
notsteady = max(abs(K(:)-oldK))>riccati_tol; notsteady = max(abs(K(:)-oldK))>riccati_tol;
oldK = K(:); oldK = K(:);
......
...@@ -90,19 +90,17 @@ while t<=last ...@@ -90,19 +90,17 @@ while t<=last
for i=1:pp for i=1:pp
if Zflag if Zflag
prediction_error = Y(i,t) - Z(i,:)*a; prediction_error = Y(i,t) - Z(i,:)*a;
Fi = Z(i,:)*PP*Z(i,:)' + H(i); PPZ = PP*Z(i,:)';
Fi = Z(i,:)*PPZ + H(i);
else else
prediction_error = Y(i,t) - a(Z(i)); prediction_error = Y(i,t) - a(Z(i));
Fi = PP(Z(i),Z(i)) + H(i); PPZ = PP(:,Z(i));
Fi = PPZ(Z(i)) + H(i);
end end
if Fi>kalman_tol if Fi>kalman_tol
if Zflag Ki = PPZ/Fi;
Ki = (PP*Z(i,:))'/Fi;
else
Ki = PP(:,Z(i))/Fi;
end
a = a + Ki*prediction_error; a = a + Ki*prediction_error;
PP = PP - (Fi*Ki)*transpose(Ki); PP = PP - PPZ*Ki';
likk(s) = likk(s) + log(Fi) + prediction_error*prediction_error/Fi + l2pi; likk(s) = likk(s) + log(Fi) + prediction_error*prediction_error/Fi + l2pi;
end end
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment