Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using least square method with Commons Math and fitting

I was trying to use commons math to figure out the constants in a polynomial. It looks like the routine exists but I got this error. Does anyone see the issue?

I was trying to convert this question to commons-math: https://math.stackexchange.com/questions/121212/how-to-find-curve-equation-from-data

From plotting you data (Wolfram|Alpha link), it does not look linear. So it better be fit by a polynomial. I assume you want to fit the data:

X Y 1 4 2 8 3 13 4 18 5 24 .. using a quadratic polynomial y=ax2+bx+c.

And wolfram alpha provided a great utility. I wish I could get the same answers like from wolfram.

http://www.wolframalpha.com/input/?i=fit+4%2C+8%2C+13%2C

E.g. By entering that data, I would get : 4.5 x-0.666667 (linear)

Here is the code and error:

import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression;
import org.apache.commons.math3.stat.regression.SimpleRegression;
final OLSMultipleLinearRegression regression2 = new OLSMultipleLinearRegression();
double[] y = {
        4.0, 
        8, 
        13,                 
};      
double[][] x2 = 
    {
        { 1.0, 1, 1  },
        { 1.0, 2, 4  },
        { 0.0, 3, 9  },             
    };
regression2.newSampleData(y, x2);
regression2.setNoIntercept(true);
regression2.newSampleData(y, x2);       
double[] beta = regression2.estimateRegressionParameters();
for (double d : beta) {
    System.out.println("D: " + d);
}

Exception in thread "main" org.apache.commons.math3.exception.MathIllegalArgumentException: not enough data (3 rows) for this many predictors (3 predictors) at org.apache.commons.math3.stat.regression.AbstractMultipleLinearRegression.validateSampleData(AbstractMultipleLinearRegression.java:236) at org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression.newSampleData(OLSMultipleLinearRegression.java:70) at org.berlin.bot.algo.BruteForceSort.main(BruteForceSort.java:108)

like image 516
Berlin Brown Avatar asked Oct 05 '22 05:10

Berlin Brown


1 Answers

The javadoc for validateSampleData() states that the two-dimensional array must have at least one more row than it has columns.

http://commons.apache.org/proper/commons-math/javadocs/api-3.3/org/apache/commons/math3/stat/regression/AbstractMultipleLinearRegression.html

like image 145
arcy Avatar answered Nov 04 '22 11:11

arcy