2021-05-17 21:24:25 +02:00
function [LIK, lik, a, P] = missing_observations_kalman_filter ( data_index,number_of_observations,no_more_missing_observations,Y,start,last,a,P,kalman_tol,riccati_tol,rescale_prediction_error_covariance,presample,T,Q,R,H,Z,mm,pp,rr,Zflag,diffuse_periods,occbin_)
2023-09-27 08:06:37 +02:00
% [LIK, lik, a, P] = missing_observations_kalman_filter(data_index,number_of_observations,no_more_missing_observations,Y,start,last,a,P,kalman_tol,riccati_tol,rescale_prediction_error_covariance,presample,T,Q,R,H,Z,mm,pp,rr,Zflag,diffuse_periods,occbin_)
2008-10-13 20:54:42 +02:00
% Computes the likelihood of a state space model in the case with missing observations.
%
% INPUTS
2008-10-16 20:03:33 +02:00
% data_index [cell] 1*smpl cell of column vectors of indices.
% number_of_observations [integer] scalar.
2011-09-19 17:01:24 +02:00
% no_more_missing_observations [integer] scalar.
2017-05-16 15:10:20 +02:00
% Y [double] pp*smpl matrix of data.
2011-09-19 17:01:24 +02:00
% start [integer] scalar, index of the first observation.
% last [integer] scalar, index of the last observation.
2021-01-18 17:33:21 +01:00
% a [double] pp*1 vector, levels of the predicted initial state variables (E_{0}(alpha_1)).
2011-09-19 17:01:24 +02:00
% P [double] pp*pp matrix, covariance matrix of the initial state vector.
% kalman_tol [double] scalar, tolerance parameter (rcond).
2017-05-16 15:10:20 +02:00
% riccati_tol [double] scalar, tolerance parameter (riccati iteration).
2011-09-19 17:01:24 +02:00
% presample [integer] scalar, presampling if strictly positive.
% T [double] mm*mm transition matrix of the state equation.
2017-05-16 15:10:20 +02:00
% Q [double] rr*rr covariance matrix of the structural innovations.
2011-09-19 17:01:24 +02:00
% R [double] mm*rr matrix, mapping structural innovations to state variables.
2017-05-16 15:10:20 +02:00
% H [double] pp*pp (or 1*1 =0 if no measurement error) covariance matrix of the measurement errors.
% Z [integer] pp*1 vector of indices for the observed variables.
2011-09-19 17:01:24 +02:00
% mm [integer] scalar, dimension of the state vector.
% pp [integer] scalar, number of observed variables.
2017-05-16 15:10:20 +02:00
% rr [integer] scalar, number of structural innovations.
%
% OUTPUTS
2010-12-10 22:31:30 +01:00
% LIK [double] scalar, MINUS loglikelihood
2008-10-13 20:54:42 +02:00
% lik [double] vector, density of observations in each period.
2021-01-18 17:33:21 +01:00
% a [double] mm*1 vector, current estimate of the state vector tomorrow (E_{T}(alpha_{T+1})).
2017-05-16 15:10:20 +02:00
% P [double] mm*mm matrix, covariance matrix of the states.
%
2008-10-13 20:54:42 +02:00
%
% NOTES
2017-05-16 15:10:20 +02:00
% The vector "lik" is used to evaluate the jacobian of the likelihood.
2009-12-16 18:17:34 +01:00
2023-09-27 08:06:37 +02:00
% Copyright © 2004-2023 Dynare Team
2008-10-13 20:54:42 +02:00
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
2021-06-09 17:33:48 +02:00
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
2009-12-16 18:17:34 +01:00
2011-09-19 17:01:24 +02:00
% Set defaults
if nargin < 20
Zflag = 0 ;
diffuse_periods = 0 ;
end
if nargin < 21
diffuse_periods = 0 ;
end
if isempty ( Zflag )
Zflag = 0 ;
end
if isempty ( diffuse_periods )
diffuse_periods = 0 ;
end
2012-06-19 16:35:44 +02:00
if isequal ( H , 0 )
H = zeros ( pp , pp ) ;
end
2011-09-19 17:01:24 +02:00
% Get sample size.
smpl = last - start + 1 ;
% Initialize some variables.
dF = 1 ;
2021-05-02 17:07:12 +02:00
isqvec = false ;
if ndim ( Q ) > 2
Qvec = Q ;
Q = Q ( : , : , 1 ) ;
isqvec = true ;
end
2011-09-19 17:01:24 +02:00
QQ = R * Q * transpose ( R ) ; % Variance of R times the vector of structural innovations.
t = start ; % Initialization of the time index.
lik = zeros ( smpl , 1 ) ; % Initialization of the vector gathering the densities.
LIK = Inf ; % Default value of the log likelihood.
2010-04-16 12:19:09 +02:00
oldK = Inf ;
2011-09-19 17:01:24 +02:00
notsteady = 1 ;
2017-04-27 10:44:27 +02:00
F_singular = true ;
2012-07-01 15:19:36 +02:00
s = 0 ;
2020-01-03 13:19:57 +01:00
rescale_prediction_error_covariance0 = rescale_prediction_error_covariance ;
2021-05-17 21:24:25 +02:00
if occbin_ . status
Qt = repmat ( Q , [ 1 1 3 ] ) ;
a0 = zeros ( mm , last ) ;
a1 = zeros ( mm , last ) ;
P0 = zeros ( mm , mm , last ) ;
P1 = zeros ( mm , mm , last ) ;
vv = zeros ( pp , last ) ;
options_ = occbin_ . info { 1 } ;
2023-09-27 08:06:37 +02:00
dr = occbin_ . info { 2 } ;
endo_steady_state = occbin_ . info { 3 } ;
exo_steady_state = occbin_ . info { 4 } ;
exo_det_steady_state = occbin_ . info { 5 } ;
M_ = occbin_ . info { 6 } ;
occbin_options = occbin_ . info { 7 } ;
2021-05-17 21:24:25 +02:00
opts_regime . regime_history = occbin_options . opts_simul . init_regime ;
opts_regime . binding_indicator = occbin_options . opts_simul . init_binding_indicator ;
2022-11-18 17:36:29 +01:00
if t > 1
first_period_occbin_update = max ( t + 1 , options_ . occbin . likelihood . first_period_occbin_update ) ;
else
first_period_occbin_update = options_ . occbin . likelihood . first_period_occbin_update ;
end
2021-05-17 21:24:25 +02:00
if isempty ( opts_regime . binding_indicator ) && isempty ( opts_regime . regime_history )
2021-07-20 12:35:15 +02:00
opts_regime . binding_indicator = zeros ( last + 2 , M_ . occbin . constraint_nbr ) ;
2021-05-17 21:24:25 +02:00
end
2023-09-27 08:06:37 +02:00
[ ~ , ~ , ~ , regimes_ ] = occbin . check_regimes ( [ ] , [ ] , [ ] , opts_regime , M_ , options_ , dr , endo_steady_state , exo_steady_state , exo_det_steady_state ) ;
if length ( occbin_ . info ) > 7
TT = occbin_ . info { 8 } ;
RR = occbin_ . info { 9 } ;
CC = occbin_ . info { 10 } ;
T0 = occbin_ . info { 11 } ;
R0 = occbin_ . info { 12 } ;
2021-05-17 21:24:25 +02:00
TT = cat ( 3 , TT , T ) ;
RR = cat ( 3 , RR , R ) ;
CC = cat ( 2 , CC , zeros ( mm , 1 ) ) ;
if size ( TT , 3 ) < ( last + 1 )
TT = repmat ( T , 1 , 1 , last + 1 ) ;
RR = repmat ( R , 1 , 1 , last + 1 ) ;
CC = repmat ( zeros ( mm , 1 ) , 1 , last + 1 ) ;
2022-11-18 17:36:29 +01:00
end
2021-05-17 21:24:25 +02:00
end
else
first_period_occbin_update = inf ;
C = 0 ;
end
2012-07-01 15:19:36 +02:00
2015-11-08 18:59:30 +01:00
while notsteady && t < = last
2021-05-17 21:24:25 +02:00
if occbin_ . status
a1 ( : , t ) = a ;
P1 ( : , : , t ) = P ;
C = CC ( : , t + 1 ) ;
R = RR ( : , : , t + 1 ) ;
T = TT ( : , : , t + 1 ) ;
if ~ ( isqvec )
QQ = R * Q * transpose ( R ) ; % Variance of R times the vector of structural innovations.
end
2022-12-02 11:11:31 +01:00
if t == 1
Pinit = P1 ( : , : , 1 ) ;
ainit = a1 ( : , 1 ) ;
end
2022-11-18 17:36:29 +01:00
end
2011-09-19 17:01:24 +02:00
s = t - start + 1 ;
2009-12-16 18:17:34 +01:00
d_index = data_index { t } ;
2021-05-02 17:07:12 +02:00
if isqvec
QQ = R * Qvec ( : , : , t + 1 ) * transpose ( R ) ;
end
2009-12-16 18:17:34 +01:00
if isempty ( d_index )
a = T * a ;
P = T * P * transpose ( T ) + QQ ;
else
2011-09-19 17:01:24 +02:00
% Compute the prediction error and its variance
if Zflag
z = Z ( d_index , : ) ;
v = Y ( d_index , t ) - z * a ;
F = z * P * z ' + H ( d_index , d_index ) ;
else
z = Z ( d_index ) ;
v = Y ( d_index , t ) - a ( z ) ;
F = P ( z , z ) + H ( d_index , d_index ) ;
2009-12-16 18:17:34 +01:00
end
2017-04-27 10:44:27 +02:00
badly_conditioned_F = false ;
if rescale_prediction_error_covariance
sig = sqrt ( diag ( F ) ) ;
if any ( diag ( F ) < kalman_tol ) || rcond ( F ./ ( sig * sig ' ) ) < kalman_tol
badly_conditioned_F = true ;
end
else
if rcond ( F ) < kalman_tol
2020-01-03 13:19:57 +01:00
sig = sqrt ( diag ( F ) ) ;
if any ( diag ( F ) < kalman_tol ) || rcond ( F ./ ( sig * sig ' ) ) < kalman_tol
badly_conditioned_F = true ;
else
rescale_prediction_error_covariance = 1 ;
end
% badly_conditioned_F = true;
2017-04-27 10:44:27 +02:00
end
end
2022-11-18 17:36:29 +01:00
if badly_conditioned_F && ( ~ occbin_ . status || ( occbin_ . status && t < first_period_occbin_update ) )
if ~ all ( abs ( F ( : ) ) < kalman_tol )
% Use univariate filter.
return
else
% Pathological case, discard draw
return
end
else
F_singular = false ;
end
2021-05-17 21:24:25 +02:00
if ~ occbin_ . status || ( occbin_ . status && ( options_ . occbin . likelihood . use_updated_regime == 0 || t < first_period_occbin_update ) )
2022-11-18 17:36:29 +01:00
if rescale_prediction_error_covariance
log_dF = log ( det ( F ./ ( sig * sig ' ) ) ) + 2 * sum ( log ( sig ) ) ;
iF = inv ( F ./ ( sig * sig ' ) ) ./ ( sig * sig ' ) ;
rescale_prediction_error_covariance = rescale_prediction_error_covariance0 ;
2011-09-19 17:01:24 +02:00
else
2022-11-18 17:36:29 +01:00
log_dF = log ( det ( F ) ) ;
iF = inv ( F ) ;
end
lik ( s ) = log_dF + transpose ( v ) * iF * v + length ( d_index ) * log ( 2 * pi ) ;
if t < first_period_occbin_update
if Zflag
K = P * z ' * iF ;
2021-05-17 21:24:25 +02:00
if occbin_ . status
2022-11-18 17:36:29 +01:00
P0 ( : , : , t ) = ( P - K * z * P ) ;
2021-05-17 21:24:25 +02:00
end
2022-11-18 17:36:29 +01:00
P = T * ( P - K * z * P ) * transpose ( T ) + QQ ;
else
K = P ( : , z ) * iF ;
if occbin_ . status
P0 ( : , : , t ) = ( P - K * P ( z , : ) ) ;
2021-05-17 21:24:25 +02:00
end
2022-11-18 17:36:29 +01:00
P = T * ( P - K * P ( z , : ) ) * transpose ( T ) + QQ ;
end
if occbin_ . status
a0 ( : , t ) = ( a + K * v ) ;
vv ( d_index , t ) = v ;
end
a = T * ( a + K * v ) + C ;
if t > = no_more_missing_observations && ~ isqvec && ~ occbin_ . status
notsteady = max ( abs ( K ( : ) - oldK ) ) > riccati_tol ;
oldK = K ( : ) ;
2021-05-17 21:24:25 +02:00
end
2010-06-24 18:18:57 +02:00
end
2009-05-28 20:14:07 +02:00
end
2008-10-13 20:54:42 +02:00
end
2021-05-17 21:24:25 +02:00
if occbin_ . status && t > = first_period_occbin_update
2022-11-18 17:36:29 +01:00
2021-05-17 21:24:25 +02:00
occbin_options . opts_simul . waitbar = 0 ;
2022-11-18 17:36:29 +01:00
if t == 1
if isqvec
Qt = cat ( 3 , Q , Qvec ( : , : , t : t + 1 ) ) ;
end
a00 = ainit ;
a10 = [ a00 a ( : , 1 ) ] ;
P00 = Pinit ;
P10 = P1 ( : , : , [ 1 1 ] ) ;
data_index0 { 1 } = [ ] ;
data_index0 ( 2 ) = data_index ( 1 ) ;
v0 ( : , 2 ) = vv ( : , 1 ) ;
Y0 ( : , 2 ) = Y ( : , 1 ) ;
Y0 ( : , 1 ) = nan ;
TT01 = cat ( 3 , T , TT ( : , : , 1 ) ) ;
RR01 = cat ( 3 , R , RR ( : , : , 1 ) ) ;
CC01 = zeros ( size ( CC , 1 ) , 2 ) ;
CC01 ( : , 2 ) = CC ( : , 1 ) ;
2023-09-27 08:06:37 +02:00
[ ax , a1x , Px , P1x , vx , Tx , Rx , Cx , regimes_ ( t : t + 2 ) , info , M_ , likx ] = occbin . kalman_update_algo_1 ( a00 , a10 , P00 , P10 , data_index0 , Z , v0 , Y0 , H , Qt , T0 , R0 , TT01 , RR01 , CC01 , regimes_ ( t : t + 1 ) , M_ , dr , endo_steady_state , exo_steady_state , exo_det_steady_state , options_ , occbin_options ) ;
2022-11-18 17:36:29 +01:00
else
if isqvec
Qt = Qvec ( : , : , t - 1 : t + 1 ) ;
end
2023-09-27 08:06:37 +02:00
[ ax , a1x , Px , P1x , vx , Tx , Rx , Cx , regimes_ ( t : t + 2 ) , info , M_ , likx ] = occbin . kalman_update_algo_1 ( a0 ( : , t - 1 ) , a1 ( : , t - 1 : t ) , P0 ( : , : , t - 1 ) , P1 ( : , : , t - 1 : t ) , data_index ( t - 1 : t ) , Z , vv ( : , t - 1 : t ) , Y ( : , t - 1 : t ) , H , Qt , T0 , R0 , TT ( : , : , t - 1 : t ) , RR ( : , : , t - 1 : t ) , CC ( : , t - 1 : t ) , regimes_ ( t : t + 1 ) , M_ , dr , endo_steady_state , exo_steady_state , exo_det_steady_state , options_ , occbin_options ) ;
2022-11-18 17:36:29 +01:00
end
2021-05-17 21:24:25 +02:00
if info
2023-01-15 19:00:04 +01:00
if options_ . debug
2023-10-29 11:30:23 +01:00
fprintf ( ' \nmissing_observations_kalman_filter:PKF failed in period %u with: %s\n' , t , get_error_message ( info , options_ ) ) ;
2023-01-15 19:00:04 +01:00
end
2021-05-17 21:24:25 +02:00
return
end
if options_ . occbin . likelihood . use_updated_regime
lik ( s ) = likx ;
end
a0 ( : , t ) = ax ( : , 1 ) ;
a1 ( : , t ) = a1x ( : , 2 ) ;
a = ax ( : , 2 ) ;
vv ( d_index , t ) = vx ( d_index , 2 ) ;
TT ( : , : , t : t + 1 ) = Tx ;
RR ( : , : , t : t + 1 ) = Rx ;
CC ( : , t : t + 1 ) = Cx ;
P0 ( : , : , t ) = Px ( : , : , 1 ) ;
P1 ( : , : , t ) = P1x ( : , : , 2 ) ;
P = Px ( : , : , 2 ) ;
2022-11-18 17:36:29 +01:00
2021-05-17 21:24:25 +02:00
end
2011-09-19 17:01:24 +02:00
t = t + 1 ;
2009-12-16 18:17:34 +01:00
end
if F_singular
error ( ' The variance of the forecast error remains singular until the end of the sample' )
end
2011-09-19 17:01:24 +02:00
% Divide by two.
lik ( 1 : s ) = . 5 * lik ( 1 : s ) ;
2009-12-16 18:17:34 +01:00
2011-09-19 17:01:24 +02:00
% Call steady state Kalman filter if needed.
2012-05-01 14:07:03 +02:00
if t < = last
2017-04-27 10:44:27 +02:00
[ tmp , lik ( s + 1 : end ) ] = kalman_filter_ss ( Y , t , last , a , T , K , iF , log_dF , Z , pp , Zflag ) ;
2011-09-19 17:01:24 +02:00
end
2009-05-28 20:14:07 +02:00
2011-09-19 17:01:24 +02:00
% Compute minus the log-likelihood.
2012-01-22 22:40:46 +01:00
if presample > = diffuse_periods
LIK = sum ( lik ( 1 + presample - diffuse_periods : end ) ) ;
else
LIK = sum ( lik ) ;
2021-05-17 21:24:25 +02:00
end