%% INITIALIZATION
clc
load('count_data.mat')
flag=2;

RM=input('Please ENTER the Regression Method \n [1-LS, 2-RLS, 3-LASSO, 4-RR, 5-BR]: ');
    if (RM<1 || RM>5)
        msgbox('Invalid Selection')
        return
    end
  
x=trainx;
y=trainy;
    
Transform =input('Apply Feature Transformation? [ 0=No Transform 1=2nd Order 2=CrossTerms ]: ');
if (Transform == 2)
    phy=[x;x.^2;x(1,:).*x(2,:);x(2,:).*x(3,:);x(3,:).*x(4,:);x(4,:).*x(5,:);x(5,:).*x(6,:);x(6,:).*x(7,:);x(7,:).*x(8,:);x(8,:).*x(9,:)];
    polyx=[testx;testx.^2;testx(1,:).*testx(2,:);testx(2,:).*testx(3,:);testx(3,:).*testx(4,:);testx(4,:).*testx(5,:);testx(5,:).*testx(6,:);testx(6,:).*testx(7,:);testx(7,:).*testx(8,:);testx(8,:).*testx(9,:)];
elseif (Transform == 1)
    phy=[x;x.^2;];
    polyx=[testx;testx.^2];
elseif (Transform == 0)
    phy=x;
    polyx=testx;
end


k=size(phy,1);
polyy=testy;

phi=zeros(k+1,length(x));
I=eye(k);
lambda=1;
noise = 5;
a=10;







%% PREDICTION
if RM==1
    Method = 'Least Squares [LS]';
    func=funcLS( phy,phi,[y],k,polyx,flag );
elseif RM==2
    Method = 'Regularized LS [RLS]';
    func=funcRLS( phy,phi,y,k,polyx,I,lambda,flag);
elseif RM==3
    Method = 'LASSO';
    func=funcLASSO( phy,phi,y,k,polyx,lambda,flag);   
elseif RM==4
    Method = 'Robust Regression [RR]';
    func=funcRR( phy,phi,x,y,k,polyx,flag );
elseif RM==5
    Method = 'Bayesian Regression [BR]';
    func=funcBR( phy,phi,y,k,polyx,noise,a,flag );
else
    msgbox('Invalid Input')
    return
end


%% ERROR CALULATION
SumMAE=0;
SumMSE=0;
for i=1:length(polyy)
    SumMAE=SumMAE+abs((polyy(i)-func(i)));
    SumMSE=SumMSE+(polyy(i)-func(i))^2;
end
MAE = (1/length(polyy))*SumMAE;
MSE = (1/length(polyy))*SumMSE;

%% PLOTS
plot(func,'-k'),hold on
plot(polyy,':b')
legend({'Sample Data','Predicted Function','Actual Function'})
title([Method '  |  MAE: ' num2str(MAE) '  |  MSE: ' num2str(MSE)])


