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');

%% 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 Models\Data for Univariate Models\Price_impact_mts\Maturity_buckets'
filename = 'priceimpact_mts_0-3y';
[data_uni_de_1, headertext_de_1] = xlsread(filename, '0-3Y_DE');
pi_0_de_1 = data_uni_de_1(:,3);
vix_us_de_1 = data_uni_de_1(:,4);
vix_res_de_1 = data_uni_de_1(:,5);
maturity_bucket_1 = data_uni_de_1(:,1);
[data_uni_es_1, headertext_es_1] = xlsread(filename, '0-3Y_ES');
pi_0_es_1 = data_uni_es_1(:,3);
vix_us_es_1 = data_uni_es_1(:,4);
vix_res_es_1 = data_uni_es_1(:,5);
[data_uni_fr_1, headertext_fr_1] = xlsread(filename, '0-3Y_FR');
pi_0_fr_1 = data_uni_fr_1(:,3);
vix_us_fr_1 = data_uni_fr_1(:,4);
vix_res_fr_1 = data_uni_fr_1(:,5);
[data_uni_it_1, headertext_it_1] = xlsread(filename, '0-3Y_IT');
pi_0_it_1 = data_uni_it_1(:,3);
vix_us_it_1 = data_uni_it_1(:,4);
vix_res_it_1 = data_uni_it_1(:,5);
[data_uni_nl_1, headertext_nl_1] = xlsread(filename, '0-3Y_NL');
pi_0_nl_1 = data_uni_nl_1(:,3);
vix_us_nl_1 = data_uni_nl_1(:,4);
vix_res_nl_1 = data_uni_nl_1(:,5);

indep_de_1 = [ones(length(pi_0_de_1),1) vix_us_de_1 vix_res_de_1];                  % Defining some explanatory variables
indep_es_1 = [ones(length(pi_0_es_1),1) vix_us_es_1 vix_res_es_1];                  % Defining some explanatory variables
indep_fr_1 = [ones(length(pi_0_fr_1),1) vix_us_fr_1 vix_res_fr_1];                  % Defining some explanatory variables
indep_it_1 = [ones(length(pi_0_it_1),1) vix_us_it_1 vix_res_it_1];                  % Defining some explanatory variables
indep_nl_1 = [ones(length(pi_0_nl_1),1) vix_us_nl_1 vix_res_nl_1];                  % Defining some explanatory variables

k_1 = 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 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)

[RSB_PI_UNI_DE_1]=MS_Regress_Fit(pi_0_de_1,indep_de_1,k_1,S,advOpt); % Estimating the model
[RSB_PI_UNI_ES_1]=MS_Regress_Fit(pi_0_es_1,indep_es_1,k_1,S,advOpt); % Estimating the model
[RSB_PI_UNI_FR_1]=MS_Regress_Fit(pi_0_fr_1,indep_fr_1,k_1,S,advOpt); % Estimating the model
[RSB_PI_UNI_IT_1]=MS_Regress_Fit(pi_0_it_1,indep_it_1,k_1,S,advOpt); % Estimating the model
[RSB_PI_UNI_NL_1]=MS_Regress_Fit(pi_0_nl_1,indep_nl_1,k_1,S,advOpt); % Estimating the model

cd 'C:\Users\My Local Documents\Working Papers\WP_Integrated Solvency and Liquidity\Markov regime-switching models\Univariate model\Price_impact\Maturity_buckets\Switching variance';
save pi_uni_sov_eur_0-3y_vix.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 Models\Data for Univariate Models\Price_impact_mts\Maturity_buckets'
filename = 'priceimpact_mts_4-6y';
[data_uni_de_2, headertext_de_2] = xlsread(filename, '4-6Y_DE');
pi_0_de_2 = data_uni_de_2(:,3);
vix_us_de_2 = data_uni_de_2(:,4);
vix_res_de_2 = data_uni_de_2(:,5);
maturity_bucket_2 = data_uni_de_2(:,1);
[data_uni_es_2, headertext_es_2] = xlsread(filename, '4-6Y_ES');
pi_0_es_2 = data_uni_es_2(:,3);
vix_us_es_2 = data_uni_es_2(:,4);
vix_res_es_2 = data_uni_es_2(:,5);
[data_uni_fr_2, headertext_fr_2] = xlsread(filename, '4-6Y_FR');
pi_0_fr_2 = data_uni_fr_2(:,3);
vix_us_fr_2 = data_uni_fr_2(:,4);
vix_res_fr_2 = data_uni_fr_2(:,5);
[data_uni_it_2, headertext_it_2] = xlsread(filename, '4-6Y_IT');
pi_0_it_2 = data_uni_it_2(:,3);
vix_us_it_2 = data_uni_it_2(:,4);
vix_res_it_2 = data_uni_it_2(:,5);
[data_uni_nl_2, headertext_nl_2] = xlsread(filename, '4-6Y_NL');
pi_0_nl_2 = data_uni_nl_2(:,3);
vix_us_nl_2 = data_uni_nl_2(:,4);
vix_res_nl_2 = data_uni_nl_2(:,5);

