title: Error in cholesky decompostion of large matrix in R
body: <p>I am trying to decompose a large covariance matrix as part of a portfolio optimisation in R, but it doesn't seem to work for matrices with more than ~100 variables. Here is an example with some random data:</p>
  
  <pre><code>set.seed(123)
  a &lt;- NULL
  
  for(i in 1:300) {
    a &lt;- cbind(a, rnorm(100))
  }
  
  chol(cov(a))
  
  Error in chol.default(cov(a)) : 
    the leading minor of order 101 is not positive definite
  </code></pre>
  
  <p>but if I reduce the sample it works fine:</p>
  
  <pre><code>b &lt;- NULL
  
  for(i in 1:100) {
    b &lt;- cbind(b, rnorm(100)) 
  }
  
  chol(cov(b))
  </code></pre>
  
  <p>I've tried it a few times, and the error seems to occur for covariance matrices with more than 100 to 105 variables. Does anyone know the source of this problem?</p>
  
  <p>EDIT 2: This works:</p>
  
  <pre><code>set.seed(123)
  c &lt;- NULL
  
  for(i in 1:300) {
    c &lt;- cbind(c, rnorm(300))
  }
  
  chol(cov(c))
  </code></pre>
  
  <p>But this does not:</p>
  
  <pre><code>d &lt;- NULL
  
  for(i in 1:301) {
    d &lt;- cbind(d, rnorm(300))
  }
  
  chol(cov(d))
  
  Error in chol.default(cov(d)) : 
    the leading minor of order 300 is not positive definite
  </code></pre>
  
  <p>So the number of variables may not exceed the number of observations?</p>