function smoother_results = DsgeSmoother(xparam1,dataset_,dataset_info,M_,dr, endo_steady_state, exo_steady_state, exo_det_steady_state,options_,estim_params_,smoother_info,occbin_smoother_opts,measurement_info)
% smoother_results = DsgeSmoother(xparam1,dataset_,dataset_info,M_,dr, endo_steady_state, exo_steady_state, exo_det_steady_state,options_,estim_params_,smoother_info,occbin_smoother_opts)
% Estimation of the smoothed variables and innovations.
%
% INPUTS
%   o xparam1       [double]   (p*1) vector of (estimated) parameters.
%   o dataset_      [structure] dataset after transformations
%   o dataset_info  [structure] storing information about the sample
%   o M_            [structure] describing the model
%   o dr            [structure] model information structure
%   o endo_steady_state       [vector]     steady state value for endogenous variables
%   o exo_steady_state        [vector]     steady state value for exogenous variables
%   o exo_det_steady_state    [vector]     steady state value for exogenous deterministic variables
%   o options_      [structure] describing the options
%   o estim_params_ [structure] characterizing parameters to be estimated
%   o occbin_smoother_opts [structure] optional structure with fields:
%       .precomputed        [struct] precomputed decision rules to skip dynare_resolve:
%           .T              [double] (m*m) transition matrix
%           .R              [double] (m*r) shock-impact matrix
%           .SteadyState    [double] (m*1) steady state
%           .dr             [struct] decision rules
%           .params         [double] model parameters
%           .T0             [double] (optional) baseline reduced T from occbin.dynare_resolve
%           .R0             [double] (optional) baseline reduced R from occbin.dynare_resolve
%       .occbin_options     [struct] OccBin regime/simulation options
%       .TT                 [double] (m*m*T) time-varying transition matrices
%       .RR                 [double] (m*r*T) time-varying shock-impact matrices
%       .CC                 [double] (m*T) time-varying constant vectors
%
% OUTPUTS
%   o results       [struct]   Smoother output organized into sub-structures:
%
%     results.smoothed   Quantities conditioned on the full sample Y_{1:T}:
%       .alphahat      [double]  (m*T) smoothed states alpha_{t|T} (decision-rule order)
%       .etahat        [double]  (r*T) smoothed structural shocks eta_{t|T}
%       .epsilonhat    [double]  (n*T) smoothed measurement errors epsilon_{t|T}
%       .alphahat0     [double]  (m*1) smoothed initial state alpha_{0|T}
%       .V_tgT         [double]  (m*m*T) Var(alpha_t|Y_{1:T}) (state_uncertainty)
%       .V_0gT         [double]  (m*m) Var(alpha_0|Y_{1:T}) (state_uncertainty0)
%       .V_eta         [double]  (r*r*T) Var(eta_t|Y_{1:T}), DK (2012) eq. 4.69
%
%     results.filtered   Forward Kalman pass objects:
%       .ahat          [double]  (m*T) updated states a_{t|t} (decision-rule order)
%       .P             [double]  (m*m*(T+1)) predicted covariance P_{t|t-1}
%       .Ptt           [double]  (m*m*T) updated covariance P_{t|t}
%       .aK            [double]  (K*m*(T+K)) k-step ahead forecast a_{t+k|t}
%       .PK            [double]  (K*m*m*(T+K)) k-step ahead forecast covariance P_{t+k|t}
%       .decomp        [double]  (K*m*r*(T+K)) shock decomposition of k-step ahead forecasts
%
%     results.model   Structural model objects:
%       .T             [double]  (m*m) baseline transition matrix
%       .R             [double]  (m*r) baseline shock-impact matrix
%       .SteadyState   [double]  (m*1) steady state (declaration order)
%       .trend_coeff   [double]  (n*1) trend slope for each observed variable
%       .trend_addition [double] (n*T) deterministic trend component
%       .dr            [struct]  decision rules
%       .mf            [integer] (n*1) indices of observed variables in decision-rule order
%
%     smoother_results.d          [integer]   number of diffuse filtering periods
%     smoother_results.info       [double]    error code and penalty (1*4 or scalar 0)
%     smoother_results.regimes    [struct]    OccBin regime history (empty if not OccBin)

% Notes:
%   m:  number of endogenous variables (M_.endo_nbr)
%   T:  number of Time periods (options_.nobs)
%   r:  number of structural shocks (M_.exo_nbr)
%   n:  number of observables (size(dataset_.data,2))
%   K:  maximum forecast horizon (max(options_.nk))
%
%   To get variables that are stored in decision rule order in order of declaration
%   as in M_.endo_names, ones needs code along the lines of:
%   variables_declaration_order(dr.order_var,:) = alphahat
%
%   Defines mf = smoother_info.var_list(smoother_info.mf) (positions of observed variables
%   and requested smoothed variables in decision rules (decision rule order)) and
%   passes it back
%
% ALGORITHM
%   Diffuse Kalman filter (Durbin and Koopman)
%
% SPECIAL REQUIREMENTS
%   None

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

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

alphahat      = [];
etahat        = [];
epsilonhat    = [];
ahat          = [];
SteadyState   = [];
trend_coeff   = [];
aK            = [];
T             = [];
R             = [];
P             = [];
PK            = [];
decomp        = [];
trend_addition= [];
state_uncertainty = [];
alphahat0     = [];
state_uncertainty0 =[];
d             = 0;
info          = 0;
mf            = [];
regimes_      = [];
Ptt           = [];
V_eta         = [];
Pinf_out      = [];
Finf_singular_out = [];

% get dataset information
gend=dataset_.nobs;
Y=transpose(dataset_.data);
[vobs,smpl] = size(Y);
data_index=dataset_info.missing.aindex;

%------------------------------------------------------------------------------
% 2. call model setup & reduction program
%------------------------------------------------------------------------------
has_precomputed = nargin>=12 && isfield(occbin_smoother_opts,'precomputed');
has_occbin_options = nargin>=12 && isfield(occbin_smoother_opts,'occbin_options');
has_tv_matrices = nargin>=12 && isfield(occbin_smoother_opts,'TT');

if has_precomputed
    T = occbin_smoother_opts.precomputed.T;
    R = occbin_smoother_opts.precomputed.R;
    SteadyState = occbin_smoother_opts.precomputed.SteadyState;
    dr = occbin_smoother_opts.precomputed.dr;
    M_.params   = occbin_smoother_opts.precomputed.params;
    M_.Sigma_e  = occbin_smoother_opts.precomputed.Sigma_e;
    M_.H        = occbin_smoother_opts.precomputed.H;
    info = 0;
    if ~options_.smoother_redux
        mf = smoother_info.var_list(smoother_info.mf);
    else
        mf = measurement_info.mf1;
    end
    if options_.occbin.smoother.status
        occbin_info.status = true;
        occbin_info_cell = {options_,dr,endo_steady_state,exo_steady_state,exo_det_steady_state,M_};
        if has_occbin_options
            occbin_info_cell = [occbin_info_cell {occbin_smoother_opts.occbin_options}];
        end
        if has_tv_matrices
            occbin_info_cell = [occbin_info_cell {occbin_smoother_opts.TT, occbin_smoother_opts.RR, occbin_smoother_opts.CC}];
        end
        if options_.smoother_redux && isfield(occbin_smoother_opts.precomputed,'T0')
            occbin_info_cell = [occbin_info_cell {occbin_smoother_opts.precomputed.T0, occbin_smoother_opts.precomputed.R0}];
        end
        occbin_info.info = occbin_info_cell;
    else
        occbin_info.status = false;
    end
