NamedVariableLeastSquaresRegressionResult extends LeastSquaresRegressionResult { public Double getPredictedValue(final Map<String, Double> namesAndValues) { if (namesAndValues == null) { throw new IllegalArgumentException("Map was null"); } if (namesAndValues.isEmpty()) { s_logger.warn("Map was empty: returning 0"); return 0.; } final double[] betas = getBetas(); double sum = 0; if (hasIntercept()) { if (namesAndValues.size() < betas.length - 1) { throw new IllegalArgumentException("Number of named variables in map was smaller than that in regression"); } } else { if (namesAndValues.size() < betas.length) { throw new IllegalArgumentException("Number of named variables in map was smaller than that in regression"); } } int i = hasIntercept() ? 1 : 0; for (final String name : getIndependentVariableNames()) { if (name.equals(INTERCEPT_STRING)) { sum += betas[0]; } else { if (!namesAndValues.containsKey(name) || namesAndValues.get(name) == null) { throw new IllegalArgumentException("Do not have value for " + name); } sum += betas[i++] * namesAndValues.get(name); } } return sum; } NamedVariableLeastSquaresRegressionResult(final List<String> independentVariableNames, final LeastSquaresRegressionResult result); List<String> getIndependentVariableNames(); LeastSquaresRegressionResult getResult(); Double getPredictedValue(final Map<String, Double> namesAndValues); @Override int hashCode(); @Override boolean equals(final Object obj);  }
@Test public void test() { final int n = 100; final double beta0 = 0.3; final double beta1 = 2.5; final double beta2 = -0.3; final Function2D<Double, Double> f1 = new Function2D<Double, Double>() { @Override public Double evaluate(final Double x1, final Double x2) { return beta1 * x1 + beta2 * x2; } }; final Function2D<Double, Double> f2 = new Function2D<Double, Double>() { @Override public Double evaluate(final Double x1, final Double x2) { return beta0 + beta1 * x1 + beta2 * x2; } }; final double[][] x = new double[n][2]; final double[] y1 = new double[n]; final double[] y2 = new double[n]; for (int i = 0; i < n; i++) { x[i][0] = RANDOM.nextDouble(); x[i][1] = RANDOM.nextDouble(); y1[i] = f1.evaluate(x[i][0], x[i][1]); y2[i] = f2.evaluate(x[i][0], x[i][1]); } final LeastSquaresRegression ols = new OrdinaryLeastSquaresRegression(); final List<String> names = Arrays.asList("1", "2"); final NamedVariableLeastSquaresRegressionResult result1 = new NamedVariableLeastSquaresRegressionResult(names, ols .regress(x, null, y1, false)); final NamedVariableLeastSquaresRegressionResult result2 = new NamedVariableLeastSquaresRegressionResult(names, ols .regress(x, null, y2, true)); try { result1.getPredictedValue((Map<String, Double>) null); Assert.fail(); } catch (final IllegalArgumentException e) { } assertEquals(result1.getPredictedValue(Collections.<String, Double> emptyMap()), 0., 1e-16); try { final Map<String, Double> map = new HashMap<>(); map.put("1", 0.); result1.getPredictedValue(map); Assert.fail(); } catch (final IllegalArgumentException e) { } double x1, x2, x3; final Map<String, Double> var = new HashMap<>(); for (int i = 0; i < 10; i++) { x1 = RANDOM.nextDouble(); x2 = RANDOM.nextDouble(); x3 = RANDOM.nextDouble(); var.put("1", x1); var.put("2", x2); assertEquals(result1.getPredictedValue(var), f1.evaluate(x1, x2), EPS); assertEquals(result2.getPredictedValue(var), f2.evaluate(x1, x2), EPS); var.put("3", x3); assertEquals(result1.getPredictedValue(var), f1.evaluate(x1, x2), EPS); assertEquals(result2.getPredictedValue(var), f2.evaluate(x1, x2), EPS); } }