2
votes

I'm calculating covariance matrix, eigenvectors and eigenvalues using apache commons math3 library. so my main function is this (given a double matrix):

private void coVariance(double[][] matrix) {
        RealMatrix mx = MatrixUtils.createRealMatrix(matrix);
        RealMatrix cov = new Covariance(mx).getCovarianceMatrix();
        System.out.println("***************************************");
        System.out.println("Covariance Matrix");
        for (int i = 0; i < cov.getRowDimension(); i++) {
            for (int j = 0; j < cov.getColumnDimension(); j++) {
                System.out.print(cov.getEntry(i, j) + " ");
            }
            System.out.println();
        }
        System.out.println("***************************************");
        EigenDecomposition e = new EigenDecomposition(cov);
        double[] arrayEigenValue = e.getRealEigenvalues();
        for (int i = 0; i < e.getRealEigenvalues().length; i++) {
            System.out.println("eigenValue with index " + i + " " + arrayEigenValue[i]);
            RealVector arrayEigenVector = e.getEigenvector(i);
            for (int j = 0; j < arrayEigenVector.getDimension(); j++) {
                System.out.print(arrayEigenVector.getEntry(j) + " ");
            }
            System.out.println();
            System.out.println();
        }
        System.out.println("***************************************");
    }

In order to understand if all is correct i'm using an example which has got covariance/eigenValues/eigenVector already calculated:

2.5,2.4
0.5,0.7
2.2,2.9
1.9,2.2
3.1,3.0
2.3,2.7
2,1.6
1,1.1
1.5,1.6
1.1,0.9

It has this results of covariance matrix eigenVectors and eigenValues:

0,616555556 0.615444444
0.615444444 0.716555556

eigenValues:

0.0490833989
1.28402771

eigenVectors:

1° = -0.735178656 -0.677873309
2° = 0.677873399  -0.735178656

Results of my program:

Covariance Matrix

0.6165555555555556 0.6154444444444446 
0.6154444444444446 0.7165555555555555 

eigenValues:

1.2840277121727839 
0.04908339893832714

eigenVectors:

1° = -0.7351786555444081  -0.6778733985280118 
2° = -0.6778733985280118   0.7351786555444081

As you can see ,the eigenvectors are different in the sign of second value of the second eigenVector 0.7351786555444081

Could someone explain me why?

1

1 Answers

0
votes

Oirc,

If you don't know the mathematically forulatioln of Eigen values in matrices we use a formula to calculate it |A-L.I| = 0 where A is matrix (row data ) L is (lambda) or constant we are saying as Eigen values here I is unit matrix equal to A matrix order.

By this we will get the L values that can be positive or negative That values we will put to that matrix equation to calulate the vector matrix . So after calculation that matrix elements can be positive or negative .

Theoritically : "-"/"+"ive sign shows the direction of the vector values.