function [fval,info,exit_flag,DLIK,Hess,model_solution] = non_linear_dsge_likelihood(xparam1,dataset_,dataset_info,options_,M_,estim_params_,Prior,BoundsInfo,dr, endo_steady_state, exo_steady_state, exo_det_steady_state, ~, measurement_info)
% Evaluates the posterior kernel of a dsge model using a non linear filter.
%
% INPUTS
% - xparam1                 [double]              n×1 vector, estimated parameters.
% - dataset_                [struct]              MATLAB's structure containing the dataset
% - dataset_info            [struct]              MATLAB's structure describing the dataset
% - options_                [struct]              MATLAB's structure describing the options
% - M_                      [struct]              MATLAB's structure describing the M_
% - estim_params_           [struct]              MATLAB's structure describing the estimated_parameters
% - Prior                   [@dprior]             prior information object
% - BoundsInfo              [struct]              MATLAB's structure specifying the bounds on the parameter values
% - dr                      [structure]           Reduced form model.
% - endo_steady_state       [vector]              steady state value for endogenous variables
% - exo_steady_state        [vector]              steady state value for exogenous variables
% - exo_det_steady_state    [vector]              steady state value for exogenous deterministic variables
%
% OUTPUTS
% - fval                    [double]              scalar, value of the likelihood or posterior kernel.
% - info                    [integer]             4×1 vector, information on whether solution and likelihood could be computed
% - exit_flag               [integer]             scalar, equal to 1 (no issues when evaluating the likelihood) or 0 (not able to evaluate the likelihood).
% - DLIK                    [double]              Empty array.
% - Hess                    [double]              Empty array.
% - model_solution          [struct]              Structure containing the model solution at the current parameter
%                                                 values with fields: dr, ys, trend_coeff, mf, and the M_ fields
%                                                 set by set_all_parameters (params, Sigma_e, Correlation_matrix,
%                                                 H, Correlation_matrix_ME, Skew_e, endo_initial_state_values, csn).

% 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/>.

% Initialization of the returned arguments.
fval            = [];
model_solution  = [];
exit_flag       = 1;
DLIK            = [];
Hess            = [];

% Ensure that xparam1 is a column vector.
% (Don't do the transformation if xparam1 is empty, otherwise it would become a
%  0×1 matrix, which create issues with older MATLABs when comparing with [] in
%  check_bounds_and_definiteness_estimation)
if ~isempty(xparam1)
    xparam1 = xparam1(:);
end

% Issue an error if loglinear option is used.
if options_.loglinear
    error('non_linear_dsge_likelihood: It is not possible to use a non linear filter with the option loglinear!')
end

%------------------------------------------------------------------------------
% 1. Get the structural parameters & define penalties
%------------------------------------------------------------------------------

M_ = set_all_parameters(xparam1,estim_params_,M_);

[fval,info,exit_flag,Q,H]=check_bounds_and_definiteness_estimation(xparam1, M_, estim_params_, BoundsInfo);
if info(1)
    return
end

%------------------------------------------------------------------------------
% 2. call model setup & reduction program
%------------------------------------------------------------------------------

% Linearize the model around the deterministic steadystate and extract the matrices of the state equation (T and R).
[dr, info, M_.params] = resol(0, M_, options_, dr , endo_steady_state, exo_steady_state, exo_det_steady_state);

if info(1)
    if info(1) == 3 || info(1) == 4 || info(1) == 5 || info(1)==6 ||info(1) == 19 || ...
                info(1) == 20 || info(1) == 21 || info(1) == 23 || info(1) == 26 || ...
                info(1) == 81 || info(1) == 84 ||  info(1) == 85
        %meaningful second entry of output that can be used
        fval = Inf;
        if ~isfinite(info(2))
            info(4) = 0.1;
        else
            info(4) = info(2);
        end
        exit_flag = 0;
        return
    else
        fval = Inf;
        info(4) = 0.1;
        exit_flag = 0;
        return
    end
end

if nargin < 15 || isempty(measurement_info)
    measurement_info = set_measurement_info(M_, dr, options_);
end

% Define a vector of indices for the observed variables. Is this really usefull?...
mf = measurement_info.mf1;

% Get needed information for Kalman filter routines.
start = options_.presample+1;
Y = transpose(dataset_.data);

%------------------------------------------------------------------------------
% 3. Initial condition of the Kalman filter
%------------------------------------------------------------------------------

mf0 = measurement_info.mf0;
restrict_variables_idx = dr.restrict_var_list;
state_variables_idx = restrict_variables_idx(measurement_info.mf0);
number_of_state_variables = length(mf0);

ReducedForm.steadystate = dr.ys(dr.order_var(restrict_variables_idx));
ReducedForm.constant = ReducedForm.steadystate + .5*dr.ghs2(restrict_variables_idx);
ReducedForm.state_variables_steady_state = dr.ys(dr.order_var(state_variables_idx));
ReducedForm.Q = Q;
ReducedForm.H = H;
ReducedForm.mf0 = mf0;
ReducedForm.mf1 = measurement_info.mf1;

if options_.order>3
    ReducedForm.use_k_order_solver = true;
    ReducedForm.dr = dr;
    ReducedForm.udr = folded_to_unfolded_dr(dr, M_, options_);
    if pruning
        error('Pruning is not available for orders > 3');
    end
