使用 Apache Maths 3.6.1 进行多项式回归

Polynomial Regression with Apache Maths 3.6.1

谁能告诉我如何使用 Apache Maths 3.6.1

进行多项式回归

以下是我用于测试的数据点

60735214881.391304  1520254800000.000000
60697824142.469570  1520258400000.000000
60651182200.208694  1520262000000.000000
60684367132.939130  1520265600000.000000
60676588613.008700  1520269200000.000000
60641816564.869570  1520272800000.000000
60604714824.233510  1520276400000.000000
60580042814.330440  1520280000000.000000
60536134542.469570  1520283600000.000000
60566323732.034780  1520287200000.000000
60578775249.252174  1520290800000.000000
60547382844.104350  1520294400000.000000
60536776546.802160  1520298000000.000000
60474342718.330440  1520301600000.000000
60452725477.286960  1520305200000.000000
60486821569.669560  1520308800000.000000
60247997139.995674  1520312400000.000000
60248432181.426090  1520316000000.000000
60217476247.373920  1520319600000.000000
60170744493.634780  1520323200000.000000

我的代码如下所示

private void polynomialFitter(List<List<Double>> pointlist) {
        final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2);
        final WeightedObservedPoints obs = new WeightedObservedPoints();
        for (List<Double> point : pointlist) {
            obs.add(point.get(1), point.get(0));
        }
        double[] fit = fitter.fit(obs.toList());
        System.out.printf("\nCoefficient %f, %f, %f", fit[0], fit[1], fit[2]);
    }

系数报告为

Coefficient 12.910025, 0.000000, 0.000000

不过这些似乎都不太正确。如果我在中使用相同的数据集 Online Polynimal Regression and in archanoid online regression - 两个报告的值与 654623237474.68250993904929103762, 28.75921919628759991574, -0.00000000023885199278

相同

谁能告诉我出了什么问题?我看过这个 但这对我没有帮助。

这已在 apache-commons 邮件列表

中得到答复

Polynomial regression is not the same as curve fitting. To do polynomial regression in Commons Math, use the OLSMultipleLinearRegression class, using, X, X^2 etc as the independent variables (as your second reference above shows).

示例代码如下

private OLSMultipleLinearRegression getMultipleLinearRegression(List<List<Double>> pointlist) {
    OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
    double y[] = new double[pointlist.size()];
    double x[][] = new double[pointlist.size()][2];
    int c = 0;
    for (List<Double> point : pointlist) {
        y[c] = point.get(0);
        x[c][0] = point.get(1);
        x[c][1] = Math.pow(point.get(1), 2);
        regression.newSampleData(y, x);
        c++;
    }
    System.out.printf("\tR2 = %f", regression.calculateRSquared());
    return regression;
}

使用 Commons Math 3.6.1,在使用 PolynomialCurveFitter 和 tuk 的 OLSMultipleLinearRegression 建议对数据拟合立方体时,我得到几乎相同的参数。

该曲线在数据范围内提供了正确的插值,但您必须小心外推。

PolynomialCurveFitter 代码行数是 OLSMultipleLinearRegression 代码行数的一半。