Mapping Portfolio Constraints to Quadratic Programming Inputs in R
Summary
The discussion explains how to translate a global minimum variance portfolio problem into the inputs expected by R's quadratic programming solver. The example has three asset weights, a budget constraint requiring weights to sum to one, and lower and upper bounds on each weight. It identifies the covariance matrix as the quadratic objective matrix and says the linear objective vector can be zero for a global minimum variance problem.
The answer describes the constraint matrix as the transpose of the constraint rows and the right-hand-side vector as the corresponding constraint values. It also notes that inequality signs may need to be reversed to match the solver's convention of constraints expressed as greater than or equal to. The questioner already recognizes that there is one equality constraint. This short answer does not work through the full example numerically or discuss solver-specific scaling or matrix conventions in depth, so users should check their package documentation when implementing more complex constraints.
Key ideas
- A global minimum variance portfolio uses the asset covariance matrix in the quadratic objective.
- The linear objective vector can be set to zero when minimizing variance alone.
- Constraint rows are transposed to form the solver's constraint matrix.
- The right-hand-side vector contains the corresponding constraint values.
- Inequality signs may need reversal to match the solver's required direction.
Tags
Full text
# Translating matrix expression of Lagrangian into solve.qp() parameters (R) # Translating matrix expression of Lagrangian into solve.qp() parameters (R) 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()`? ## Answer by milkmotel (score 1) https://quant.stackexchange.com/a/30363 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.
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.