else
    if ~isempty(xparam1) %not calibrated model
        M_ = set_all_parameters(xparam1,estim_params_,M_);
    end
    if ~options_.smoother_redux

        [T,R,SteadyState,info,dr,M_.params] = dynare_resolve(M_,options_,dr,endo_steady_state,exo_steady_state,exo_det_steady_state);

        %get location of observed variables and requested smoothed variables in
        %decision rules
        mf = smoother_info.var_list(smoother_info.mf);

    else
        if ~options_.occbin.smoother.status
            [T,R,SteadyState,info,dr,M_.params] = dynare_resolve(M_,options_,dr,endo_steady_state,exo_steady_state,exo_det_steady_state,'restrict');
        else
            [T,R,SteadyState,info,dr, M_.params,~,~,~, T0, R0] = ...
                occbin.dynare_resolve(M_,options_,dr, endo_steady_state, exo_steady_state, exo_det_steady_state,[],'restrict');
        end
        mf = measurement_info.mf1;
    end
    if options_.occbin.smoother.status
        occbin_info.status = true;
        occbin_info_cell = {options_,dr,endo_steady_state,exo_steady_state,exo_det_steady_state,M_};
        if has_occbin_options
            occbin_info_cell = [occbin_info_cell {occbin_smoother_opts.occbin_options}];
        end
        if has_tv_matrices
            occbin_info_cell = [occbin_info_cell {occbin_smoother_opts.TT, occbin_smoother_opts.RR, occbin_smoother_opts.CC}];
        end
        if options_.smoother_redux && exist('T0','var')
            occbin_info_cell = [occbin_info_cell {T0, R0}];
        end
        occbin_info.info = occbin_info_cell;
    else
        occbin_info.status = false;
    end
end
if info~=0
    smoother_results = pack_smoother_results(alphahat, etahat, epsilonhat, ahat, ...
        SteadyState, trend_coeff, aK, T, R, P, PK, decomp, trend_addition, ...
        state_uncertainty, dr, mf, alphahat0, state_uncertainty0, d, info, ...
        regimes_, Ptt, V_eta);
    print_info(info,options_.noprint, options_);
    return
end

if options_.noconstant
    constant = zeros(vobs,1);
else
    if options_.loglinear
        constant = log(SteadyState(measurement_info.mfys));
    else
        constant = SteadyState(measurement_info.mfys);
    end
end
trend_coeff = zeros(vobs,1);
if M_.with_trend == 1
    [trend_addition, trend_coeff] =compute_trend_coefficients(M_,options_,vobs,gend);
    trend = constant*ones(1,gend)+trend_addition;
else
    trend_addition=zeros(size(constant,1),gend);
    trend = constant*ones(1,gend);
end

np    = size(T,1);
% ------------------------------------------------------------------------------
%  3. Initial condition of the Kalman filter
% ------------------------------------------------------------------------------
%
%  Here, Pinf and Pstar are determined. If the model is stationary, determine
%  Pstar as the solution of the Lyapunov equation and set Pinf=[] (Notation follows
%  Koopman/Durbin (2003), Journal of Time Series Analysis 24(1))
%  Note: Pinf and Pstar are in decision-rule order, like P, T, and R.
%
Q = M_.Sigma_e;
if options_.heteroskedastic_filter
    [~, Q]=get_Qvec_heteroskedastic_filter(Q,smpl,M_);
end
H = M_.H;

if isequal(H,0)
    H = zeros(vobs,vobs);
end

% Construct observation matrix Z: maps state vector (decision-rule order) to observed variables
% Z(i,mf(i)) = 1 means the i-th observed variable equals the mf(i)-th state variable
Z = zeros(vobs,size(T,2));
for i=1:vobs
    Z(i,mf(i)) = 1;
end

expanded_state_vector_for_univariate_filter=0;
kalman_algo = options_.kalman_algo;
if options_.lik_init == 1               % Kalman filter
    if (kalman_algo~=2) && (kalman_algo~=5) % Use standard Kalman filter except if the univariate filter or pruned skewed filter is explicitly chosen.
        kalman_algo = 1;
    end
    Pstar=lyapunov_solver(T,R,Q,options_);
    Pinf        = [];
elseif options_.lik_init == 2           % Old Diffuse Kalman filter
    if (kalman_algo~=2) && (kalman_algo~=5) % Use standard Kalman filter except if the univariate filter or pruned skewed filter is explicitly chosen.
        kalman_algo = 1;
    end
    Pstar = options_.Harvey_scale_factor*eye(np);
    Pinf        = [];
elseif options_.lik_init == 3           % Diffuse Kalman filter
    my_mf = mf;
    if kalman_algo ~= 4
        kalman_algo = 3;
    else
        if ~all(all(abs(H-diag(diag(H)))<1e-14))% ie, the covariance matrix is not diagonal...
            %Augment state vector (follows Section 6.4.3 of DK (2012))
            expanded_state_vector_for_univariate_filter=1;
            T  = blkdiag(T,zeros(vobs));
            np    = size(T,1);
            Q   = blkdiag(Q,H);
            R  = blkdiag(R,eye(vobs));
            H   = zeros(vobs,vobs);
            Z   = [Z, eye(vobs)];
            my_mf = find(any(Z))';
        end
    end
    if options_.smoother_redux
        % dr.restrict_columns identifies the structurally non-zero columns of T.
        % After state-vector expansion via blkdiag(T,zeros(vobs)), the appended
        % columns are structurally zero and correctly land in the complement set.
        [Pstar,Pinf] = compute_Pinf_Pstar(my_mf,T,R,Q,options_.qz_criterium, dr.restrict_columns);
    else
        [Pstar,Pinf] = compute_Pinf_Pstar(my_mf,T,R,Q,options_.qz_criterium);
    end
elseif options_.lik_init == 4           % Start from the solution of the Riccati equation.
    Pstar = kalman_steady_state(transpose(T),R*Q*transpose(R),transpose(build_selection_matrix(mf,np,vobs)),H);
    Pinf  = [];
    if (kalman_algo~=2) && (kalman_algo~=5) % Use standard Kalman filter except if the univariate filter or pruned skewed filter is explicitly chosen.
        kalman_algo = 1;
    end
elseif options_.lik_init == 5            % Old diffuse Kalman filter only for the non stationary variables
    [eigenvect, eigenv] = eig(T);
    eigenv = diag(eigenv);
    V = eigenvect(:,abs(abs(eigenv)-1) < 1e-7);
    stable = find(sum(abs(V),2)<1e-5);
    Pstar = options_.Harvey_scale_factor*eye(np);
    if (kalman_algo~=2) && (kalman_algo~=5) % Use standard Kalman filter except if the univariate filter or pruned skewed filter is explicitly chosen.
        kalman_algo = 1;
    end
    R_tmp = R(stable, :);
    T_tmp = T(stable,stable);
    Pstar_tmp=lyapunov_solver(T_tmp,R_tmp,Q,options_);
    Pstar(stable, stable) = Pstar_tmp;
    Pinf  = [];