else
    ReducedForm.use_k_order_solver = false;
    ReducedForm.ghx  = dr.ghx(restrict_variables_idx,:);
    ReducedForm.ghu  = dr.ghu(restrict_variables_idx,:);
    ReducedForm.ghxx = dr.ghxx(restrict_variables_idx,:);
    ReducedForm.ghuu = dr.ghuu(restrict_variables_idx,:);
    ReducedForm.ghxu = dr.ghxu(restrict_variables_idx,:);
    ReducedForm.ghs2 = dr.ghs2(restrict_variables_idx,:);
    if options_.order==3
        ReducedForm.ghxxx = dr.ghxxx(restrict_variables_idx,:);
        ReducedForm.ghuuu = dr.ghuuu(restrict_variables_idx,:);
        ReducedForm.ghxxu = dr.ghxxu(restrict_variables_idx,:);
        ReducedForm.ghxuu = dr.ghxuu(restrict_variables_idx,:);
        ReducedForm.ghxss = dr.ghxss(restrict_variables_idx,:);
        ReducedForm.ghuss = dr.ghuss(restrict_variables_idx,:);
    end
end

% Set initial condition.
switch options_.particle.initialization
  case 1% Initial state vector covariance is the ergodic variance associated to the first order Taylor-approximation of the model.
    StateVectorMean = ReducedForm.constant(mf0);
    [A,B] = kalman_transition_matrix(dr,dr.restrict_var_list,dr.restrict_columns);
    StateVectorVariance = lyapunov_symm(A, B*Q*B', options_.lyapunov_fixed_point_tol, ...
                                        options_.qz_criterium, options_.lyapunov_complex_threshold, [], options_.debug);
    StateVectorVariance = StateVectorVariance(mf0,mf0);
  case 2% Initial state vector covariance is a monte-carlo based estimate of the ergodic variance (consistent with a k-order Taylor-approximation of the model).
    StateVectorMean = ReducedForm.constant(mf0);
    old_DynareOptionsperiods = options_.periods;
    options_.periods = 5000;
    old_DynareOptionspruning =  options_.pruning;
    options_.pruning = options_.particle.pruning;
    y_ = simult(endo_steady_state, dr,M_,options_);
    y_ = y_(dr.order_var(state_variables_idx),2001:5000); %state_variables_idx is in dr-order while simult_ is in declaration order
    if any(any(isnan(y_))) ||  any(any(isinf(y_))) && ~ options_.pruning
        fval = Inf;
        info(1) = 202;
        info(4) = 0.1;
        exit_flag = 0;
        return;
    end
    StateVectorVariance = cov(y_');
    options_.periods = old_DynareOptionsperiods;
    options_.pruning = old_DynareOptionspruning;
    clear('old_DynareOptionsperiods','y_');
  case 3% Initial state vector covariance is a diagonal matrix (to be used
        % if model has stochastic trends).
    StateVectorMean = ReducedForm.constant(mf0);
    StateVectorVariance = options_.particle.initial_state_prior_std*eye(number_of_state_variables);
  otherwise
    error('Unknown initialization option!')
end
ReducedForm.StateVectorMean = StateVectorMean;
ReducedForm.StateVectorVariance = StateVectorVariance;

[~, flag] = chol(ReducedForm.StateVectorVariance);%reduced_rank_cholesky(ReducedForm.StateVectorVariance)';
if flag
    fval = Inf;
    info(1) = 201;
    info(4) = 0.1;
    exit_flag = 0;
    return;
end
%------------------------------------------------------------------------------
% 4. Likelihood evaluation
%------------------------------------------------------------------------------
options_.warning_for_steadystate = 0;
[s1,s2,current_stream] = get_dynare_random_generator_state();

LIK = feval(options_.particle.algorithm, ReducedForm, Y, start, options_.particle, options_.threads, options_, M_);
set_dynare_random_generator_state(s1,s2,current_stream);
if imag(LIK)
    fval = Inf; info(1) = 46; info(4) = 0.1; exit_flag = 0;
    return
elseif isnan(LIK)
    fval = Inf; info(1) = 45; info(4) = 0.1; exit_flag = 0;
    return
elseif isinf(LIK)
    fval = Inf; info(1) = 50; info(4) = 0.1; exit_flag = 0;
    return
else
    likelihood = LIK;
end
options_.warning_for_steadystate = 1;
% ------------------------------------------------------------------------------
% Adds prior if necessary
% ------------------------------------------------------------------------------
lnprior = Prior.density(xparam1(:));
if isinf(lnprior)
    fval = Inf; info(1) = 40; info(4) = 0.1; exit_flag = 0;
    return
end
if isnan(lnprior)
    fval = Inf; info(1) = 47; info(4) = 0.1; exit_flag = 0;
    return
end
if imag(lnprior)~=0
    fval = Inf; info(1) = 48; info(4) = 0.1; exit_flag = 0;
    return
end
fval = (likelihood-lnprior);

if nargout >= 6
    model_solution.dr = dr;
    model_solution.ys = dr.ys;
    model_solution.trend_coeff = [];
    model_solution.params = M_.params;
    model_solution.Sigma_e = M_.Sigma_e;
    model_solution.Correlation_matrix = M_.Correlation_matrix;
    model_solution.H = M_.H;
    model_solution.Correlation_matrix_ME = M_.Correlation_matrix_ME;
    model_solution.Skew_e = M_.Skew_e;
    if isfield(M_,'endo_initial_state')
        model_solution.endo_initial_state_values = M_.endo_initial_state.values;
    end
    if isfield(M_,'csn')
        model_solution.csn = M_.csn;
    end
    model_solution.mf = mf;
end