%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Calculate the Hedge ratio for in-sample case of eight models and four%%
%% pairs of (spot & futures)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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'); 
forecast_step=1;
T=size(spot,1);
index=1;       % index=1~4,represents: SP500, FTSE100, Nikk225, Hengsheng

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Let's first calculate the residuals using VEC(2) model from OLS
%% regression for each pair of spot & future index
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for index=1:4
    for i=1:T-500
        ret=[100*diff(log(spot(:,index))), 100*diff(log(future(:,index)))]; % return of spot and future
        rolling_ret=ret(i:i+500-1,:);
        
        % Error Cointegration calculation using OLS
        log_price=[100*log(spot(:,index)), 100*log(future(:,index))]; % log price of spot and future        
        rolling_log_price=log_price(i:i+500-1,:);        
        OLS_ECM=[ones(size(rolling_log_price,1),1),rolling_log_price(:,2)]\[rolling_log_price(:,1)]; %Log_s=a+b*Log_f
        ECM=rolling_log_price(:,1)-[ones(size(rolling_log_price,1),1),rolling_log_price(:,2)]*OLS_ECM;
        
        % test in Stata(R), we use VEC(2), Lag(1)_ECM for those data sets        
        y=[rolling_ret(3:end,1),rolling_ret(3:end,2)];
        x=[ones(size(y,1),1), ECM(2:end-1,:),rolling_ret(2:end-1,1),rolling_ret(2:end-1,2),...
            rolling_ret(1:end-2,1), rolling_ret(1:end-2,2)];
        coeff=x\y;
        resid{index,i}=y-x*coeff;
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% ************* Moddel 10: MRS-Scalar-BEKK-GARCH *************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for index=3:4 % index=1~4,represents: SP500, FTSE100, Nikk225, Hengsheng
    for i=1:ceil((T-500)/forecast_step)

        tic            
        [parameters, Ht_forecast_MRS_Scalar_BEKK, Ht_MRS_Scalar_BEKK, Rt_forecast_MRS_Scalar_BEKK, Rt_MRS_Scalar_BEKK]=...
            MLE_RS_Multi_Scalar_BEKK_GARCH_forecast(resid{index,(i-1)*forecast_step+1}, forecast_step);  
        
        Hedge_ratio_MRS_Scalar_BEKK_1(i,index)=Ht_forecast_MRS_Scalar_BEKK{1}(1,2,end)/Ht_forecast_MRS_Scalar_BEKK{1}(2,2,end);   % Hedge_ratio = cov(r1,r2)/var(r2) where r2 is the return of future index
        fprintf('\n *********************************************************************** \n');
        fprintf('\n MRS-Scalar-Bekk model in sub-sample %3g, now updating to %3g-period in test-window, be patient!! \n', index, i);
        save('C:\Research_Weijun_Xu\Research\Hedging_MRS\results\hedge_MRS\hedge_ratio_MRS_Scalar_BEKK_1', 'Hedge_ratio_MRS_Scalar_BEKK_1');
        fprintf('\n Time cost for the Scalar-Bekk-Normal model in sub_sample %3g  is: %3g \n', index, toc)
    end
end
 
%% ************* Moddel 12: MRS-DCC-GARCH *************************
for index=4:4
    for i=1:ceil((T-500)/forecast_step)
        tic
        [parameters, Ht_forecast_MRS_DCC_GARCH, Ht_MRS_DCC_GARCH, Rt_forecast_MRS_DCC_GARCH, Rt_MRS_DCC_GARCH]=...
            MLE_RS_Multi_DCC_forecast(resid{index,(i-1)*forecast_step+1}, forecast_step);

        % form the hedge ratio and calculate the portfolio return
        Hedge_ratio_MRS_DCC_GARCH_4(i,index)=Ht_forecast_MRS_DCC_GARCH{1}(1,2,end)/Ht_forecast_MRS_DCC_GARCH{1}(2,2,end);   % Hedge_ratio = cov(r1,r2)/var(r2) where r2 is the return of future index
        fprintf('\n *********************************************************************** \n');
        fprintf('\n MRS-Scalar-Bekk model in sub-sample %3g, now updating to %3g-period in test-window, be patient!! \n', index, i);
        save('C:\Research_Weijun_Xu\Research\Hedging_MRS\results\hedge_MRS\Hedge_ratio_MRS_DCC_GARCH_4', 'Hedge_ratio_MRS_DCC_GARCH_4');
        fprintf('\n Time cost for the Scalar-Bekk-Normal model in sub_sample %3g  is: %3g \n', index, toc)
    end
end


forecast_step=4;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% ************* Moddel 10: MRS-Scalar-BEKK-GARCH *************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for index=1:2 % index=1~4,represents: SP500, FTSE100, Nikk225, Hengsheng
    for i=1:ceil((T-500)/forecast_step)
        tic            
        [parameters, Ht_forecast_MRS_Scalar_BEKK, Ht_MRS_Scalar_BEKK, Rt_forecast_MRS_Scalar_BEKK, Rt_MRS_Scalar_BEKK]=...
            MLE_RS_Multi_Scalar_BEKK_GARCH_forecast(resid{index,(i-1)*forecast_step+1}, forecast_step);  
        
        Hedge_ratio_MRS_Scalar_BEKK_4_1(i,index)=Ht_forecast_MRS_Scalar_BEKK{1}(1,2,end)/Ht_forecast_MRS_Scalar_BEKK{1}(2,2,end);   % Hedge_ratio = cov(r1,r2)/var(r2) where r2 is the return of future index
        fprintf('\n *********************************************************************** \n');
        fprintf('\n MRS-Scalar-Bekk model in sub-sample %3g, now updating to %3g-period in test-window, be patient!! \n', index, i);
        save('C:\Research_Weijun_Xu\Research\Hedging_MRS\results\hedge_MRS\Hedge_ratio_MRS_Scalar_BEKK_4_1', 'Hedge_ratio_MRS_Scalar_BEKK_4_1');
        fprintf('\n Time cost for the Scalar-Bekk-Normal model in sub_sample %3g  is: %3g \n', index, toc)
    end
end
 

%% ************* Moddel 12: MRS-DCC-GARCH *************************
for index=1:2
    for i=1:ceil((T-500)/forecast_step)
        tic
        [parameters, Ht_forecast_MRS_DCC_GARCH, Ht_MRS_DCC_GARCH, Rt_forecast_MRS_DCC_GARCH, Rt_MRS_DCC_GARCH]=...
            MLE_RS_Multi_DCC_forecast(resid{index,(i-1)*forecast_step+1}, forecast_step);

        % form the hedge ratio and calculate the portfolio return
        Hedge_ratio_MRS_DCC_GARCH_4_1(i,index)=Ht_forecast_MRS_DCC_GARCH{1}(1,2,end)/Ht_forecast_MRS_DCC_GARCH{1}(2,2,end);   % Hedge_ratio = cov(r1,r2)/var(r2) where r2 is the return of future index
        fprintf('\n *********************************************************************** \n');
        fprintf('\n MRS-Scalar-Bekk model in sub-sample %3g, now updating to %3g-period in test-window, be patient!! \n', index, i);
        save('C:\Research_Weijun_Xu\Research\Hedging_MRS\results\hedge_MRS\Hedge_ratio_MRS_DCC_GARCH_4_1', 'Hedge_ratio_MRS_DCC_GARCH_4_1');
        fprintf('\n Time cost for the Scalar-Bekk-Normal model in sub_sample %3g  is: %3g \n', index, toc)
    end
end





