clear all;
% Add the path of the MS package.
addpath('C:\Users\My Local Documents\Working Papers\WP_Integrated Solvency and Liquidity\Markov regime-switching models\MS_Regress-Matlab-master\m_Files');
filename = 'priceimpact_mts_buckets.xlsx';

%% For European sovereign bonds with remaining maturity of 0-3 years.
cd 'C:\Users\My Local Documents\Working Papers\WP_Integrated Solvency and Liquidity\Data\Data for Multivariate Models\Price_impact_mts\Maturity_buckets'
[data_multi_1, headertext_1] = xlsread(filename, '0-3Y_Daily');
pi_0_de_1 = data_multi_1(:,2);
pi_0_es_1 = data_multi_1(:,4);
pi_0_fr_1 = data_multi_1(:,6);
pi_0_it_1 = data_multi_1(:,8);
pi_0_nl_1 = data_multi_1(:,10);
remainingmat_1 = data_multi_1(:,11);

dep_1 = [pi_0_de_1, pi_0_es_1, pi_0_fr_1, pi_0_it_1, pi_0_nl_1];     % Defining dependent variables
constVec_1 = ones(length(dep_1),1);       % Defining a constant vector in mean equation
indep_1{1} = constVec_1;                  % Defining some explanatory variables
indep_1{2} = constVec_1;                  % Defining some explanatory variables
indep_1{3} = constVec_1;                  % Defining some explanatory variables
indep_1{4} = constVec_1;                  % Defining some explanatory variables
indep_1{5} = constVec_1;                  % Defining some explanatory variables

k = 2;                                % Number of States
advOpt.distrib='Normal';            % The Distribution assumption ('Normal', 't' or 'GED')
advOpt.std_method=2;                % Defining the method for calculation of standard errors. See pdf file for more details
advOpt.diagCovMat=0;
advOpt.optimizer='fminsearch';

S{1} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)
S{2} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)
S{3} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)
S{4} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)
S{5} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)

[RSB_PI_1]=MS_Regress_Fit(dep_1,indep_1,k,S,advOpt); % Estimating the model
cd 'C:\Users\My Local Documents\Working Papers\WP_Integrated Solvency and Liquidity\Markov regime-switching models\Multivariate model\Price_impact\Maturity_buckets';
save pi_multi_sov_eur_0-3y.mat;

%% For European sovereign bonds with remaining maturity of 4-6 years.
cd 'C:\Users\My Local Documents\Working Papers\WP_Integrated Solvency and Liquidity\Data\Data for Multivariate Models\Price_impact_mts\Maturity_buckets'
[data_multi_2, headertext_2] = xlsread(filename, '4-6Y_Daily');
pi_0_de_2 = data_multi_2(:,2);
pi_0_es_2 = data_multi_2(:,4);
pi_0_fr_2 = data_multi_2(:,6);
pi_0_it_2 = data_multi_2(:,8);
pi_0_nl_2 = data_multi_2(:,10);
remainingmat_2 = data_multi_2(:,11);

dep_2 = [pi_0_de_2, pi_0_es_2, pi_0_fr_2, pi_0_it_2, pi_0_nl_2];     % Defining dependent variables
constVec_2 = ones(length(dep_2),1);       % Defining a constant vector in mean equation
indep_2{1} = constVec_2;                  % Defining some explanatory variables
indep_2{2} = constVec_2;                  % Defining some explanatory variables
indep_2{3} = constVec_2;                  % Defining some explanatory variables
indep_2{4} = constVec_2;                  % Defining some explanatory variables
indep_2{5} = constVec_2;                  % Defining some explanatory variables

k = 2;                                % Number of States
advOpt.distrib='Normal';            % The Distribution assumption ('Normal', 't' or 'GED')
advOpt.std_method=2;                % Defining the method for calculation of standard errors. See pdf file for more details
advOpt.diagCovMat=0;
advOpt.optimizer='fminsearch';

S{1} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)
S{2} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)
S{3} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)
S{4} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)
S{5} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)

[RSB_PI_2]=MS_Regress_Fit(dep_2,indep_2,k,S,advOpt); % Estimating the model
cd 'C:\Users\My Local Documents\Working Papers\WP_Integrated Solvency and Liquidity\Markov regime-switching models\Multivariate model\Price_impact\Maturity_buckets';
save pi_multi_sov_eur_0-6y.mat;

