Polynomial regression on a large dataset in Java

For an internal project at my day job I needed to find the n degree polynomial that best fits a (potentially very large) dataset.

I asked a question on Reddit about this, and Paul Lutus was kind enough to respond with a link to some code that did something close to what I needed, however Paul’s code was not well suited to very large amounts of data.

So with his help, I modified his code to decouple it from the original application it was a part of, make it follow good Java coding practices a bit more closely, and in doing so make it more flexible and well suited to handling very large datasets.

The code is under the GNU Public License, which is unfortunate since it’s a library and the GPL is viral and Paul is unwilling to relicense under the LGPL, meaning that I or someone else will need to re-implement under a different license if the library is to be used within non-GPL’d code :-(

Here is the source, collaborative free software at work, and please comment if you find it useful!: PolynomialFitter.java

4 thoughts on “Polynomial regression on a large dataset in Java

  1. Mike

    It seems that fx method is not used in the code. I will try how it works now for my polynomial fitting ;) Big thanks for code anyway!

    Reply
  2. JR

    I have got to the point of running the code and receiving a Polynomial object, but this seems to be a string; I am not sure how to access the coefficients.
    I am a newcomer to java and oop.

    myPolynome =(PolynomialFitter) new PolynomialFitter(3);
    for (i=0;i<10;i++)
    {
    y[i]=A*i*i+B*i+C;
    System.out.println ("x "+i+": y "+y[i]);
    myPolynome.addPoint(i,y[i]);
    }
    myPoly=myPolynome.getBestFit();
    System.out.println(myPoly);
    // coeffs=myPolynome.fit();
    // A=(double)myPoly.getY;
    // System.out.println(myPoly[0]);
    }
    }

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *