Пример #1
0
            //-------------------------------------------------------------------------
            protected internal override double doInterpolate(double xValue)
            {
                // x-value is less than the x-value of the last node (lowerIndex < intervalCount)
                int lowerIndex  = lowerBoundIndex(xValue, xValues);
                int higherIndex = lowerIndex + 1;

                // at start of curve
                if (lowerIndex == 0)
                {
                    RealPolynomialFunction1D quadratic = quadratics_Renamed[0];
                    double x = xValue - xValues[1];
                    return(quadratic.applyAsDouble(x));
                }
                // at end of curve
                if (higherIndex == intervalCount)
                {
                    RealPolynomialFunction1D quadratic = quadratics_Renamed[intervalCount - 2];
                    double x = xValue - xValues[intervalCount - 1];
                    return(quadratic.applyAsDouble(x));
                }
                // normal case
                RealPolynomialFunction1D quadratic1 = quadratics_Renamed[lowerIndex - 1];
                RealPolynomialFunction1D quadratic2 = quadratics_Renamed[higherIndex - 1];
                double w = WEIGHT_FUNCTION.getWeight((xValues[higherIndex] - xValue) / (xValues[higherIndex] - xValues[lowerIndex]));

                return(w * quadratic1.applyAsDouble(xValue - xValues[lowerIndex]) + (1 - w) * quadratic2.applyAsDouble(xValue - xValues[higherIndex]));
            }
Пример #2
0
            protected internal override double doFirstDerivative(double xValue)
            {
                int lowerIndex  = lowerBoundIndex(xValue, xValues);
                int higherIndex = lowerIndex + 1;

                RealPolynomialFunction1D[] quadFirstDerivative = quadraticsFirstDerivative_Renamed.get();
                // at start of curve, or only one interval
                if (lowerIndex == 0 || intervalCount == 1)
                {
                    RealPolynomialFunction1D quadraticFirstDerivative = quadFirstDerivative[0];
                    double x = xValue - xValues[1];
                    return(quadraticFirstDerivative.applyAsDouble(x));
                }
                // at end of curve
                if (higherIndex >= intervalCount)
                {
                    RealPolynomialFunction1D quadraticFirstDerivative = quadFirstDerivative[intervalCount - 2];
                    double x = xValue - xValues[intervalCount - 1];
                    return(quadraticFirstDerivative.applyAsDouble(x));
                }
                RealPolynomialFunction1D quadratic1 = quadratics_Renamed[lowerIndex - 1];
                RealPolynomialFunction1D quadratic2 = quadratics_Renamed[higherIndex - 1];
                RealPolynomialFunction1D quadratic1FirstDerivative = quadFirstDerivative[lowerIndex - 1];
                RealPolynomialFunction1D quadratic2FirstDerivative = quadFirstDerivative[higherIndex - 1];
                double w = WEIGHT_FUNCTION.getWeight((xValues[higherIndex] - xValue) / (xValues[higherIndex] - xValues[lowerIndex]));

                return(w * quadratic1FirstDerivative.applyAsDouble(xValue - xValues[lowerIndex]) + (1 - w) * quadratic2FirstDerivative.applyAsDouble(xValue - xValues[higherIndex]) + (quadratic2.applyAsDouble(xValue - xValues[higherIndex]) - quadratic1.applyAsDouble(xValue - xValues[lowerIndex])) / (xValues[higherIndex] - xValues[lowerIndex]));
            }
        /// <summary>
        /// Checks coefficients of polynomial f(x) are recovered and residuals, { y_i -f(x_i) }, are accurate
        /// </summary>
        public virtual void PolynomialFunctionRecoverTest()
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter();
            PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter();

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] coeff = new double[] {3.4, 5.6, 1.0, -4.0 };
            double[] coeff = new double[] { 3.4, 5.6, 1.0, -4.0 };

            DoubleFunction1D func = new RealPolynomialFunction1D(coeff);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int degree = coeff.length - 1;
            int degree = coeff.Length - 1;

            const int nPts = 7;

            double[] xValues = new double[nPts];
            double[] yValues = new double[nPts];

            for (int i = 0; i < nPts; ++i)
            {
                xValues[i] = -5.0 + 10 * i / (nPts - 1);
                yValues[i] = func.applyAsDouble(xValues[i]);
            }

            double[] yValuesNorm = new double[nPts];

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double mean = _meanCal.apply(xValues);
            double mean = _meanCal.apply(xValues);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double std = _stdCal.apply(xValues);
            double std = _stdCal.apply(xValues);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double ratio = mean / std;
            double ratio = mean / std;

            for (int i = 0; i < nPts; ++i)
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double tmp = xValues[i] / std - ratio;
                double tmp = xValues[i] / std - ratio;
                yValuesNorm[i] = func.applyAsDouble(tmp);
            }

            /// <summary>
            /// Tests for regress(..)
            /// </summary>

            LeastSquaresRegressionResult result = regObj.regress(xValues, yValues, degree);

            double[] coeffResult = result.Betas;

            for (int i = 0; i < degree + 1; ++i)
            {
                assertEquals(coeff[i], coeffResult[i], EPS * Math.Abs(coeff[i]));
            }

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] residuals = result.getResiduals();
            double[] residuals = result.Residuals;
            func = new RealPolynomialFunction1D(coeffResult);
            double[] yValuesFit = new double[nPts];
            for (int i = 0; i < nPts; ++i)
            {
                yValuesFit[i] = func.applyAsDouble(xValues[i]);
            }

            for (int i = 0; i < nPts; ++i)
            {
                assertEquals(Math.Abs(yValuesFit[i] - yValues[i]), 0.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.0;

            for (int i = 0; i < nPts; ++i)
            {
                sum += residuals[i] * residuals[i];
            }
            sum = Math.Sqrt(sum);

            /// <summary>
            /// Tests for regressVerbose(.., false)
            /// </summary>

            PolynomialsLeastSquaresFitterResult resultVer = regObj.regressVerbose(xValues, yValues, degree, false);

            coeffResult = resultVer.Coeff;
            func        = new RealPolynomialFunction1D(coeffResult);
            for (int i = 0; i < nPts; ++i)
            {
                yValuesFit[i] = func.applyAsDouble(xValues[i]);
            }

            assertEquals(nPts - (degree + 1), resultVer.Dof, 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.0, Math.Abs(yValues[i]) * EPS);
            }

            assertEquals(sum, resultVer.DiffNorm, EPS);

            /// <summary>
            /// Tests for regressVerbose(.., true)
            /// </summary>

            PolynomialsLeastSquaresFitterResult resultNorm = regObj.regressVerbose(xValues, yValuesNorm, degree, true);

            coeffResult = resultNorm.Coeff;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] meanAndStd = resultNorm.getMeanAndStd();
            double[] meanAndStd = resultNorm.MeanAndStd;

            assertEquals(nPts - (degree + 1), resultNorm.Dof, 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)
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double tmp = xValues[i] / std - ratio;
                double tmp = xValues[i] / std - ratio;
                yValuesFit[i] = func.applyAsDouble(tmp);
            }

            for (int i = 0; i < nPts; ++i)
            {
                assertEquals(Math.Abs(yValuesFit[i] - yValuesNorm[i]), 0.0, Math.Abs(yValuesNorm[i]) * EPS);
            }

            sum = 0.0;
            for (int i = 0; i < nPts; ++i)
            {
                sum += (yValuesFit[i] - yValuesNorm[i]) * (yValuesFit[i] - yValuesNorm[i]);
            }
            sum = Math.Sqrt(sum);

            assertEquals(sum, resultNorm.DiffNorm, EPS);
        }