1function estim_params=check_for_calibrated_covariances(xparam1,estim_params,M)
2% function check_for_calibrated_covariances(xparam1,estim_params,M)
3% find calibrated covariances to consider during estimation
4% Inputs
5%   -xparam1        [vector] parameters to be estimated
6%   -estim_params   [structure] describing parameters to be estimated
7%   -M              [structure] describing the model
8%
9% Outputs
10%   -estim_params   [structure] describing parameters to be estimated
11%
12% Notes: M is local to this function and not updated when calling
13% set_all_parameters
14
15% Copyright (C) 2013-2017 Dynare Team
16%
17% This file is part of Dynare.
18%
19% Dynare is free software: you can redistribute it and/or modify
20% it under the terms of the GNU General Public License as published by
21% the Free Software Foundation, either version 3 of the License, or
22% (at your option) any later version.
23%
24% Dynare is distributed in the hope that it will be useful,
25% but WITHOUT ANY WARRANTY; without even the implied warranty of
26% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27% GNU General Public License for more details.
28%
29% You should have received a copy of the GNU General Public License
30% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
31Sigma_e_calibrated=M.Sigma_e;
32H_calibrated=M.H;
33%check covariance for structural errors
34covariance_pos=find(tril(Sigma_e_calibrated,-1)); %off-diagonal elements set by covariances before updating correlation matrix to reflect estimated covariances
35covariance_pos_ME=find(tril(H_calibrated,-1)); %off-diagonal elements set by covariances before updating correlation matrix to reflect estimated covariances
36
37%locally updated M
38M = set_all_parameters(xparam1,estim_params,M);
39
40correlation_pos=find(tril(M.Correlation_matrix,-1)); %off-diagonal elements set by correlations after accounting for estimation
41calibrated_covariance_pos=covariance_pos(~ismember(covariance_pos,correlation_pos));
42if any(calibrated_covariance_pos)
43    [rows, columns]=ind2sub(size(M.Sigma_e),calibrated_covariance_pos); %find linear indices of lower triangular covariance entries
44    estim_params.calibrated_covariances.position=[calibrated_covariance_pos;sub2ind(size(M.Sigma_e),columns,rows)]; %get linear entries of upper triangular parts
45    estim_params.calibrated_covariances.cov_value=Sigma_e_calibrated(estim_params.calibrated_covariances.position);
46end
47
48correlation_pos_ME=find(tril(M.Correlation_matrix_ME,-1)); %off-diagonal elements set by correlations after accounting for estimation
49calibrated_covariance_pos_ME=covariance_pos_ME(~ismember(covariance_pos_ME,correlation_pos_ME));
50if any(calibrated_covariance_pos_ME)
51    [rows, columns]=ind2sub(size(M.H),calibrated_covariance_pos_ME); %find linear indices of lower triangular covariance entries
52    estim_params.calibrated_covariances_ME.position=[calibrated_covariance_pos_ME;sub2ind(size(M.H),columns,rows)]; %get linear entries of upper triangular parts
53    estim_params.calibrated_covariances_ME.cov_value=H_calibrated(estim_params.calibrated_covariances_ME.position);
54end
55