Why CVXPY Ignores Portfolio Weight Constraints
Summary
The document shows a maximum-return portfolio optimization with a covariance penalty and intended limits on individual asset weights. Although the author creates absolute-value bounds and a full-investment constraint, the reported portfolio contains positions far beyond the stated bounds. The answer identifies the cause: the constraints were assembled but not passed into the optimization problem when it was constructed.
This is a concise debugging lesson for portfolio optimization in CVXPY. Defining constraint expressions alone does not make the solver enforce them; they must be included in the problem definition. The extreme reported positions illustrate the consequence of solving without the intended restrictions. The exchange does not discuss whether the objective, covariance inputs, or chosen limits are otherwise appropriate, nor does it offer a broader treatment of portfolio construction or solver behavior.
Key ideas
- Creating constraint expressions does not enforce them unless they are included in the optimization problem.
- The example seeks to maximize expected return with a covariance-based risk penalty.
- The displayed extreme weights are consistent with the intended asset bounds being omitted from the problem definition.
- The answer diagnoses a modeling omission rather than evaluating the portfolio objective or its inputs.
Tags
Full text
# CVXPY 's constrains doesn't work
# CVXPY 's constrains doesn't work
I am trying to implement a max return optimization with a large number of assets. I am not sure why this problem won't work.
```
w = cvxpy.Variable(num_asset) #30 assets
constraints = []
constraints.append(cvxpy.abs(w) <= 1)
constraints.append(cvxpy.sum_entries(w) == 1)
objective = cvxpy.Maximize(combined_return.T * w - 0.5 * alpha * cvxpy.quad_form(w, combined_covariance))
problem = cvxpy.Problem(objective)
problem.solve(solver='CVXOPT', verbose=True)
```
The result have 3 of the weights way greater than 1.
```
Holding
PCLN 0.024891
MHK 0.005428
**AZO -14.429484**
SJM 0.006767
COST 0.027336
CLX 0.006752
TSO 0.003282
VLO 0.008457
PSX 0.014839
BLK 0.021508
ICE 0.011146
EQR 0.009720
ISRG 0.009747
BIIB 0.020765
GILD 0.043480
UAL 0.005269
GWW 0.005330
LMT 0.028927
**GOOGL 14.624353**
GOOG 0.182734
**IBM 121.617234**
SHW 0.010577
LYB 0.012353
EMN 0.003821
LVLT 0.007118
VZ 0.085949
T 0.098943
NEE 0.022617
SRE 0.010750
AEP 0.013104
```
Could someone help me understand why that constraint cvxpy.abs(w)<=1 doesn't do the job? Many thanks
## Answer by Bob Jansen (score 1)
https://quant.stackexchange.com/a/30992
You forgot to include `constraints` as a parameter to the call to `Problem`.Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.