MIQP for portfolio optimization
Answered
I have to find the optimal portfolio weights for a set of K, where k is a sort of cardinality of my portfolio. X is a continuous variable, while y is a binary one. The real problem is that the script below doesn't work (I think for the LB and UB)
Q=[sigma zeros(n);zeros(n,2*n)];
Aeq=[ones(1,n) zeros(1,n); zeros(1,n) ones(1,n)];
K=5
model.Q=sparse(Q);
model.A=sparse(Aeq);
model.obj=zeros(2*n,1);
model.rhs=[1;K];
model.sense=['=';'<'];
model.lb=[zeros(1,n) zeros(1,n)];
model.ub=[ones(1,n) ones(1,n)];
model.vtype=[repmat('C',1,n) ; repmat('B',1,n)];
Obviously the first row of the Aeq matrix is deleted (expected return constraint) cause I'm using the epsilonconstraints method
P.S I'm using gurobi in the Matlab interface

The constraints \( \ell_i y_i \leq x_i \leq u_i y_i,\ i = 1, \ldots, n \) can't be modeled as simple bound constraints, because the bounds are not constants. These constraints should be incorporated into the constraint matrix as
$$\begin{alignat*}{2} x_i + \ell_i y_i &\leq 0 \quad && i = 1, \ldots, n \\ x_i  u_i y_i &\leq 0 && i = 1, \ldots, n.\end{alignat*}$$
If you're only trying to add \( [0, 1] \) bounds to your variables for now, that is implemented correctly.
The \( \texttt{vtype} \) field should be a onedimensional vector of length \( 2n \). It is currently defined as a \( 2 \times n \) \( \texttt{char} \) array:
>> [repmat('C',1,n) ; repmat('B',1,n)]
ans =
2×10 char array
'CCCCCCCCCC'
'BBBBBBBBBB'This will cause the variables types to not match what you intend. Instead, try:
model.vtype = [repmat('C',n,1); repmat('B',n,1)];
Note that you can use the gurobi_write() function to save the model as an LP file for visual inspection:
gurobi_write(model, 'model.lp');
0 
I've modified my model, but probably I wrote in a wrong way this new constraints
I 've used the vector [eye(n) zeros(1,n)*eye(n) ; eye(n) (ones(1,n)*eye(n) ]
while for the lower and upper bounds, respectively zeros(2*n,1) and ones(2*n,1)
Something is wrong
P.S thanks for your help, is the first time for me in this kind of optimization using Gurobi
0 
The constraints
$$\begin{alignat*}{2} x_i + \ell_i y_i &\leq 0 \quad && i = 1, \ldots, n \\ x_i  u_i y_i &\leq 0 && i = 1, \ldots, n.\end{alignat*}$$
can be equivalently written as
$$\begin{align*}\begin{bmatrix}I &\phantom{}L\\ \phantom{}I &U\end{bmatrix} \begin{bmatrix}x \\ y\end{bmatrix} &\leq \begin{bmatrix} {\bf{0}} \\ {\bf{0}} \end{bmatrix},\end{align*}$$
where \( I \in \mathbb{R}^{n \times n} \) is the identity matrix, \( L \in \mathbb{R}^{n \times n} \) is the diagonal matrix formed by the \( \ell_i \), \( U \in \mathbb{R}^{n \times n} \) is the diagonal matrix formed by the \( u_i \), and \( {\bf{0}} \in \mathbb{R}^n \) is the vector of zeros.
You can add these constraints to the model by modifying the \( \texttt{A} \), \( \texttt{sense} \), and \( \texttt{rhs} \) fields of your \( \texttt{model} \) \( \texttt{struct} \). Let's assume \( \ell_i = 0 \) and \( u_i = 1 \) for all \( i = 1, \ldots, n \). Then \( L \) is the matrix of zeros and \( U \) is the identity matrix:
Aeq = [ones(1,n), zeros(1,n); zeros(1,n), ones(1,n); eye(n), zeros(n); eye(n), eye(n)];
model.A = sparse(Aeq);
model.rhs = [1; K; zeros(2*n,1)];
model.sense = ['='; repmat('<',2*n+1,1)];In this case, because \( \ell_i = 0 \) for all \( i = 1, \ldots, n \) and \( x \) is nonnegative, the constraints \( x_i + \ell_i y_i \leq 0 \) are redundant. Thus, you can skip these constraints:
Aeq = [ones(1,n), zeros(1,n); zeros(1,n), ones(1,n); eye(n), eye(n)];
model.A = sparse(Aeq);
model.rhs = [1; K; zeros(n,1)];
model.sense = ['='; repmat('<',n+1,1)];0
Please sign in to leave a comment.
Comments
3 comments