Exercise 5.19c: Markovitz portfolio optimization w/ diversification constraint

% Boyd & Vandenberghe, "Convex Optimization"
% Joëlle Skaf - 08/29/05
%
% Solves an extension of the classical Markovitz portfolio optimization
% problem:      minimize    x'Sx
%                   s.t.    p_'*x >= r_min
%                           1'*x = 1,   x >= 0
%                           sum_{i=1}^{0.1*n}x[i] <= alpha
% where p_ and S are the mean and covariance matrix of the price range
% vector p, x[i] is the ith greatest component in x.
% The last constraint can be replaced by this equivalent set of constraints
%                           r*t + sum(u) <= alpha
%                           t*1 + u >= x
%                           u >= 0

% Input data
randn('state',0);
n = 25;
p_mean = randn(n,1);
temp = randn(n);
sig = temp'*temp;
r = floor(0.1*n);
alpha = 0.8;
r_min = 1;

% original formulation
fprintf(1,'Computing the optimal Markovitz portfolio: \n');
fprintf(1,'# using the original formulation ... ');

cvx_begin
    variable x1(n)
    minimize ( quad_form(x1,sig) )
    p_mean'*x1 >= r_min;
    ones(1,n)*x1 == 1;
    x1 >= 0;
    sum_largest(x1,r) <= alpha;
cvx_end

fprintf(1,'Done! \n');
opt1 = cvx_optval;

% equivalent formulation
fprintf(1,'# using an equivalent formulation by replacing the diversification\n');
fprintf(1,'  constraint by an equivalent set of linear constraints...');

cvx_begin
    variables x2(n) u(n) t(1)
    minimize ( quad_form(x2,sig) )
    p_mean'*x2 >= r_min;
    sum(x2) == 1;
    x2 >= 0;
    r*t + sum(u) <= alpha;
    t*ones(n,1) + u >= x2;
    u >= 0;
cvx_end

fprintf(1,'Done! \n');
opt2 = cvx_optval;

% Displaying results
disp('------------------------------------------------------------------------');
disp('The optimal portfolios obtained from the original problem formulation and');
disp('from the equivalent formulation are respectively: ');
disp([x1 x2])
disp('They are equal as expected!');
Computing the optimal Markovitz portfolio: 
# using the original formulation ...  
Calling SeDuMi: 104 variables (1 free), 54 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 = 54, order n = 81, dim = 106, blocks = 2
nnz(A) = 555 + 0, nnz(ADA) = 2212, nnz(L) = 1133
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            1.04E+001 0.000
  1 : -7.01E-001 3.91E+000 0.000 0.3765 0.9000 0.9000   2.34  1  1  1.1E+001
  2 : -1.96E-003 2.05E+000 0.000 0.5250 0.9000 0.9000   3.37  1  1  2.9E+000
  3 :  2.64E-001 9.18E-001 0.000 0.4468 0.9000 0.9000   1.52  1  1  1.1E+000
  4 :  3.95E-001 5.34E-001 0.000 0.5822 0.9000 0.9000   1.00  1  1  6.7E-001
  5 :  5.36E-001 2.76E-001 0.000 0.5167 0.9000 0.9000   1.10  1  1  3.4E-001
  6 :  6.11E-001 1.55E-001 0.000 0.5627 0.9000 0.9000   0.89  1  1  2.0E-001
  7 :  7.06E-001 4.61E-002 0.000 0.2968 0.9000 0.9000   1.14  1  1  5.5E-002
  8 :  7.37E-001 1.35E-002 0.000 0.2922 0.9000 0.9000   0.95  1  1  1.7E-002
  9 :  7.50E-001 8.62E-005 0.000 0.0064 0.9990 0.9990   1.02  1  1  1.1E-004
 10 :  7.50E-001 1.80E-005 0.000 0.2089 0.9000 0.8890   1.00  1  1  2.4E-005
 11 :  7.50E-001 3.79E-006 0.000 0.2108 0.9033 0.9000   1.00  1  1  4.7E-006
 12 :  7.50E-001 8.49E-007 0.000 0.2237 0.9098 0.9000   1.00  1  1  8.4E-007
 13 :  7.50E-001 2.45E-007 0.232 0.2881 0.9140 0.9000   1.00  1  1  1.9E-007
 14 :  7.50E-001 7.65E-008 0.133 0.3129 0.9162 0.9000   1.00  1  1  5.0E-008
 15 :  7.50E-001 1.25E-008 0.000 0.1627 0.9000 0.9049   1.00  2  2  8.7E-009

