title: Eigen: Is there an inbuilt way to calculate sample covariance
body: <p>I am using the Eigen library in C++: I am currently calculating the covariance matrix myself as follows:</p>
  
  <pre><code>Eigen::MatrixXd covariance_matrix = Eigen::MatrixXd::Constant(21, 21, 0);
  data mean = calc_mean(all_data)
  for(int j = 0; j &lt; 21; j++){
      for(int k = 0; k &lt; 21; k++){
          for(std::vector&lt;data&gt;::iterator it = all_data.begin(); it!= all_data.end(); it++){
              covariance_matrix(j,k) += ((*it)[j] - mean[j]) * ((*it)[k] - mean[k]);
          }
          covariance_matrix(j,k) /= all_data.size() - 1;
      }
  }
  </code></pre>
  
  <p>Is there an inbuilt/more optimized way to do this with the Eigen library? For example if I store my data in a <code>MatrixXd</code> where each row is an observation and each column a feature?</p>
  
  <p>Thanks</p>