%svmforregression.m
close all 
clear all 
%SVM for regression 
x=-3.14:0.1:3.14; 
y=sin(x)+0.1*randn(1,length(x));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
TRAINDATAx=x(1:2:length(x)); 
TRAINDATAy=y(1:2:length(y)); 
TESTDATAx=x(2:2:length(x)); 
TESTDATAy=y(2:2:length(y)); 
%Formulation of matrix to compute a’s and b’s 
sigma=0.9; 
M=[];
PART1=[]; 
PART2=[];
for i=1:1:length(TRAINDATAx)
    temp=[];
    for j=1:1:length(TRAINDATAx)
        temp=[temp gaussiankernel(TRAINDATAx(i),TRAINDATAx(j), sigma)];
    end
    temp1=temp;
    temp1(i)=0;
    PART1=[PART1;temp -1*temp1];
    PART2=[PART2;-1*temp1 temp];
end
M=[PART1;PART2];
n=size(TRAINDATAx,2);
M=[M;ones(1,n) -1*ones(1,n)];
v=[TRAINDATAy-0.01*ones(1,n) -ones(1,n)*0.01-TRAINDATAy 0];
vab=pinv(M)*v';
vab1=reshape(vab,length(vab)/2,2);
vab1=vab1';
POS=[];
for i=1:1:size(vab1,2)
    if((vab1(1,i)>0)|((vab1(2,i)>0))) 
        POS=[POS i];
    end
end
POS 
COL=vab1(:,POS);
s=0; 
for i=1:1:length(COL)
    temp=0;
    for j=1:1:length(COL)
        temp=temp+(COL(1,j)-COL(2,j))*gaussiankernel(TRAINDATAx(POS(j)),TRAINDATAx(POS(i)),sigma); 
    end
    s=s+(TRAINDATAy(POS(i))-temp);
end
s=s/(length(COL));
ESTIMATEy=[];
for i=1:1:length(TESTDATAx)
    temp=0;
    for j=1:1:length(COL)
        temp=temp+(COL(1,j)-COL(2,j))*gaussiankernel(TRAINDATAx(POS(j)),TESTDATAx(i),sigma);
    end
    ESTIMATEy=[ESTIMATEy temp+s]; 
end
figure
plot(TESTDATAx,ESTIMATEy,'r')
hold on 
plot(TESTDATAx,TESTDATAy,'b')
