clear;

nmax = 160; % data length
nwin = 120; % length of window
ntest = 10; % length of out-of-sample forecast evaluation

[num,varname] = xlsread('macrodata.xls');
data = num(1:nmax,:);

usd = log(data(1:nmax-12,18)./data(13:nmax,18))*100;
rmbx = log(data(1:nmax-12,19)./data(13:nmax,19))*100;
pmi = data(:,23)-50;
pmino = data(:,24)-50;

yxvar = [(nmax-12:-1:1)' data(1:nmax-12,4) data(1:nmax-12,1:3) data(1:nmax-12,5:17) usd rmbx data(1:nmax-12,20:22) pmi(1:nmax-12) pmino(1:nmax-12)];
yxvar = sortrows(yxvar,1);
y = yxvar(2:nmax-12,2);
x = yxvar(1:nmax-12-1,2:size(yxvar,2));
n = length(y);

oserr = zeros(ntest,3);
for j = 1:ntest
    index = (n-nwin-ntest+j:n-ntest+j-1)';
    yhat_lasso = predlasso(y(index),x(index,:),x(n-ntest+j,:)',1);%lasso
    yhat_el = predlasso(y(index),x(index,:),x(n-ntest+j,:)',0.75);%elastic net
    yhat_ar1 = predar1(y(index),x(index,1),[1 x(n-ntest+j,1)]');
    oserr(j,1) = yhat_lasso - y(n-ntest+j);
    oserr(j,2) = yhat_el - y(n-ntest+j);
    oserr(j,3) = yhat_ar1 - y(n-ntest+j);
end

disp(['     Lasso    ElasticNet   AR(1)'])
disp(oserr);
disp('Mean Abs Error')
disp(['     Lasso    ElasticNet   AR(1)'])
disp(mean(abs(oserr)));


