%-----------------------------------------------------------
% The function for the likelihood function for GARCH-MIDAS
% section refered as git in Engle (2008), equ. 4. 
% Input: Parameters 
%x1: Realized Volatility
%x2:Economics Variables
%y: restuns
%v:quaerterly indicator
%w:weight2
%l : exponential form of the Tau specification
%Author: Ai Jun Hou
%-----------------------------------------------------------

function f=GMS_two_1(param,x1,x2,y,v,l)

mu=param(1);  
alfa=param(2);
beta=param(3);
m=param(4);
theta1=param(5);
w11=1;
w12=param(7);
theta2=param(6);
w22=param(8);
w21=1;

if alfa+beta<1 
   smpl1=size(x1,2);
   smpl2=size(x2,2);
   beta_values1=beta_weights(smpl1,w11,w12);
   beta_values2=beta_weights(smpl2,w21,w22);
   if l==1
       tau= exp(m+theta1*(x1*beta_values1)+...
           theta2*(x2*beta_values2));
   else
      tau=m+theta1*(x1*beta_values1)+...
           theta2*(x2*beta_values2)
   end
   IT=size(y,1);
   res=y-mu;
   res2=res.^2;
   g=zeros(IT,1);
   sig2=zeros(IT,1);
   sig2(1)=var(res);
   g(1)=sig2(1)/tau(1);
   t=1;
   for it=2:IT
       if v(it)~=v(it-1)
           t=t+1;
       end
       g(it)=(1-alfa-beta)+alfa*res2(it-1)/tau(t)+beta*g(it-1);
       sig2(it)=g(it)*tau(t);
   end
   

f=-sum(-0.5*log(sig2)-(res2./(2*sig2)));

if isnan(f)==1 || isinf(f)==1 
    f=100000000;
end    

else
    f=100000000;
end    