%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Example: Quarterly Iowa Nonfarm Income
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%




%%%% Import Iowa_Income data to matlab

load Iowa_Income.mat;

%%%%  Compute the sample size of data

n=length(Iowa_Income);

%%%%  Compute quarterly growth rates, change data to z_t

z=100*(Iowa_Income(2:n)-Iowa_Income(1:n-1))./Iowa_Income(1:n-1);

%%%%  Compute the auto-correlation of z_t from lag=1 to 6

for i=1:6
    r(i)=auto_corr(z,i);
end

%%% Show the autocorrelation value on screen 

[1:6; r]

%%%% Compute SSE from alpha=0.01 to 0.40

alpha=0.01:0.01:0.40;

m=length(alpha);

sse=zeros(m,1);

r_error=zeros(m,6);

error_mean=zeros(m,1);

error_std=zeros(m,1);

% sse: SSE(alpha), 
% t_error: auto_correlation of one step forecast error,
% error_mean: mean of forecast error. 
% error_std: standard deviation of the estimate of the mean of forecast error

for i=1:m
   
    [sse(i), r_error(i,:), error_mean(i), error_std(i)]=SSE(z,alpha(i));
   
end

% show the value of SSE on screen according to the order of alpha 

[alpha' sse]


%%% Plot a figure to show SSE with alpha, and select alpha which minimize
%%% SSE. 

plot(alpha,sse);

title('Plot of SSE, for simple expontial smoothing---growth rates of Iowa nonfarm income');

xlabel('\alpha');

ylabel('SSE(\alpha)');

% show the  autocorrelation value of residuals when alpha=0.11 and 0.40

[(1:6)' r_error(11,:)' r_error(40,:)']

% show the  mean and standard deviation of residuals when alpha=0.11 and 0.40

[error_mean(11)  error_mean(40);  error_std(11) error_std(40)]
