I tried to program in RATS a routine to perform non-linear Support Vector Regression (SVR). In order to obtain the fitted values for the dependent variable, the function L in the attached PDF document has to be maximized. The parameter epsilon has to be set and is the width of a range above and below the optimal fitted line for the dependent variable. The parameter C has also be set as a cost factor. k() is a kernel function. If an observation of y is within the band of plus and minus epsilon, then alpha and alpha-asterisk have to be zero.
i have written the following code
Code: Select all
*
* First steps in developing the implementation of
* Support Vector Regression in RATS
*
*
* Gaussian RBF-Kernelfunction
*
function rbfkernel x1 x2 sigma
type real sigma
type real rbfkernel
type vector[real] x1 x2
local MATRIX[REAL] d
comp d = x1-x2
comp rbfkernel = exp(%dot(d,d)*-sigma)
end
* set-up the x and y vectors
declare vector[real] x(401) y(401) d(401)
do i=1, 401
comp x(i) = -20.1+i*0.1
comp y(i) = sin(x(i))/x(i)+ %ran(0.03)
end
comp y(201) = (y(200)+y(202))/2.0
*
* set the parameter for e-insensitive, cost and sigma (Gaussian kernel)
*
comp eps = 0.025
comp cost = 3
comp sigma = 0.05
*
* Trying find to estimate the alpha vectors
*
comp nrows = %rows(x)
declare vector[real] alpha(nrows) alphaast(nrows)
declare real costf1 costf2 coatf3 costfunc
nonlin(parmset=pars) alpha alphaast
nonlin(parmset=constraints) %sum(alpha-alphaast)==0.0 alpha>=0.0 alphaast>=0.0 $
alpha<=cost alphaast<=cost
comp alpha = cost/2*%ones(nrows,1)
comp alphaast = cost/2*%ones(nrows,1)
find(parmset=pars+constraints,method=BFGS,iterations=100,cvcrit=0.1) maximum costfunc
comp costf1 = 0.0
comp costf2 = 0.0
comp costf3 = 0.0
do i=1, nrows
do j=1, nrows
comp x1 = %xrow(X,i)
comp x2 = %xrow(X,j)
comp costf1 = costf1 + (alpha(i)-alphaast(i))*(alpha(j)-alphaast(j))*rbfkernel(x1,x2,sigma)
end
comp costf2 = costf2 + (alpha(i)+alphaast(i))
comp costf3 = costf3 + y(i)*(alpha(i)-alphaast(i))
end
comp costfunc = -0.5*costf1 - eps*costf2 + costf3
disp costfunc -0.5*costf1 -eps*costf2 costf3
endUnfortunately, i do not get a solution. If I changed the method in the Find instruction then I get the error message ## M4. I have also tried to estimate the alpha vectors with quadratic programming and using either the LQPROG or the FIND instruction but had no success. What am I doing wrong? Or is it not possible to find the solution for the maximization problem? The function for the y-variable is from an example of a package in R and the fitted y-values were estimated within a second or two. I would appreciate it very much to receive an answer.
Best regards
PeterF