CVXGEN: Code Generation for Convex Optimization

Example: Support vector machine (SVM)

This example, from machine learning, demonstrates the creation of a support vector machine (SVM). For some more details, see Boyd and Vandenberghe, S8.6.1.

Optimization problem

We are given observations

  • (x_i, y_i) in mathbf{R}^n times {-1,1}, for i = 1,ldots,N, and a parameter

  • lambda in mathbf{R}_+

We wish to choose two optimization variables: a weight vector

  • w in mathbf{R}^{n}, and offset

  • b in mathbf{R}

to solve the optimization problem

 begin{array}{ll} mbox{minimize} &    |w|_2^2 + lambda sum_{i=1,ldots,N}   left(     1 - y_i(w^T x_i - b)   right)_+ end{array}

CVXGEN code

dimensions
  N = 50  # observations.
  n = 10  # support vector dimension.
end

parameters
  x[i] (n), i=1..N
  y[i], i=1..N
  lambda positive
end

variables
  w (n)  # weight vector.
  b  # offset.
end

minimize
  quad(w) + lambda*sum[i=1..N](pos(1 - y[i]*(w'*x[i] - b)))
end