%%% 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, 
%%% and using this ordinary estimate of beta  to compute S0 and S1 as
%%% initial value of S(n,1) and S(n,2)
    
   X=[ones(n,1) (1:n)'];
   
   beta=inv(X'*X)*X'*z;
    
   S0=beta(1)-w/(1-w)*beta(2);
   
   S1=beta(1)-2*w/(1-w)*beta(2);

%%% Updating S from S(1,1:2) to S(n,1:2). 

   S=zeros(n,2);
   
   z_pred=zeros(n+1,1);
   
   beta_new=zeros(2,1);
   
   beta_new(1)=2*S0-S1;
       
   beta_new(2)=(1-w)/w*(S0-S1);
       
   z_pred(1)=beta_new(1)+beta_new(2);
   
   for i=1:n
       
       
       if i>1           
           
           S(i,1)=(1-w)*z(i)+w*S(i-1,1);
           
           S(i,2)=(1-w)*S(i,1)+w*S(i-1,2);
                   
       else
           
           S(i,1)=(1-w)*z(i)+w*S0;
           
           S(i,2)=(1-w)*S(i,1)+w*S1;
       end

 %%%%% Updating the estimate of beta


       beta_new(1)=2*S(i,1)-S(i,2);
       
       beta_new(2)=(1-w)/w*(S(i,1)-S(i,2));
       
 %%%%% Updating one step forecast
       
       z_pred(i+1)=beta_new(1)+beta_new(2);
       
   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 
       

   