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.9 - 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: 963.88ms
Iter: Objective: Primal Res: Dual Res: Rho:
1 9.8585e+00 5.9778e-01 7.6572e-01 1.0000e-01
25 2.2101e+00 4.4633e-07 7.3877e-08 1.0000e-01
------------------------------------------------------------------
>>> Results
Status: Solved
Iterations: 25
Optimal objective: 2.21
Runtime: 5.201s (5201.02ms)
Double check result against known solution:
known_opt_val = 12.5406
known_solution = [
1.0 0.732562 -0.319491 -0.359985 -0.287543 -0.15578 0.0264044 -0.271438;
0.732562 1.0 0.0913246 -0.0386357 0.299199 -0.122733 0.126612 -0.187489;
-0.319491 0.0913246 1.0 -0.0863377 0.432948 0.461783 -0.248641 -0.395299;
-0.359985 -0.0386357 -0.0863377 1.0 0.503379 0.250601 0.141151 0.286088;
-0.287543 0.299199 0.432948 0.503379 1.0 -0.0875199 0.137518 0.0262425;
-0.15578 -0.122733 0.461783 0.250601 -0.0875199 1.0 -0.731556 0.0841783;
0.0264044 0.126612 -0.248641 0.141151 0.137518 -0.731556 1.0 -0.436274;
-0.271438 -0.187489 -0.395299 0.286088 0.0262425 0.0841783 -0.436274 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.