Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
cvxopt optimization

Hi, wondering if anyone might have an idea how to handle this case when it comes to portfolio optimization and how to construct the matrices. Simple case is fine using portfolios = [solvers.qp(mu*P, -q, G, h, A, b)['x'] for mu in mus] where sum(x)=1 and x>=0 but what happens if you have n assets and you set upper and lower bounds. Example, if I have 3 assets and say lower bound is 40% and upper bound is 100%, the optimizer will fail because lower weights on all 3 assets will sum to greater than 1. Therefore, that means, 1 asset must fall out. Only way I can understand this case is optimize on the combinations of 3 assets with same lower bound of 40%. Thus, optimize(0,1) then (0,2) and finally (1,2) and find the combination with lowest stdev if that is your objective but if assets are large this process will be computationally heavy. Each combination can have lower bound of 40% and sum to 1. So, my question, how do you change G, h, A, b so this will work. I think you need to change lower bound <= x <= upper bound to an equality but can not figure this out. Any feedback greatly appreciated. thanks

13 responses

I don't understand? One is an equality constraint (sum all to one) and another is an inequality constraint (bounds). Where is the issue? use A and b for equality and G and h for inequality.

here is a quick example using cvxpy which is similar to cvxopt
n=3 (3 assets)
prob = Problem(Maximize(ret - gamma*risk), constraints)
constraints=[sum_entries(w) == 1, w >= 0.30, w<=1.0]

weights
[matrix([[ 0.4], [ 0.3],
[ 0.3]])]
no problem

but....
constraints=[sum_entries(w) == 1, w >= 0.40, w<=1.0]
weights=[None]

with three assets and say lower bound is 40%, solution can not be found. Not sure how to alter the matrices

When working with optimizers and their matrices, I find I always have to write out the constraints I want mathematically, and then write out the equation matrices, and only then try to code it. Anything else and I confuse myself. I use a http://www.amazon.com/uni-ball-KuruToga-Mechanical-Starter-1751934/dp/B0026ICM1E for this.

How about changing
constraints=[sum_entries(w) == 1, w >= 0.40, w<=1.0]
to
constraints=[sum_entries(w) <= 1, w >= 0.40, w<=1.0]

and if the answer sum isn't 1, just normalize it.
So for an answer of Ans=[0.6, 0.0, 0.6] normalize it to [0.5, 0.0, 0.5]

Alan Coppola's method could work but your formulation of problem is wrong.

One one hand you say that weights should sum to 1 and on the other you say minimum weight is 40% here:

constraints=[sum_entries(w) == 1, w >= 0.40, w<=1.0]

That is a contradiction.

hi, yes, i know the constraints are a contradiction and that is the whole point of me posting my question. I know the answer needs to be one weight at 0 and remaining 2 sum to 1. Will take a look at boolean and see if i can get an answer. If i target a portfolio based on 3 combinations, there will be one that has the lowest stdev and that will be the answer but how to code in python or which solver to use not sure. Will look at gurobi examples as well.

many thanks for the feedback so far

The constraints you've defined cannot be represented as a convex set. For CVXOPT or CVXPY, the feasible set must be convex. You can try MILP or something like that using Gurobi.

Quantopian supports:

http://docs.scipy.org/doc/scipy/reference/optimize.html

Have a look at:

http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html#scipy.optimize.minimize

I've been using:

Method SLSQP uses Sequential Least SQuares Programming to minimize a function of several variables with any combination of bounds, equality and inequality constraints. The method wraps the SLSQP Optimization subroutine originally implemented by Dieter Kraft [12]. Note that the wrapper handles infinite values in bounds by converting them into large floating values.

If you are interested, I could cook up an example. The bounds parameter can be used to limit the optimization search and the constraints can include the normalization, numpy.sum(x) = 1, where x is the asset allocation vector.

yes please, any example will be great and thank you for your time. For the three asset example

mu= [-0.01121522 -0.0029662 0.02089164]
covar = [[ 1.01555868e+00 2.82769766e-04 1.13099898e-03]
[ 2.82769766e-04 1.00342791e+00 -1.00797232e-02]
[ 1.13099898e-03 -1.00797232e-02 1.01961298e+00]]

minimize (1/2)x' covar x + mu' x where lower bound= 0.40 and upper =1 and sum(x) =1
will be cool to see how to set this up and see the weights.

thanks

Here's an example I had on-hand. It needs to be re-factored, but it should give you an idea of how things work. Note that SLSQP also accepts the Jacobian, so if an analytic form exists, you should probably include it, as well. It'll probably be several days before I can get back to this, but I think it'd be straightforward to tweak up the code I posted, with your objective function, etc. --Grant

many thanks for your help....greatly appreciated.

You're welcome. Just to show that the bounds work, here's a version with limits on the bond portion of the portfolio.

Note that there are additional settings and you can see what the optimizer is doing, if you want:

http://docs.scipy.org/doc/scipy/reference/optimize.minimize-slsqp.html#optimize-minimize-slsqp

If you go this route, it is worth understanding the best settings. Also, you might try normalizing the returns to 1 in the example I posted, and then feeding them to the minimizer.