1function [xparam1,estim_params_,xparam1_explicitly_initialized,xparam1_properly_calibrated]=do_parameter_initialization(estim_params_,xparam1_calib,xparam1_NaN_set_to_prior_mean)
2% function [xparam1,estim_params_]=get_initialized_parameters(estim_params_,xparam1_calib)
3% gets explicitly initialized variables and properly calibrated parameters
4%
5% INPUTS
6%    o estim_params_    [structure] characterizing parameters to be estimated.
7%    o xparam1_calib    [double]    vector of parameters to be estimated, with parameters
8%                                   initialized from calibration using get_all_parameters
9%
10%    o xparam1_NaN_set_to_prior_mean [double]    vector of parameters to be estimated, with parameters
11%                                                initialized using dynare_estimation_init; not explicitly initialized
12%                                                parameters are at prior mean
13% OUTPUTS
14%    o xparam1                           [double]    vector of initialized parameters; uses the hierarchy: 1) explicitly initialized parameters,
15%                                                    2) calibrated parameters, 3) prior mean
16%    o estim_params_    [structure] characterizing parameters to be estimated; it is
17%                                   updated here to reflect calibrated parameters
18%    o xparam1_explicitly_initialized    [double]    vector of parameters to be estimated that
19%                                                    were explicitly initialized
20%    o xparam1_properly_calibrated       [double]    vector of parameters to be estimated that
21%                                                    were properly calibrated
22%
23% SPECIAL REQUIREMENTS
24%    None
25
26% Copyright (C) 2013-2017 Dynare Team
27%
28% This file is part of Dynare.
29%
30% Dynare is free software: you can redistribute it and/or modify
31% it under the terms of the GNU General Public License as published by
32% the Free Software Foundation, either version 3 of the License, or
33% (at your option) any later version.
34%
35% Dynare is distributed in the hope that it will be useful,
36% but WITHOUT ANY WARRANTY; without even the implied warranty of
37% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38% GNU General Public License for more details.
39%
40% You should have received a copy of the GNU General Public License
41% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
42
43nvx = size(estim_params_.var_exo,1);
44nvn = size(estim_params_.var_endo,1);
45ncx = size(estim_params_.corrx,1);
46ncn = size(estim_params_.corrn,1);
47np = size(estim_params_.param_vals,1);
48
49estim_params_.nvx = nvx; %exogenous shock variances
50estim_params_.nvn = nvn; %endogenous variances, i.e. measurement error
51estim_params_.ncx = ncx; %exogenous shock correlations
52estim_params_.ncn = ncn; % correlation between endogenous variables, i.e. measurement error.
53estim_params_.np = np;   % other parameters of the model
54
55xparam1_explicitly_initialized = NaN(nvx+nvn+ncx+ncn+np,1);
56xparam1_properly_calibrated = NaN(nvx+nvn+ncx+ncn+np,1);
57
58offset=0;
59if nvx
60    initialized_par_index=find(~isnan(estim_params_.var_exo(:,2)));
61    calibrated_par_index=find(isnan(estim_params_.var_exo(:,2)) & ~isnan(xparam1_calib(offset+1:offset+nvx,1)));
62    uninitialized_par_index=find(isnan(estim_params_.var_exo(:,2)) & isnan(xparam1_calib(offset+1:offset+nvx,1)));
63    xparam1_explicitly_initialized(offset+initialized_par_index,1) = estim_params_.var_exo(initialized_par_index,2);
64    %update estim_params_ with calibrated starting values
65    estim_params_.var_exo(calibrated_par_index,2)=xparam1_calib(offset+calibrated_par_index,1);
66    %find parameters that are calibrated and do not violate inverse gamma prior
67    xparam1_properly_calibrated(offset+calibrated_par_index,1) = xparam1_calib(offset+calibrated_par_index,1);
68    inv_gamma_violation=find(estim_params_.var_exo(calibrated_par_index,2)==0 & estim_params_.var_exo(calibrated_par_index,5)==4);
69    if inv_gamma_violation
70        estim_params_.var_exo(calibrated_par_index(inv_gamma_violation),2)=NaN;
71        xparam1_properly_calibrated(offset+calibrated_par_index(inv_gamma_violation),1)=NaN;
72        fprintf('PARAMETER INITIALIZATION: Some standard deviations of shocks of the calibrated model are 0 and\n')
73        fprintf('PARAMETER INITIALIZATION: violate the inverse gamma prior. They will instead be initialized with the prior mean.\n')
74    end
75    if uninitialized_par_index
76        fprintf('PARAMETER INITIALIZATION: Warning, some estimated standard deviations of shocks are not\n')
77        fprintf('PARAMETER INITIALIZATION: initialized. They will be initialized with the prior mean.\n')
78    end
79end
80offset=offset+nvx;
81if nvn
82    initialized_par_index=find(~isnan(estim_params_.var_endo(:,2)));
83    calibrated_par_index=find(isnan(estim_params_.var_endo(:,2)) & ~isnan(xparam1_calib(offset+1:offset+nvn,1)));
84    uninitialized_par_index=find(isnan(estim_params_.var_endo(:,2)) & isnan(xparam1_calib(offset+1:offset+nvn,1)));
85    xparam1_explicitly_initialized(offset+initialized_par_index,1) = estim_params_.var_endo(initialized_par_index,2);
86    estim_params_.var_endo(calibrated_par_index,2)=xparam1_calib(offset+calibrated_par_index,1);
87    %find parameters that are calibrated and do not violate inverse gamma prior
88    xparam1_properly_calibrated(offset+calibrated_par_index,1) = xparam1_calib(offset+calibrated_par_index,1);
89    inv_gamma_violation=find(estim_params_.var_endo(calibrated_par_index,2)==0 & estim_params_.var_endo(calibrated_par_index,5)==4);
90    if inv_gamma_violation
91        estim_params_.var_endo(calibrated_par_index(inv_gamma_violation),2)=NaN;
92        xparam1_properly_calibrated(offset+calibrated_par_index(inv_gamma_violation),1)=NaN;
93        fprintf('PARAMETER INITIALIZATION: Some measurement errors of the calibrated model are 0 and violate the\n')
94        fprintf('PARAMETER INITIALIZATION: inverse gamma prior. They will instead be initialized with the prior mean.\n')
95    end
96    if uninitialized_par_index
97        fprintf('PARAMETER INITIALIZATION: Warning, some measurement errors are not initialized. They will be initialized\n')
98        fprintf('PARAMETER INITIALIZATION: with the prior mean.\n')
99    end
100end
101offset=offset+nvn;
102if ncx
103    initialized_par_index=find(~isnan(estim_params_.corrx(:,3)));
104    calibrated_par_index=find(isnan(estim_params_.corrx(:,3)) & ~isnan(xparam1_calib(offset+1:offset+ncx,1)));
105    uninitialized_par_index=find(isnan(estim_params_.corrx(:,3)) & isnan(xparam1_calib(offset+1:offset+ncx,1)));
106    xparam1_explicitly_initialized(offset+initialized_par_index,1) = estim_params_.corrx(initialized_par_index,3);
107    estim_params_.corrx(calibrated_par_index,3)=xparam1_calib(offset+calibrated_par_index,1);
108    xparam1_properly_calibrated(offset+calibrated_par_index,1) = xparam1_calib(offset+calibrated_par_index,1);
109    if uninitialized_par_index
110        fprintf('PARAMETER INITIALIZATION: Warning, some correlations between structural shocks are not initialized.\n')
111        fprintf('PARAMETER INITIALIZATION: They will be initialized with the prior mean.\n')
112    end
113end
114offset=offset+ncx;
115if ncn
116    initialized_par_index=find(~isnan(estim_params_.corrn(:,3)));
117    calibrated_par_index=find(isnan(estim_params_.corrn(:,3)) & ~isnan(xparam1_calib(offset+1:offset+ncn,1)));
118    uninitialized_par_index=find(isnan(estim_params_.corrn(:,3)) & isnan(xparam1_calib(offset+1:offset+ncn,1)));
119    xparam1_explicitly_initialized(offset+initialized_par_index,1) = estim_params_.corrn(initialized_par_index,3);
120    estim_params_.corrn(calibrated_par_index,3)=xparam1_calib(offset+calibrated_par_index,1);
121    xparam1_properly_calibrated(offset+calibrated_par_index,1) = xparam1_calib(offset+calibrated_par_index,1);
122    if uninitialized_par_index
123        fprintf('PARAMETER INITIALIZATION: Warning, some correlations between measurement errors are not initialized.\n')
124        fprintf('PARAMETER INITIALIZATION: They will be initialized with the prior mean.\n')
125    end
126end
127offset=offset+ncn;
128if np
129    initialized_par_index=find(~isnan(estim_params_.param_vals(:,2)));
130    calibrated_par_index=find(isnan(estim_params_.param_vals(:,2)) & ~isnan(xparam1_calib(offset+1:offset+np,1)));
131    uninitialized_par_index=find(isnan(estim_params_.param_vals(:,2)) & isnan(xparam1_calib(offset+1:offset+np,1)));
132    xparam1_explicitly_initialized(offset+initialized_par_index,1) = estim_params_.param_vals(initialized_par_index,2);
133    estim_params_.param_vals(calibrated_par_index,2)=xparam1_calib(offset+calibrated_par_index,1);
134    xparam1_properly_calibrated(offset+calibrated_par_index,1) = xparam1_calib(offset+calibrated_par_index,1);
135    if uninitialized_par_index
136        fprintf('PARAMETER INITIALIZATION: Warning, some deep parameters are not initialized. They will be\n')
137        fprintf('PARAMETER INITIALIZATION: initialized with the prior mean.\n')
138    end
139end
140xparam1=xparam1_explicitly_initialized;
141xparam1(isnan(xparam1))=xparam1_properly_calibrated(isnan(xparam1)); %set not explicitly initialized parameters that do not obviously violate prior distribution to calibrated parameter values
142xparam1(isnan(xparam1))=xparam1_NaN_set_to_prior_mean(isnan(xparam1)); %set not yet initialized parameters to prior mean coming from dynare_estimation_init
143