I am trying to implement a simple minminimum variance portfolio optimisation with a few simple constraints, long-only, sums to one. I also want to constraint on my betas to create a market neutral portfolio, i.e sum(betas) = 0.:
- long-only portfolio
- fully invested (sums to one)
- market-neutrality, i.e sum(betas) = 0.
I am not very experienced with cvxpycvxpy but I quite like it and want to implement my stuff with it going forward. AsBelow is an example( from the cvxpy website), which uses
$$Min_x\;\; \frac{1}{2}x^T\Sigma x$$$$\min_x\;\; \frac{1}{2}x^T\Sigma x$$ Under the constraints $$x^T \mathbb{1}=1$$ $$\mu^Tx \geq \tau$$
I now want to add
$$B^Tx=0$$ The constraint $B^Tx=0$, which will ensure that the portfolios beta is zero.
Here is the example:
from cvxpy import *
import numpy as np
np.random.seed(1)
n = 10
Sigma = np.random.randn(n, n)
Sigma = Sigma.T.dot(Sigma)
betas = [np.random.uniform(-1,1) for _ in range(10)]
w = Variable(n)
risk = quad_form(w, Sigma)
constraints = [sum_entries(w) == 1, w >= 0]
prob = Problem(Minimize(risk), constraints)
for i in range(100):
prob.solve()
print('Weights :', w.value)
How can I define the additional variable for beta and how do you alter your constraints list.
From the manual I assume we need s.thsomething in the form a quad_form()quad_form()
, but does this have to be defines similardefined similarly to the risk variable in the example or inside the constraints object, and? how do you link it to the betas data vector ?
I would have done s.thsomething like
sum(quad_form(w, betas)) == 0
inside the constraints object which unfortunately doesn't work.