% Boyd & Vandenberghe "Convex Optimization"
% Joelle Skaf - 10/09/05
%
% Let C be a correlation matrix. Given lower and upper bounds on
% some of the angles (or correlation coeff.), find the maximum and minimum
% possible values of rho_14 by solving 2 SDP's
%           minimize/maximize   rho_14
%                        s.t.   C >=0
%                               0.6 <= rho_12 <=  0.9
%                               0.8 <= rho_13 <=  0.9
%                               0.5 <= rho_24 <=  0.7
%                              -0.8 <= rho_34 <= -0.4

n = 4;

% Upper bound SDP
fprintf(1,'Solving the upper bound SDP ...');

cvx_begin sdp
    variable C1(n,n) symmetric
    maximize ( C1(1,4) )
    C1 >= 0;
    diag(C1) == ones(n,1);
    C1(1,2) >= 0.6;
    C1(1,2) <= 0.9;
    C1(1,3) >= 0.8;
    C1(1,3) <= 0.9;
    C1(2,4) >= 0.5;
    C1(2,4) <= 0.7;
    C1(3,4) >= -0.8;
    C1(3,4) <= -0.4;
cvx_end

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

% Lower bound SDP
fprintf(1,'Solving the lower bound SDP ...');

cvx_begin sdp
    variable C2(n,n) symmetric
    minimize ( C2(1,4) )
    C2 >= 0;
    diag(C2) == ones(n,1);
    C2(1,2) >= 0.6;
    C2(1,2) <= 0.9;
    C2(1,3) >= 0.8;
    C2(1,3) <= 0.9;
    C2(2,4) >= 0.5;
    C2(2,4) <= 0.7;
    C2(3,4) >= -0.8;
    C2(3,4) <= -0.4;
cvx_end

fprintf(1,'Done! \n');
% Displaying results
disp('--------------------------------------------------------------------------------');
disp(['The minimum and maximum values of rho_14 are: ' num2str(C2(1,4)) ' and ' num2str(C1(1,4))]);
disp('with corresponding correlation matrices: ');
disp(C2)
disp(C1)
Solving the upper bound SDP ... 
Calling sedumi: 18 variables, 12 equality constraints
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 12, order n = 13, dim = 25, blocks = 2
nnz(A) = 20 + 0, nnz(ADA) = 144, nnz(L) = 78
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            1.49E+00 0.000
  1 :  -2.96E-01 4.40E-01 0.000 0.2954 0.9000 0.9000   1.63  1  1  1.2E+00
  2 :  -4.38E-01 1.20E-01 0.000 0.2726 0.9000 0.9000   1.48  1  1  3.0E-01
  3 :  -2.52E-01 9.66E-03 0.000 0.0806 0.9900 0.9900   1.55  1  1  2.4E-02
  4 :  -2.31E-01 5.64E-04 0.355 0.0584 0.9900 0.9900   1.08  1  1  1.4E-03
  5 :  -2.30E-01 2.30E-05 0.000 0.0408 0.9900 0.9900   1.00  1  1  5.7E-05
  6 :  -2.30E-01 1.31E-07 0.325 0.0057 0.9853 0.9990   1.00  1  1  5.0E-07
  7 :  -2.30E-01 8.10E-09 0.000 0.0617 0.9900 0.9629   1.00  1  1  3.1E-08
  8 :  -2.30E-01 2.90E-10 0.389 0.0358 0.9900 0.9900   1.00  2  2  1.1E-09

iter seconds digits       c*x               b*y
  8      0.0   Inf -2.2990908487e-01 -2.2990908453e-01
|Ax-b| =   1.7e-09, [Ay-c]_+ =   2.4E-10, |x|=  2.8e+00, |y|=  2.3e+00

Detailed timing (sec)
   Pre          IPM          Post
1.000E-02    3.000E-02    0.000E+00    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1159.48.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.229909
Done! 
Solving the lower bound SDP ... 
Calling sedumi: 18 variables, 12 equality constraints
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 12, order n = 13, dim = 25, blocks = 2
nnz(A) = 20 + 0, nnz(ADA) = 144, nnz(L) = 78
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            1.49E+00 0.000
  1 :  -3.13E-01 4.52E-01 0.000 0.3033 0.9000 0.9000   1.63  1  1  1.2E+00
  2 :  -5.41E-01 1.28E-01 0.000 0.2843 0.9000 0.9000   1.52  1  1  3.1E-01
  3 :  -4.09E-01 1.21E-02 0.000 0.0943 0.9900 0.9900   1.62  1  1  2.3E-02
  4 :  -3.93E-01 2.43E-04 0.000 0.0201 0.9900 0.9900   1.11  1  1  4.4E-04
  5 :  -3.93E-01 1.26E-05 0.145 0.0520 0.9900 0.9900   1.00  1  1  2.3E-05
  6 :  -3.93E-01 1.76E-07 0.000 0.0139 0.9574 0.9900   1.00  1  1  5.8E-07
  7 :  -3.93E-01 6.56E-09 0.030 0.0373 0.9900 0.9900   1.00  1  1  2.2E-08
  8 :  -3.93E-01 1.29E-09 0.061 0.1973 0.9000 0.9000   1.00  2  2  4.3E-09

iter seconds digits       c*x               b*y
  8      0.0   Inf -3.9282032728e-01 -3.9282032607e-01
|Ax-b| =   6.6e-09, [Ay-c]_+ =   9.6E-10, |x|=  2.8e+00, |y|=  1.8e+00

Detailed timing (sec)
   Pre          IPM          Post
0.000E+00    3.000E-02    1.000E-02    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 2421.04.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -0.39282
Done! 
--------------------------------------------------------------------------------
The minimum and maximum values of rho_14 are: -0.39282 and 0.22991
with corresponding correlation matrices: 
    1.0000    0.6000    0.8486   -0.3928
    0.6000    1.0000    0.2940    0.5000
    0.8486    0.2940    1.0000   -0.5807
   -0.3928    0.5000   -0.5807    1.0000

    1.0000    0.7291    0.8000    0.2299
    0.7291    1.0000    0.3202    0.5943
    0.8000    0.3202    1.0000   -0.4000
    0.2299    0.5943   -0.4000    1.0000