%%% Compute SSE for alpha


function [sse, r, error_mean,error_std]=SSE1(data, alpha)


%%% Change alpha to w

   w=1-alpha;
   
   z=data;
   
%%% Compute sample size of data

   n=length(z);
   
%%% Compute the ordinary least estimate of beta, Let it as an initial estimate of beta_0 
%%% 
    
 X=[ones(n,1) (1:n)'];

t=(1:n)';

for i=1:2
    
  X=[X sin(2*pi*i/12*t) cos(2*pi*i/12*t)];
 
end

beta_int=inv(X'*X)*X'*z;

%%%% Construct Matrix L and f(0)

L=[1 0 0 0 0 0; 1 1 0 0 0 0; 0 0 sqrt(3)/2 1/2 0 0; 0 0 -1/2 sqrt(3)/2 0 0; 0 0 0 0 1/2 sqrt(3)/2; 0 0 0 0 -sqrt(3)/2 1/2];

f_0=[1 0 0 1 0 1]';

%%%% Compute Matrix F by computer approximation method

t=(-10000:1:0)';

F=zeros(6,6);

for i=1:length(t)
    
ff=w^(-t(i))*[1 t(i) sin(2*pi/12*t(i)) cos(2*pi/12*t(i)) sin(4*pi/12*t(i))  cos(4*pi/12*t(i))]'*[1 t(i) sin(2*pi/12*t(i)) cos(2*pi/12*t(i)) sin(4*pi/12*t(i)) cos(4*pi/12*t(i))];
 
F=F+ff;

end

%%%% Update estimate of beta and compete one step forecast estimate
    
beta_old=beta_int;
   
f_1=[1 1 sin(2*pi/12) cos(2*pi/12) sin(4*pi/12) cos(4*pi/12)];


z_pred=zeros(n+1,1);

z_pred(1)=f_1*beta_int;

for i=1:n              
       
 %%%%% Updating the estimate of beta

       beta(:,i)=L'*beta_old+inv(F)*f_0*(z(i)-z_pred(i));
              
 %%%%% Updating one step forecast
       
       z_pred(i+1)=f_1*beta(:,i);
       
       beta_old=beta(:,i);
       
 end
   
 forecast_error=zeros(n,1);
   
  %%% Compute forecast error 
  
 forecast_error(1:n)=z(1:n)-z_pred(1:n);
   
  %%% Compute SSE
  
 sse=sum(forecast_error.^2);
  
  %%% Compute mean of forecast error
  
 error_mean=mean(forecast_error);
   
  %%% Compute standard deviation of the estimate of mean of forecast error
  
 error_std=std(forecast_error)/sqrt(n);
   
  %%% Compute auto-correlation of the forecast error from lag=1 to lag=6
   
   r=zeros(1,6);
   
   for i=1:6 
   
        r(i)=auto_corr(forecast_error,i);
        
   end 
       

   