Error in solve.QP
<p>So essentially I have two matrices containing the excess returns of stocks (R) and the expected excess return (ER).</p>

<pre><code>R&lt;-matrix(runif(47*78),ncol = 78) 
ER&lt;-matrix(runif(47*78),ncol = 78) 
</code></pre>

<p>I then combine these removing the first row of R and adding the first row of ER to form a new matrix R1.</p>

<p>I then do this for R2 i.e. removing first two rows of and R and rbinding it with the first 2 rows of ER.</p>

<p>I do this until I have n-1 new matrices from R1 to R47.</p>

<p>I then find the Var-Cov matrix of each of the Return matrices using cov() i.e. Var-Cov1 to Var-Cov47.</p>

<pre><code>n&lt;-47
switch_matrices &lt;- function(mat1, mat2, nrows){
  rbind(mat1[(1+nrows):nrow(mat1),],mat2[1:nrows,]) 
}

l&lt;-lapply(1:n-1, function(nrows) switch_matrices(R,ER, nrows))
list2env(setNames(l,paste0("R",seq_along(l))), envir = parent.frame())

b&lt;-lapply(l, cov)
list2env(setNames(b,paste0("VarCov",seq_along(b))), envir = parent.frame())
</code></pre>

<p>I am now trying to find the asset allocation using quadprog. So for example:</p>

<pre><code>D_mat &lt;- 2*VarCov1
d_vec &lt;- rep(0,78)
A_mat &lt;- cbind(rep(1,78),diag(78))
b_vec &lt;- c(1,d_vec)

library(quadprog)
output &lt;- solve.QP(Dmat = D_mat, dvec = d_vec,Amat = A_mat, bvec = b_vec,meq =1)
# The asset allocation
(round(output$solution, 4))
</code></pre>

<p>For some reason when running solve.QP with any Var-Cov matrix found I get this error:</p>

<pre><code>Error in solve.QP(Dmat = D_mat, dvec = d_vec, Amat = A_mat, bvec = b_vec,  : 
  matrix D in quadratic function is not positive definite!
</code></pre>

<p>I'm wondering what I am doing wrong or even why this is not working.</p>