LinearRegression implements Model<double[], double[]> { public LinearRegression() { this(DEFAULT_ERROR); } LinearRegression();  LinearRegression(double error); @Override void estimate(List<? extends IndependentPair<double[], double[]>> data); void estimate(double[][] yd, double[][] xd); void estimate(Matrix y, Matrix x); @Override @SuppressWarnings("unchecked") boolean validate(IndependentPair<double[], double[]> data); @Override double[] predict(double[] data); Matrix predict(Matrix x); @Override int numItemsToEstimate(); @Override double calculateError(List<? extends IndependentPair<double[], double[]>> data); @Override LinearRegression clone(); @Override boolean equals(Object obj); @Override String toString(); static final double DEFAULT_ERROR; }
@Test public void testLinearRegression() throws Exception { double[][] xt = new double[][]{new double[]{0,1,2,3,4,5,6,7,8,9}}; double m = 2; double c = -2; double[][] perfectyt = new double[1][xt[0].length]; for (int i = 0; i < perfectyt[0].length; i++) { perfectyt[0][i] = xt[0][i] * m + c; } Matrix x = new Matrix(xt).transpose(); Matrix perfecty = new Matrix(perfectyt).transpose(); Matrix noise = new Matrix(RandomData.getRandomDoubleArray(perfecty.getRowDimension(), 1, -1, 1, 1)); Matrix y = perfecty.plus(noise); List<IndependentPair<double[], double[]>> ipairs = aspairs(x,y); LinearRegression lr = new LinearRegression(); lr.estimate(y, x); LinearRegression lr2 = new LinearRegression(); lr2.estimate(ipairs); assertTrue(lr.equals(lr2)); System.out.println("Error: " + lr.calculateError(ipairs)); assertTrue(lr.calculateError(ipairs) < 5); }