Commons Math3和ojalgo库的SVD解决方案输出之间的巨大差异

时间:2019-06-25 11:05:08

标签: java apache-commons-math ojalgo

commons-math3库和ojalgo库之间的SVD差异解决方案非常高。是否有任何方法可以根据任何输入参数来减少差异。

        double[][] olsColumns = { { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 },
                { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 },
                { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 },
                { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 },
                { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 },
                { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 },
                { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 },
                { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 },
                { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 },
                { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 },
                { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 },
                { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 },
                { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 },
                { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 } };
        double[] observationVector = { 1.0, 0.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, 0.0, 0.0,
                0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };

//Ojalgo
final PrimitiveDenseStore tmpOriginal = PrimitiveDenseStore.FACTORY.rows(olsColumns);
        SingularValue<Double> tmpSVD = SingularValue.make(tmpOriginal);
        tmpSVD.decompose(tmpOriginal);
        double[] singularValues = tmpSVD.getSingularValues().toRawCopy1D();
        double[][] V = tmpSVD.getQ2().toRawCopy2D();
        System.out.println("V" + Arrays.deepToString(V));
        System.out.println("Singular values" + Arrays.toString(singularValues));
        try {

            // MatrixStore<Double> doubleMat = tmpSVD.solve(tmpOriginal,
            // PrimitiveDenseStore.FACTORY.column(Utils.prepareObservationVector()));
            MatrixStore<Double> solution = tmpSVD.getSolution(PrimitiveDenseStore.FACTORY.column(observationVector),
                    tmpSVD.preallocate(tmpOriginal));
            System.out.println("Solution " + Arrays.toString(solution.toRawCopy1D()));
        } catch (Exception e) {
            e.printStackTrace();
        }

//Commons-Math3

        RealMatrix newPredM = new Array2DRowRealMatrix(olsColumns);
        SingularValueDecomposition svd = new SingularValueDecomposition(newPredM);
        // RealMatrix covariance = svd.getCovariance(0);
        // System.out.println("covariance"+Arrays.deepToString(covariance.getData()));
        System.out.println("V" + Arrays.deepToString(svd.getV().getData()));
        System.out.println("Singular values" + Arrays.toString(svd.getSingularValues()));
        double[] solution = svd.getSolver().solve(new ArrayRealVector(observationVector)).toArray();
        System.out.println("Solution" + Arrays.toString(solution));

Math3通用解决方案:[0.01612903225806451, 0.016129032258064502]

OjAlgo解决方案解决方案:[7.614155324982286E13, -7.614155324982295E13]

1 个答案:

答案 0 :(得分:0)

您正在使用哪个版本的ojAlgo?

当我尝试该代码时,出现异常,因为您提供给tmpSVD.getSolution(...)方法的“预分配”矩阵的大小/形状错误。如果您只是删除第二个参数,分配将为您完成,代码将起作用。我得到这个结果:

V[[0.707106781186548, -0.707106781186547], [0.707106781186547, 0.707106781186548]]
Singular values[13.638181696985853, 9.035878689445474E-15]
Solution [0.016129032258064484, 0.01612903225806446]