Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error / Usage issue in polynomial regression #42

Closed
WolfgangFahl opened this issue Feb 20, 2017 · 5 comments
Closed

Error / Usage issue in polynomial regression #42

WolfgangFahl opened this issue Feb 20, 2017 · 5 comments

Comments

@WolfgangFahl
Copy link

WolfgangFahl commented Feb 20, 2017

the following unit tests compares the org.jfree.data.statistics.Regression.getPolynomialRegression
to the apache commons math3 solution. The results of getPolynomialRegression
are
-5,333 10,000 0,951
while the correct result would be:

coeff 0: 4,000
coeff 1: 2,000
coeff 2: 1,000

you might want to add testcases like the ones in http://alvinalexander.com/java/jwarehouse/commons-math3-3.6.1/src/test/java/org/apache/commons/math3/fitting/PolynomialCurveFitterTest.java.shtml
or improve the documentation on how to use the function correctly.

 @Test
  public void testPolynomicalRegression() {
    final double[] coeff = { 4.0, 2.0, 1 }; // 12.9 - 3.4 x + 2.1 x^2
    final PolynomialFunction f = new PolynomialFunction(coeff);
    final WeightedObservedPoints obs = new WeightedObservedPoints();
    double[][] data=new double[2][9];
    for (int i=0;i<=8;i++) {
      double x=i;
      double y=f.value(x);
      data[0][i]=x;
      data[1][i]=y;
      obs.add(x,y);
    }
    final DefaultXYDataset dataset = new DefaultXYDataset();
    dataset.addSeries(0, data);
    double[] preg = org.jfree.data.statistics.Regression.getPolynomialRegression(dataset, 0, 1);
    assertEquals(3,preg.length);
    System.out.println(String.format("%6.3f %6.3f %6.3f",preg[0],preg[1],preg[2]));
    // http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math4/fitting/PolynomialCurveFitter.html
    // http://alvinalexander.com/java/jwarehouse/commons-math3-3.6.1/src/test/java/org/apache/commons/math3/fitting/PolynomialCurveFitterTest.java.shtml
    int degree=2;
    final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(degree);
    final PolynomialFunction fitted = new PolynomialFunction(fitter.fit(obs.toList()));
    final double fcoeff[]=fitted.getCoefficients();
    for (int i=0;i<fcoeff.length;i++) {
      System.out.println(String.format("coeff %3d: %6.3f",i,fcoeff[i]));
    }
@erlenmeyer
Copy link

Just stumbled over that issue, and did not want to leave it uncommented.
The documentation of Regression.getPolynomialRegression states that

  • the third parameter is the order of the regression
  • the method returns an array of length order+2, where the last value indicates the correlation coefficient

So, in the test above, a linear regression was fitted through the data, giving a regression with slope of 10, offset of -5.333, and R2 of 0.951. Using an instance of org.apache.commons.math3.fitting.PolynomialCurveFitter with a degree of 1 gives the same results for slope and offset.
Just for completion: calling Regression.getPolynomialRegression with a proper "order" value of 2 returns the expected values of 4 (offset), 2 (linear term), 1 (quadratic term), and 1 (R2).

@trashgod
Copy link
Contributor

For reference: Some Regression method tests appear in RegressionTest.java.

@WolfgangFahl
Copy link
Author

WolfgangFahl commented Apr 12, 2022

Was the documentation improved? Looks like the parameter names "degree" and "order" which are different for the two APIs might lead to confusion for users such as me.

@trashgod
Copy link
Contributor

I see no substantive, interim change in the relevant API history. I did not find the API confusing, but this discussion offers some perspective.

@WolfgangFahl
Copy link
Author

WolfgangFahl commented Apr 13, 2022

@trashgod thx for the excellent link which also points to https://en.wikipedia.org/wiki/Degree_of_a_polynomial - it seems its often worthwile to have a proper glossary ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants