%%%%%%%%% { Model 1} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%  {MRS-OLS}%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
future=xlsread('C:\Research_Weijun_Xu\Research\Hedging_MRS\data\future_week.xls');  % Weekly data sets
spot=xlsread('C:\Research_Weijun_Xu\Research\Hedging_MRS\data\spot_week.xls');  
index=2; % index=1~4,represents: SP500, FTSE100, Nikk225, Hengsheng
period=[1:500];
ret=[100*diff(log(spot(:,index))), 100*diff(log(future(:,index))), 100*spot(2:end,end)];
ret=ret(period,1:2);
y=ret(:,1); x=[ones(size(y)),ret(:,2)];

tic
[smpl_coeff, st_keep, smooth_t_keep, filter_t_keep,hedge_insample]=Bay_RS_uni_all_OLS_fit(y, x, 500, 200);
toc

post_coeff=mean(smpl_coeff);
post_stdev=std(smpl_coeff);
post_smooth_t=mean(smooth_t_keep)'; % high volatility, regime 1
post_filter_t=mean(filter_t_keep)'; % high volatility, regime 1

fprintf( ['    **************Bayesian Paramters Estimations:*************** \n', num2str(post_coeff)], '\n')
fprintf( ['\n    **************Bayesian Paramters Standard error: ************\n', num2str(post_stdev)], '\n')

% plot coefficients & state
posterior_plot(smpl_coeff, post_smooth_t)

%%%%%%%%% { Model 3} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% {MRS-VAR}, allow regime shifts in interceept and cov-var matrix
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
future=xlsread('C:\Research_Weijun_Xu\Research\Hedging_MRS\data\future_week.xls');  % Weekly data sets
spot=xlsread('C:\Research_Weijun_Xu\Research\Hedging_MRS\data\spot_week.xls');      % Monthly data sets

index=2; % index=1~4,represents: SP500, FTSE100, Nikk225, Hengsheng
period=[1:500];
ret=[100*diff(log(spot(:,index))), 100*diff(log(future(:,index))), 100*spot(2:end,end)];
ret=ret(period,1:2);                                    
y=[ret(2:end,:)]; x=[ones(size(y,1),1), ret(1:end-1,:)];

tic
[smpl_coeff, Sigma1_keep, Sigma0_keep, smpl_st, smooth_t_keep,  filter_t_keep, hedge_insample, hedge_outsample]=...
    Bay_RS_Multi_intercept_VAR_fit(y, x, 1000, 200,4);
toc

% get the estimation results, and print the results, plot the smooth probability
post_coeff=mean(smpl_coeff);
post_stdev=std(smpl_coeff);
smooth_t=mean(smooth_t_keep)';
filter_t=mean(filter_t_keep)';

Sigma0=mean(Sigma0_keep,3);
Sigma1=mean(Sigma1_keep,3);

fprintf( ['    **************Bayesian Paramters Estimations:*************** \n', num2str(post_coeff)], '\n')
fprintf( ['\n    **************Bayesian Paramters Standard error: ************\n', num2str(post_stdev)], '\n')
% plot coefficients & state
fprintf( '\n    ************** Bayesian  Cov-Var Matrix: Sigma0 & Sigma1: ************ \n')
Sigma0
Sigma1
%fprintf( '\n    ************** Bayesian  Hedge Ratio: Hedge0 & Hedge1 & Hedge_OLS: ************ \n')
%fprintf(['\n Hedge0 ', num2str(hedge0), '  /// Hedge1  ', num2str(hedge1), '  /// Hedge_OLS  ', num2str(hedgeOLS(2)), '\n' ])
%fprintf('\n time cost %3g     \n', toc)
posterior_plot(smpl_coeff, smooth_t)


%%%%%%%%% { Model 4} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% {MRS-SUR}  allow regime shift in everything: intercept, coefficients, and cov-var matrix %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear
future=xlsread('C:\Research_Weijun_Xu\Research\Hedging_MRS\data\future_week.xls');  % Weekly data sets
spot=xlsread('C:\Research_Weijun_Xu\Research\Hedging_MRS\data\spot_week.xls');      % Monthly data sets
index=3; % index=1~4,represents: SP500, FTSE100, Nikk225, Hengsheng
period=[1:500]';
ret=[100*diff(log(spot(:,index))), 100*diff(log(future(:,index)))];   % return of spot and future
ret=ret(period,:);
% calculate the Moving average 4 returns
for i=5:length(ret);
    MA4_ret(i,:)=mean(ret(i-4:i-1,:));
end
ret=ret(5:end,:);
MA4_ret=MA4_ret(5:end,:);

y=ret; x1=[ones(length(y),1),MA4_ret(:,1)]; x2=[ones(length(y),1),MA4_ret(:,2)];

tic
[smpl_coeff, Sigma1_keep, Sigma0_keep, smpl_st, smooth_t_keep, filter_t_keep,hedge_insample, hedge_outsample]=...
    Bay_RS_Multi_intercept_SUR_fit(y, x1, x2, 500, 100,4);

% get the estimation results, and print the results, plot the smooth probability
post_coeff=mean(smpl_coeff);
post_stdev=std(smpl_coeff);
smooth_t=mean(smooth_t_keep)';
filter_t=mean(filter_t_keep)';

Sigma0=mean(Sigma0_keep,3);
Sigma1=mean(Sigma1_keep,3);

fprintf( ['    **************Bayesian Paramters Estimations:*************** \n', num2str(post_coeff)], '\n')
fprintf( ['\n    **************Bayesian Paramters Standard error: ************\n', num2str(post_stdev)], '\n')
% plot coefficients & state
fprintf( '\n    ************** Bayesian  Cov-Var Matrix: Sigma0 & Sigma1: ************ \n')
Sigma0
Sigma1
%fprintf( '\n    ************** Bayesian  Hedge Ratio: Hedge0 & Hedge1 & Hedge_OLS: ************ \n')
%fprintf(['\n Hedge0 ', num2str(hedge0), '  /// Hedge1  ', num2str(hedge1), '  /// Hedge_OLS  ', num2str(hedgeOLS(2)), '\n' ])
%fprintf('\n time cost %3g     \n', toc)
posterior_plot(smpl_coeff, smooth_t)




