Implementation of the 2nu-SVM

Instructions for modifying LIBSVM to implement the 2nu-SVM

The 2nu-SVM is a cost-sensitive version of the nu-SVM. You can learn more about the 2nu-SVM and it's applications here. The 2nu-SVM is not currently supported in the LIBSVM package, but it is easy to modify the source code (you can download a premodified version based on LIBSVM 2.8 here.)

You will need to edit the following sections of code:

In svm-train.c :
If you want to do cross-validation and you care about the false alarm / miss rates, you should change

for(i=0;i < prob.l;i++)
   if(target[i] == prob.y[i])
      ++total_correct;
printf("Cross Validation Accuracy = %g%%\n",100.0*total_correct/prob.l);

to

int countp = 0;
int countn = 0;
int correctp = 0;
int correctn = 0;

for(i=0;i < prob.l;i++)
{
   if(prob.y[i] > 0.0)
   {
      countp++;
    if(target[i] == prob.y[i])
       correctp++;
   }
   else
   {
      countn++;
    if(target[i] == prob.y[i])
       correctn++;
   }
}
printf("Positive Cross Validation Accuracy = %g%%\n",100.0*correctp/countp)
printf("Negative Cross Validation Accuracy = %g%%\n",100.0*correctn/countn)

In svm.cpp : First change

static void solve_nu_svc(
   const svm_problem *prob, const svm_parameter *param,
   double *alpha, Solver::SolutionInfo* si)

to

static void solve_nu_svc(
   const svm_problem *prob, const svm_parameter *param,
   double *alpha, Solver::SolutionInfo* si, double Cp, double Cn)

Next, in solve_nu_svc, change

alpha[i] = min(1.0,sum_pos);

to

alpha[i] = min(Cp,sum_pos);

and similarly, change

alpha[i] = min(1.0,sum_neg);

to

alpha[i] = min(Cn,sum_neg);

Finally, still in solve_nu_svc, change

s.Solve(l, SVC_Q(*prob,*param,y), zeros, y,
   alpha, 1.0, 1.0, param->eps, si,  param->shrinking);

to

s.Solve(l, SVC_Q(*prob,*param,y), zeros, y,
   alpha, Cp, Cn, param->eps, si,  param->shrinking);

Next, in svm_train_one, change

solve_nu_svc(prob,param,alpha,&si);

to

solve_nu_svc(prob,param,alpha,&si,Cp,Cn);

Finally, you may wish to disable some of the tests in svm_check_parameter. The standard checks implemented here will return an error for some parameter settings that would yield an infeasible problem in the standard nu-SVM but would not yield an infeasible problem in the 2nu-SVM. For detailed information on the feasible range for the parameters of the 2nu-SVM, see the papers listed here.

Note that these changes are backwards-compatible - they do not affect the functioning of LIBSVM for the original nu-SVM.