OrdinaryLeastSquaresRegression extends LeastSquaresRegression { @Override public LeastSquaresRegressionResult regress(final double[][] x, final double[][] weights, final double[] y, final boolean useIntercept) { if (weights != null) { s_logger.info("Weights were provided for OLS regression: they will be ignored"); } return regress(x, y, useIntercept); }  @Override LeastSquaresRegressionResult regress(final double[][] x, final double[][] weights, final double[] y, final boolean useIntercept); LeastSquaresRegressionResult regress(final double[][] x, final double[] y, final boolean useIntercept);  }
@Test public void test() { final int n = 20; final double[][] x = new double[n][5]; final double[] y1 = new double[n]; final double[] y2 = new double[n]; final double[] a1 = new double[] {3.4, 1.2, -0.62, -0.44, 0.65}; final double[] a2 = new double[] {0.98, 3.4, 1.2, -0.62, -0.44, 0.65}; for (int i = 0; i < n; i++) { for (int j = 0; j < 5; j++) { x[i][j] = RANDOM.nextDouble() + (RANDOM.nextDouble() - 0.5) / FACTOR; } y1[i] = a1[0] * x[i][0] + a1[1] * x[i][1] + a1[2] * x[i][2] + a1[3] * x[i][3] + a1[4] * x[i][4] + RANDOM.nextDouble() / FACTOR; y2[i] = a2[0] + a2[1] * x[i][0] + a2[2] * x[i][1] + a2[3] * x[i][2] + a2[4] * x[i][3] + a2[5] * x[i][4] + RANDOM.nextDouble() / FACTOR; } final LeastSquaresRegressionResult result1 = REGRESSION.regress(x, null, y1, false); final LeastSquaresRegressionResult result2 = REGRESSION.regress(x, null, y2, true); assertRegression(result1, a1); assertRegression(result2, a2); final double[] residuals1 = result1.getResiduals(); for (int i = 0; i < n; i++) { assertEquals(y1[i], a1[0] * x[i][0] + a1[1] * x[i][1] + a1[2] * x[i][2] + a1[3] * x[i][3] + a1[4] * x[i][4] + residuals1[i], 10 * EPS); } final double[] residuals2 = result2.getResiduals(); for (int i = 0; i < n; i++) { assertEquals(y2[i], a2[0] + a2[1] * x[i][0] + a2[2] * x[i][1] + a2[3] * x[i][2] + a2[4] * x[i][3] + a2[5] * x[i][4] + residuals2[i], 10 * EPS); } }