indep_de_2 = [ones(length(pi_0_de_2),1) vix_us_de_2 vix_res_de_2];                  % Defining some explanatory variables
indep_es_2 = [ones(length(pi_0_es_2),1) vix_us_es_2 vix_res_es_2];                  % Defining some explanatory variables
indep_fr_2 = [ones(length(pi_0_fr_2),1) vix_us_fr_2 vix_res_fr_2];                  % Defining some explanatory variables
indep_it_2 = [ones(length(pi_0_it_2),1) vix_us_it_2 vix_res_it_2];                  % Defining some explanatory variables
indep_nl_2 = [ones(length(pi_0_nl_2),1) vix_us_nl_2 vix_res_nl_2];                  % Defining some explanatory variables

k_2 = 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 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)

[RSB_PI_UNI_DE_2]=MS_Regress_Fit(pi_0_de_2,indep_de_2,k_2,S,advOpt); % Estimating the model
[RSB_PI_UNI_ES_2]=MS_Regress_Fit(pi_0_es_2,indep_es_2,k_2,S,advOpt); % Estimating the model
[RSB_PI_UNI_FR_2]=MS_Regress_Fit(pi_0_fr_2,indep_fr_2,k_2,S,advOpt); % Estimating the model
[RSB_PI_UNI_IT_2]=MS_Regress_Fit(pi_0_it_2,indep_it_2,k_2,S,advOpt); % Estimating the model
[RSB_PI_UNI_NL_2]=MS_Regress_Fit(pi_0_nl_2,indep_nl_2,k_2,S,advOpt); % Estimating the model

cd 'C:\Users\My Local Documents\Working Papers\WP_Integrated Solvency and Liquidity\Markov regime-switching models\Univariate model\Price_impact\Maturity_buckets\Switching variance';
save pi_uni_sov_eur_0-6y_vix.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 Models\Data for Univariate Models\Price_impact_mts\Maturity_buckets'
filename = 'priceimpact_mts_7-11y';
[data_uni_de_3, headertext_de_3] = xlsread(filename, '7-11Y_DE');
pi_0_de_3 = data_uni_de_3(:,3);
vix_us_de_3 = data_uni_de_3(:,4);
vix_res_de_3 = data_uni_de_3(:,5);
maturity_bucket_3 = data_uni_de_3(:,1);
[data_uni_es_3, headertext_es_3] = xlsread(filename, '7-11Y_ES');
pi_0_es_3 = data_uni_es_3(:,3);
vix_us_es_3 = data_uni_es_3(:,4);
vix_res_es_3 = data_uni_es_3(:,5);
[data_uni_fr_3, headertext_fr_3] = xlsread(filename, '7-11Y_FR');
pi_0_fr_3 = data_uni_fr_3(:,3);
vix_us_fr_3 = data_uni_fr_3(:,4);
vix_res_fr_3 = data_uni_fr_3(:,5);
[data_uni_it_3, headertext_it_3] = xlsread(filename, '7-11Y_IT');
pi_0_it_3 = data_uni_it_3(:,3);
vix_us_it_3 = data_uni_it_3(:,4);
vix_res_it_3 = data_uni_it_3(:,5);
[data_uni_nl_3, headertext_nl_3] = xlsread(filename, '7-11Y_NL');
pi_0_nl_3 = data_uni_nl_3(:,3);
vix_us_nl_3 = data_uni_nl_3(:,4);
vix_res_nl_3 = data_uni_nl_3(:,5);

indep_de_3 = [ones(length(pi_0_de_3),1) vix_us_de_3 vix_res_de_3];                  % Defining some explanatory variables
indep_es_3 = [ones(length(pi_0_es_3),1) vix_us_es_3 vix_res_es_3];                  % Defining some explanatory variables
indep_fr_3 = [ones(length(pi_0_fr_3),1) vix_us_fr_3 vix_res_fr_3];                  % Defining some explanatory variables
indep_it_3 = [ones(length(pi_0_it_3),1) vix_us_it_3 vix_res_it_3];                  % Defining some explanatory variables
indep_nl_3 = [ones(length(pi_0_nl_3),1) vix_us_nl_3 vix_res_nl_3];                  % Defining some explanatory variables

