Julia error using convex package with diagind function
Asked Answered
A

1

0

I'm trying to solve the problem

d = 0.5 * ||X - \Sigma||_{Frobenius Norm} + 0.01 * ||XX||_{1}, 

where X is a symmetric positive definite matrix, and all the diagnoal element should be 1. XX is same with X except the diagonal matrix is 0. \Sigma is known, I want minimum d with X.

My code is as following:

using Convex
m = 5;
A = randn(m, m); 
x = Semidefinite(5);
xx=x;
xx[diagind(xx)].=0;
obj=vecnorm(A-x,2)+sumabs(xx)*0.01;
pro= minimize(obj, [x >= 0]);
pro.constraints+=[x[diagind(x)].=1];
solve!(pro)

MethodError: no method matching diagind(::Convex.Variable)

I just solve the optimal problem by constrain the diagonal elements in matrix, but it seems diagind function could not work here, How can I solve the problem.

Applesauce answered 22/4, 2018 at 12:47 Comment(5)
Can you add the optimization problem you're trying to solve in mathematical form?Jaunitajaunt
d=0.5*||X-\Sigma||_{Frobenius Norm}+0.01*||XX||_{1}, X is a symmetric positive definite matrix, and all the diagnoal element should be 1. XX is same with X except the diagonal matrix is 0. \Sigma is known, I want minimum d with X.Applesauce
Ok, I added that to the question, please check whether that is accurate.Jaunitajaunt
Yes, you understand it exactly.Applesauce
Out of interest, what kind of 1-norm is ||XX||_1 if you define it as sum(abs(XX))? As far as I remember, the operator (1,1)-norm would be the maximum of column sums, and the Schatten 1-norm the sum of singular values. Am I overlooking something?Jaunitajaunt
J
1

I think the following does what you want:

m = 5
Σ = randn(m, m)
X = Semidefinite(m)
XX = X - diagm(diag(X))
obj = 0.5 * vecnorm(X - Σ, 2) + 0.01 * sum(abs(XX))
constraints = [X >= 0, diag(X) == 1]
pro = minimize(obj, constraints)
solve!(pro)

For the types of operations:

  • diag extracts the diagonal of a matrix, as a vector
  • diagm constructs a diagonal matrix out of a vector

So, to have XX be X with zero diagonal, we subtract the diagonal of X from it. And to constrain X having diagonal 1, we compare its diagonal with 1, using ==.

It is a good idea to keep immutable values as far as possible, instead of trying to modify things. I don't know whether Convex even supports that.

Jaunitajaunt answered 23/4, 2018 at 8:25 Comment(1)
I see that there's also a way to fix variables, maybe you could use that for ensuring your diagonals' values. But I don't really have any experience with optimization packages.Jaunitajaunt

© 2022 - 2024 — McMap. All rights reserved.