title: Quadprog for Portfolio Optimization with Constraints
body: <p>Let me start by saying I am new to Stack Exchange sites, so apologies if this turns out to be a low quality question.</p>
  <p>I am trying to run a mean-variance portfolio optimization using Solve.QP in R and have looked at several previous questions regarding this on Stack Overflow to figure out how to do this correctly but with no luck.</p>
  <p>The constraints I want are as follows:</p>
  <ol>
  <li>Long/Short portfolio: weights may be negative</li>
  <li>Market neutral: Sum of weights = 0</li>
  <li>No leverage: Every weight is bound between -1 and 1 + sum of all positive weights = 1
  and sum of all negative weights = -1</li>
  </ol>
  <p>I am running this optimization with approx. 100 assets, so for demonstration purposes, I have reproduced the optimization part of the code with 5 assets (using fake data). Full code is found at the end of the post.</p>
  <p>Let's say I have the following 5 expected returns for my 5 assets</p>
  <pre><code>&gt; dvec
  [1]  1.0791124  0.9088370  0.0992847 -0.2028962 -1.1632315
  </code></pre>
  <p>Two assets are expected to perform negatively, whilst 3 are expected to give a positive return.</p>
  <p>Amat and bvec then looks like this</p>
  <pre><code>&gt; cbind(amat, bvec)
                       bvec
   [1,]  1  1  1  1  1    0
   [2,]  1  1  1  0  0    1
   [3,]  0  0  0  1  1   -1
   [4,]  1  0  0  0  0   -1
   [5,]  0  1  0  0  0   -1
   [6,]  0  0  1  0  0   -1
   [7,]  0  0  0  1  0   -1
   [8,]  0  0  0  0  1   -1
   [9,] -1  0  0  0  0   -1
  [10,]  0 -1  0  0  0   -1
  [11,]  0  0 -1  0  0   -1
  [12,]  0  0  0 -1  0   -1
  [13,]  0  0  0  0 -1   -1
  </code></pre>
  <ul>
  <li>First row indicates the market neutrality constraint</li>
  <li>Second row indicates the long leg of the portfolio must have weights that add to 1</li>
  <li>Third row indicates the short leg must have weights that add to -1</li>
  <li>Next 5 rows indicates that each individual weight must be &gt;= -1</li>
  <li>Last 5 rows indicates that each individual weight must be &lt;= 1</li>
  </ul>
  <p>The optimization runs without throwing an error, but the leverage constraint is (not always) enforced</p>
  <pre><code>&gt; sum(w_opt[w_opt&gt;0])
  [1] 1.22445
  &gt; sum(w_opt[w_opt&lt;0])
  [1] -1.22445
  </code></pre>
  <p>Where <code>w_opt</code> contains the optimized weights.</p>
  <pre><code>&gt; w_opt
  [1]  0.73304775  0.49140239 -0.22445014 -0.01629763 -0.98370237
  </code></pre>
  <p>I have set <code>meq=3</code> to specify the three first constraints are equalities.</p>
  <pre><code>library(quadprog)
  
  omega &lt;- cov(data.frame(x=rnorm(200), y=rnorm(200), z=rnorm(200), p=rnorm(200), q=rnorm(200)))
  
  N &lt;- 5
  maxw &lt;- 1
  minw &lt;- -1
  dvec &lt;- rnorm(N)
  dvec &lt;- sort(dvec, decreasing=TRUE)
  dmat &lt;- omega
  amat &lt;- rbind(1, c(rep(1, length(dvec[dvec&gt;0])),rep(0, length(dvec[dvec&lt;0]))), c(rep(0,length(dvec[dvec&gt;0])),rep(1, length(dvec[dvec&lt;0]))), diag(N), -diag(N))
  bvec &lt;- c(0,1,-1, rep(minw,N), -rep(maxw, N))
  meq &lt;- 3
  
  w_opt &lt;- solve.QP(Dmat=dmat, dvec=dvec, Amat=t(amat), bvec=bvec, meq=meq)$solution
  
  sum(w_opt[w_opt&gt;0])
  sum(w_opt[w_opt&lt;0])
  </code></pre>