
%% %%%%%%% { Model 1} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%  {Univariate Regime shift in intercept & variance}%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
period=[1:1000];
ret=[100*diff(log(spot(:,index))), 100*diff(log(future(:,index))), 100*spot(2:end,end)];
ret=ret(period,:);
y=ret(:,1); x=[ones(size(y)),ret(:,2)]; z=ret(:,3);
% y, x, z are returns of spot, future, and t-bill // weekly data

tic
%[MLE_coeff, LLF,smooth_t, filter_t]=MLE_RS_uni_fit_all(y, x, 1);
[MLE_coeff, LLF,smooth_t,filter_t, tp11, tp00]=MLE_RS_uni_TVTP_fit_all(y, x, z);

dynamic_hedge=sum(filter_t.*repmat([MLE_coeff(2),MLE_coeff(4)],[size(y)]),2);
fprintf( ['    **************MLE Paramters Estimations:*************** \n', num2str(MLE_coeff')], '\n')
%fprintf( ['\n    **************MLE Paramters Standard error: ************\n', num2str(post_stdev)], '\n')
%fprintf( '\n    **************MLE Transistion Probability: Tp11 and Tp00: ************ \n')
%fprintf(['\n Tp11  ', num2str(MLE_coeff(end-1)), '   ///  Tp00  ', num2str(MLE_coeff(end)), '\n' ])
fprintf('\n time cost %3g     \n', toc)

for i=1:10
    delete(figure(i))
end
scrsz = get(0,'ScreenSize');
figure('Position',[4 scrsz(4)/2 scrsz(3) scrsz(4)/2])
plot(smooth_t(:,1)); xlabel('Smooth Probability');

scrsz = get(0,'ScreenSize');
figure('Position',[4 scrsz(4)/2 scrsz(3) scrsz(4)/2])
plot(filter_t(:,1)); xlabel('Filter Probability');

scrsz = get(0,'ScreenSize');
figure('Position',[4 scrsz(4)/2 scrsz(3) scrsz(4)/2])
plot(dynamic_hedge); xlabel('Dynamic Hedge ratio');


%% %%%%%%% { Model 2} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%  {VAR(1) model, shift in intercept, 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=1; % 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,:)];


[T,k]=size(x);
num_eq=size(y,2);
num_cov=num_eq*(num_eq+1)/2;   %num of covariance parameters need to be estimated

tic
[parameters,LLF,smooth_t,filter_t]=MLE_RS_Multi_intercept_fit(y, x);
toc

% get the results & plot the smooth, fileter probability
sigma0_chol_low=parameters(num_eq*(1+k)+3:num_eq*(1+k)+num_cov+2,:);            % low tranigular matrix of low volatility cov-var matrix
sigma1_chol_low=parameters(num_eq*(1+k)+num_cov+3:num_eq*(1+k)+2*num_cov+2,:);  % low tranigular matrix of high volatility cov-var matrix
sigma0_chol_low=ivech(sigma0_chol_low);
sigma1_chol_low=ivech(sigma1_chol_low);       % transfer the column vector to low-trangular matrix
sigma0_draw=sigma0_chol_low*sigma0_chol_low'; 
sigma1_draw=sigma1_chol_low*sigma1_chol_low'; % transfer low-trangular matrix to cov-variance matrix
OLS_sigma=(y-x*(x\y))'*(y-x*(x\y))/(T-k*num_eq);

hedge0=sigma0_draw(1,2)/sigma0_draw(2,2);     
hedge1=sigma1_draw(1,2)/sigma1_draw(2,2);
hedgeOLS=ret(:,1)\[ones(size(ret(:,1))),ret(:,2)];

fprintf( ['\n\n  **************MLE Paramters Estimations:*************** \n', num2str(parameters(1:num_eq*(k+1),:)')], '\n')
%fprintf( ['\n    **************MLE Paramters Standard error: ************\n', num2str(post_stdev)], '\n')
fprintf( '\n    **************MLE Transistion Probability: Tp11 and Tp00: ************ \n')
fprintf(['\n Tp11  ', num2str(parameters(num_eq*(k+1)+1,:)), '   ///  Tp00  ', num2str(parameters(num_eq*(k+1)+2,:)), '\n' ])
fprintf( '\n    **************MLE Cov-Var Matrix: Sigma0 & Sigma1: ************ \n')
sigma0_draw
sigma1_draw
OLS_sigma
fprintf( '\n    **************MLE 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)
for i=1:10
    delete(figure(i))
end
scrsz = get(0,'ScreenSize');
figure('Position',[4 scrsz(4)/2 scrsz(3) scrsz(4)/2])
plot(smooth_t(:,1)); xlabel('Smooth Probability');

scrsz = get(0,'ScreenSize');
figure('Position',[4 scrsz(4)/2 scrsz(3) scrsz(4)/2])
plot(filter_t(:,1)); xlabel('Filter Probability');


%%%%%%%% { Model 3} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  {Multivariate Regime shift in intercept & variance with Time varying transition probability}%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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

future=xlsread('C:\Research_Weijun_Xu\Research\Hedging_MRS\data\future_month.xls'); 
spot=xlsread('C:\Research_Weijun_Xu\Research\Hedging_MRS\data\spot_month.xls'); 
index=1; % index=1~4,represents: SP500, FTSE100, Nikk225, Hengsheng
period=[1:231];
ret=[100*diff(log(spot(:,index))), 100*diff(log(future(:,index))), 100*spot(2:end,end)];
ret=ret(period,:);                                                      % direct return
ret=[ret(period,1:2)-repmat(ret(period,3),[1,2]), ret(period,3)];       % excess return, minus T-Bill rate

y=[ret(:,1),ret(:,2)]; x=[ones(size(y,1),1), ret(:,3)]; z=ret(:,3);
% y, x, z are returns of spot, future, and t-bill // weekly data

[T,k]=size(x);
num_eq=size(y,2);
num_cov=num_eq*(num_eq+1)/2;   %num of covariance parameters need to be estimated

tic
[parameters,LLF,smooth_t,filter_t]=MLE_RS_Multi_TVTP_fit(y, x,z);
toc


% get the results & plot the smooth, fileter probability
sigma0_chol_low=parameters(num_eq*(1+k)+3:num_eq*(1+k)+num_cov+2,:);            % low tranigular matrix of low volatility cov-var matrix
sigma1_chol_low=parameters(num_eq*(1+k)+num_cov+3:num_eq*(1+k)+2*num_cov+2,:);  % low tranigular matrix of high volatility cov-var matrix
sigma0_chol_low=ivech(sigma0_chol_low);
sigma1_chol_low=ivech(sigma1_chol_low);       % transfer the column vector to low-trangular matrix
sigma0_draw=sigma0_chol_low*sigma0_chol_low'; 
sigma1_draw=sigma1_chol_low*sigma1_chol_low'; % transfer low-trangular matrix to cov-variance matrix
OLS_sigma=(y-x*(x\y))'*(y-x*(x\y))/(T-k*num_eq);

hedge0=sigma0_draw(1,2)/sigma0_draw(2,2);     
hedge1=sigma1_draw(1,2)/sigma1_draw(2,2);
hedgeOLS=data(:,1)\[ones(size(data(:,1))),data(:,2)];

fprintf( ['\n\n  **************MLE Paramters Estimations:*************** \n', num2str(parameters(1:num_eq*(k+1),:)')], '\n')
%fprintf( ['\n    **************MLE Paramters Standard error: ************\n', num2str(post_stdev)], '\n')
fprintf( '\n    **************MLE Transistion Probability: Tp11 and Tp00: ************ \n')
fprintf(['\n Tp11  ', num2str(parameters(num_eq*(k+1)+1,:)), '   ///  Tp00  ', num2str(parameters(num_eq*(k+1)+2,:)), '\n' ])
fprintf( '\n    **************MLE Cov-Var Matrix: Sigma0 & Sigma1: ************ \n')
sigma0_draw
sigma1_draw
OLS_sigma
fprintf( '\n    **************MLE 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)
for i=1:10
    delete(figure(i))
end
scrsz = get(0,'ScreenSize');
figure('Position',[4 scrsz(4)/2 scrsz(3) scrsz(4)/2])
plot(smooth_t(:,1)); xlabel('Smooth Probability');

scrsz = get(0,'ScreenSize');
figure('Position',[4 scrsz(4)/2 scrsz(3) scrsz(4)/2])
plot(filter_t(:,1)); xlabel('Filter Probability');
plot(smooth_t(:,1))









figure (1)
plot(smooth_t(:,1))

figure (3)
plot(smooth_t(:,1))



tic
index=4;

tic
[parameters, LLF, Ht, Ht_1, Ht_0, smooth_t, filter_t]=MLE_RS_Multi_Scalar_BEKK_GARCH_fit2(data_raw1,index});
toc

hedge=Ht(1,2,:);
hedge=hedge(:);

plot(hedge)


index=1;
data=data_raw(:,index);

tic 
[parameters2, LLF2, log_likelihood2, ht2, smooth_t2, filter_t2]=MLE_RS_Uni_GARCH_fit(data);
toc 

spec=garchset('C',NaN,'P',1,'Q',1);
[Coeff,Errors,LLF_garch,Innovations,Sigmas]=garchfit(spec, data);


subplot(2,2,1)
plot(smooth_t{1,1}(:,1))
subplot(2,2,2)
plot(smooth_t{1,2}(:,1))
subplot(2,2,3)
plot(smooth_t{1,3}(:,1))

figure (2)
plot(smooth_t(:,1))
 
 
data=data_raw(:,3:4);


xx=Ht{1}(1,2,1:end)./Ht{1}(2,2,1:end);
xx=xx(:);

plot(xx)

tic
[parameters, LLF, Ht, Rt, smooth_t]=MLE_RS_Multi_DCC_fit(data);
toc

tic
[parameters, LLF, Ht, smooth_t, stderrors]=MLE_RS_Multi_Scalar_BEKK_GARCH_fit(data);
toc








