Question about TPrincipal::GetCovarianceMatrix()


I am really afraid if there is another similar topic about this issue here, but the last topic I was able to find is about 2 years ago with no solution.

I am trying to use the GetCovarianceMatrix method from TPrincipal class. I am supposed to use the covariance matrix to do some Math. However, I cannot be able to obtain a symmetric matrix. The member returns with a pointer to a Matrix that is NOT symmetric. This matrix shows just the lower left elements of the actual covariance matrix. How could I solve this problem?

(const char *) "6.10/08"


This is strange, maybe the PCA did not work correctly. Can you please post the code reproducing this problem ?

It looks like this was discussed year ago here:

Apparently, the authors only stored half of the covariance matrix. @BiaseGabriel, you could try the following (untested):

auto original = pca.GetCovarianceMatrix();
TMatrixD copy(*original);
TMatrixDSym sym;
sym.Use(covarianceMatrix.GetNrows(), covarianceMatrix.GetMatrixArray());

If you are lucky, it understands how to take the correct half of the matrix, and you can do math with sym. If it doesn’t understand it, you can symmetrise the matrix by assigning the elements
copy[i][j] = copy[j][i]
in the right order.

It looks like TPrincipal should be using an eigen-matrix from the start …

Thank you so much for your help. I do not know why TPrincipal::GetCovarianceMatrix() does not offer a symmetric matrix, showing just the lower left now. I noted that some people mentioned this information in the past.
I worked on some math to turns it into symmetric with the help of TMatrixDDiag(). That works for what I need for now, although it may not be the best way to more complex implementations.

  1. TPrincipal p(7);
  2. p.AddRow(data) //some data
  3. const TMatrixD* covar = p.GetCovarianceMatrix();
  4. TMatrixD dataCov(7,7); //7 variables
  5. dataCov = *covar;
  6. dataCov.T();
  7. TMatrixD cov(7,7);
  8. TMatrixDDiag aux(dataCov);
  9. aux = 0;
  10. cov = *covar + dataCov;

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.