title: Portfolio Optimization constraints Matrix/bvec explanation
body: <p>I recently got very interested in portfolio optimization and started playing around in R, to create a minimum variance portfolio,</p>
  
  <pre><code>library(quadprog)
  Dmat &lt;- matrix(c(356.25808, 12.31581, 261.88302, 12.31581, 27.24840,
  18.50515,261.88302, 18.50515,535.45960), nrow=3, ncol=3)
  dvec &lt;- matrix(c(9.33, 3.33, 9.07), nrow=3, ncol=1)
  A.Equality &lt;- matrix(c(1,1,1), ncol=1)
  Amat &lt;- cbind(A.Equality, dvec, diag(3), -diag(3))
  bvec &lt;- c(1, 5.2, rep(0, 3), rep(-0.5, 3))
  qp &lt;- solve.QP(Dmat, dvec, Amat, bvec, meq=1)
  </code></pre>
  
  <p>Example above has the following constraints ( example from <a href="https://stackoverflow.com/questions/24090037/r-portfolio-optimization-solve-qp-constraints-are-inconsistent">here</a>)</p>
  
  <p>There are 4 constraints:</p>
  
  <ul>
  <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>
  </ul>
  
  <p>I am currently trying to refresh my matrix/vector math, I would really appreciate if someone could tell me how you add the individual constraints together in aMat and bvec and the basic algebra background for it. And as another question how a constraint for weights &lt;0 (shorting) would look like.</p>
  
  <p>thanks in advance</p>