
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Model 1: MLE: Regime-Switching: OLS(allow each parameter to shift)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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');  

%future=xlsread('C:\Research_Weijun_Xu\Research\Hedging_MRS\data\future_month.xls');  % Monthly data sets
%spot=xlsread('C:\Research_Weijun_Xu\Research\Hedging_MRS\data\spot_month.xls'); 
index=3; % index=1~4,represents: SP500, FTSE100, Nikk225, Hengsheng
period=[1:1000];
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
[parameters, LLF, smooth_t,filter_t]=MLE_RS_uni_all_fit(y, x);

dynamic_hedge=sum(filter_t.*repmat([parameters(2),parameters(4)],[size(y)]),2);
fprintf( ['    **************MLE Paramters Estimations:*************** \n', num2str(parameters')], '\n')
%fprintf( ['\n    **************MLE Paramters Standard error: ************\n', num2str(stderrors')], '\n')
fprintf('\n time cost %3g     \n', toc)
MLE_state_probability_plot(smooth_t, filter_t, dynamic_hedge, 1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Model 2: MLE-VAR 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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=1; % index=1~4,represents: SP500, FTSE100, Nikk225, Hengsheng
ret=[100*diff(log(spot(:,index))), 100*diff(log(future(:,index)))];

period=[500:1000]';
ret=ret(period,1:2);
y=[ret(2:end,1),ret(2:end,2)]; 
x=[ones(size(y,1),1), ret(1:end-1,1), ret(1:end-1,2)];
tic
[parameters, LLF, smooth_t,filter_t, cov_matrix]=MLE_RS_Multi_intercept_VAR_fit(y, x);

fprintf( ['\n\n  **************MLE Paramters Estimations:*************** \n', num2str(parameters')], '\n')
%fprintf( ['\n    **************MLE Paramters Standard error: ************\n', num2str(stderrors')], '\n')
fprintf( '\n    **************MLE Cov-Var Matrix: Sigma0 & Sigma1: ************ \n')
sigma_low=cov_matrix(:,:,1)
sigma_high=cov_matrix(:,:,2)
OLS_sigma=cov_matrix(:,:,3)
fprintf('\n time cost %3g     \n', toc)
plot(smooth_t(:,1))

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Model 2: MLE-SUR 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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=1; % index=1~4,represents: SP500, FTSE100, Nikk225, Hengsheng
ret=[100*diff(log(spot(:,index))), 100*diff(log(future(:,index)))];
period=[500:1000]';
ret=ret(period,:);
for i=5:length(ret)  % calculate the Moving average 4 returns
    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
[parameters, LLF, smooth_t,filter_t, cov_matrix]=MLE_RS_Multi_intercept_SUR_fit(y, x1, x2);
%[parameters, LLF, smooth_t,filter_t, cov_matrix, stderrors]=MLE_RS_Multi_all_SUR_fit(y, x1, x2);
toc
hedge0=cov_matrix(1,2,1)/cov_matrix(2,2,1);     
hedge1=cov_matrix(1,2,2)/cov_matrix(2,2,2);   
hedge_single=cov_matrix(1,2,3)/cov_matrix(2,2,3);   

dynamic_hedge=sum(filter_t.*repmat([hedge1,hedge0],[length(y),1]),2);
fprintf( ['\n\n  **************MLE Paramters Estimations:*************** \n', num2str(parameters')], '\n')
%fprintf( ['\n    **************MLE Paramters Standard error: ************\n', num2str(stderrors')], '\n')
fprintf( '\n    **************MLE Cov-Var Matrix: Sigma0 & Sigma1: ************ \n')
sigma_low=cov_matrix(:,:,1)
sigma_high=cov_matrix(:,:,2)
OLS_sigma=cov_matrix(:,:,3)
fprintf( '\n    **************MLE Hedge Ratio: Hedge0 & Hedge1 & Hedge_OLS: ************ \n')
fprintf(['\n Hedge0 ', num2str(hedge0), '  /// Hedge1  ', num2str(hedge1), '  /// Hedge_OLS  ', num2str(hedge_single), '\n' ])
fprintf('\n time cost %3g     \n', toc)

MLE_state_probability_plot(smooth_t, filter_t, dynamic_hedge, 1);









%%%%%%Backup for calculating the conditional variance.mean...
alpha_draw=parameters(1:2,:);        % regime shift intercetpts
beta_draw=parameters(3:2*(1+2),:);   % unshift coefficients
T=length(y);
% Calculate the conditional mean and variance and covariance
mu_spot(:,1)=x1*beta_draw(1:2,:)+repmat(alpha_draw(1),[length(x1),1]);    % regime 1
mu_spot(:,2)=x1*beta_draw(1:2,:);                                         % regime 2
mu_futures(:,1)=x2*beta_draw(3:4,:)+repmat(alpha_draw(2),[length(x2),1]); % regime 1
mu_futures(:,2)=x2*beta_draw(3:4,:);                                      % regime 2
 
var_spot=[cov_matrix(1,1,2), cov_matrix(1,1,1)];         % volatility in regime 1 and regime 2
var_futures=[cov_matrix(2,2,2), cov_matrix(2,2,1)];      % volatility in regime 1 and regime 2
cov_sf=[cov_matrix(1,2,2), cov_matrix(1,2,1)];           % volatility in regime 1 and regime 2

cond_var_futures=sum(filter_t.*repmat(var_futures,[T,1]),2) + ...
    filter_t(:,1).* filter_t(:,2).*(mu_futures(:,1)-mu_futures(:,2)).^2; 
% Cond_var_f=P1t*Var1+P2t*Var2+P1tP2t*[U_f(st=1)-U_f(st=2)]^2
cond_cov_sf=sum(filter_t.*repmat(cov_sf,[T,1]),2) + ...
    filter_t(:,1).* filter_t(:,2).*(mu_futures(:,1)-mu_futures(:,2)).* (mu_spot(:,1)-mu_spot(:,2));
% Cond_cov_sf=P1t*Cov1+P2t*Cov2+P1tP2t*[U_s(st=1)-U_s(st=2)][U_f(st=1)-U_f(st=2)]
dynamic_hedge=cond_cov_sf./cond_var_futures






