2021-08-15 21:00:29 +02:00
function W_opt = optimal_weighting_matrix ( m_data, moments, q_lag)
% W_opt = optimal_weighting_matrix(m_data, moments, q_lag)
2020-06-11 13:21:31 +02:00
% -------------------------------------------------------------------------
2020-06-29 07:40:34 +02:00
% This function computes the optimal weigthing matrix by a Bartlett kernel with maximum lag q_lag
2020-06-11 13:21:31 +02:00
% Adapted from replication codes of
2021-10-01 18:30:39 +02:00
% o Andreasen, Fernández-Villaverde, Rubio-Ramírez (2018): "The Pruned State-Space System for Non-Linear DSGE Models: Theory and Empirical Applications", Review of Economic Studies, 85(1):1-49.
2020-06-11 13:21:31 +02:00
% =========================================================================
% INPUTS
2020-07-10 21:42:18 +02:00
% o m_data [T x numMom] selected data moments at each point in time
% o moments [numMom x 1] selected estimated moments (either data_moments or estimated model_moments)
2020-06-29 07:40:34 +02:00
% o q_lag [integer] Bartlett kernel maximum lag order
2020-06-11 13:21:31 +02:00
% -------------------------------------------------------------------------
% OUTPUTS
2020-06-29 07:40:34 +02:00
% o W_opt [numMom x numMom] optimal weighting matrix
2020-06-11 13:21:31 +02:00
% -------------------------------------------------------------------------
% This function is called by
2021-08-15 21:00:29 +02:00
% o mom.run.m
2020-06-11 13:21:31 +02:00
% -------------------------------------------------------------------------
% This function calls:
% o CorrMatrix (embedded)
% =========================================================================
2022-04-13 13:15:19 +02:00
% Copyright © 2020-2021 Dynare Team
2020-06-11 13:21:31 +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/>.
2020-06-11 13:21:31 +02:00
% -------------------------------------------------------------------------
% Author(s):
% o Willi Mutschler (willi@mutschler.eu)
2023-09-12 21:07:57 +02:00
% o Johannes Pfeifer (johannes.pfeifer@unibw.de)
2020-06-11 13:21:31 +02:00
% =========================================================================
% Initialize
2020-06-29 07:40:34 +02:00
[ T , num_Mom ] = size ( m_data ) ; %note that in m_data NaN values (due to leads or lags in matched_moments and missing data) were replaced by the mean
2020-06-11 13:21:31 +02:00
2020-06-29 07:40:34 +02:00
% center around moments (could be either data_moments or model_moments)
h_Func = m_data - repmat ( moments ' , T , 1 ) ;
2020-06-11 13:21:31 +02:00
% The required correlation matrices
2020-06-29 07:40:34 +02:00
GAMA_array = zeros ( num_Mom , num_Mom , q_lag ) ;
GAMA0 = Corr_Matrix ( h_Func , T , num_Mom , 0 ) ;
if q_lag > 0
for ii = 1 : q_lag
GAMA_array ( : , : , ii ) = Corr_Matrix ( h_Func , T , num_Mom , ii ) ;
2020-06-11 13:21:31 +02:00
end
end
% The estimate of S
S = GAMA0 ;
2020-06-29 07:40:34 +02:00
if q_lag > 0
for ii = 1 : q_lag
S = S + ( 1 - ii / ( q_lag + 1 ) ) * ( GAMA_array ( : , : , ii ) + GAMA_array ( : , : , ii ) ' ) ;
2020-06-11 13:21:31 +02:00
end
end
% The estimate of W
2020-06-29 07:40:34 +02:00
W_opt = S \ eye ( size ( S , 1 ) ) ;
2020-06-11 13:21:31 +02:00
2022-09-16 10:36:55 +02:00
W_opt = ( W_opt + W_opt ' ) / 2 ; %assure symmetry
2020-06-11 13:21:31 +02:00
end
% The correlation matrix
2020-06-29 07:40:34 +02:00
function GAMA_corr = Corr_Matrix ( h_Func,T,num_Mom,v)
GAMA_corr = zeros ( num_Mom , num_Mom ) ;
2020-06-11 13:21:31 +02:00
for t = 1 + v : T
2020-06-29 07:40:34 +02:00
GAMA_corr = GAMA_corr + h_Func ( t - v , : ) ' * h_Func ( t , : ) ;
2020-06-11 13:21:31 +02:00
end
2020-06-29 07:40:34 +02:00
GAMA_corr = GAMA_corr / T ;
2021-10-01 18:30:39 +02:00
end