Example 7.4: Binary hypothesis testing

% Figure 7.4
% Boyd & Vandenberghe "Convex Optimization"
% Original version by Lieven Vandenberghe
% Updated for CVX by Michael Grant, 2005-12-19

% Generate the data
P = [0.70  0.10
     0.20  0.10
     0.05  0.70
     0.05  0.10];
[n,m] = size(P);

% Construct the tradeoff curve by finding the
% the Pareto optimal deterministic detectors,
% which are the curve's vertices

nopts   = 1000;
weights = logspace(-5,5,nopts);
obj     = [0;1];
inds    = ones(n,1);

% minimize  -t1'*q1 - w*t2'*q2
% s.t.      t1+t2 = 1,  t1,t2 \geq 0

next = 2;
for i = 1 : nopts,
   PW = P * diag( [ 1 ; weights(i) ] );
   [ maxvals, maxinds ] = max( PW' );  % max elt in each row
   if (~isequal(maxinds', inds(:,next-1)))
       inds(:,next) = maxinds';
       T = zeros(m,n);
       for j=1:n
          T(maxinds(1,j),j) = 1;
       end;
       obj(:,next) = 1-diag(T*P);
       next = next+1;
   end;
end;
plot(obj(1,:), obj(2,:),[0 1], [0 1],'--');
grid on
for i=2:size(obj,2)-1
   text(obj(1,i),obj(2,i),['a', num2str(i-1)]);
end;

% Minimax detector: not deterministic

cvx_begin
    variables T( m, n ) D( m, m )
    minimize max( D(1,2), D(2,1) )
    subject to
        D == T * P;
        sum( T, 1 ) == 1;
        T >= 0;
cvx_end

objmp = 1 - diag( D );
text( objmp(1), objmp(2), 'b' );
xlabel('P_{fp}'); ylabel('P_{fn}');

%print -deps roc.eps
 
Calling SeDuMi: 11 variables (1 free), 6 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 1 free variables
eqs m = 6, order n = 13, dim = 13, blocks = 1
nnz(A) = 22 + 0, nnz(ADA) = 24, nnz(L) = 15
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            2.33E+000 0.000
  1 :  3.77E-001 5.63E-001 0.000 0.2420 0.9000 0.9000   3.01  1  1  6.9E-001
  2 :  2.04E-001 1.17E-001 0.000 0.2070 0.9000 0.9000   1.27  1  1  1.3E-001
  3 :  1.77E-001 2.70E-002 0.000 0.2315 0.9000 0.9000   1.05  1  1  3.0E-002
  4 :  1.68E-001 2.25E-003 0.000 0.0835 0.9900 0.9900   1.01  1  1  2.5E-003
  5 :  1.67E-001 8.62E-007 0.000 0.0004 0.9999 0.9999   1.00  1  1  
iter seconds digits       c*x               b*y
  5      0.1  15.5  1.6666666667e-001  1.6666666667e-001
|Ax-b| =  3.6e-016, [Ay-c]_+ =  1.6E-016, |x|= 2.2e+000, |y|= 8.7e-001

Detailed timing (sec)
   Pre          IPM          Post
1.001E-002    7.010E-002    0.000E+000    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.
------------------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.166667