public void PolynomialFunctionRecoverTest() {
final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter();
final double[] coeff = new double[] {3.4, 5.6, 1., -4. };
DoubleFunction1D func = new RealPolynomialFunction1D(coeff);
final int degree = coeff.length - 1;
final int nPts = 7;
double[] xValues = new double[nPts];
double[] yValues = new double[nPts];
for (int i = 0; i < nPts; ++i) {
xValues[i] = -5. + 10 * i / (nPts - 1);
yValues[i] = func.evaluate(xValues[i]);
}
double[] yValuesNorm = new double[nPts];
final double mean = _meanCal.evaluate(xValues);
final double std = _stdCal.evaluate(xValues);
final double ratio = mean / std;
for (int i = 0; i < nPts; ++i) {
final double tmp = xValues[i] / std - ratio;
yValuesNorm[i] = func.evaluate(tmp);
}
/**
* Tests for regress(..)
*/
LeastSquaresRegressionResult result = regObj.regress(xValues, yValues, degree);
double[] coeffResult = result.getBetas();
for (int i = 0; i < degree + 1; ++i) {
assertEquals(coeff[i], coeffResult[i], EPS * Math.abs(coeff[i]));
}
final double[] residuals = result.getResiduals();
func = new RealPolynomialFunction1D(coeffResult);
double[] yValuesFit = new double[nPts];
for (int i = 0; i < nPts; ++i) {
yValuesFit[i] = func.evaluate(xValues[i]);
}
for (int i = 0; i < nPts; ++i) {
assertEquals(Math.abs(yValuesFit[i] - yValues[i]), 0., Math.abs(yValues[i]) * EPS);
}
for (int i = 0; i < nPts; ++i) {
assertEquals(Math.abs(yValuesFit[i] - yValues[i]), Math.abs(residuals[i]), Math.abs(yValues[i]) * EPS);
}
double sum = 0.;
for (int i = 0; i < nPts; ++i) {
sum += residuals[i] * residuals[i];
}
sum = Math.sqrt(sum);
/**
* Tests for regressVerbose(.., false)
*/
PolynomialsLeastSquaresFitterResult resultVer = regObj.regressVerbose(xValues, yValues, degree, false);
coeffResult = resultVer.getCoeff();
func = new RealPolynomialFunction1D(coeffResult);
for (int i = 0; i < nPts; ++i) {
yValuesFit[i] = func.evaluate(xValues[i]);
}
assertEquals(nPts - (degree + 1), resultVer.getDof(), 0);
for (int i = 0; i < degree + 1; ++i) {
assertEquals(coeff[i], coeffResult[i], EPS * Math.abs(coeff[i]));
}
for (int i = 0; i < nPts; ++i) {
assertEquals(Math.abs(yValuesFit[i] - yValues[i]), 0., Math.abs(yValues[i]) * EPS);
}
assertEquals(sum, resultVer.getDiffNorm(), EPS);
/**
* Tests for regressVerbose(.., true)
*/
PolynomialsLeastSquaresFitterResult resultNorm = regObj.regressVerbose(xValues, yValuesNorm, degree, true);
coeffResult = resultNorm.getCoeff();
final double[] meanAndStd = resultNorm.getMeanAndStd();
assertEquals(nPts - (degree + 1), resultNorm.getDof(), 0);
assertEquals(mean, meanAndStd[0], EPS);
assertEquals(std, meanAndStd[1], EPS);
for (int i = 0; i < degree + 1; ++i) {
assertEquals(coeff[i], coeffResult[i], EPS * Math.abs(coeff[i]));
}
func = new RealPolynomialFunction1D(coeffResult);
for (int i = 0; i < nPts; ++i) {
final double tmp = xValues[i] / std - ratio;
yValuesFit[i] = func.evaluate(tmp);
}
for (int i = 0; i < nPts; ++i) {
assertEquals(Math.abs(yValuesFit[i] - yValuesNorm[i]), 0., Math.abs(yValuesNorm[i]) * EPS);
}