end
kalman_tol = options_.kalman_tol;
diffuse_kalman_tol = options_.diffuse_kalman_tol;
data1 = Y-trend;
% -----------------------------------------------------------------------------
%  4. Kalman smoother
% -----------------------------------------------------------------------------

if ~dataset_info.missing.state
    for i=1:smpl
        data_index{i}=(1:vobs)';
    end
end

ST = T;
R1 = R;

if options_.heteroskedastic_filter
    Q=get_Qvec_heteroskedastic_filter(Q,smpl,M_);
end

if options_.occbin.smoother.status
    if kalman_algo == 1
        kalman_algo = 2;
    end
    if kalman_algo == 3
        kalman_algo = 4;
    end
end

if kalman_algo == 1 || kalman_algo == 3 || kalman_algo == 5
    a_initial     = zeros(np,1);
    if options_.smoother_redux
        a_initial=set_Kalman_starting_values(a_initial,M_,dr,options_);
    else
        a_initial=set_Kalman_smoother_starting_values(a_initial,M_,dr,options_,smoother_info.restrict_columns);
    end
    if kalman_algo == 5
        Gamma_0 = zeros(size(Pstar)); nu_0 = zeros(size(a_initial)); Delta_0 = eye(size(a_initial,1)); % initialize at Gaussian distribution
        [alphahat, epsilonhat, etahat, ahat, ~, P, aK, PK, decomp, state_uncertainty, alphahat_tm1gt, etahat_tgt, alphahat0, state_uncertainty0] = ...
            kalman_smoother_pruned_skewed(data1, ... % data
                                          a_initial, Pstar, Gamma_0, nu_0, Delta_0, ... % initialize CSN at Gaussian distribution
                                          ST, R1, Z, ... % state space matrices
                                          M_.csn.mu_e, M_.csn.Sigma_e, M_.csn.Gamma_e, M_.csn.nu_e, M_.csn.Delta_e, H, ... % shock CSN parameters, measurement error covariance
                                          kalman_tol, options_.skewed_kalman.prune_tol, options_.skewed_kalman.mvnlogcdf, ... % skewed Kalman filter and smoother options
                                          options_.console_mode); % console mode for waitbar
    else
        if options_.lik_init==2 && options_.Harvey_scale_factor==0
            a_initial=[a_initial T*a_initial]; %set state prediction for first Kalman step;
            Pstar = cat(3,Pstar*0, R1*Q(:,:,1)*R1');
        else
            a_initial=T*a_initial; %set state prediction for first Kalman step;
        end
        [alphahat,epsilonhat,etahat,ahat,P,aK,PK,decomp,state_uncertainty, alphahat_tm1gt, etahat_tgt, d, alphahat0, alphahat_0g1, state_uncertainty0, filter_error_flag, Ptt, V_eta, Pinf_out, Finf_singular_out] = missing_DiffuseKalmanSmootherH1_Z(a_initial,ST, ...
            Z,R1,Q,H,Pinf,Pstar, ...
            data1,vobs,np,smpl,data_index, ...
            options_.nk,kalman_tol,diffuse_kalman_tol,options_.filter_decomposition,options_.smoothed_state_uncertainty,options_.filter_covariance,options_.smoother_redux,dataset_.name,options_.debug,options_.updated_covariance,options_.smoothed_shock_variance);
    end
    if isinf(alphahat)
        % Store Kalman filter error code before switching algorithms
        % error_flag from filter: 0=success, 420=Finf rank-deficient, 421=Fstar rank-deficient, 422=F singular
        if filter_error_flag > 0
            info = [filter_error_flag, 0, 0, 0];
            if filter_error_flag==422 && ~options_.use_univariate_smoother_if_singularity_is_detected %stochastic singularity in F
                disp_verbose('DsgeSmoother: Stochastic singularity in stationary period detected. If you are sure the model is correctly specified,',options_.verbosity)
                disp_verbose('DsgeSmoother: you may want to enable use_univariate_smoother_if_singularity_is_detected to deal with pathological draws.',options_.verbosity)
                print_info(info,options_.noprint, options_);                
            end
        end
        if kalman_algo == 1
            disp_verbose('DsgeSmoother: Switching to univariate filter. This may be a sign of stochastic singularity.',options_.verbosity)
            kalman_algo = 2;
        elseif kalman_algo == 3
            disp_verbose('DsgeSmoother: Switching to univariate filter. This is usually due to co-integration and/or having',options_.verbosity)
            disp_verbose('DsgeSmoother: fewer stochastic trends than observables. This is unproblematic.',options_.verbosity)
            disp_verbose('DsgeSmoother: However, occasionally, there is a problem of stochastic singularity.',options_.verbosity)
            kalman_algo = 4;
        elseif kalman_algo == 5
            fprintf('\nDsgeSmoother: Switching to univariate filter. This may be a sign of stochastic singularity.\n');
            error('DsgeSmoother: Univariate pruned skewed Kalman filter is not yet implemented.')
        else
            error('This case shouldn''t happen')
        end
    end
end

if kalman_algo == 2 || kalman_algo == 4
    if ~all(all(abs(H-diag(diag(H)))<1e-14))% ie, the covariance matrix is diagonal...
        if ~expanded_state_vector_for_univariate_filter
            %Augment state vector (follows Section 6.4.3 of DK (2012))
            expanded_state_vector_for_univariate_filter=1;
            Z   = [Z, eye(vobs)];
            ST  = blkdiag(ST,zeros(vobs));
            np  = size(ST,1);
            if options_.heteroskedastic_filter
                Qvec=Q;
                Q=NaN(size(Qvec,1)+size(H,1),size(Qvec,1)+size(H,1),smpl+1);
                for kv=1:size(Qvec,3)
                    Q(:,:,kv) = blkdiag(Qvec(:,:,kv),H);
                end
            else
                Q   = blkdiag(Q,H);
            end
            R1  = blkdiag(R,eye(vobs));
            if kalman_algo == 4
                %recompute Schur state space transformation with
                %expanded state space
                if options_.smoother_redux
                    % dr.restrict_columns remains valid after blkdiag expansion:
                    % appended zero columns are automatically treated as static.
                    [Pstar,Pinf] = compute_Pinf_Pstar(mf,ST,R1,Q,options_.qz_criterium, dr.restrict_columns);
                else
                    [Pstar,Pinf] = compute_Pinf_Pstar(mf,ST,R1,Q,options_.qz_criterium);
                end
            else
                Pstar = blkdiag(Pstar,H);
                if ~isempty(Pinf)
                    Pinf  = blkdiag(Pinf,zeros(vobs));
                end
            end
            %now reset H to 0
            H   = zeros(vobs,vobs);
        else
            %do nothing, state vector was already expanded
        end
    end

    a_initial     = zeros(np,1);
    if options_.smoother_redux
        a_initial=set_Kalman_starting_values(a_initial,M_,dr,options_);
    else
        a_initial=set_Kalman_smoother_starting_values(a_initial,M_,dr,options_,smoother_info.restrict_columns);
    end
    if not(options_.occbin.smoother.status) % && options_.occbin.smoother.first_period_occbin_update==1)
        if options_.lik_init==2 && options_.Harvey_scale_factor==0
            a_initial=[a_initial ST*a_initial]; %set state prediction for first Kalman step;
            Pstar = cat(3,Pstar, R1*Q(:,:,1)*R1');
        else
    a_initial=ST*a_initial; %set state prediction for first Kalman step;
        end
    end

    [alphahat,epsilonhat,etahat,ahat,P,aK,PK,decomp,state_uncertainty, alphahat_tm1gt, etahat_tgt, d, alphahat0, alphahat_0g1, state_uncertainty0, Ptt, V_eta, Pinf_out, Finf_singular_out, regimes_,TT,RR,CC,TTx,RRx,CCx] = missing_DiffuseKalmanSmootherH3_Z(a_initial,ST, ...
        Z,R1,Q,diag(H), ...
        Pinf,Pstar,data1,vobs,np,smpl,data_index, ...
        options_.nk,kalman_tol,diffuse_kalman_tol, ...
        options_.filter_decomposition,options_.smoothed_state_uncertainty,options_.filter_covariance,options_.smoother_redux,occbin_info,options_.smoothed_shock_variance);
end

if expanded_state_vector_for_univariate_filter && (kalman_algo == 2 || kalman_algo == 4)
    % extracting measurement errors
    % removing observed variables from the state vector
    k = (1:np-vobs);
    alphahat = alphahat(k,:);
    ahat = ahat(k,:);
    aK = aK(:,k,:,:);
    epsilonhat=etahat(end-vobs+1:end,:);
    etahat=etahat(1:end-vobs,:);
    if ~isempty(PK)
        PK = PK(:,k,k,:);
    end
    if ~isempty(decomp)
        decomp = decomp(:,k,:,:);
    end
    if ~isempty(P)
        P = P(k,k,:);
    end
    if ~isempty(state_uncertainty)
        state_uncertainty = state_uncertainty(k,k,:);
    end
    if ~isempty(Ptt)
        Ptt = Ptt(k,k,:);
    end
    if ~isempty(V_eta)
        rr_orig = size(V_eta,1) - vobs;  % number of structural shocks (before augmentation)
        V_eta = V_eta(1:rr_orig,1:rr_orig,:);
    end
end

if options_.smoother_redux
    ic = [ M_.nstatic+(1:M_.nspred) M_.endo_nbr+(1:size(dr.ghx,2)-M_.nspred) ]';

    if isempty(options_.nk)
        nk=1;
    else
        nk=options_.nk;
    end

    if options_.occbin.smoother.status
        if isempty(alphahat0)
            % something went wrong
            smoother_results = pack_smoother_results(alphahat, etahat, epsilonhat, ahat, ...
                SteadyState, trend_coeff, aK, T, R, P, PK, decomp, trend_addition, ...
                state_uncertainty, dr, mf, alphahat0, state_uncertainty0, d, info, ...
                regimes_, Ptt, V_eta);
            return
        end
        % reconstruct occbin smoother
        if has_occbin_options
            % sequence of regimes is provided in input
            isoccbin=1;
        else
            isoccbin=0;
        end
        if has_tv_matrices
            TT=occbin_smoother_opts.TT;
            RR=occbin_smoother_opts.RR;
            CC=occbin_smoother_opts.CC;
            if size(TT,3)<(smpl+1)
                TT=repmat(T,1,1,smpl+1);
                RR=repmat(R,1,1,smpl+1);
                CC=repmat(zeros(size(T,1),1),1,smpl+1);
            end
        end
        if isoccbin==0
            [A,B] = kalman_transition_matrix(dr,(1:M_.endo_nbr)',ic);
        else
            opts_simul = options_.occbin.simul;
        end
        % reconstruct smoothed variables
        aaa=zeros(M_.endo_nbr,gend+1);
        aaa(dr.restrict_var_list,1)=alphahat0;
        aaa(dr.restrict_var_list,2:end)=alphahat;
        iTx = zeros(size(TTx));
        for k=1:gend
            if isoccbin
                A = TT(:,:,k);
                B = RR(:,:,k);
                C = CC(:,k);
            else
                C=0;
            end
            iT = pinv(TTx(:,:,k));
            % store pinv
            iTx(:,:,k) = iT;
            Tstar = A(~ismember(1:M_.endo_nbr,dr.restrict_var_list),dr.restrict_var_list);
            Rstar = B(~ismember(1:M_.endo_nbr,dr.restrict_var_list),:);
            Cstar = C(~ismember(1:M_.endo_nbr,dr.restrict_var_list));
            AS = Tstar*iT;
            BS = Rstar-AS*RRx(:,:,k);
            CS = Cstar-AS*CCx(:,k);
            static_var_list = ~ismember(1:M_.endo_nbr,dr.restrict_var_list);
            ilagged = any(abs(AS*TTx(:,:,k)-Tstar)'>1.e-12);
            static_var_list0 = static_var_list;
            static_var_list0(static_var_list) = ilagged;
            static_var_list(static_var_list) = ~ilagged;
            aaa(static_var_list,k+1) = AS(~ilagged,:)*alphahat(:,k)+BS(~ilagged,:)*etahat(:,k)+CS(~ilagged);
            if any(ilagged)
                if k>1
                    aaa(static_var_list0,k+1) = Tstar(ilagged,:)*alphahat(:,k-1)+Rstar(ilagged,:)*etahat(:,k)+Cstar(ilagged);
                else
                    aaa(static_var_list0,2) = Tstar(ilagged,:)*alphahat0+Rstar(ilagged,:)*etahat(:,1)+Cstar(ilagged);
                end

            end

        end
        alphahat0=aaa(:,1);
        alphahat=aaa(:,2:end);

        % reconstruct updated variables
        bbb=zeros(M_.endo_nbr,gend);
        bbb(dr.restrict_var_list,:)=ahat; % this is t|t
        for k=1:gend
            if isoccbin
                A = TT(:,:,k);
                B = RR(:,:,k);
                C = CC(:,k);
                iT = iTx(:,:,k);
                Tstar = A(~ismember(1:M_.endo_nbr,dr.restrict_var_list),dr.restrict_var_list);
                Rstar = B(~ismember(1:M_.endo_nbr,dr.restrict_var_list),:);
                Cstar = C(~ismember(1:M_.endo_nbr,dr.restrict_var_list));
                AS = Tstar*iT;
                BS = Rstar-AS*RRx(:,:,k);
                CS = Cstar-AS*CCx(:,k);
                static_var_list = ~ismember(1:M_.endo_nbr,dr.restrict_var_list);
                ilagged = any(abs(AS*TTx(:,:,k)-Tstar)'>1.e-12);
                static_var_list0 = static_var_list;
                static_var_list0(static_var_list) = ilagged;
                static_var_list(static_var_list) = ~ilagged;
                bbb(static_var_list,k) = AS(~ilagged,:)*ahat(:,k)+BS(~ilagged,:)*etahat_tgt(:,k)+CS(~ilagged);
                if any(ilagged) && k>d+1
                    bbb(static_var_list0,k) = Tstar(ilagged,:)*alphahat_tm1gt(:,k-1)+Rstar(ilagged,:)*etahat_tgt(:,k)+Cstar(ilagged);
                end
            elseif k>d+1
                opts_simul.curb_retrench = options_.occbin.smoother.curb_retrench;
                opts_simul.waitbar = options_.occbin.smoother.waitbar;
                opts_simul.maxit = options_.occbin.smoother.maxit;
                opts_simul.periods = options_.occbin.smoother.periods;
                opts_simul.check_ahead_periods = options_.occbin.smoother.check_ahead_periods;
                opts_simul.full_output = options_.occbin.smoother.full_output;
                opts_simul.piecewise_only = options_.occbin.smoother.piecewise_only;
                opts_simul.SHOCKS = zeros(nk,M_.exo_nbr);
                opts_simul.SHOCKS(1,:) =  etahat_tgt(:,k);
                tmp=zeros(M_.endo_nbr,1);
                tmp(dr.restrict_var_list,1)=alphahat_tm1gt(:,k-1);
                opts_simul.endo_init = tmp(dr.inv_order_var,1);
                opts_simul.init_regime = []; %regimes_(k);
                opts_simul.waitbar=0;
                options_.occbin.simul=opts_simul;
                [~, out] = occbin.solver(M_,options_,dr,endo_steady_state,exo_steady_state,exo_det_steady_state);
                % regime in out should be identical to regimes_(k-2) moved one
                % period ahead (so if regimestart was [1 5] it should be [1 4]
                % in out
                %         end
                bbb(dr.inv_order_var,k) = out.piecewise(1,:) - out.ys';
            end
        end
        ahat0=ahat;
        ahat=bbb;
        if ~isempty(P)
            PP=zeros(M_.endo_nbr,M_.endo_nbr,gend+1);
            PP(dr.restrict_var_list,dr.restrict_var_list,:)=P;
            P=PP;
            clear PP
        end
        if ~isempty(Ptt)
            PPtt=zeros(M_.endo_nbr,M_.endo_nbr,gend);
            PPtt(dr.restrict_var_list,dr.restrict_var_list,:)=Ptt;
            Ptt=PPtt;
            clear PPtt
        end

        if ~isempty(state_uncertainty)
            mm=size(T,1);
            sstate_uncertainty=zeros(M_.endo_nbr,M_.endo_nbr,gend);
            sstate_uncertainty(dr.restrict_var_list,dr.restrict_var_list,:)=state_uncertainty(1:mm,1:mm,:);
            state_uncertainty=sstate_uncertainty;
            clear sstate_uncertainty
        end
        if ~isempty(state_uncertainty0)
            mm=size(T,1);
            sstate_uncertainty=zeros(M_.endo_nbr,M_.endo_nbr);
            sstate_uncertainty(dr.restrict_var_list,dr.restrict_var_list)=state_uncertainty0(1:mm,1:mm);
            state_uncertainty0=sstate_uncertainty;
            clear sstate_uncertainty
        end

        aaa = zeros(nk,M_.endo_nbr,gend+nk);
        aaa(:,dr.restrict_var_list,:)=aK;

        if isoccbin
            tstart = 1;
        else
            % we enter here in the first occbin smoother iteration
            % occbin Kalman update is not yet able to accommodate diffuse steps!
            tstart=d+2;
        end
        for k=2:gend+1
            opts_simul.curb_retrench = options_.occbin.smoother.curb_retrench;
            opts_simul.waitbar = options_.occbin.smoother.waitbar;
            opts_simul.maxit = options_.occbin.smoother.maxit;
            opts_simul.periods = options_.occbin.smoother.periods;
            opts_simul.check_ahead_periods = options_.occbin.smoother.check_ahead_periods;
            opts_simul.full_output = options_.occbin.smoother.full_output;
            opts_simul.piecewise_only = options_.occbin.smoother.piecewise_only;
            opts_simul.SHOCKS = zeros(nk,M_.exo_nbr);
            tmp=zeros(M_.endo_nbr,1);
            tmp(dr.restrict_var_list,1)=ahat0(:,k-1);
            opts_simul.endo_init = tmp(dr.inv_order_var,1);
            this_regime=[];
            if not(isempty(regimes_)) && length(regimes_)>=gend
                this_regime = regimes_(k-1);
            elseif length(occbin_smoother_opts.occbin_options.opts_regime.regime_history)>=gend
                this_regime = occbin_smoother_opts.occbin_options.opts_regime.regime_history(k-1);
            end
            if not(isempty(this_regime))
                if M_.occbin.constraint_nbr ==1
                    binding_indicator = occbin.backward_map_regime(this_regime.regime, this_regime.regimestart);
                    [this_regime.regime, this_regime.regimestart]=occbin.map_regime(binding_indicator(2:end));
                else
                    binding_indicator = occbin.backward_map_regime(this_regime.regime1, this_regime.regimestart1);
                    [this_regime.regime1, this_regime.regimestart1]=occbin.map_regime(binding_indicator(2:end));
                    binding_indicator = occbin.backward_map_regime(this_regime.regime2, this_regime.regimestart2);
                    [this_regime.regime2, this_regime.regimestart2]=occbin.map_regime(binding_indicator(2:end));
                end
            end
            opts_simul.init_regime = this_regime; %regimes_(k);
            opts_simul.maxit=1;
            opts_simul.waitbar=0;
            options_.occbin.simul=opts_simul;
            [~, out] = occbin.solver(M_,options_,dr,endo_steady_state,exo_steady_state,exo_det_steady_state);
            % regime in out should be identical to regimes_(k-2) moved one
            % period ahead (so if regimestart was [1 5] it should be [1 4]
            % in out
            %         end
            if out.error_flag(1)==0
                for jnk=1:nk
                    aaa(jnk,dr.inv_order_var,k+jnk-1) = out.piecewise(jnk,:) - out.ys';
                end
            elseif k>tstart
                this_regime=[];
                if not(isempty(regimes_)) && length(regimes_)>=(k-1)
                    this_regime = regimes_(k-1);
                elseif length(occbin_smoother_opts.occbin_options.opts_regime.regime_history)>=(k-1)
                    this_regime = occbin_smoother_opts.occbin_options.opts_regime.regime_history(k-1);
                end
                if not(isempty(this_regime))
                    if M_.occbin.constraint_nbr ==1
                        binding_indicator = occbin.backward_map_regime(this_regime.regime, this_regime.regimestart);
                        [this_regime.regime, this_regime.regimestart]=occbin.map_regime(binding_indicator(2:end));
                    else
                        binding_indicator = occbin.backward_map_regime(this_regime.regime1, this_regime.regimestart1);
                        [this_regime.regime1, this_regime.regimestart1]=occbin.map_regime(binding_indicator(2:end));
                        binding_indicator = occbin.backward_map_regime(this_regime.regime2, this_regime.regimestart2);
                        [this_regime.regime2, this_regime.regimestart2]=occbin.map_regime(binding_indicator(2:end));
                    end
                    opts_simul.init_regime = this_regime;
                    options_.occbin.simul=opts_simul;
                    [~, out] = occbin.solver(M_,options_,dr,steady_state,exo_steady_state,exo_det_steady_state);
                end
                if out.error_flag(1)==0
                    for jnk=1:nk
                        aaa(jnk,dr.inv_order_var,k+jnk-1) = out.piecewise(jnk,:) - out.ys';
                    end
                else
                    % the issue only matters non-stationary models, with
                    % diffuse filter, and for the first occbin smoother iteration,
                    % where tstart>1
                    %
                    % if k>tstart, the same simulation should have been done
                    % already in occbin.kalman_update, so it should never give
                    % an error
                    %
                    % if k<=tstart, the simulation may crash, since we ignore OBC in the first (diffuse) steps
                    % and it may happen that, given the linear updated states,
                    % the occbin simulation does not converge
                    oo_.occbin.smoother.error_flag=327;
                    smoother_results = pack_smoother_results(alphahat, etahat, epsilonhat, ahat, ...
                        SteadyState, trend_coeff, aK, T, R, P, PK, decomp, trend_addition, ...
                        state_uncertainty, dr, mf, alphahat0, state_uncertainty0, d, info, ...
                        regimes_, Ptt, V_eta);
                    return

                end
            end
        end
        aK=aaa;

        if ~isempty(PK)
            PP = zeros(nk,M_.endo_nbr,M_.endo_nbr,gend+nk);
            PP(:,dr.restrict_var_list,dr.restrict_var_list,:) = PK;
            PK=PP;
            clear PP
        end
    else
        % reconstruct smoother
        [A,B] = kalman_transition_matrix(dr,(1:M_.endo_nbr)',ic);
        iT = pinv(T);
        Tstar = A(~ismember(1:M_.endo_nbr,dr.restrict_var_list),dr.restrict_var_list);
        Rstar = B(~ismember(1:M_.endo_nbr,dr.restrict_var_list),:);
        C = Tstar*iT;
        D = Rstar-C*R;
        static_var_list = ~ismember(1:M_.endo_nbr,dr.restrict_var_list);
        ilagged = any(abs(C*T-Tstar)'>1.e-12);
        static_var_list0 = static_var_list;
        static_var_list0(static_var_list) = ilagged;
        static_var_list(static_var_list) = ~ilagged;
        % reconstruct smoothed variables
        aaa=zeros(M_.endo_nbr,gend+1);
        if ~isempty(alphahat0)
            aaa(dr.restrict_var_list,1)=alphahat0;
        end
        aaa(dr.restrict_var_list,2:end)=alphahat;
        for k=1:gend
            aaa(static_var_list,k+1) = C(~ilagged,:)*alphahat(:,k)+D(~ilagged,:)*etahat(:,k);
        end
        if any(ilagged)
            for k=2:gend
                aaa(static_var_list0,k+1) = Tstar(ilagged,:)*alphahat(:,k-1)+Rstar(ilagged,:)*etahat(:,k);
            end
            aaa(static_var_list0,2) = Tstar(ilagged,:)*alphahat0+Rstar(ilagged,:)*etahat(:,1);
        end
        alphahat0=aaa(:,1);
        alphahat=aaa(:,2:end);

        % reconstruct updated variables
        aaa=zeros(M_.endo_nbr,gend);
        aaa(dr.restrict_var_list,:)=ahat;
        for k=1:gend
            aaa(static_var_list,k) = C(~ilagged,:)*ahat(:,k)+D(~ilagged,:)*etahat_tgt(:,k);
        end
        if any(ilagged)
            %         bbb=zeros(M_.endo_nbr,gend);
            %         bbb(dr.restrict_var_list,:)=alphahat_tm1gt;
            if ~isempty(alphahat_0g1)
                aaa(static_var_list0,d+1) = Tstar(ilagged,:)*alphahat_0g1+Rstar(ilagged,:)*etahat_tgt(:,d+1);
            end
            for k=d+2:gend
                aaa(static_var_list0,k) = Tstar(ilagged,:)*alphahat_tm1gt(:,k-1)+Rstar(ilagged,:)*etahat_tgt(:,k);
            end
        end
        ahat1=aaa;
        % reconstruct aK
        aaa = zeros(nk,M_.endo_nbr,gend+nk);
        aaa(:,dr.restrict_var_list,:)=aK;
        for k=1:gend
            for jnk=1:nk
                aaa(jnk,static_var_list,k+jnk) = C(~ilagged,:)*dynare_squeeze(aK(jnk,:,k+jnk));
            end
        end
        if any(ilagged)
            for k=1:gend
                aaa(1,static_var_list0,k+1) = Tstar(ilagged,:)*ahat(:,k);
                for jnk=2:nk
                    aaa(jnk,static_var_list0,k+jnk) = Tstar(ilagged,:)*dynare_squeeze(aK(jnk-1,:,k+jnk-1));
                end
            end
        end
        aK=aaa;
        ahat=ahat1;

        % reconstruct P
        if ~isempty(P)
            PP=zeros(M_.endo_nbr,M_.endo_nbr,gend+1);
            PP(dr.restrict_var_list,dr.restrict_var_list,:)=P;
            if ~options_.heteroskedastic_filter
                DQD=D(~ilagged,:)*Q*transpose(D(~ilagged,:))+C(~ilagged,:)*R*Q*transpose(D(~ilagged,:))+D(~ilagged,:)*Q*transpose(C(~ilagged,:)*R);
                DQR=D(~ilagged,:)*Q*transpose(R);
            end
            for k=1:gend+1
                if options_.heteroskedastic_filter
                    DQD=D(~ilagged,:)*Q(:,:,k)*transpose(D(~ilagged,:))+C(~ilagged,:)*R*Q(:,:,k)*transpose(D(~ilagged,:))+D(~ilagged,:)*Q(:,:,k)*transpose(C(~ilagged,:)*R);
                    DQR=D(~ilagged,:)*Q(:,:,k)*transpose(R);
                end
                PP(static_var_list,static_var_list,k)=C(~ilagged,:)*P(:,:,k)*C(~ilagged,:)'+DQD;
                PP(static_var_list,dr.restrict_var_list,k)=C(~ilagged,:)*P(:,:,k)+DQR;
                PP(dr.restrict_var_list,static_var_list,k)=transpose(PP(static_var_list,dr.restrict_var_list,k));
            end
            P=PP;
            clear PP
        end

        % reconstruct Ptt (updated covariance)
        %
        % Unlike the predicted covariance P_{t+1|t}, the updated covariance
        % P_{t|t} cannot use the same DQD/DQR cross terms, because the
        % Kalman update changes Cov(x_d,t, eta_t | Y_{1:t}) from its prior
        % value R*Q to R*Q - P_dd*Z'*F^{-1}*Z*R*Q.
        %
        % With x_s,t = C*x_d,t + D*eta_t, the full predicted covariance is:
        %   P_ss,pred = C*P_dd*C' + DQD
        %   P_sd,pred = C*P_dd + DQR
        % where DQD = D*Q*D' + C*R*Q*D' + D*Q*R'*C', DQR = D*Q*R'.
        %
        % STATIONARY PERIODS (k > d):
        % The Kalman update Ptt_full = P_full - P_full*Z'*F^{-1}*Z*P_full
        % with F = Z*P_dd*Z' + H acts on all blocks. Since Z only hits
        % the dynamic variables (Z_full = [Z_reduced, 0]), the static
        % blocks of the updated covariance are (DK 2012, eq. 4.24):
        %   Ptt_ss = P_ss,pred - P_sd,pred*Z'*F^{-1}*Z*P_sd,pred'
        %   Ptt_sd = P_sd,pred - P_sd,pred*Z'*F^{-1}*Z*P_dd
        %
        % DIFFUSE PERIODS (k <= d) when Finf is invertible:
        % The diffuse Kalman update (DK 2012, eq. 5.14) gives:
        %   Ptt_dd = Pstar - Pstar*Z'*Kinf' - Kinf*Finf*Kstar'
        % where Kinf = Pinf*Z'*inv(Finf), Kstar = (Pstar*Z'-Kinf*Fstar)*inv(Finf).
        %
        % For the full model, Pinf_full has Pinf_ss = C*Pinf*C', Pinf_sd = C*Pinf,
        % and the cross-terms between shocks and Pinf are zero. This leads to:
        %   Kinf_s = C*Kinf (static rows of full Kinf)
        %   G = DQR*Z'*Kinf' (correction from shock-state cross-covariance)
        %   Ptt_ss = C*Ptt_dd*C' + DQD - G*C' - C*G'
        %   Ptt_sd = C*Ptt_dd + DQR - G
        %
        % DIFFUSE PERIODS (k <= d) when Finf_singular (Finf = 0):
        % The update uses Fstar = Z*Pstar*Z' + H (same as stationary formula).
        %
        % When no observations are available at time t, Ptt equals P_pred.
        if ~isempty(Ptt)
            PPtt=zeros(M_.endo_nbr,M_.endo_nbr,gend);
            PPtt(dr.restrict_var_list,dr.restrict_var_list,:)=Ptt;
            for k=1:gend
                if options_.heteroskedastic_filter
                    Q_k=Q(:,:,k);
                else
                    Q_k=Q;
                end
                DQD_k=D(~ilagged,:)*Q_k*transpose(D(~ilagged,:))+C(~ilagged,:)*R*Q_k*transpose(D(~ilagged,:))+D(~ilagged,:)*Q_k*transpose(C(~ilagged,:)*R);
                DQR_k=D(~ilagged,:)*Q_k*transpose(R);
                P_pred_k=P(dr.restrict_var_list,dr.restrict_var_list,k);     % reduced Pstar_{t|t-1} (predicted)
                P_ss_pred=C(~ilagged,:)*P_pred_k*C(~ilagged,:)'+DQD_k;
                P_sd_pred=C(~ilagged,:)*P_pred_k+DQR_k;
                di=data_index{k};
                if k<=d && ~isempty(Pinf_out) && ~isempty(di) && ~Finf_singular_out(k)
                    % Diffuse period with nonzero Finf
                    ZZ=Z(di,:);
                    Pinf_k=Pinf_out(:,:,k);
                    Finf_k=ZZ*Pinf_k*ZZ';
                    if rcond(Finf_k) < diffuse_kalman_tol
                        % Finf is rank-deficient (e.g. univariate filter case with
                        % more observables than diffuse directions): fall through
                        % to standard formula using Fstar + H.
                        F_k=ZZ*P_pred_k*ZZ'+H(di,di);
                        if rcond(F_k) < kalman_tol
                            PPtt(static_var_list,static_var_list,k)=P_ss_pred;
                            PPtt(static_var_list,dr.restrict_var_list,k)=P_sd_pred;
                            PPtt(dr.restrict_var_list,static_var_list,k)=transpose(P_sd_pred);
                        else
                            iF_k=inv(F_k);
                            P_sd_Z=P_sd_pred*ZZ';
                            P_dd_Z=P_pred_k*ZZ';
                            PPtt(static_var_list,static_var_list,k)=P_ss_pred-P_sd_Z*iF_k*P_sd_Z';
                            PPtt(static_var_list,dr.restrict_var_list,k)=P_sd_pred-P_sd_Z*iF_k*P_dd_Z';
                            PPtt(dr.restrict_var_list,static_var_list,k)=transpose(PPtt(static_var_list,dr.restrict_var_list,k));
                        end
                    else
                        iFinf_k=inv(Finf_k);
                        Kinf_k=Pinf_k*ZZ'*iFinf_k;                              % mm x vobs_k
                        G=DQR_k*ZZ'*Kinf_k';                                    % ss x mm: correction from Cov(eta,x_d)*Z'*Kinf'
                        Ptt_dd_k=Ptt(1:size(T,1),1:size(T,1),k);                % reduced Ptt_{t|t} (dynamic block, already correct)
                        PPtt(static_var_list,static_var_list,k)=C(~ilagged,:)*Ptt_dd_k*C(~ilagged,:)'+DQD_k-G*C(~ilagged,:)'-C(~ilagged,:)*G';
                        PPtt(static_var_list,dr.restrict_var_list,k)=C(~ilagged,:)*Ptt_dd_k+DQR_k-G;
                        PPtt(dr.restrict_var_list,static_var_list,k)=transpose(PPtt(static_var_list,dr.restrict_var_list,k));
                    end
                elseif ~isempty(di)
                    % Stationary period (k>d) or diffuse with Finf_singular
                    % Standard Kalman update: F = Z*Pstar*Z' + H
                    ZZ=Z(di,:);
                    F_k=ZZ*P_pred_k*ZZ'+H(di,di);
                    if rcond(F_k) < kalman_tol
                        % F is singular (e.g. Fstar rank 0 in diffuse): no update, Ptt = P_pred
                        PPtt(static_var_list,static_var_list,k)=P_ss_pred;
                        PPtt(static_var_list,dr.restrict_var_list,k)=P_sd_pred;
                        PPtt(dr.restrict_var_list,static_var_list,k)=transpose(P_sd_pred);
                    else
                        iF_k=inv(F_k);
                        P_sd_Z=P_sd_pred*ZZ';                               % P_sd,pred*Z'
                        P_dd_Z=P_pred_k*ZZ';                                % P_dd*Z'
                        PPtt(static_var_list,static_var_list,k)=P_ss_pred-P_sd_Z*iF_k*P_sd_Z';
                        PPtt(static_var_list,dr.restrict_var_list,k)=P_sd_pred-P_sd_Z*iF_k*P_dd_Z';
                        PPtt(dr.restrict_var_list,static_var_list,k)=transpose(PPtt(static_var_list,dr.restrict_var_list,k));
                    end
                else
                    % no observations: updated = predicted
                    PPtt(static_var_list,static_var_list,k)=P_ss_pred;
                    PPtt(static_var_list,dr.restrict_var_list,k)=P_sd_pred;
                    PPtt(dr.restrict_var_list,static_var_list,k)=transpose(P_sd_pred);
                end
            end
            Ptt=PPtt;
            clear PPtt
        end

        % reconstruct state_uncertainty
        if ~isempty(state_uncertainty)
            mm=size(T,1);
            ss=length(find(static_var_list));
            sstate_uncertainty=zeros(M_.endo_nbr,M_.endo_nbr,gend);
            sstate_uncertainty(dr.restrict_var_list,dr.restrict_var_list,:)=state_uncertainty(1:mm,1:mm,:);
            for k=1:gend
                sstate_uncertainty(static_var_list,static_var_list,k)=[C(~ilagged,:) D(~ilagged,:)]*state_uncertainty(:,:,k)*[C(~ilagged,:) D(~ilagged,:)]';
                tmp = [C(~ilagged,:) D(~ilagged,:)]*state_uncertainty(:,:,k);
                sstate_uncertainty(static_var_list,dr.restrict_var_list,k)=tmp(1:ss,1:mm);
                sstate_uncertainty(dr.restrict_var_list,static_var_list,k)=transpose(sstate_uncertainty(static_var_list,dr.restrict_var_list,k));
            end
            state_uncertainty=sstate_uncertainty;
            clear sstate_uncertainty
        end
        if ~isempty(state_uncertainty0)
            mm=size(T,1);
            ss=length(find(static_var_list));
            sstate_uncertainty=zeros(M_.endo_nbr,M_.endo_nbr);
            sstate_uncertainty(dr.restrict_var_list,dr.restrict_var_list)=state_uncertainty0(1:mm,1:mm);
                sstate_uncertainty(static_var_list,static_var_list)=[C(~ilagged,:) D(~ilagged,:)]*state_uncertainty0*[C(~ilagged,:) D(~ilagged,:)]';
                tmp = [C(~ilagged,:) D(~ilagged,:)]*state_uncertainty0;
                sstate_uncertainty(static_var_list,dr.restrict_var_list)=tmp(1:ss,1:mm);
                sstate_uncertainty(dr.restrict_var_list,static_var_list)=transpose(sstate_uncertainty(static_var_list,dr.restrict_var_list));
            state_uncertainty0=sstate_uncertainty;
            clear sstate_uncertainty
        end

        % reconstruct PK
        if ~isempty(PK)
            PP = zeros(nk,M_.endo_nbr,M_.endo_nbr,gend+nk);
            PP(:,dr.restrict_var_list,dr.restrict_var_list,:) = PK;
            if ~options_.heteroskedastic_filter
                DQD=D(~ilagged,:)*Q*transpose(D(~ilagged,:))+C(~ilagged,:)*R*Q*transpose(D(~ilagged,:))+D(~ilagged,:)*Q*transpose(C(~ilagged,:)*R);
                DQR=D(~ilagged,:)*Q*transpose(R);
                for f=1:nk
                    for k=1:gend
                        PP(f,static_var_list,static_var_list,k+f)=C(~ilagged,:)*squeeze(PK(f,:,:,k+f))*C(~ilagged,:)'+DQD;
                        PP(f,static_var_list,dr.restrict_var_list,k+f)=C(~ilagged,:)*squeeze(PK(f,:,:,k+f))+DQR;
                        PP(f,dr.restrict_var_list,static_var_list,k+f)=transpose(squeeze(PP(f,static_var_list,dr.restrict_var_list,k+f)));
                    end
                end
            end
            PK=PP;
            clear PP
        end
    end

    mf = smoother_info.var_list(smoother_info.mf);
end

smoother_results = pack_smoother_results(alphahat, etahat, epsilonhat, ahat, ...
    SteadyState, trend_coeff, aK, T, R, P, PK, decomp, trend_addition, ...
    state_uncertainty, dr, mf, alphahat0, state_uncertainty0, d, info, ...
    regimes_, Ptt, V_eta);

function a=set_Kalman_smoother_starting_values(a,M_,dr,options_,smoother_restrict_columns)
% function a=set_Kalman_smoother_starting_values(a,M_,dr,options_,smoother_restrict_columns)
% Sets initial states guess for Kalman filter/smoother based on M_.filter_initial_state
%
% INPUTS
%   o a                          [double]   (p*1) vector of states
%   o M_                         [structure] describing the model
%   o dr                         [structure] storing the decision rules
%   o options_                   [structure] describing the options
%   o smoother_restrict_columns  [double]    positions of states in smoother variable list
%
% OUTPUTS
%   o a             [double]    (p*1) vector of set initial states

if isfield(M_,'endo_initial_state') && ~isempty(M_.endo_initial_state) && M_.endo_initial_state.status
    % direct assignment
    state_indices=dr.order_var(smoother_restrict_columns);
    if ~isempty(M_.endo_initial_state.values)
        if options_.loglinear && ~options_.logged_steady_state
            a(smoother_restrict_columns) = log(M_.endo_initial_state.values(state_indices)) - log(dr.ys(state_indices));
        elseif ~options_.loglinear && ~options_.logged_steady_state
            a(smoother_restrict_columns) = M_.endo_initial_state.values(state_indices) - dr.ys(state_indices);
        else
            error('The steady state is logged. This should not happen. Please contact the developers')
        end
    end

elseif isfield(M_,'filter_initial_state') && ~isempty(M_.filter_initial_state)
    state_indices=dr.order_var(smoother_restrict_columns);
    for ii=1:size(state_indices,1)
        if ~isempty(M_.filter_initial_state{state_indices(ii),1})
            if options_.loglinear && ~options_.logged_steady_state
                a(smoother_restrict_columns(ii)) = log(eval(M_.filter_initial_state{state_indices(ii),2})) - log(dr.ys(state_indices(ii)));
            elseif ~options_.loglinear && ~options_.logged_steady_state
                a(smoother_restrict_columns(ii)) = eval(M_.filter_initial_state{state_indices(ii),2}) - dr.ys(state_indices(ii));
            else
                error('The steady state is logged. This should not happen. Please contact the developers')
            end
        end
    end
end

function smoother_results = pack_smoother_results(alphahat, etahat, epsilonhat, ahat, ...
    SteadyState, trend_coeff, aK, T, R, P, PK, decomp, trend_addition, ...
    state_uncertainty, dr, mf, alphahat0, state_uncertainty0, d, info, ...
    regimes_, Ptt, V_eta)
% Pack smoother local variables into a structured result.
smoother_results.smoothed.alphahat  = alphahat;
smoother_results.smoothed.etahat    = etahat;
smoother_results.smoothed.epsilonhat= epsilonhat;
smoother_results.smoothed.alphahat0 = alphahat0;
smoother_results.smoothed.V_tgT     = state_uncertainty;
smoother_results.smoothed.V_0gT     = state_uncertainty0;
smoother_results.smoothed.V_eta     = V_eta;
smoother_results.filtered.ahat      = ahat;
smoother_results.filtered.P         = P;
smoother_results.filtered.Ptt       = Ptt;
smoother_results.filtered.aK        = aK;
smoother_results.filtered.PK        = PK;
smoother_results.filtered.decomp    = decomp;
smoother_results.model.T            = T;
smoother_results.model.R            = R;
smoother_results.model.SteadyState  = SteadyState;
smoother_results.model.trend_coeff  = trend_coeff;
smoother_results.model.trend_addition = trend_addition;
smoother_results.model.dr           = dr;
smoother_results.model.mf           = mf;
smoother_results.d                  = d;
smoother_results.info               = info;
smoother_results.regimes            = regimes_;

