fixing bug related to measurement errors
parent
9ac3e7a582
commit
9d91625c10
|
@ -145,6 +145,7 @@ ys = [];
|
||||||
trend_coeff = [];
|
trend_coeff = [];
|
||||||
exit_flag = 1;
|
exit_flag = 1;
|
||||||
info = 0;
|
info = 0;
|
||||||
|
singularity_flag = 0;
|
||||||
|
|
||||||
% Set flag related to analytical derivatives.
|
% Set flag related to analytical derivatives.
|
||||||
if nargout > 9
|
if nargout > 9
|
||||||
|
@ -315,18 +316,13 @@ Y = DynareDataset.data-trend;
|
||||||
kalman_algo = DynareOptions.kalman_algo;
|
kalman_algo = DynareOptions.kalman_algo;
|
||||||
|
|
||||||
% resetting measurement errors covariance matrix for univariate filters
|
% resetting measurement errors covariance matrix for univariate filters
|
||||||
no_correlation_flag = 1;
|
|
||||||
if (kalman_algo == 2) || (kalman_algo == 4)
|
if (kalman_algo == 2) || (kalman_algo == 4)
|
||||||
if isequal(H,0)
|
if isequal(H,0)
|
||||||
H = zeros(nobs,1);
|
H = zeros(nobs,1);
|
||||||
|
mmm = mm;
|
||||||
else
|
else
|
||||||
if all(all(abs(H-diag(diag(H)))<1e-14))% ie, the covariance matrix is diagonal...
|
if all(all(abs(H-diag(diag(H)))<1e-14))% ie, the covariance matrix is diagonal...
|
||||||
H = diag(H);
|
H = diag(H);
|
||||||
else
|
|
||||||
no_correlation_flag = 0;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if no_correlation_flag
|
|
||||||
mmm = mm;
|
mmm = mm;
|
||||||
else
|
else
|
||||||
Z = [Z, eye(pp)];
|
Z = [Z, eye(pp)];
|
||||||
|
@ -335,8 +331,10 @@ if (kalman_algo == 2) || (kalman_algo == 4)
|
||||||
R = blkdiag(R,eye(pp));
|
R = blkdiag(R,eye(pp));
|
||||||
Pstar = blkdiag(Pstar,H);
|
Pstar = blkdiag(Pstar,H);
|
||||||
Pinf = blckdiag(Pinf,zeros(pp));
|
Pinf = blckdiag(Pinf,zeros(pp));
|
||||||
|
H = zeros(nobs,1);
|
||||||
mmm = mm+pp;
|
mmm = mm+pp;
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -386,10 +384,33 @@ switch DynareOptions.lik_init
|
||||||
if isinf(dLIK)
|
if isinf(dLIK)
|
||||||
% Go to univariate diffuse filter if singularity problem.
|
% Go to univariate diffuse filter if singularity problem.
|
||||||
kalman_algo = 4;
|
kalman_algo = 4;
|
||||||
|
singularity_flag = 1;
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if (kalman_algo==4)
|
if (kalman_algo==4)
|
||||||
% Univariate Diffuse Kalman Filter
|
% Univariate Diffuse Kalman Filter
|
||||||
|
if singularity_flag
|
||||||
|
if isequal(H,0)
|
||||||
|
H = zeros(nobs,1);
|
||||||
|
mmm = mm;
|
||||||
|
else
|
||||||
|
if all(all(abs(H-diag(diag(H)))<1e-14))% ie, the covariance matrix is diagonal...
|
||||||
|
H = diag(H);
|
||||||
|
mmm = mm;
|
||||||
|
else
|
||||||
|
Z = [Z, eye(pp)];
|
||||||
|
T = blkdiag(T,zeros(pp));
|
||||||
|
Q = blkdiag(Q,H);
|
||||||
|
R = blkdiag(R,eye(pp));
|
||||||
|
Pstar = blkdiag(Pstar,H);
|
||||||
|
Pinf = blckdiag(Pinf,zeros(pp));
|
||||||
|
H = zeros(nobs,1);
|
||||||
|
mmm = mm+pp;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
% no need to test again for correlation elements
|
||||||
|
singularity_flag = 0;
|
||||||
|
end
|
||||||
[dLIK,tmp,a,Pstar] = univariate_kalman_filter_d(DynareDataset.missing.aindex,DynareDataset.missing.number_of_observations,DynareDataset.missing.no_more_missing_observations, ...
|
[dLIK,tmp,a,Pstar] = univariate_kalman_filter_d(DynareDataset.missing.aindex,DynareDataset.missing.number_of_observations,DynareDataset.missing.no_more_missing_observations, ...
|
||||||
Y, 1, size(Y,2), ...
|
Y, 1, size(Y,2), ...
|
||||||
zeros(mmm,1), Pinf, Pstar, ...
|
zeros(mmm,1), Pinf, Pstar, ...
|
||||||
|
@ -476,8 +497,6 @@ end
|
||||||
% 4. Likelihood evaluation
|
% 4. Likelihood evaluation
|
||||||
%------------------------------------------------------------------------------
|
%------------------------------------------------------------------------------
|
||||||
|
|
||||||
singularity_flag = 0;
|
|
||||||
|
|
||||||
if ((kalman_algo==1) || (kalman_algo==3))% Multivariate Kalman Filter
|
if ((kalman_algo==1) || (kalman_algo==3))% Multivariate Kalman Filter
|
||||||
if no_missing_data_flag
|
if no_missing_data_flag
|
||||||
if DynareOptions.block == 1
|
if DynareOptions.block == 1
|
||||||
|
@ -520,14 +539,10 @@ if ( singularity_flag || (kalman_algo==2) || (kalman_algo==4) )
|
||||||
if singularity_flag
|
if singularity_flag
|
||||||
if isequal(H,0)
|
if isequal(H,0)
|
||||||
H = zeros(nobs,1);
|
H = zeros(nobs,1);
|
||||||
|
mmm = mm;
|
||||||
else
|
else
|
||||||
if all(all(abs(H-diag(diag(H)))<1e-14))% ie, the covariance matrix is diagonal...
|
if all(all(abs(H-diag(diag(H)))<1e-14))% ie, the covariance matrix is diagonal...
|
||||||
H = diag(H);
|
H = diag(H);
|
||||||
else
|
|
||||||
no_correlation_flag = 0;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if no_correlation_flag
|
|
||||||
mmm = mm;
|
mmm = mm;
|
||||||
else
|
else
|
||||||
Z = [Z, eye(pp)];
|
Z = [Z, eye(pp)];
|
||||||
|
@ -536,9 +551,11 @@ if ( singularity_flag || (kalman_algo==2) || (kalman_algo==4) )
|
||||||
R = blkdiag(R,eye(pp));
|
R = blkdiag(R,eye(pp));
|
||||||
Pstar = blkdiag(Pstar,H);
|
Pstar = blkdiag(Pstar,H);
|
||||||
Pinf = blckdiag(Pinf,zeros(pp));
|
Pinf = blckdiag(Pinf,zeros(pp));
|
||||||
|
H = zeros(nobs,1);
|
||||||
mmm = mm+pp;
|
mmm = mm+pp;
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
LIK = univariate_kalman_filter(DynareDataset.missing.aindex,DynareDataset.missing.number_of_observations,DynareDataset.missing.no_more_missing_observations,Y,diffuse_periods+1,size(Y,2), ...
|
LIK = univariate_kalman_filter(DynareDataset.missing.aindex,DynareDataset.missing.number_of_observations,DynareDataset.missing.no_more_missing_observations,Y,diffuse_periods+1,size(Y,2), ...
|
||||||
a,Pstar, ...
|
a,Pstar, ...
|
||||||
|
|
|
@ -39,7 +39,7 @@ function [LIK, likk,a,P] = univariate_kalman_filter(data_index,number_of_observa
|
||||||
%! @item R
|
%! @item R
|
||||||
%! Matrix (@var{mm}*@var{rr}) of doubles, second matrix of the state equation relating the structural innovations to the state variables.
|
%! Matrix (@var{mm}*@var{rr}) of doubles, second matrix of the state equation relating the structural innovations to the state variables.
|
||||||
%! @item H
|
%! @item H
|
||||||
%! Matrix (@var{pp}*@var{pp}) of doubles, covariance matrix of the measurement errors (if no measurement errors set H as a zero scalar).
|
%! Vector (@var{pp}) of doubles, diagonal of covariance matrix of the measurement errors (corelation among measurement errors is handled by a model transformation).
|
||||||
%! @item Z
|
%! @item Z
|
||||||
%! Matrix (@var{pp}*@var{mm}) of doubles or vector of integers, matrix relating the states to the observed variables or vector of indices (depending on the value of @var{Zflag}).
|
%! Matrix (@var{pp}*@var{mm}) of doubles or vector of integers, matrix relating the states to the observed variables or vector of indices (depending on the value of @var{Zflag}).
|
||||||
%! @item mm
|
%! @item mm
|
||||||
|
@ -132,10 +132,10 @@ 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),d_index(i));
|
Fi = z(i,:)*P*z(i,:)' + 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),d_index(i));
|
Fi = P(z(i),z(i)) + H(d_index(i));
|
||||||
end
|
end
|
||||||
if Fi>kalman_tol
|
if Fi>kalman_tol
|
||||||
if Zflag
|
if Zflag
|
||||||
|
|
|
@ -41,7 +41,7 @@ function [dLIK, dlikk, a, Pstar, llik] = univariate_kalman_filter_d(data_index,
|
||||||
%! @item Q
|
%! @item Q
|
||||||
%! Matrix (@var{rr}*@var{rr}) of doubles, covariance matrix of the structural innovations (noise in the state equation).
|
%! Matrix (@var{rr}*@var{rr}) of doubles, covariance matrix of the structural innovations (noise in the state equation).
|
||||||
%! @item H
|
%! @item H
|
||||||
%! Matrix (@var{pp}*@var{pp}) of doubles, covariance matrix of the measurement errors (if no measurement errors set H as a zero scalar).
|
%! Vector (@var{pp}) of doubles, diagonal of covariance matrix of the measurement errors (corelation among measurement errors is handled by a model transformation).
|
||||||
%! @item Z
|
%! @item Z
|
||||||
%! Matrix (@var{pp}*@var{mm}) of doubles, matrix relating the states to the observed variables.
|
%! Matrix (@var{pp}*@var{mm}) of doubles, matrix relating the states to the observed variables.
|
||||||
%! @item mm
|
%! @item mm
|
||||||
|
@ -119,7 +119,7 @@ while newRank && (t<=last)
|
||||||
for i=1:length(d_index)
|
for i=1:length(d_index)
|
||||||
Zi = Z(d_index(i),:);
|
Zi = Z(d_index(i),:);
|
||||||
prediction_error = Y(d_index(i),t) - Zi*a;
|
prediction_error = Y(d_index(i),t) - Zi*a;
|
||||||
Fstar = Zi*Pstar*Zi' + H(d_index(i),d_index(i));
|
Fstar = Zi*Pstar*Zi' + H(d_index(i));
|
||||||
Finf = Zi*Pinf*Zi';
|
Finf = Zi*Pinf*Zi';
|
||||||
Kstar = Pstar*Zi';
|
Kstar = Pstar*Zi';
|
||||||
if Finf>kalman_tol && newRank
|
if Finf>kalman_tol && newRank
|
||||||
|
|
|
@ -25,6 +25,7 @@ function [LIK,likk,a] = univariate_kalman_filter_ss(Y,start,last,a,P,kalman_tol,
|
||||||
%! @item T
|
%! @item T
|
||||||
%! Matrix (@var{mm}*@var{mm}) of doubles, transition matrix of the state equation.
|
%! Matrix (@var{mm}*@var{mm}) of doubles, transition matrix of the state equation.
|
||||||
%! @item H
|
%! @item H
|
||||||
|
%! Vector (@var{pp}) of doubles, diagonal of covariance matrix of the measurement errors (corelation among measurement errors is handled by a model transformation).
|
||||||
%! Matrix (@var{pp}*@var{pp}) of doubles, covariance matrix of the measurement errors (if no measurement errors set H as a zero scalar).
|
%! Matrix (@var{pp}*@var{pp}) of doubles, covariance matrix of the measurement errors (if no measurement errors set H as a zero scalar).
|
||||||
%! @item Z
|
%! @item Z
|
||||||
%! Matrix (@var{pp}*@var{mm}) of doubles or vector of integers, matrix relating the states to the observed variables or vector of indices (depending on the value of @var{Zflag}).
|
%! Matrix (@var{pp}*@var{mm}) of doubles or vector of integers, matrix relating the states to the observed variables or vector of indices (depending on the value of @var{Zflag}).
|
||||||
|
@ -89,10 +90,10 @@ 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,i);
|
Fi = Z(i,:)*PP*Z(i,:)' + 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,i);
|
Fi = PP(Z(i),Z(i)) + H(i);
|
||||||
end
|
end
|
||||||
if Fi>kalman_tol
|
if Fi>kalman_tol
|
||||||
if Zflag
|
if Zflag
|
||||||
|
|
Loading…
Reference in New Issue