%% For European sovereign bonds with remaining maturity of 7-11 years.
cd 'C:\Users\My Local Documents\Working Papers\WP_Integrated Solvency and Liquidity\Data\Data for Multivariate Models\Price_impact_mts\Maturity_buckets'
[data_multi_3, headertext_3] = xlsread(filename, '7-11Y_Daily');
pi_0_de_3 = data_multi_3(:,2);
pi_0_es_3 = data_multi_3(:,4);
pi_0_fr_3 = data_multi_3(:,6);
pi_0_it_3 = data_multi_3(:,8);
pi_0_nl_3 = data_multi_3(:,10);
remainingmat_3 = data_multi_3(:,11);

dep_3 = [pi_0_de_3, pi_0_es_3, pi_0_fr_3, pi_0_it_3, pi_0_nl_3];     % Defining dependent variables
constVec_3 = ones(length(dep_3),1);       % Defining a constant vector in mean equation
indep_3{1} = constVec_3;                  % Defining some explanatory variables
indep_3{2} = constVec_3;                  % Defining some explanatory variables
indep_3{3} = constVec_3;                  % Defining some explanatory variables
indep_3{4} = constVec_3;                  % Defining some explanatory variables
indep_3{5} = constVec_3;                  % Defining some explanatory variables

k = 2;                                % Number of States
advOpt.distrib='Normal';            % The Distribution assumption ('Normal', 't' or 'GED')
advOpt.std_method=2;                % Defining the method for calculation of standard errors. See pdf file for more details
advOpt.diagCovMat=0;
advOpt.optimizer='fminsearch';

S{1} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)
S{2} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)
S{3} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)
S{4} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)
S{5} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)

[RSB_PI_3]=MS_Regress_Fit(dep_3,indep_3,k,S,advOpt); % Estimating the model
cd 'C:\Users\My Local Documents\Working Papers\WP_Integrated Solvency and Liquidity\Markov regime-switching models\Multivariate model\Price_impact\Maturity_buckets';
save pi_multi_sov_eur_0-11y.mat;

%% For European sovereign bonds with remaining maturity of 11+ years.
cd 'C:\Users\My Local Documents\Working Papers\WP_Integrated Solvency and Liquidity\Data\Data for Multivariate Models\Price_impact_mts\Maturity_buckets'
[data_multi_4, headertext_4] = xlsread(filename, '11+Y_Daily');
pi_0_de_4 = data_multi_4(:,2);
pi_0_es_4 = data_multi_4(:,4);
pi_0_fr_4 = data_multi_4(:,6);
pi_0_it_4 = data_multi_4(:,8);
pi_0_nl_4 = data_multi_4(:,10);
remainingmat_4 = data_multi_4(:,11);

dep_4 = [pi_0_de_4, pi_0_es_4, pi_0_fr_4, pi_0_it_4, pi_0_nl_4];     % Defining dependent variables
constVec_4 = ones(length(dep_4),1);       % Defining a constant vector in mean equation
indep_4{1} = constVec_4;                  % Defining some explanatory variables
indep_4{2} = constVec_4;                  % Defining some explanatory variables
indep_4{3} = constVec_4;                  % Defining some explanatory variables
indep_4{4} = constVec_4;                  % Defining some explanatory variables
indep_4{5} = constVec_4;                  % Defining some explanatory variables

k = 2;                                % Number of States
advOpt.distrib='Normal';            % The Distribution assumption ('Normal', 't' or 'GED')
advOpt.std_method=2;                % Defining the method for calculation of standard errors. See pdf file for more details
advOpt.diagCovMat=0;
advOpt.optimizer='fminsearch';

S{1} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)
S{2} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)
S{3} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)
S{4} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)
S{5} = [1 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)

[RSB_PI_4]=MS_Regress_Fit(dep_4,indep_4,k,S,advOpt); % Estimating the model
cd 'C:\Users\My Local Documents\Working Papers\WP_Integrated Solvency and Liquidity\Markov regime-switching models\Multivariate model\Price_impact\Maturity_buckets';
save pi_multi_sov_eur_all_buckets.mat;