The source files for all examples can be found in /examples.

Closest Correlation Matrix

We consider the problem of finding the closest correlation matrix $X$ to a given random matrix $C$. With closest correlation matrix we mean a positive semidefinite matrix with ones on the diagonal. The problem is given by:

\[\begin{array}{ll} \text{minimize} & \frac{1}{2}||X - C||_F^2\\ \text{subject to} & X_{ii} = 1, \quad i=1,\dots,n \\ & X \succeq 0. \end{array}\]

Notice that we use JuMP to model the problem. COSMO is chosen as the backend solver. And COSMO-specific settings are passed using the optimizer_with_attributes() function.

using COSMO, JuMP, LinearAlgebra, SparseArrays, Test, Random
rng = Random.MersenneTwister(12345);
# create a random test matrix C
n = 8;
C = -1 .+ rand(rng, n, n) .* 2;
c = vec(C);

Define problem in JuMP:

q = -vec(C);
r = 0.5 * vec(C)' * vec(C);
m = JuMP.Model(optimizer_with_attributes(COSMO.Optimizer, "verbose" => true));
@variable(m, X[1:n, 1:n], PSD);
x = vec(X);
@objective(m, Min, 0.5 * x' * x  + q' * x + r);
for i = 1:n
  @constraint(m, X[i, i] == 1.);
end

Solve the JuMP model with COSMO and query the solution X_sol:

status = JuMP.optimize!(m);
obj_val = JuMP.objective_value(m);
X_sol = JuMP.value.(X);
------------------------------------------------------------------
          COSMO v0.8.11 - A Quadratic Objective Conic Solver
                         Michael Garstka
                University of Oxford, 2017 - 2022
------------------------------------------------------------------

Problem:  x ∈ R^{36},
          constraints: A ∈ R^{44x36} (44 nnz),
          matrix size to factor: 80x80,
          Floating-point precision: Float64
Sets:     DensePsdConeTriangle of dim: 36 (8x8)
          ZeroSet of dim: 8
Settings: ϵ_abs = 1.0e-05, ϵ_rel = 1.0e-05,
          ϵ_prim_inf = 1.0e-04, ϵ_dual_inf = 1.0e-04,
          ρ = 0.1, σ = 1e-06, α = 1.6,
          max_iter = 5000,
          scaling iter = 10 (on),
          check termination every 25 iter,
          check infeasibility every 40 iter,
          KKT system solver: QDLDL
Acc:      Anderson Type2{QRDecomp},
          Memory size = 15, RestartedMemory,
          Safeguarded: true, tol: 2.0
Setup Time: 828.34ms

Iter:	Objective:	Primal Res:	Dual Res:	Rho:
1	 5.7298e+00	5.9277e-01	7.1084e-01	1.0000e-01
25	-3.9953e-01	4.6201e-06	2.1964e-06	1.0000e-01

------------------------------------------------------------------
>>> Results
Status: Solved
Iterations: 25
Optimal objective: -0.3995
Runtime: 4.235s (4234.94ms)

Double check result against known solution:

known_opt_val = 8.75427
known_solution =  [1.0         0.354428    0.238389    0.323015  -0.117854    0.620975    0.0969092   0.231657
  0.354428    1.0        -0.590998   -0.172179  -0.571414    0.232656    0.109805    0.0229169
  0.238389   -0.590998    1.0         0.379909   0.0205233  -0.237496    0.0431976  -0.0364916
  0.323015   -0.172179    0.379909    1.0       -0.250821    0.349762    0.13669     0.49255
 -0.117854   -0.571414    0.0205233  -0.250821   1.0         0.145922   -0.0418897   0.0604382
  0.620975    0.232656   -0.237496    0.349762   0.145922    1.0         0.457861    0.0215352
  0.0969092   0.109805    0.0431976   0.13669   -0.0418897   0.457861    1.0        -0.626215
  0.231657    0.0229169  -0.0364916   0.49255    0.0604382   0.0215352  -0.626215    1.0   ];

@test isapprox(obj_val, known_opt_val , atol=1e-3)
Test Passed
@test norm(X_sol - known_solution, Inf) < 1e-3
Test Passed

This page was generated using Literate.jl.