I have no idea how to do this. I can set up the Lagrangian, but I don't know how to translate it into solve.qp() inputs.

The inputs are Dmat, dvec, amat, bvec, meq.

I don't know LaTeX so excuse my notation here. My problem is a global minimum variance portfolio optimization subject to the constraints x1+x2+x3 = 1, as well as 0 < x1 < w1, etc. Essentially, the weights must equal one, and the weights have a min/max condition.

So the matrix notation is (where Sigma is the covariance matrix of 3 assets)

[2*Sigma  [x1  = [0
 2*Sigma   x2  =  0
 2*Sigma   x3  =  0
 1  1  1   L1  =  1
-1  0  0   L2  =  0
 0 -1  0   L3  =  0
 0  0 -1   L4  =  0
 1  0  0   L5  =  w1
 0  1  0   L6  =  w2
 0  0  1]  L7] =  w3]

I know meq=1, as there is only one equality constraint. How do I translate the rest of the matrices into inputs for solve.qp()?

  • $\begingroup$ So x1,x2,x3 are the primal variables and l1,...,l7 are the dual variables and you wrote out the optimality condition in matrix form, but I think solve.qp() does not require that, it requires the statement of the minimization problem. It will compute the Lagrangian internally. $\endgroup$
    – nbbo2
    Sep 29, 2016 at 16:21
  • $\begingroup$ Well, I figured out that Dmat is Sigma, dvec is [0,0,0], Amat is the constraints below Sigma, so matrix(rbind(rep(1,NumAsset),-diag(NumAsset), diag(NumAsset)), ncol=NumAsset), and bvec is the vector of targets, so [1, 0, 0, 0, w1, w2, w3]. The problem is now I'm getting "constraints are inconsistent, no solution" $\endgroup$
    – milkmotel
    Sep 29, 2016 at 16:24
  • $\begingroup$ The 1st inequality constraint you have is $-x_1\ge0$ and the fourth is $x_1\ge w_1$ which indeed seem inconsistent $\endgroup$
    – nbbo2
    Sep 29, 2016 at 16:43
  • $\begingroup$ I guess it should be $x_1≥0$ and $-x_1≥-w_1$, so just need to change some signs. $\endgroup$
    – nbbo2
    Sep 29, 2016 at 16:46
  • $\begingroup$ You're right. I just had to invert my diag matrices and constraints in Amat and bvec such that the constraints were $-x_1 \geq -w_1$ rather than $x_1 \leq w_1$ $\endgroup$
    – milkmotel
    Sep 29, 2016 at 16:48

1 Answer 1


I figured it out, mostly.

Dmat is your [n x n] covariance matrix.

dvec is your [n] length vector of expected returns, or if you want to find the GMV portfolio, it is a vector of 0s,.

(Can someone explain to me where this fits into the Lagrangian matrices? It doesn't matter since I only calculate the GMV portfolio subject to a target return to find an efficient frontier, then calculate the return using my solution weights, but it'd be nice to know.)

Amat is your transposed matrix of constraints t([m x n]), but you also have to invert the signs on your inequality constraints so that they represent >= constraints.

bvec is your [m] vector of constraints, i.e. the other side of the equation opposite the matrix of constraints.

Hope this helps.

  • $\begingroup$ I believe in the value returned to you by qp.solve() there is a vector called Lagrangian which contains the Lagrangian at the solution. $\endgroup$
    – nbbo2
    Sep 29, 2016 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.