clc;clear;
load hedge.mat
%addpath('H:\MatlabCode\Garch\Ucsd_garch')
addpath('H:\Submit\RegimeSwitching\Code\Ucsd_garch\MV Garch\Correlation MVGarch')
 Euro = crude;
 Yen = gasoline;

raw_data=[Euro Yen];


%选择分布，1 表示正态分布，0表示二元t分布
isnorm = 1;

[Coeff_Euro,Errors_Euro,LLF_Euro,Innovations_Euro,Sigmas_Euro,Summary_Euro] = garchfit(Euro);
[Coeff_Yen,Errors_Yen,LLF_Yen,Innovations_Yen,Sigmas_Yen,Summary_Yen] = garchfit(Yen);
% 计算标准化的残差
Stdresid = [(Euro-Coeff_Euro.C)./Sigmas_Euro, (Yen-Coeff_Yen.C)./Sigmas_Yen]; 

 options = optimset('Display','iter','TolCon',10^-12,'TolFun',10^-6,'TolX',10^-6);
 data = [Stdresid Sigmas_Euro Sigmas_Yen];
 if isnorm==1
    A =[1 1  0 0 0 0   
          0 0  1 1  0 0 ];
    b = [1;1];
%     LB  = [0 0 0.001 0 0 0.004 0 -3];     
%     UB =  [1 1 1 1 1 1 3 3];
%     para0=[0.02;0.97;0.003;0.04;0.90;0.04;1.5;0.5];
    LB  = [0.001 0  0 0  0 -3];     
    UB =  [1 1  1 1  3 3];
    param0=[0.02;0.97;0.04;0.90;1.5;0.5];
 end
 [parameters, LLF, EXITFLAG, OUTPUT, LAMBDA, GRAD,hessian] =  fmincon('P_RS_dcc_likelihood',param0,A ,b ,[] , [] ,LB ,UB,[],options,data,isnorm);
se = sqrt(diag(inv(hessian)));
t = parameters./se;
%计算样本内的相关系数和平滑概率
[logL, likelihoods ,rho, smoothed] = P_RS_dcc_likelihood(parameters,data,isnorm);
roll_forecast = P_RS_dcc_forecast(parameters,data,isnorm,1)