Skip to content
Snippets Groups Projects
Select Git revision
  • 4c71c49f392f4ed2f8d43b036b497ff428dc2ac5
  • master default protected
  • julia protected
  • 6.x protected
  • python-codegen
  • llvm-15
  • 5.x protected
  • 4.6 protected
  • uop
  • rework_pac
  • aux_vars_fix
  • julia-7.0.0
  • julia-6.4.0
  • julia-6.3.0
  • julia-6.2.0
15 results

ParsingDriver.hh

Blame
  • dynare_estimation_init.m 11.92 KiB
    function [dataset_,xparam1, M_, options_, oo_, estim_params_,bayestopt_, fake] = dynare_estimation_init(var_list_, dname, gsa_flag, M_, options_, oo_, estim_params_, bayestopt_)
    
    % function dynare_estimation_init(var_list_, gsa_flag)
    % preforms initialization tasks before estimation or
    % global sensitivity analysis
    %
    % INPUTS
    %   var_list_:  selected endogenous variables vector
    %   dname:      alternative directory name
    %   gsa_flag:   flag for GSA operation (optional)
    %
    % OUTPUTS
    %   data:    data after required transformation
    %   rawdata:  data as in the data file
    %   xparam1:    initial value of estimated parameters as returned by
    %               set_prior()
    %
    % SPECIAL REQUIREMENTS
    %   none
    
    % Copyright (C) 2003-2011 Dynare Team
    %
    % 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
    % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
    
    if isempty(gsa_flag)
        gsa_flag = 0;
    else% Decide if a DSGE or DSGE-VAR has to be estimated.
        if ~isempty(strmatch('dsge_prior_weight',M_.param_names))
            options_.dsge_var = 1;
        end
        var_list_ = check_list_of_variables(options_, M_, var_list_);
        options_.varlist = var_list_;
    end
    
    % Get the indices of the observed variables in M_.endo_names.
    options_.lgyidx2varobs = zeros(size(M_.endo_names,1),1);
    for i = 1:size(M_.endo_names,1)
        tmp = strmatch(deblank(M_.endo_names(i,:)),options_.varobs,'exact');
        if ~isempty(tmp)
            if length(tmp)>1
                disp(' ')
                error(['Multiple declarations of ' deblank(M_.endo_names(i,:)) ' as an observed variable is not allowed!'])
            end
            options_.lgyidx2varobs(i) = tmp;
        end
    end
    
    % Set the order of approximation to one (if needed).
    if options_.order > 1 && isempty(options_.nonlinear_filter)
        disp('This version of Dynare cannot estimate non linearized models!')
        disp('Set "order" equal to 1.')
        disp(' ')
        options_.order = 1;
    end
    
    % Set options_.lik_init equal to 3 if diffuse filter is used or
    % kalman_algo refers to a diffuse filter algorithm.
    if (options_.diffuse_filter==1) || (options_.kalman_algo > 2)
        if options_.lik_init == 2
            error(['options diffuse_filter, lik_init and/or kalman_algo have ' ...
                   'contradictory settings'])
        else
            options_.lik_init = 3;
        end
    end
    
    % If options_.lik_init == 1
    %  set by default options_.qz_criterium to 1-1e-6
    %  and check options_.qz_criterium < 1-eps if options_.lik_init == 1
    % Else set by default options_.qz_criterium to 1+1e-6
    if options_.lik_init == 1
        if isempty(options_.qz_criterium)
            options_.qz_criterium = 1-1e-6;
        elseif options_.qz_criterium > 1-eps
            error(['estimation: option qz_criterium is too large for estimating ' ...
                   'a stationary model. If your model contains unit roots, use ' ...
                   'option diffuse_filter'])
        end
    elseif isempty(options_.qz_criterium)
        options_.qz_criterium = 1+1e-6;
    end
    
    % If the data are prefiltered then there must not be constants in the
    % measurement equation of the DSGE model or in the DSGE-VAR model.
    if options_.prefilter == 1
        options_.noconstant = 1;
    end
    
    % Set options related to filtered variables.
    if ~isequal(options_.filtered_vars,0) && isempty(options_.filter_step_ahead)
        options_.filter_step_ahead = 1;
    end
    if ~isequal(options_.filtered_vars,0) && isequal(options_.filter_step_ahead,0)
        options_.filter_step_ahead = 1;
    end
    if ~isequal(options_.filter_step_ahead,0)
        options_.nk = max(options_.filter_step_ahead);
    end
    
    % Set the name of the directory where (intermediary) results will be saved.
    if isempty(dname)
        M_.dname = M_.fname;
    else
        M_.dname = dname;
    end
    
    % Set the number of observed variables.
    n_varobs = size(options_.varobs,1);
    
    % Set priors over the estimated parameters.
    if ~isempty(estim_params_)
        [xparam1,estim_params_,bayestopt_,lb,ub,M_] = set_prior(estim_params_,M_,options_);
        if any(bayestopt_.pshape > 0)
            % Plot prior densities.
            if ~options_.nograph && options_.plot_priors
                plot_priors(bayestopt_,M_,estim_params_,options_)
            end
            % Set prior bounds
            bounds = prior_bounds(bayestopt_,options_);
            bounds(:,1)=max(bounds(:,1),lb);
            bounds(:,2)=min(bounds(:,2),ub);
        else
            % No priors are declared so Dynare will estimate the model by
            % maximum likelihood with inequality constraints for the parameters.
            options_.mh_replic = 0;% No metropolis.
            bounds(:,1) = lb;
            bounds(:,2) = ub;
        end
        % Test if initial values of the estimated parameters are all between
        % the prior lower and upper bounds.
        if any(xparam1 < bounds(:,1)) || any(xparam1 > bounds(:,2))
            find(xparam1 < bounds(:,1))
            find(xparam1 > bounds(:,2))
            error('Initial parameter values are outside parameter bounds')
        end
        lb = bounds(:,1);
        ub = bounds(:,2);
        bayestopt_.lb = lb;
        bayestopt_.ub = ub;
    else% If estim_params_ is empty...
        xparam1 = [];
        bayestopt_.lb = [];
        bayestopt_.ub = [];
        bayestopt_.jscale = [];
        bayestopt_.pshape = [];
        bayestopt_.p1 = [];
        bayestopt_.p2 = [];
        bayestopt_.p3 = [];
        bayestopt_.p4 = [];
        bayestopt_.p5 = [];
        bayestopt_.p6 = [];
        bayestopt_.p7 = [];
        estim_params_.nvx = 0;
        estim_params_.nvn = 0;
        estim_params_.ncx = 0;
        estim_params_.ncn = 0;
        estim_params_.np = 0;
    end
    
    % Is there a linear trend in the measurement equation?
    if ~isfield(options_,'trend_coeffs') % No!
        bayestopt_.with_trend = 0;
    else% Yes!
        bayestopt_.with_trend = 1;
        bayestopt_.trend_coeff = {};
        trend_coeffs = options_.trend_coeffs;
        nt = length(trend_coeffs);
        for i=1:n_varobs
            if i > length(trend_coeffs)
                bayestopt_.trend_coeff{i} = '0';
            else
                bayestopt_.trend_coeff{i} = trend_coeffs{i};
            end
        end
    end
    
    % Set the "size" of penalty.
    bayestopt_.penalty = 1e8;
    
    % Get informations about the variables of the model.
    dr = set_state_space(oo_.dr,M_);
    oo_.dr = dr;
    nstatic = dr.nstatic;          % Number of static variables.
    npred = dr.npred;              % Number of predetermined variables.
    nspred = dr.nspred;            % Number of predetermined variables in the state equation.
    
    % Test if observed variables are declared.
    if isempty(options_.varobs)
        error('VAROBS is missing')
    end
    
    % Setting resticted state space (observed + predetermined variables)
    var_obs_index = [];
    k1 = [];
    for i=1:n_varobs
        var_obs_index = [var_obs_index; strmatch(deblank(options_.varobs(i,:)),M_.endo_names(dr.order_var,:),'exact')];
        k1 = [k1; strmatch(deblank(options_.varobs(i,:)),M_.endo_names, 'exact')];
    end
    
    % Define union of observed and state variables
    k2 = union(var_obs_index,[dr.nstatic+1:dr.nstatic+dr.npred]', 'rows');
    % Set restrict_state to postion of observed + state variables in expanded state vector.
    oo_.dr.restrict_var_list = k2;
    % set mf0 to positions of state variables in restricted state vector for likelihood computation.
    [junk,bayestopt_.mf0] = ismember([dr.nstatic+1:dr.nstatic+dr.npred]',k2);
    % Set mf1 to positions of observed variables in restricted state vector for likelihood computation.
    [junk,bayestopt_.mf1] = ismember(var_obs_index,k2);
    % Set mf2 to positions of observed variables in expanded state vector for filtering and smoothing.
    bayestopt_.mf2  = var_obs_index;
    bayestopt_.mfys = k1;
    
    [junk,ic] = intersect(k2,nstatic+(1:npred)');
    oo_.dr.restrict_columns = [ic; length(k2)+(1:nspred-npred)'];
    
    k3 = [];
    k3p = [];
    if options_.selected_variables_only
        for i=1:size(var_list_,1)
            k3 = [k3; strmatch(var_list_(i,:),M_.endo_names(dr.order_var,:), ...
                               'exact')];
            k3p = [k3; strmatch(var_list_(i,:),M_.endo_names, ...
                               'exact')];
        end
    else
        k3 = (1:M_.endo_nbr)';
        k3p = (1:M_.endo_nbr)';
    end
    
    % Define union of observed and state variables
    if options_.block == 1
        [k2, i_posA, i_posB] = union(k1', M_.state_var', 'rows');
        % Set restrict_state to postion of observed + state variables in expanded state vector.
        oo_.dr.restrict_var_list  = [k1(i_posA) M_.state_var(sort(i_posB))];
        % set mf0 to positions of state variables in restricted state vector for likelihood computation.
        [junk,bayestopt_.mf0] = ismember(M_.state_var',oo_.dr.restrict_var_list);
        % Set mf1 to positions of observed variables in restricted state vector for likelihood computation.
        [junk,bayestopt_.mf1] = ismember(k1,oo_.dr.restrict_var_list);
        % Set mf2 to positions of observed variables in expanded state vector for filtering and smoothing.
        bayestopt_.mf2  = var_obs_index;
        bayestopt_.mfys = k1;
        oo_.dr.restrict_columns = [size(i_posA,1)+(1:size(M_.state_var,2))];
    
        [k2, i_posA, i_posB] = union(k3p, M_.state_var', 'rows');
        bayestopt_.smoother_var_list = [k3p(i_posA); M_.state_var(sort(i_posB))'];
        [junk,bayestopt_.smoother_saved_var_list] = intersect(k3p,bayestopt_.smoother_var_list(:));
        [junk,ic] = intersect(bayestopt_.smoother_var_list,M_.state_var);
        bayestopt_.smoother_restrict_columns = ic;
        [junk,bayestopt_.smoother_mf] = ismember(k1, ...
                                                 bayestopt_.smoother_var_list);
    else
        k2 = union(var_obs_index,[dr.nstatic+1:dr.nstatic+dr.npred]', 'rows');
        % Set restrict_state to postion of observed + state variables in expanded state vector.
        oo_.dr.restrict_var_list = k2;
        % set mf0 to positions of state variables in restricted state vector for likelihood computation.
        [junk,bayestopt_.mf0] = ismember([dr.nstatic+1:dr.nstatic+dr.npred]',k2);
        % Set mf1 to positions of observed variables in restricted state vector for likelihood computation.
        [junk,bayestopt_.mf1] = ismember(var_obs_index,k2);
        % Set mf2 to positions of observed variables in expanded state vector for filtering and smoothing.
        bayestopt_.mf2  = var_obs_index;
        bayestopt_.mfys = k1;
        [junk,ic] = intersect(k2,nstatic+(1:npred)');
        oo_.dr.restrict_columns = [ic; length(k2)+(1:nspred-npred)'];
    
        bayestopt_.smoother_var_list = union(k2,k3);
        [junk,bayestopt_.smoother_saved_var_list] = intersect(k3,bayestopt_.smoother_var_list(:));
        [junk,ic] = intersect(bayestopt_.smoother_var_list,nstatic+(1:npred)');
        bayestopt_.smoother_restrict_columns = ic;
        [junk,bayestopt_.smoother_mf] = ismember(var_obs_index, ...
                                                 bayestopt_.smoother_var_list);
    end;
    
    % Test if the data file is declared.
    if isempty(options_.datafile)
        if gsa_flag
            dataset_ = [];
    %         rawdata = [];
    %         data_info = [];
            return
        else
            error('datafile option is missing')
        end
    end
    
    % If jscale isn't specified for an estimated parameter, use global option options_.jscale, set to 0.2, by default.
    k = find(isnan(bayestopt_.jscale));
    bayestopt_.jscale(k) = options_.mh_jscale;
    
    % Load and transform data.
    transformation = [];
    if options_.loglinear && ~options_.logdata
        transformation = @log;
    end
    xls.sheet = options_.xls_sheet;
    xls.range = options_.xls_range;
    
    if ~isfield(options_,'nobs')
        options_.nobs = [];
    end
    
    dataset_ = initialize_dataset(options_.datafile,options_.varobs,options_.first_obs,options_.nobs,transformation,options_.prefilter,xls);
    
    options_.nobs = dataset_.info.ntobs;
    
    % setting noconstant option
    if options_.diffuse_filter
        steadystate_check_flag = 0;
    else
        steadystate_check_flag = 1;
    end
    
    oo_.steady_state = evaluate_steady_state(oo_.steady_state,M_,options_,oo_,steadystate_check_flag);
    if all(abs(oo_.steady_state(bayestopt_.mfys))<1e-9)
        options_.noconstant = 1;
    else
        options_.noconstant = 0;
    end