/** * This regression method is private and called in other regression methods * @param xDataMatrix _nData x (_degree + 1) matrix whose low vector is (xData[i]^0, xData[i]^1, ..., xData[i]^{_degree}) * @param yDataVector the y-values * @param nData Number of data points * @param degree the degree */ private LeastSquaresRegressionResult regress( DoubleMatrix xDataMatrix, DoubleArray yDataVector, int nData, int degree) { Decomposition<QRDecompositionResult> qrComm = new QRDecompositionCommons(); DecompositionResult decompResult = qrComm.apply(xDataMatrix); _qrResult = (QRDecompositionResult) decompResult; DoubleMatrix qMatrix = _qrResult.getQ(); DoubleMatrix rMatrix = _qrResult.getR(); double[] betas = backSubstitution(qMatrix, rMatrix, yDataVector, degree); double[] residuals = residualsSolver(xDataMatrix, betas, yDataVector); for (int i = 0; i < degree + 1; ++i) { ArgChecker.isFalse(Double.isNaN(betas[i]), "Input is too large or small"); } for (int i = 0; i < nData; ++i) { ArgChecker.isFalse(Double.isNaN(residuals[i]), "Input is too large or small"); } return new LeastSquaresRegressionResult(betas, residuals, 0.0, null, 0.0, 0.0, null, null, true); }
/** * Alternative regression method with different output. * @param xData X values of data * @param yData Y values of data * @param degree Degree of polynomial which fits the given data * @param normalize Normalize xData by mean and standard deviation if normalize == true * @return PolynomialsLeastSquaresRegressionResult containing coefficients, rMatrix, degrees of freedom, norm of residuals, and mean, standard deviation */ public PolynomialsLeastSquaresFitterResult regressVerbose( double[] xData, double[] yData, int degree, boolean normalize) { LeastSquaresRegressionResult result = regress(xData, yData, degree, normalize); int nData = xData.length; DoubleMatrix rMatriX = _qrResult.getR(); DoubleArray resResult = DoubleArray.copyOf(result.getResiduals()); double resNorm = OG_ALGEBRA.getNorm2(resResult); if (normalize == true) { return new PolynomialsLeastSquaresFitterResult(result.getBetas(), rMatriX, nData - degree - 1, resNorm, _renorm); } return new PolynomialsLeastSquaresFitterResult(result.getBetas(), rMatriX, nData - degree - 1, resNorm); }
@Test public void testRecoverOrginal() { final DecompositionResult result = QR.apply(A); assertTrue(result instanceof QRDecompositionResult); final QRDecompositionResult qr = (QRDecompositionResult) result; final DoubleMatrix q = qr.getQ(); final DoubleMatrix r = qr.getR(); final DoubleMatrix a = (DoubleMatrix) ALGEBRA.multiply(q, r); checkEquals(A, a); }