k_3 = 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 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)

[RSB_PI_UNI_DE_3]=MS_Regress_Fit(pi_0_de_3,indep_de_3,k_3,S,advOpt); % Estimating the model
[RSB_PI_UNI_ES_3]=MS_Regress_Fit(pi_0_es_3,indep_es_3,k_3,S,advOpt); % Estimating the model
[RSB_PI_UNI_FR_3]=MS_Regress_Fit(pi_0_fr_3,indep_fr_3,k_3,S,advOpt); % Estimating the model
[RSB_PI_UNI_IT_3]=MS_Regress_Fit(pi_0_it_3,indep_it_3,k_3,S,advOpt); % Estimating the model
[RSB_PI_UNI_NL_3]=MS_Regress_Fit(pi_0_nl_3,indep_nl_3,k_3,S,advOpt); % Estimating the model

cd 'C:\Users\My Local Documents\Working Papers\WP_Integrated Solvency and Liquidity\Markov regime-switching models\Univariate model\Price_impact\Maturity_buckets\Switching variance';
save pi_uni_sov_eur_0-11y_vix.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 Models\Data for Univariate Models\Price_impact_mts\Maturity_buckets'
filename = 'priceimpact_mts_11+y';
[data_uni_de_4, headertext_de_4] = xlsread(filename, '11+Y_DE');
pi_0_de_4 = data_uni_de_4(:,3);
vix_us_de_4 = data_uni_de_4(:,4);
vix_res_de_4 = data_uni_de_4(:,5);
maturity_bucket_4 = data_uni_de_4(:,1);
[data_uni_es_4, headertext_es_4] = xlsread(filename, '11+Y_ES');
pi_0_es_4 = data_uni_es_4(:,3);
vix_us_es_4 = data_uni_es_4(:,4);
vix_res_es_4 = data_uni_es_4(:,5);
[data_uni_fr_4, headertext_fr_4] = xlsread(filename, '11+Y_FR');
pi_0_fr_4 = data_uni_fr_4(:,3);
vix_us_fr_4 = data_uni_fr_4(:,4);
vix_res_fr_4 = data_uni_fr_4(:,5);
[data_uni_it_4, headertext_it_4] = xlsread(filename, '11+Y_IT');
pi_0_it_4 = data_uni_it_4(:,3);
vix_us_it_4 = data_uni_it_4(:,4);
vix_res_it_4 = data_uni_it_4(:,5);
[data_uni_nl_4, headertext_nl_4] = xlsread(filename, '11+Y_NL');
pi_0_nl_4 = data_uni_nl_4(:,3);
vix_us_nl_4 = data_uni_nl_4(:,4);
vix_res_nl_4 = data_uni_nl_4(:,5);

indep_de_4 = [ones(length(pi_0_de_4),1) vix_us_de_4 vix_res_de_4];                  % Defining some explanatory variables
indep_es_4 = [ones(length(pi_0_es_4),1) vix_us_es_4 vix_res_es_4];                  % Defining some explanatory variables
indep_fr_4 = [ones(length(pi_0_fr_4),1) vix_us_fr_4 vix_res_fr_4];                  % Defining some explanatory variables
indep_it_4 = [ones(length(pi_0_it_4),1) vix_us_it_4 vix_res_it_4];                  % Defining some explanatory variables
indep_nl_4 = [ones(length(pi_0_nl_4),1) vix_us_nl_4 vix_res_nl_4];                  % Defining some explanatory variables

k_4 = 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 1];                        % Defining which parts of the equation will switch states (column 1 and variance only)

[RSB_PI_UNI_DE_4]=MS_Regress_Fit(pi_0_de_4,indep_de_4,k_4,S,advOpt); % Estimating the model
[RSB_PI_UNI_ES_4]=MS_Regress_Fit(pi_0_es_4,indep_es_4,k_4,S,advOpt); % Estimating the model
[RSB_PI_UNI_FR_4]=MS_Regress_Fit(pi_0_fr_4,indep_fr_4,k_4,S,advOpt); % Estimating the model
[RSB_PI_UNI_IT_4]=MS_Regress_Fit(pi_0_it_4,indep_it_4,k_4,S,advOpt); % Estimating the model
[RSB_PI_UNI_NL_4]=MS_Regress_Fit(pi_0_nl_4,indep_nl_4,k_4,S,advOpt); % Estimating the model

cd 'C:\Users\My Local Documents\Working Papers\WP_Integrated Solvency and Liquidity\Markov regime-switching models\Univariate model\Price_impact\Maturity_buckets\Switching variance';
save pi_uni_sov_eur_all_buckets_vix.mat;