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); }
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); }