/** * Given a set of data (X_i, Y_i) and degrees of a polynomial, determines optimal coefficients of the polynomial. * @param xData X values of data * @param yData Y values of data * @param degree Degree of polynomial which fits the given data * @return LeastSquaresRegressionResult Containing optimal coefficients of the polynomial and difference between yData[i] and f(xData[i]), * where f() is the polynomial with the derived coefficients */ public LeastSquaresRegressionResult regress(double[] xData, double[] yData, int degree) { return regress(xData, yData, degree, false); }
LeastSquaresRegressionResult result = regObj.regress(xValues, yValues, degree);
/** * Degree of polynomial must be positive */ @Test(expectedExceptions = IllegalArgumentException.class) public void MinusDegreeTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = -4; final double[] xValues = new double[] {0, 1, 2, 3, 5, 6 }; final double[] yValues = new double[] {1, 2, 3, 4, 2, 1 }; regObj.regress(xValues, yValues, degree); }
/** * */ @Test(expectedExceptions = IllegalArgumentException.class) public void ExtremeValueTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 4; final double[] xValues = new double[] {0, 1e-307, 2e-307, 3e18, 4 }; final double[] yValues = new double[] {1, 2, 3, 4, 5 }; regObj.regress(xValues, yValues, degree); }
/** * */ @Test(expectedExceptions = IllegalArgumentException.class) public void LargeNumberTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 4; double[] xValues = new double[] {1, 2, 3, 4e2, 5, 6, 7 }; double[] yValues = new double[] {1, 2, 3, 4, 5, 6, 7 }; regObj.regress(xValues, yValues, degree); }
/** * Number of data points should be larger than (degree + 1) of a polynomial */ @Test(expectedExceptions = IllegalArgumentException.class) public void DataShortTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 6; final double[] xValues = new double[] {0, 1, 2, 3, 5, 6 }; final double[] yValues = new double[] {1, 2, 3, 4, 2, 1 }; regObj.regress(xValues, yValues, degree); }
/** * xValues length should be the same as yValues length */ @Test(expectedExceptions = IllegalArgumentException.class) public void WrongDataLengthTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 4; final double[] xValues = new double[] {0, 1, 2, 3, 5, 6 }; final double[] yValues = new double[] {1, 2, 3, 4, 2, 1, 2 }; regObj.regress(xValues, yValues, degree); }
/** * An error is thrown if too many repeated data are found */ @Test(expectedExceptions = IllegalArgumentException.class) public void RepeatDataTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 4; final double[] xValues = new double[] {0, 1, 2, 3, 1, 1 }; final double[] yValues = new double[] {1, 2, 3, 4, 2, 2 }; regObj.regress(xValues, yValues, degree); }
/** * */ @Test(expectedExceptions = IllegalArgumentException.class) public void NullTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 4; final int nPts = 5; double[] xValues = new double[nPts]; double[] yValues = new double[nPts]; xValues = null; yValues = null; regObj.regress(xValues, yValues, degree); }
/** * */ @Test(expectedExceptions = IllegalArgumentException.class) public void InfinityTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 4; final int nPts = 5; double[] xValues = new double[nPts]; double[] yValues = new double[nPts]; final double zero = 0.; for (int i = 0; i < nPts; ++i) { xValues[i] = 1. / zero; yValues[i] = 1. / zero; } regObj.regress(xValues, yValues, degree); }
/** * */ @Test(expectedExceptions = IllegalArgumentException.class) public void NaNTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 4; final int nPts = 5; double[] xValues = new double[nPts]; double[] yValues = new double[nPts]; for (int i = 0; i < nPts; ++i) { xValues[i] = Double.NaN; yValues[i] = Double.NaN; } regObj.regress(xValues, yValues, degree); }
ArgChecker.isFalse(vandNorm > 1e9, "Too large input data or too many degrees"); return regress(xDataMatrix, yDataVector, nData, degree);
/** * 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); }