@#include "NKM_common.inc"

options_.TeX=1;

    // -----------------Occbin ----------------------------------------------//
    options_.occbin.smoother.debug=1;
    occbin_setup(likelihood_piecewise_kalman_filter);

    // use PKF
    estimation(
            datafile=dataobsfile, mode_file=NKM_mh_mode_saved,
            mode_compute=0, nobs=120, first_obs=1,
            mh_replic=0, plot_priors=0, smoother,
            consider_all_endogenous,filter_step_ahead=[1:8],smoothed_state_uncertainty);

    // plot regimes
    occbin.plot_regimes(oo_.occbin.smoother.regime_history,M_,options_)

    // forecast starting from period 42, zero shocks (default)
    smoother2histval(period=42);
    [forecast, error_flag] = occbin.forecast(options_,M_,oo_.dr,oo_.steady_state,oo_.exo_steady_state,oo_.exo_det_steady_state,8);
    // forecast with stochastic shocks
    options_.occbin.forecast.qmc=true;
    options_.occbin.forecast.replic=127;
    [forecast1, error_flag] = occbin.forecast(options_,M_,oo_.dr,oo_.steady_state,oo_.exo_steady_state,oo_.exo_det_steady_state,8);

    figure('Name','OccBin: Forecasts')
    subplot(2,1,1)
    plot(1:8,forecast.piecewise.Mean.inom,'b-',1:8,forecast1.piecewise.Mean.inom,'r--')
    subplot(2,1,2)
    plot(1:8,forecast.piecewise.Mean.y,'b-',1:8,forecast1.piecewise.Mean.y,'r--')

    // GIRF given states in 42 and shocks in 43
    t0=42;
    options_.occbin.irf.exo_names=M_.exo_names;
    options_.occbin.irf.t0=t0;
    oo_.occbin.irfs = occbin.irf(M_,oo_,options_);

    var_list_ = {'c','n','y','pigap','inom','inomnot'};

    // if you want to scale ...
    // options_occbin_.plot_irf.endo_scaling_factor = vars_irf(:,3);
    options_.occbin.plot_irf.simulname = ['t0_' int2str(t0)];
    options_.irf=40;
    occbin.plot_irfs(M_,oo_.occbin.irfs,options_,var_list_);
    var_list_={};
    options_.occbin.plot_irf.simulname = ['t0_' int2str(t0) '_full'];
    occbin.plot_irfs(M_,oo_.occbin.irfs,options_,var_list_);
    oo0=oo_;
    // use smoother_redux
    estimation(
            datafile=dataobsfile, mode_file=NKM_mh_mode_saved,
            mode_compute=0, nobs=120, first_obs=1,
            mh_replic=0, plot_priors=0, smoother, smoother_redux,
            consider_all_endogenous,filter_step_ahead=[1:8],smoothed_state_uncertainty);

    // check consistency of smoother_redux
    for k=1:M_.endo_nbr,
        mer(k)=max(abs(oo_.SmoothedVariables.(M_.endo_names{k})-oo0.SmoothedVariables.(M_.endo_names{k})));
    end
    if max(mer)>1.e-10
        error('smoother redux does not recover full smoother results!')
    else
        disp('smoother redux successfully recovers full smoother results!')
    end

    for k=1:M_.endo_nbr,
    mer(k)=max(abs(oo_.UpdatedVariables.(M_.endo_names{k})-oo0.UpdatedVariables.(M_.endo_names{k})));
    end
    if max(mer)>1.e-10
    error('smoother redux does not recover full updated variables results!')
    else
    disp('smoother redux successfully recovers full updated variables results!')
    end


    for k=1:M_.endo_nbr,
    mer(k)=max(abs(oo_.FilteredVariables.(M_.endo_names{k})-oo0.FilteredVariables.(M_.endo_names{k})));
    end
    if max(mer)>1.e-10
    error('smoother redux does not recover full filtered variables results!')
    else
    disp('smoother redux successfully recovers full filtered variables results!')
    end

    for k=1:M_.endo_nbr,
        mer(k)=max(max(max(abs(oo_.FilteredVariablesKStepAhead(:,k,:)-oo0.FilteredVariablesKStepAhead(:,k,:)))));
    end
    if max(mer)>1.e-10
    error('smoother redux does not recover full k-step ahead variables results!')
    else
    disp('smoother redux successfully recovers full k-step ahead variables results!')
    end

    //run PKF with MCMC
    options_.smoother_redux=false;
    estimation(
        datafile=dataobsfile, mode_file=NKM_mh_mode_saved,
        mode_compute=0, nobs=120, first_obs=1,
        mh_replic=50, plot_priors=0, smoother,
        consider_all_endogenous,filter_step_ahead=[1:8],smoothed_state_uncertainty);

    // use inversion filter (note that IF provides smoother together with likelihood)
    occbin_setup(likelihood_inversion_filter,smoother_inversion_filter);

    estimation(
            datafile=dataobsfile, mode_file=NKM_mh_mode_saved,
            mode_compute=0, nobs=120, first_obs=1,
            mh_replic=50, plot_priors=0, smoother,
            consider_all_endogenous,filter_step_ahead=[1:8],smoothed_state_uncertainty);

    // show initial condition effect of IF
    figure('Name','OccBin: Smoothed shocks')
    subplot(221)
    plot([oo0.SmoothedShocks.epsg oo_.SmoothedShocks.epsg]), title('epsg')
    subplot(222)
    plot([oo0.SmoothedShocks.epsi oo_.SmoothedShocks.epsi]), title('epsi')
    subplot(223)
    plot([oo0.SmoothedShocks.epss oo_.SmoothedShocks.epss]), title('epss')
    legend('PKF','IF')
    figure('Name','OccBin: Smoothed Variables')
    subplot(221)
    plot([oo0.SmoothedVariables.inom oo_.SmoothedVariables.inom]), title('inom')
    subplot(222)
    plot([oo0.SmoothedVariables.yg oo_.SmoothedVariables.yg]), title('yg')
    subplot(223)
    plot([oo0.SmoothedVariables.pi oo_.SmoothedVariables.pi]), title('pi')
    legend('PKF','IF')
    occbin_write_regimes(smoother);

    estimation(
            datafile=dataobsfile, mode_file=NKM_mh_mode_saved,
            mode_compute=0, nobs=120, first_obs=1,
            mh_replic=50, plot_priors=0, smoother,
            consider_all_endogenous,filter_step_ahead=[1:8],smoothed_state_uncertainty);

write_latex_dynamic_model;
collect_latex_files;
[status, cmdout]=system(['pdflatex -halt-on-error -interaction=nonstopmode ' M_.fname '_TeX_binder.tex']);
if status
    cmdout
    error('TeX-File did not compile.')
end
