protected void checkData(double[][] x, double[] weights, double[] y) { checkData(x, y); if (weights != null) { if (weights.length == 0) { throw new IllegalArgumentException("No data in weights array"); } if (weights.length != x.length) { throw new IllegalArgumentException("Independent variable and weight arrays are not the same length"); } } }
public LeastSquaresRegressionResult regress(double[][] x, double[] y, boolean useIntercept) { checkData(x, y); double[][] indep = addInterceptVariable(x, useIntercept); double[] dep = new double[y.length]; for (int i = 0; i < y.length; i++) { dep[i] = y[i]; } DoubleMatrix matrix = DoubleMatrix.copyOf(indep); DoubleArray vector = DoubleArray.copyOf(dep); DoubleMatrix transpose = _algebra.getTranspose(matrix); DoubleMatrix betasVector = (DoubleMatrix) _algebra.multiply( _algebra.multiply(_algebra.getInverse(_algebra.multiply(transpose, matrix)), transpose), vector); double[] yModel = super.writeArrayAsVector(((DoubleMatrix) _algebra.multiply(matrix, betasVector)).toArray()); double[] betas = super.writeArrayAsVector(betasVector.toArray()); return getResultWithStatistics(x, y, betas, yModel, transpose, matrix, useIntercept); }
@Override public LeastSquaresRegressionResult regress(double[][] x, double[][] weights, double[] y, boolean useIntercept) { if (weights == null) { throw new IllegalArgumentException("Cannot perform GLS regression without an array of weights"); } checkData(x, weights, y); double[][] dep = addInterceptVariable(x, useIntercept); double[] indep = new double[y.length]; double[][] wArray = new double[y.length][y.length]; for (int i = 0; i < y.length; i++) { indep[i] = y[i]; for (int j = 0; j < y.length; j++) { wArray[i][j] = weights[i][j]; } } DoubleMatrix matrix = DoubleMatrix.copyOf(dep); DoubleArray vector = DoubleArray.copyOf(indep); DoubleMatrix w = DoubleMatrix.copyOf(wArray); DoubleMatrix transpose = ALGEBRA.getTranspose(matrix); DoubleMatrix betasVector = (DoubleMatrix) ALGEBRA.multiply( ALGEBRA.multiply( ALGEBRA.multiply( ALGEBRA.getInverse(ALGEBRA.multiply(transpose, ALGEBRA.multiply(w, matrix))), transpose), w), vector); double[] yModel = super.writeArrayAsVector(((DoubleMatrix) ALGEBRA.multiply(matrix, betasVector)).toArray()); double[] betas = super.writeArrayAsVector(betasVector.toArray()); return getResultWithStatistics(x, y, betas, yModel, useIntercept); }
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);
protected void checkData(double[][] x, double[][] weights, double[] y) { checkData(x, y); if (weights != null) { if (weights.length == 0) { throw new IllegalArgumentException("No data in weights array"); } if (weights.length != x.length) { throw new IllegalArgumentException("Independent variable and weight arrays are not the same length"); } int n = weights[0].length; for (double[] w : weights) { if (w.length != n) { throw new IllegalArgumentException("Need a rectangular array of weight"); } } } }
public LeastSquaresRegressionResult regress(double[][] x, double[] weights, double[] y, boolean useIntercept) { if (weights == null) { throw new IllegalArgumentException("Cannot perform WLS regression without an array of weights"); } checkData(x, weights, y); double[][] dep = addInterceptVariable(x, useIntercept); double[] w = new double[weights.length]; for (int i = 0; i < y.length; i++) { w[i] = weights[i]; } DoubleMatrix matrix = DoubleMatrix.copyOf(dep); DoubleArray vector = DoubleArray.copyOf(y); RealMatrix wDiag = new DiagonalMatrix(w); DoubleMatrix transpose = ALGEBRA.getTranspose(matrix); DoubleMatrix wDiagTimesMatrix = DoubleMatrix.ofUnsafe(wDiag.multiply( new Array2DRowRealMatrix(matrix.toArrayUnsafe())).getData()); DoubleMatrix tmp = (DoubleMatrix) ALGEBRA.multiply( ALGEBRA.getInverse(ALGEBRA.multiply(transpose, wDiagTimesMatrix)), transpose); DoubleMatrix wTmpTimesDiag = DoubleMatrix.copyOf(wDiag.preMultiply(new Array2DRowRealMatrix(tmp.toArrayUnsafe())).getData()); DoubleMatrix betasVector = (DoubleMatrix) ALGEBRA.multiply(wTmpTimesDiag, vector); double[] yModel = super.writeArrayAsVector(((DoubleMatrix) ALGEBRA.multiply(matrix, betasVector)).toArray()); double[] betas = super.writeArrayAsVector(betasVector.toArray()); return getResultWithStatistics(x, convertArray(wDiag.getData()), y, betas, yModel, transpose, matrix, useIntercept); }
final LeastSquaresRegression regression = new OrdinaryLeastSquaresRegression(); try { regression.checkData(null, null); Assert.fail(); } catch (final IllegalArgumentException e) { regression.checkData(x, null); Assert.fail(); } catch (final IllegalArgumentException e) { regression.checkData(x, (double[]) null, y); Assert.fail(); } catch (final IllegalArgumentException e) { y = new double[3]; try { regression.checkData(x, (double[]) null, y); Assert.fail(); } catch (final IllegalArgumentException e) { regression.checkData(x, (double[]) null, y); Assert.fail(); } catch (final IllegalArgumentException e) { regression.checkData(x, (double[]) null, y); Assert.fail(); } catch (final IllegalArgumentException e) { double[] w1 = new double[0];