Eigen: Is there an inbuilt way to calculate sample covariance

前端 未结 2 479
情歌与酒
情歌与酒 2020-12-25 15:30

I am using the Eigen library in C++: I am currently calculating the covariance matrix myself as follows:

Eigen::MatrixXd covariance_matrix = Eigen::MatrixXd:         


        
相关标签:
2条回答
  • 2020-12-25 15:48

    When each row is an observation, you can use the matrix formulation for the sample covariance matrix as shown on wikipedia ( http://en.wikipedia.org/wiki/Sample_mean_and_sample_covariance#Sample_covariance )

    Sample covariance, source: wikipedia article linked above .

    This is fairly easy to write in terms of Eigen matrix multiplications etc. Whether it will be more performant isn't obvious to me, I suspect the optimizer would have to do a really good job (be sure to use at least -O2). It may be worth trying and profiling it.

    0 讨论(0)
  • 2020-12-25 15:59

    Using Eigen expressions will leverage SIMD and cache optimized algorithms, so yes it should definitely be faster, and in any case, much simpler to write:

    MatrixXd centered = mat.rowwise() - mat.colwise().mean();
    MatrixXd cov = (centered.adjoint() * centered) / double(mat.rows() - 1);
    

    Moreover, assuming "data" is a typedef for a double[21], then you can use the Map<> feature to view your std::vector as an Eigen object:

    Map<Matrix<double,Dynamic,21,RowMajor> > mat(&(all_data[0][0], all_data.size(), 21);
    
    0 讨论(0)
提交回复
热议问题