%% INITIALIZATION
clc
load('poly_data.mat')
flag=1;

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
Sample=input('Please ENTER the Subset Sample Size in %: ');
    if Sample>100
        Sample=100;
    elseif Sample<2
        msgbox('Invalid Input')
        return
    end
k=input('Please ENTER the Order of polynomial: ');
k=k+1;
    if k<2
        msgbox('Invalid Input')
        return
    end

[R,idx] = datasample(sampx,round((Sample/100)*length(sampx)),'Replace',false);
x=sampx(idx);
y=sampy(idx);

outliers =input('Check with Outliers? [Y=1 N=0]: ');
if (outliers == 1)
    [R2,idx2] = datasample(sampx,round((6/100)*length(sampx)),'Replace',false);
    y(idx2) = y(idx2) + 50;
end

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

for i=1:length(x)
    for j=1:k
        phy(j,i)=x(i)^(j-1);
    end
end
%% 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,SD]=funcBR( phy,phi,y,k,polyx,noise,a,flag );
else
    msgbox('Invalid Input')
    return
end

%% ERROR CALULATION
Sum=0;
for i=1:length(polyy)
    Sum=Sum+(polyy(i)-func(i))^2;
end

Error = (1/length(polyy))*Sum;

%% PLOTS
plot(x,y,'xr'),hold on
plot(polyx,func,'-k'),hold on
plot(polyx,polyy,':b')
if RM==5
    %errorbar(polyx,func,SD)
end
legend({'Sample Data','Predicted Function','Actual Function'})
title([Method '  |  ' num2str(k-1) 'th Order  |  Sample Size: '  num2str(Sample) '%  |  Error: ' num2str(Error)])
addpath E:\Sabeeh_code\fwdcode_v_2\fwdcode