iter seconds digits       c*x               b*y
 15      0.1   7.9  7.5016966062e-001  7.5016965005e-001
|Ax-b| =  7.4e-009, [Ay-c]_+ =  3.7E-010, |x|= 1.7e+000, |y|= 3.8e+000

Detailed timing (sec)
   Pre          IPM          Post
2.003E-002    1.001E-001    0.000E+000    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 188.554.
------------------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.75017
Done! 
# using an equivalent formulation by replacing the diversification
  constraint by an equivalent set of linear constraints... 
Calling SeDuMi: 104 variables (1 free), 54 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 = 54, order n = 81, dim = 106, blocks = 2
nnz(A) = 580 + 0, nnz(ADA) = 2268, nnz(L) = 1161
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            3.47E+001 0.000
  1 : -7.01E-001 1.31E+001 0.000 0.3765 0.9000 0.9000   2.34  1  1  2.1E+001
  2 : -1.96E-003 6.86E+000 0.000 0.5250 0.9000 0.9000   3.37  1  1  5.3E+000
  3 :  2.64E-001 3.06E+000 0.000 0.4468 0.9000 0.9000   1.52  1  1  2.0E+000
  4 :  3.95E-001 1.78E+000 0.000 0.5822 0.9000 0.9000   1.00  1  1  1.3E+000
  5 :  5.36E-001 9.22E-001 0.000 0.5167 0.9000 0.9000   1.10  1  1  6.4E-001
  6 :  6.11E-001 5.19E-001 0.000 0.5627 0.9000 0.9000   0.89  1  1  3.9E-001
  7 :  7.06E-001 1.54E-001 0.000 0.2968 0.9000 0.9000   1.14  1  1  1.1E-001
  8 :  7.37E-001 4.50E-002 0.000 0.2922 0.9000 0.9000   0.95  1  1  3.2E-002
  9 :  7.50E-001 2.88E-004 0.000 0.0064 0.9990 0.9990   1.02  1  1  2.0E-004
 10 :  7.50E-001 6.81E-005 0.000 0.2366 0.9000 0.8890   1.00  1  1  4.7E-005
 11 :  7.50E-001 1.31E-005 0.000 0.1924 0.9033 0.9000   1.00  1  1  9.1E-006
 12 :  7.50E-001 2.15E-006 0.000 0.1644 0.9098 0.9000   1.00  1  1  1.5E-006
 13 :  7.50E-001 4.64E-007 0.231 0.2153 0.9140 0.9000   1.00  2  1  3.1E-007
 14 :  7.50E-001 1.15E-007 0.132 0.2471 0.9161 0.9000   1.00  2  2  7.2E-008
 15 :  7.50E-001 2.04E-008 0.000 0.1780 0.9000 0.9049   1.00  2  2  1.3E-008

iter seconds digits       c*x               b*y
 15      0.1   7.9  7.5016966061e-001  7.5016965007e-001
|Ax-b| =  1.1e-008, [Ay-c]_+ =  3.7E-010, |x|= 1.7e+000, |y|= 3.8e+000

Detailed timing (sec)
   Pre          IPM          Post
0.000E+000    1.102E-001    0.000E+000    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 221.691.
------------------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.75017
Done! 
------------------------------------------------------------------------
The optimal portfolios obtained from the original problem formulation and
from the equivalent formulation are respectively: 
    0.0000    0.0000
    0.0000    0.0000
    0.1342    0.1342
    0.0000    0.0000
    0.0000    0.0000
    0.1177    0.1177
    0.1134    0.1134
    0.0123    0.0123
    0.0904    0.0904
    0.0256    0.0256
    0.0451    0.0451
    0.0437    0.0437
    0.0000    0.0000
    0.1435    0.1435
    0.0000    0.0000
    0.0086    0.0086
    0.1177    0.1177
    0.0000    0.0000
    0.0000    0.0000
    0.0000    0.0000
    0.0000    0.0000
    0.0000    0.0000
    0.0313    0.0313
    0.1164    0.1164
   -0.0000   -0.0000

They are equal as expected!