0

We are currently evaluating OjAlgo library as a replacement to commons-math library for SVD computation. For below input, there is a difference between singular values of OjAlgo and commons-math.

I have worked with OjAlgo 47.1.2 and with latest code from develop branch, still the output is same. Is this a bug

double[][] olsColumns = { { 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
                { 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 }, { 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
                { 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
                { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }, { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
                { 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
                { 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 }, { 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
                { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 }, { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 },
                { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 } };
        double[] observationVector = {26.0, 12.0, 9.0, 18.0, 16.0, 17.0, 24.0, 32.0, 30.0, 21.0, 16.0, 12.0, 21.0, 16.0};

//OjAlgo


        final PrimitiveDenseStore tmpOriginal = PrimitiveDenseStore.FACTORY.rows(olsColumns);
        SingularValue<Double> tmpSVD = SingularValue.make(tmpOriginal);
        tmpSVD.decompose(tmpOriginal);
        double[] singularValues = tmpSVD.getSingularValues().toRawCopy1D();

        System.out.println("Singular values" + Arrays.toString(singularValues));

//Commons-Math

        RealMatrix newPredM = new Array2DRowRealMatrix(olsColumns);
        SingularValueDecomposition svd = new SingularValueDecomposition(newPredM);
        System.out.println("Singular values" + Arrays.toString(svd.getSingularValues()));

OjAlgo output Singular values[4.000000000000001, 1.4142135623730956, 1.4142135623730951, 1.4142135623730951, 1.4142135623730951, 1.4142135623730945, 1.4142135623730945, 4.566099776030485E-16]

Commons-Math output Singular values[4.000000000000001, 1.4142135623730951, 1.4142135623730951, 1.414213562373095, 1.414213562373095, 1.4142135623730945, 1.4142135623730945, 0.0]

Is there a way to make the last value of OjAlgo singular values i.e.,4.566099776030485E-16 as 0.0

Sudheer
  • 5
  • 2
  • @apete do you have any comment on this – Sudheer Jul 11 '19 at 06:53
  • The difference is E-16. What problem does this cause? (I don't think this qualifies as a stack overflow question.) – apete Jul 11 '19 at 07:14
  • @apete We iteratively call SVD in our code and remove an element from input and again call SVD on the changed input . And also we calculate covariance and act upon the data. I agree it is a small number and as we call SVD, there is difference in the final output. hence the question – Sudheer Jul 12 '19 at 10:24
  • Generally it's not a good idea to use ==0.0 comparisons. (I assume that's what you're doing.) The ojAlgo SVD implementations have a method getRankThreshold() that return a suitable threshold for irrelevant singular values. – apete Jul 12 '19 at 12:40

0 Answers0