function [oo_,M_,options_,Smoothed_variables_declaration_order_deviation_form,initial_date]=evaluate_smoother(parameters,var_list,M_,oo_,options_,estim_params_)
% [oo_,M_,options_,Smoothed_variables_declaration_order_deviation_form,initial_date]=evaluate_smoother(parameters,var_list,M_,oo_,options_,estim_params_)
% Evaluate the smoother at parameters.
%
% INPUTS
%    o parameters  a string ('posterior mode','posterior mean','posterior median','prior mode','prior mean','mle_mode','calibration') or a vector of values for
%                  the (estimated) parameters of the model.
%    o var_list    subset of endogenous variables
%    o M_          [structure]  Definition of the model
%    o oo_         [structure]  Storage of results
%    o options_    [structure]  Options
%    o estim_params_ [structure] characterizing parameters to be estimated
%
% OUTPUTS
%    o oo       [structure]  results:
%                              - SmoothedVariables
%                              - SmoothedShocks
%                              - FilteredVariablesShockDecomposition
%                              - UpdatedVariables
%                              - FilteredVariables
%                              - SmoothedMeasurementErrors
%                              - FilteredVariablesKStepAhead
%                              - FilteredVariablesKStepAheadVariances
%    o M_          [structure]  Definition of the model
%    o options_    [structure]  Options; returns options_.first_obs
%    o Smoothed_variables_declaration_order_deviation_form
%                           Smoothed variables from the Kalman smoother in
%                           order of declaration of variables (M_.endo_names)
%                           in deviations from their respective mean, i.e.
%                           without trend and constant part (used for shock_decomposition)
%    o initial_date [dseries]   initial period, used for shock_decomposition
%
% SPECIAL REQUIREMENTS
%    None
%

% Copyright © 2010-2026 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 <https://www.gnu.org/licenses/>.

% store qz_criterium
qz_criterium_old=options_.qz_criterium;

if ischar(parameters) && strcmp(parameters,'calibration')
    options_.smoother=1;
end

[dataset_,dataset_info, ~, ~, M_, options_, oo_, estim_params_,bounds,Prior,smoother_info,measurement_info] = dynare_estimation_init(var_list, M_.fname, [], M_, options_, oo_, estim_params_);
initial_date=dataset_.firstdate;

% set the qz_criterium
options_=select_qz_criterium_value(options_);

if nargin==0
    parameters = 'posterior_mode';
end

if ischar(parameters)
    switch parameters
      case 'posterior_mode'
        parameters = get_posterior_parameters('mode',M_,estim_params_,oo_,options_);
      case 'posterior_mean'
        parameters = get_posterior_parameters('mean',M_,estim_params_,oo_,options_);
      case 'posterior_median'
        parameters = get_posterior_parameters('median',M_,estim_params_,oo_,options_);
      case 'mle_mode'
        parameters = get_posterior_parameters('mode',M_,estim_params_,oo_,options_,'mle_');
      case 'prior_mode'
        parameters = Prior.p5(:);
      case 'prior_mean'
        parameters = Prior.p1;
      case 'calibration'
        if isempty(oo_.dr)
            error('You must run ''stoch_simul'' first.');
        end
        parameters = [];
      otherwise
        disp('evaluate_smoother:: If the input argument is a string, then it has to be equal to:')
        disp('                     ''posterior_mode'', ')
        disp('                     ''posterior_mean'', ')
        disp('                     ''posterior_median'', ')
        disp('                     ''prior_mode'' or')
        disp('                     ''prior_mean''.')
        disp('                     ''calibration''.')
        error('evaluate_smoother: unknown parameter_set option %s.',parameters)
    end
end

if options_.occbin.smoother.status
    if options_.occbin.smoother.inversion_filter
        [~, info, ~, ~, ~, model_solution, smoother_results] = occbin.IVF_posterior(parameters,dataset_,dataset_info,options_,M_,estim_params_,Prior,bounds,oo_.dr, oo_.steady_state,oo_.exo_steady_state,oo_.exo_det_steady_state);
        if ismember(info(1),[303,304,306])
            oo_.occbin.smoother.error_flag=1;
        else
            oo_.occbin.smoother.error_flag=0;
            smoother_results.model.mf = smoother_info.var_list(smoother_info.mf);
        end
        oo_.dr=model_solution.dr; %required for subsequent calls like shock_decomposition
        occbin_info = [];
    else
        [smoother_results, occbin_info] = ...
            occbin.DSGE_smoother(parameters,M_,oo_.dr,oo_.steady_state,oo_.exo_steady_state,oo_.exo_det_steady_state,options_,estim_params_,dataset_,dataset_info,smoother_info);
        oo_.occbin.smoother.error_flag = occbin_info.error_flag;
        oo_.occbin.smoother.regime_history = occbin_info.regime_history;
        oo_.dr=smoother_results.model.dr; %required for subsequent calls like shock_decomposition
    end
    atT = smoother_results.smoothed.alphahat;
else
    smoother_results = DsgeSmoother(parameters,dataset_,dataset_info,M_,oo_.dr,oo_.steady_state,oo_.exo_steady_state,oo_.exo_det_steady_state,options_,estim_params_,smoother_info);
    oo_.dr = smoother_results.model.dr;
    atT = smoother_results.smoothed.alphahat;
    oo_.dr=smoother_results.model.dr; %required for subsequent calls like shock_decomposition
end
if ~options_.occbin.smoother.status || (options_.occbin.smoother.status && oo_.occbin.smoother.error_flag==0)
    if options_.occbin.smoother.status && options_.occbin.smoother.inversion_filter
        options_nk=options_.nk;
        options_.nk=[]; %unset options_.nk and reset it later
        [oo_,yf]=store_smoother_results(M_,oo_,options_,smoother_info,dataset_,dataset_info,smoother_results);
        options_.nk=options_nk;
    elseif options_.occbin.smoother.status
        [oo_,yf]=store_smoother_results(M_,oo_,options_,smoother_info,dataset_,dataset_info,smoother_results,occbin_info);
    else
        [oo_,yf]=store_smoother_results(M_,oo_,options_,smoother_info,dataset_,dataset_info,smoother_results);
    end
else
    fprintf('\nIVF: smoother did not succeed. No results will be written to oo_.\n')
end
if nargout>3
    Smoothed_variables_declaration_order_deviation_form=atT(oo_.dr.inv_order_var(smoother_info.var_list),:);
end

if ~options_.occbin.smoother.status || (options_.occbin.smoother.status && oo_.occbin.smoother.error_flag==0)
    plot_classical_smoother_results(M_,oo_,options_,dataset_info,dataset_,estim_params_,yf)
end

%reset qz_criterium
options_.qz_criterium=qz_criterium_old;
oo_.gui.ran_calib_smoother = true;
