Reputation: 6872
Can someone let me know how I can do Polynomial Regression with Apache Maths 3.6.1
Below are the data points I used for my testing
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
My code looks like below
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]);
}
The coefficients are reported as
Coefficient 12.910025, 0.000000, 0.000000
But these does not seem to be quite correct. If I use the same dataset in
Online Polynimal Regression and in archanoid online regression - both reports same value as 654623237474.68250993904929103762, 28.75921919628759991574, -0.00000000023885199278
Can someone let me know what is going wrong? I have seen this question but that is not helping me.
Upvotes: 0
Views: 3189
Reputation: 91
With Commons Math 3.6.1, I get almost identical parameters when fitting a cubic to data using PolynomialCurveFitter
and tuk's OLSMultipleLinearRegression
suggestion.
The curve gives correct interpolations in the range of the data, but you've got to be careful about extrapolations.
The PolynomialCurveFitter
code was half the number of lines of the OLSMultipleLinearRegression
code.
Upvotes: 1
Reputation: 6872
This has been answered in apache-commons mailing list
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).
A sample code is like below
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;
}
Upvotes: 2