%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Estimate the Hedge Ratio of MRS models for Four pairs spot &
%% futures, with 500-winodw, one-step out-sample forecast, weekly data
%% MRS-OLS, MRS-VAR, MRS-SUR models with MLE method
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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');
forecast_step=1;
T=size(spot,1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Let's first orginize the return data sets
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
        sample_ret{index,i}=ret(i:i+500-1,:);
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Model 1: MRS-OLS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for index=1:4 % index=1~4,represents: SP500, FTSE100, Nikk225, Hengsheng
    for i=1:ceil((T-500)/forecast_step)
        y=sample_ret{index,(i-1)*forecast_step+1}(:,1);
        x=[ones(size(y)),sample_ret{index,(i-1)*forecast_step+1}(:,2)];
        % y is the spot return, x is the future return
        tic
        [parameters, LLF, smooth_t,filter_t]=MLE_RS_uni_all_fit(y, x);

        hedge1=parameters(2); hedge0=parameters(4);                                     % hedge ratio in regime 1 and regime 0
        P=[parameters(end-1), 1-parameters(end-1); 1-parameters(end), parameters(end)]; % Transistion probability Matrix
        state_probability(1,:)=filter_t(end,:)*P;
        if forecast_step<=1
            % when forecast=1
            hedge_MRS_OLS_MLE(i,index)=state_probability(1,:)*[hedge1,hedge0]';
        elseif forecast_step>1
            % when forecast>1, we use state_probability(1,:) as new filter probability
            for j=2:forecast_step
                state_probability(j,:)=state_probability(j-1,:)*P;
            end
            hedge_MRS_OLS_MLE(i,index)=state_probability(end,:)*[hedge1,hedge0]';
        end
        fprintf('\n *********************************************************************** \n');
        save('C:\Research_Weijun_Xu\Research\Hedging_MRS\results\hedge_MRS\hedge_MRS_OLS_MLE', 'hedge_MRS_OLS_MLE');
        fprintf('\n Time cost MLE-MRS-OLS model in sub_sample %3g  is: %3g \n', index, toc)
    end
end

clear parameters  filter_t  state_probability y x hedge1 hedge0 P
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Model 2: MRS-VAR
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for index=1:4 % index=1~4,represents: SP500, FTSE100, Nikk225, Hengsheng
    for i=1:ceil((T-500)/forecast_step)
        y=sample_ret{index,(i-1)*forecast_step+1}(2:end,:);
        x=[ones(size(y,1),1),sample_ret{index,(i-1)*forecast_step+1}(1:end-1,:)];
        % y=[Rs_t, Rf_t], x=[1 Rs_t-1, Rf_t-1]
        tic
        [parameters, LLF, smooth_t,filter_t, cov_matrix]=MLE_RS_Multi_intercept_VAR_fit(y, x);

        hedge0=cov_matrix(1,2,1)/cov_matrix(2,2,1);
        hedge1=cov_matrix(1,2,2)/cov_matrix(2,2,2);

        P=[parameters(end-1), 1-parameters(end-1); 1-parameters(end), parameters(end)]; % Transistion probability Matrix
        state_probability(1,:)=filter_t(end,:)*P;
        if forecast_step<=1
            % when forecast=1

            hedge_MRS_VAR_MLE(i,index)=state_probability(1,:)*[hedge1,hedge0]';
        elseif forecast_step>1
            % when forecast>1, we use state_probability(1,:) as new filter probability
            for j=2:forecast_step
                state_probability(j,:)=state_probability(j-1,:)*P;
            end
            hedge_MRS_VAR_MLE(i,index)=state_probability(end,:)*[hedge1,hedge0]';
        end
        fprintf('\n *********************************************************************** \n');
        save('C:\Research_Weijun_Xu\Research\Hedging_MRS\results\hedge_MRS\hedge_MRS_VAR_MLE', 'hedge_MRS_VAR_MLE');
        fprintf('\n Time cost for the Multivarate MRS-VAR model in sub_sample %3g  is: %3g \n', index, toc)

    end
end

clear parameters  filter_t  state_probability y x hedge1 hedge0 P

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Model 2: MRS-VAR
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for index=1:4 % index=1~4,represents: SP500, FTSE100, Nikk225, Hengsheng
    for i=1:ceil((T-500)/forecast_step)
        y=sample_ret{index,(i-1)*forecast_step+1}(2:end,:);
        x=[ones(size(y,1),1),sample_ret{index,(i-1)*forecast_step+1}(1:end-1,:)];
        % y=[Rs_t, Rf_t], x=[1 Rs_t-1, Rf_t-1]
        tic
        [parameters, LLF, smooth_t,filter_t, cov_matrix]=MLE_RS_Multi_intercept_VAR_fit(y, x);

        hedge0=cov_matrix(1,2,1)/cov_matrix(2,2,1);
        hedge1=cov_matrix(1,2,2)/cov_matrix(2,2,2);

        P=[parameters(end-1), 1-parameters(end-1); 1-parameters(end), parameters(end)]; % Transistion probability Matrix
        state_probability(1,:)=filter_t(end,:)*P;
        if forecast_step<=1
            % when forecast=1

            hedge_MRS_VAR_MLE(i,index)=state_probability(1,:)*[hedge1,hedge0]';
        elseif forecast_step>1
            % when forecast>1, we use state_probability(1,:) as new filter probability
            for j=2:forecast_step
                state_probability(j,:)=state_probability(j-1,:)*P;
            end
            hedge_MRS_VAR_MLE(i,index)=state_probability(end,:)*[hedge1,hedge0]';
        end
        fprintf('\n *********************************************************************** \n');
        save('C:\Research_Weijun_Xu\Research\Hedging_MRS\results\hedge_MRS\hedge_MRS_VAR_MLE', 'hedge_MRS_VAR_MLE');
        fprintf('\n Time cost for the Multivarate MRS-VAR model in sub_sample %3g  is: %3g \n', index, toc)

    end
end

clear parameters  filter_t  state_probability y x hedge1 hedge0 P

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Model 3: MRS-SUR
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

        ret_SUR=sample_ret{index,(i-1)*forecast_step+1};
        for j=5:length(ret_SUR)  % calculate the Moving average 4 returns
            MA4_ret(j,:)=mean(ret_SUR(j-4:j-1,:));
        end
        ret_SUR=ret_SUR(5:end,:);
        MA4_ret=MA4_ret(5:end,:);

        y=ret_SUR; 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);

        hedge0=cov_matrix(1,2,1)/cov_matrix(2,2,1);
        hedge1=cov_matrix(1,2,2)/cov_matrix(2,2,2);

        P=[parameters(end-1), 1-parameters(end-1); 1-parameters(end), parameters(end)]; % Transistion probability Matrix
        state_probability(1,:)=filter_t(end,:)*P;
        if forecast_step<=1
            % when forecast=1

            hedge_MRS_SUR_MLE(i,index)=state_probability(1,:)*[hedge1,hedge0]';
        elseif forecast_step>1
            % when forecast>1, we use state_probability(1,:) as new filter probability
            for j=2:forecast_step
                state_probability(j,:)=state_probability(j-1,:)*P;
            end
            hedge_MRS_SUR_MLE(i,index)=state_probability(end,:)*[hedge1,hedge0]';
        end
        fprintf('\n *********************************************************************** \n');
        save('C:\Research_Weijun_Xu\Research\Hedging_MRS\results\hedge_MRS\hedge_MRS_SUR_MLE', 'hedge_MRS_SUR_MLE');
        fprintf('\n Time cost for the Multivarate MRS-SUR model in sub_sample %3g  is: %3g \n', index, toc)

    end
end

clear parameters  filter_t  state_probability y x hedge1 hedge0 P



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Model 4: Simple OLS model
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for index=1:4 % index=1~4,represents: SP500, FTSE100, Nikk225, Hengsheng
    for i=1:ceil((T-500)/forecast_step)
        y=sample_ret{index,(i-1)*forecast_step+1}(:,1);
        x=[ones(size(y)),sample_ret{index,(i-1)*forecast_step+1}(:,2)];
        % y is the spot return, x is the future return
        OLS_coeff=x\y;
        hedge_OLS(i,index)=OLS_coeff(2);
        save('C:\Research_Weijun_Xu\Research\Hedging_MRS\results\hedge_MRS\hedge_OLS', 'hedge_OLS');
    end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% When Forecast Step =4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
forecast_step=4;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Model 1: MRS-OLS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for index=1:4 % index=1~4,represents: SP500, FTSE100, Nikk225, Hengsheng
    for i=1:ceil((T-500)/forecast_step)
        y=sample_ret{index,(i-1)*forecast_step+1}(:,1);
        x=[ones(size(y)),sample_ret{index,(i-1)*forecast_step+1}(:,2)];
        % y is the spot return, x is the future return
        tic
        [parameters, LLF, smooth_t,filter_t]=MLE_RS_uni_all_fit(y, x);

        hedge1=parameters(2); hedge0=parameters(4);                                     % hedge ratio in regime 1 and regime 0
        P=[parameters(end-1), 1-parameters(end-1); 1-parameters(end), parameters(end)]; % Transistion probability Matrix
        state_probability(1,:)=filter_t(end,:)*P;
        if forecast_step<=1
            % when forecast=1
            hedge_MRS_OLS_MLE_4(i,index)=state_probability(1,:)*[hedge1,hedge0]';
        elseif forecast_step>1
            % when forecast>1, we use state_probability(1,:) as new filter probability
            for j=2:forecast_step
                state_probability(j,:)=state_probability(j-1,:)*P;
            end
            hedge_MRS_OLS_MLE_4(i,index)=state_probability(end,:)*[hedge1,hedge0]';
        end
        fprintf('\n *********************************************************************** \n');
        save('C:\Research_Weijun_Xu\Research\Hedging_MRS\results\hedge_MRS\hedge_MRS_OLS_MLE_4', 'hedge_MRS_OLS_MLE_4');
        fprintf('\n Time cost MLE-MRS-OLS-4 model in sub_sample %3g  is: %3g \n', index, toc)
    end
end

clear parameters  filter_t  state_probability y x hedge1 hedge0 P
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Model 2: MRS-VAR
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for index=1:4 % index=1~4,represents: SP500, FTSE100, Nikk225, Hengsheng
    for i=1:ceil((T-500)/forecast_step)
        y=sample_ret{index,(i-1)*forecast_step+1}(2:end,:);
        x=[ones(size(y,1),1),sample_ret{index,(i-1)*forecast_step+1}(1:end-1,:)];
        % y=[Rs_t, Rf_t], x=[1 Rs_t-1, Rf_t-1]
        tic
        [parameters, LLF, smooth_t,filter_t, cov_matrix]=MLE_RS_Multi_intercept_VAR_fit(y, x);

        hedge0=cov_matrix(1,2,1)/cov_matrix(2,2,1);
        hedge1=cov_matrix(1,2,2)/cov_matrix(2,2,2);

        P=[parameters(end-1), 1-parameters(end-1); 1-parameters(end), parameters(end)]; % Transistion probability Matrix
        state_probability(1,:)=filter_t(end,:)*P;
        if forecast_step<=1
            % when forecast=1

            hedge_MRS_VAR_MLE_4(i,index)=state_probability(1,:)*[hedge1,hedge0]';
        elseif forecast_step>1
            % when forecast>1, we use state_probability(1,:) as new filter probability
            for j=2:forecast_step
                state_probability(j,:)=state_probability(j-1,:)*P;
            end
            hedge_MRS_VAR_MLE_4(i,index)=state_probability(end,:)*[hedge1,hedge0]';
        end
        fprintf('\n *********************************************************************** \n');
        save('C:\Research_Weijun_Xu\Research\Hedging_MRS\results\hedge_MRS\hedge_MRS_VAR_MLE_4', 'hedge_MRS_VAR_MLE_4');
        fprintf('\n Time cost for the Multivarate MRS-VAR-4 model in sub_sample %3g  is: %3g \n', index, toc)

    end
end

clear parameters  filter_t  state_probability y x hedge1 hedge0 P


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Model 3: MRS-SUR
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

        ret_SUR=sample_ret{index,(i-1)*forecast_step+1};
        for j=5:length(ret_SUR)  % calculate the Moving average 4 returns
            MA4_ret(j,:)=mean(ret_SUR(j-4:j-1,:));
        end
        ret_SUR=ret_SUR(5:end,:);
        MA4_ret=MA4_ret(5:end,:);

        y=ret_SUR; 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);

        hedge0=cov_matrix(1,2,1)/cov_matrix(2,2,1);
        hedge1=cov_matrix(1,2,2)/cov_matrix(2,2,2);

        P=[parameters(end-1), 1-parameters(end-1); 1-parameters(end), parameters(end)]; % Transistion probability Matrix
        state_probability(1,:)=filter_t(end,:)*P;
        if forecast_step<=1
            % when forecast=1

            hedge_MRS_SUR_MLE_4(i,index)=state_probability(1,:)*[hedge1,hedge0]';
        elseif forecast_step>1
            % when forecast>1, we use state_probability(1,:) as new filter probability
            for j=2:forecast_step
                state_probability(j,:)=state_probability(j-1,:)*P;
            end
            hedge_MRS_SUR_MLE_4(i,index)=state_probability(end,:)*[hedge1,hedge0]';
        end
        fprintf('\n *********************************************************************** \n');
        save('C:\Research_Weijun_Xu\Research\Hedging_MRS\results\hedge_MRS\hedge_MRS_SUR_MLE_4', 'hedge_MRS_SUR_MLE_4');
        fprintf('\n Time cost for the Multivarate MRS-SUR-4 model in sub_sample %3g  is: %3g \n', index, toc)

    end
end

clear parameters  filter_t  state_probability y x hedge1 hedge0 P



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Model 4: Simple OLS model
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for index=1:4 % index=1~4,represents: SP500, FTSE100, Nikk225, Hengsheng
    for i=1:ceil((T-500)/forecast_step)
        y=sample_ret{index,(i-1)*forecast_step+1}(:,1);
        x=[ones(size(y)),sample_ret{index,(i-1)*forecast_step+1}(:,2)];
        % y is the spot return, x is the future return
        OLS_coeff=x\y;
        hedge_OLS_4(i,index)=OLS_coeff(2);
        save('C:\Research_Weijun_Xu\Research\Hedging_MRS\results\hedge_MRS\hedge_OLS_4', 'hedge_OLS_4');
    end
end


