function [initial_conditions, pfm, options_, oo_] = extended_path_initialization(initial_conditions, options_, M_, oo_)

% Initialization of the extended path routines.
%
% INPUTS
% - initial_conditions     [double]    m×1 array, where m is the number of endogenous variables in the model.
% - options_               [struct]    Dynare's options structure
% - M_                     [struct]    Dynare's model structure
% - oo_                    [struct]    Dynare's result structure
%
% OUTPUTS
% - initial_conditions     [double]    m*1 array, initial conditions (if empty on input, set to steady state or histval).
% - pfm                    [struct]    Structure for the perfect foresight
%                                      model solver, containing:
%                                        * positive_var_indx: indices of shocks with positive variance
%                                        * effective_number_of_shocks: number of shocks with positive variance
%                                        * covariance_matrix: covariance matrix of effective shocks
%                                        * covariance_matrix_upper_cholesky: upper Cholesky factor of covariance matrix
% - options_               [struct]    Modified Dynare's options structure.
% - oo_                    [struct]    Modified Dynare's result structure.
%
% ALGORITHM
% None.
%
% SPECIAL REQUIREMENTS
% None.

% Copyright © 2016-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/>.

ep  = options_.ep;

% Set verbosity levels.
options_.verbosity = ep.verbosity+ep.debug;

% Set maximum number of iterations for the deterministic solver.
options_.simul.maxit = ep.maxit;

% Prepare a structure needed by the MATLAB implementation of the perfect foresight model solver
pfm = setup_stochastic_perfect_foresight_model_solver(M_, options_, oo_);

% Check that the user did not use varexo_det
if M_.exo_det_nbr~=0
    error('Extended path does not support varexo_det.')
end

% Set default initial conditions.
if isempty(initial_conditions)
    if M_.maximum_lag==0
        error('extended_path_initialization: you cannot use an initial condition in a model without lags.')
    end
    if isempty(M_.endo_histval)
        initial_conditions = oo_.steady_state;
    else
        initial_conditions = M_.endo_histval;
    end
end

% Set the number of periods for the (stochastic) perfect foresight model
pfm.periods = ep.periods;

pfm.i_upd = pfm.ny+(1:pfm.periods*pfm.ny);

pfm.block = options_.block;

% Set the algorithm for the perfect foresight solver
options_.stack_solve_algo = ep.stack_solve_algo;

% Compute the first order reduced form if needed.
dr = struct();
if ep.use_first_order_solution_as_initial_guess
    options_.order = 1;
    oo_.dr=set_state_space(dr,M_);
    [oo_.dr,info,M_.params] = resol(0,M_,options_,oo_.dr,oo_.steady_state, oo_.exo_steady_state, oo_.exo_det_steady_state);
    if info(1)
        print_info(info,options_.noprint,options_);
    end
end

% Do not use a minimal number of periods for the perfect foresight solver (with bytecode and blocks)
options_.minimal_solving_period = options_.ep.periods;

% Set seed.
if ep.set_dynare_seed_to_default
    set_dynare_seed_local_options([],false,'default');
end

% hybrid correction
pfm.hybrid_order = ep.stochastic.hybrid_order;
if pfm.hybrid_order==1
    warning('extended_path:: hybrid=1 is equivalent to hybrid=0 (option value must be an integer greater than 1 to be effective).')
end

if pfm.hybrid_order>1
    oo_.dr = set_state_space(oo_.dr, M_);
    options = options_;
    options.order = pfm.hybrid_order;
    [pfm.dr, M_.params] = resol(0, M_, options, oo_.dr, oo_.steady_state, oo_.exo_steady_state, oo_.exo_det_steady_state);
else
    pfm.dr = [];
end

% Deactivate homotopy with SEP
if ep.stochastic.order>0
    options_.no_homotopy = true;
end

% setting up integration nodes if order > 0
if ep.stochastic.order>0
    [nodes,weights,nnodes] = setup_integration_nodes(options_.ep,pfm);
    pfm.nodes = nodes;
    pfm.weights = weights;
    pfm.nnodes = nnodes;
    % compute number of blocks
    [pfm.block_nbr, pfm.world_nbr] = get_block_world_nbr(ep.stochastic.algo,nnodes,ep.stochastic.order,ep.periods);
end

% set boundaries if mcp
if ~ep.stochastic.order
    [lb, ub] = feval(sprintf('%s.dynamic_complementarity_conditions', M_.fname), M_.params);
    pfm.eq_index = M_.dynamic_mcp_equations_reordering;
    if options_.ep.solve_algo == 10
        options_.lmmcp.lb = repmat(lb, ep.periods, 1);
        options_.lmmcp.ub = repmat(ub, ep.periods, 1);
    elseif options_.ep.solve_algo == 11
        options_.mcppath.lb = repmat(lb, ep.periods, 1);
        options_.mcppath.ub = repmat(ub, ep.periods, 1);
    end
else
    % For SEP, boundaries are set in solve_stochastic_perfect_foresight_model_{0,1}.m
end
