I am using the Eigen library in C++: I am currently calculating the covariance matrix myself as follows:
Eigen::MatrixXd covariance_matrix = Eigen::MatrixXd::Constant(21, 21, 0);
data mean = calc_mean(all_data)
for(int j = 0; j < 21; j++){
for(int k = 0; k < 21; k++){
for(std::vector<data>::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;
}
}
Is there an inbuilt/more optimized way to do this with the Eigen library? For example if I store my data in a MatrixXd
where each row is an observation and each column a feature?
Thanks