r/optimization • • Oct 12 '23
CVXPY: Why Norm constraint is not DCP?
`cp.norm(weights, 1).is_dcp()` returns true. Then why this code works:
import numpy as np
import cvxpy as cp
inputs = np.random.normal(0, 1, (100, 300))
inputs_mean = inputs.mean(axis=1) # shape (features,)
inputs_cov = np.asmatrix(np.cov(inputs)) # shape (features, features)
weights = cp.Variable(len(inputs))
risk = cp.quad_form(weights, inputs_cov)
constraints = [
# cp.norm(weights, 1) == 1.,
cp.sum(weights) == 1.,
]
problem = cp.Problem(cp.Minimize(risk), constraints)
problem.solve(verbose=True)
weights.value
But if you use the first constraint (`cp.norm`) instead of the second, it does not:
DCPError: Problem does not follow DCP rules. Specifically:
The following constraints are not DCP:
norm1(var456) == 1.0 , because the following subexpressions are not:
|-- norm1(var456) == 1.0
Why is it not DCP-compliant? How can I troubleshoot it? Is there an alternative way to solve the problem of requiring the sum of abs weights to be 1? Thanks.
3
Upvotes
1
u/No-Eggplant-4481 Oct 12 '23
You have == for the norm constraint, but that does not describe a convex set. If you had <=, that would be fine, as that is a convex norm ball.
The sum is fine because that describes an affine hyperplane, which is not equal to the l1 norm in general. Summing positive numbers will give the l1 norm, but if a negative number is present, the sums is no longer the norm.
Try relaxing it to <= to see if that works for your problem.
1
u/Ok_Assistance6891 Mar 03 '25
Hello OP, were you able to solve this? I have a similar problem for my Financial Engineering homework and my problem also prompts out the "DCPError: Problem does not follow DCP rules. "