%Regression using gaussian kernel method 
W1=rand(3,2);
B1=rand(2,1); 
W2=rand(2,1); 
B2=rand(1,1);
%DATA
N=1000;
INPUT=randn(N,3)*2-1; 
H=INPUT*W1+repmat(B1',N,1);
H=1./(1+exp(H));
OUTPUT=H*W2+repmat(B2',N,1);
OUTPUT=1./(1+exp(OUTPUT));
INPUTTRAIN=INPUT(1:1:400,:);
INPUTVAL=INPUT(401:1:700,:);
INPUTTEST=INPUT(701:1:1000,:); 
OUTPUTTRAIN=OUTPUT(1:1:400,:)+randn(400,1)*0.02;
OUTPUTVAL=OUTPUT(401:1:700,:);
OUTPUTTEST=OUTPUT(701:1:1000,:); 
%Formulation of smoother matrix 
s=[0.1:0.1:5];
OUTPUTest=[];
MSE=[];
temp=0; 
KERNELVAL=[];
for k=1:1:length(s)
    OUTPUTVALest=[];
    for i=1:1:size(INPUTVAL,1) 
        temp=[];
        KERNELVAL=[];
        for j=1:1:size(INPUTTRAIN,1) 
            KERNELVAL=[KERNELVAL gbf(INPUTTRAIN(j,:), INPUTVAL(i,:),s(k))];
            KERNELVAL=KERNELVAL/sum(KERNELVAL);
        end
        temp=sum(KERNELVAL*OUTPUTTRAIN(:,1));
        OUTPUTVALest=[OUTPUTVALest temp];
    end
    MSE=[MSE sum(sum((OUTPUTVAL-OUTPUTVALest).^2))/ length(OUTPUTVAL)];
end
[P,Q]=min(MSE); 
ssel=s(Q);
OUTPUTTESTest=[]; 
MSETEST=[];
for i=1:1:size(INPUTTEST,1)
    temp=[];
    KERNELTEST=[];
    for j=1:1:size(INPUTTRAIN,1)
        KERNELTEST=[KERNELTEST gbf(INPUTTRAIN(j,:), INPUTTEST(i,:),ssel)];
        KERNELTEST=KERNELTEST/sum(KERNELTEST);
    end
    temp=sum(KERNELTEST*OUTPUTTRAIN(:,1));
    OUTPUTTESTest=[OUTPUTTESTest temp];
end
MSETEST=sum(sum((OUTPUTTEST'-OUTPUTTESTest).^2))/ length(OUTPUTTEST);
figure 
subplot(2,1,1) 
plot(OUTPUTTEST,'b')
subplot(2,1,2)
plot(OUTPUTTESTest,'r')
