Polynomial discrimination

% Section 8.6.2, Boyd & Vandenberghe "Convex Optimization"
% Original by Lieven Vandenberghe
% Adapted for CVX by Joelle Skaf - 10/23/05
% (a figure is generated)
%
% The goal is to find the polynomial of degree 4 on R^n that separates
% two sets of points {x_1,...,x_N} and {y_1,...,y_N}. We are trying to find
% the coefficients of an order-4-polynomial P(x) that would satisfy:
%           minimize    t
%               s.t.    P(x_i) <= t  for i = 1,...,N
%                       P(y_i) >= t   for i = 1,...,M

% Data generation
rand('state',0);  randn('state',0);
N=100;
M=120;
X1 = -1+2*rand(2,N);  X1 = X1*diag(0.9*rand(1,N)./sqrt(sum(X1.^2)));
ind = find(sqrt(sum((X1-[1.1*ones(1,N);zeros(1,N)]).^2)) < 0.9);
Y1 = X1(:,ind);
ind = find(sqrt(sum((X1-[1.1*ones(1,N);zeros(1,N)]).^2)) > 1);
X = X1(:,ind);
Y = -1+2*rand(2,M);  Y = Y*diag((1.1+rand(1,M))./sqrt(sum(Y.^2)));
Y = [Y Y1];

N = size(X,2);
M = size(Y,2);
% "Vandermonde"-like matrix
monX = [ ones(1,N); X(1,:); X(2,:); X(1,:).^2;  X(1,:).*X(2,:); ...
         X(2,:).^2;  X(1,:).^3;  X(2,:).^2.*X(2,:); ...
         X(1,:).*X(2,:).^2;  X(2,:).^3;  X(1,:).^4; ...
         X(1,:).^3.*X(2,:); X(1,:).^2.*X(2,:).^2; X(1,:).*X(2,:).^3; ...
         X(2,:).^4 ];
monY = [ones(1,M); Y(1,:); Y(2,:); Y(1,:).^2;  Y(1,:).*Y(2,:); ...
        Y(2,:).^2;  Y(1,:).^3;  Y(2,:).^2.*Y(2,:); ...
        Y(1,:).*Y(2,:).^2;  Y(2,:).^3;  Y(1,:).^4; ...
        Y(1,:).^3.*Y(2,:); Y(1,:).^2.*Y(2,:).^2; Y(1,:).*Y(2,:).^3; ...
        Y(2,:).^4 ];

[m1,m2] = size(monX);

% Solution via CVX
fprintf(1,'Finding the optimal polynomial of order 4 that separates the 2 classes...');

cvx_begin
    variables a(m1) t(1)
    minimize ( t )
    a'*monX <= t;
    a'*monY >= -t;
cvx_end

fprintf(1,'Done! \n');

% Displaying results
nopts = 2000;
angles = linspace(0,2*pi,nopts);
cont = zeros(2,nopts);
for i=1:nopts
   v = [cos(angles(i)); sin(angles(i))];
   l = 0;  u = 1;
   while (u-l > 1e-3)
      s = (u+l)/2;
      x = s*v;
      if (a'*[1; x(1); x(2); x(1)^2; x(1)*x(2); x(2)^2; x(1)^3; ...
             x(1)^2*x(2); x(1)*x(2)^2; x(2)^3; x(1)^4; x(1)^3*x(2); ...
             x(1)^2*x(2)^2; x(1)*x(2)^3; x(2)^4]  > 0), u = s;
      else, l=s;
      end;
   end;
   cont(:,i) = s*v;
end;

graph = plot(X(1,:),X(2,:),'o', Y(1,:), Y(2,:),'o', cont(1,:), cont(2,:), '-');
set(graph(2),'MarkerFaceColor',[0 0.5 0]);
title('Optimal order-4 polynomial that separates the 2 classes')
% print -deps min-deg-discr.eps

%%%% Dual infeasible ?????
Finding the optimal polynomial of order 4 that separates the 2 classes... 
Calling SeDuMi: 227 variables (16 free), 211 equality constraints
------------------------------------------------------------------------
SeDuMi 1.1 by AdvOL, 2005 and Jos F. Sturm, 1998, 2001-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
Split 16 free variables
eqs m = 211, order n = 244, dim = 244, blocks = 1
nnz(A) = 211 + 6752, nnz(ADA) = 211, nnz(L) = 211
Handling 32 + 0 dense columns.
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            1.00E-001 0.000
  1 :  0.00E+000 3.94E-002 0.000 0.3926 0.9000 0.9000   1.98  1  1  1.7E+000
  2 :  0.00E+000 2.61E-002 0.000 0.6616 0.9000 0.9000   9.06  1  1  2.2E-001
  3 :  0.00E+000 1.37E-002 0.000 0.5260 0.9000 0.9000   5.62  1  1  8.1E-002
  4 :  0.00E+000 5.73E-003 0.000 0.4174 0.9000 0.9000   1.65  1  1  5.3E-002
  5 :  0.00E+000 2.30E-003 0.000 0.4021 0.9000 0.9000   0.60  1  1  6.3E-002
  6 :  0.00E+000 6.09E-004 0.000 0.2646 0.9000 0.9000  -0.26  1  1  1.9E-001
  7 :  0.00E+000 1.87E-005 0.000 0.0307 0.9900 0.9900  -0.77  1  1  5.3E-003
  8 :  0.00E+000 6.99E-010 0.000 0.0000 1.0000 1.0000  -0.99  1  1  
Dual infeasible, primal improving direction found.
iter seconds  |Ax|    [Ay]_+     |x|       |y|
  8      0.2  6.1e-012  3.3e-018  3.3e+003  6.6e-018

Detailed timing (sec)
   Pre          IPM          Post
3.004E-002    1.702E-001    0.000E+000    
Max-norms: ||b||=0, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.
------------------------------------------------------------------------
Status: Unbounded
Optimal value (cvx_optval): -Inf
Done!