title: r - Portfolio Optimization - solve.QP - Constraints are Inconsistent
body: <p>I am trying to use solve.QP to solve a portfolio optimization problem (quadratic problem)</p>
  
  <p>Total 3 assets</p>
  
  <p>There are 4 constraints:</p>
  
  <ol>
  <li>sum of weights equal to 1</li>
  <li>portfolio expected return equals to 5.2%</li>
  <li>each asset weight greater than 0</li>
  <li>each asset weight smaller than .5</li>
  </ol>
  
  <p>Dmat is the covariance matrix</p>
  
  <pre><code>Dmat &lt;- matrix(c(356.25808, 12.31581, 261.8830, 212.31581, 27.24840, 18.50515, 261.88302, 18.50515,535.45960), nrow=3, ncol=3)
  </code></pre>
  
  <p>dvec is each asset's expected return</p>
  
  <pre><code>dvec &lt;- matrix(c(9.33, 3.33, 9.07), nrow=3, ncol=1)
  </code></pre>
  
  <p>Amat is the constraint matrix</p>
  
  <pre><code>A.Equality &lt;- matrix(c(1,1,1), ncol=1)
  Amat &lt;- cbind(A.Equality, dvec, diag(3), -diag(3))
  </code></pre>
  
  <p>constraint A^T b >= b_0, b vector</p>
  
  <pre><code>bvec &lt;- c(1, 5.2, rep(0, 3), rep(-0.5, 3))
  </code></pre>
  
  <p>meq=2, since there are two equality constraints, first and second constraints are equality</p>
  
  <p>Then I run the function solve.QP</p>
  
  <pre><code>library(quadprog)
  qp &lt;- solve.QP(Dmat, dvec, Amat, bvec, meq=2)
  </code></pre>
  
  <p>But it gives the error</p>
  
  <pre><code>Error in solve.QP(Dmat, dvec, Amat, bvec, meq = 2) : constraints are inconsistent, no solution!
  </code></pre>
  
  <p>I am not sure where I did wrong.</p>