// Generic quantile computation with quantile details.
        private QuantileResult quantileDetails(double level, DoubleArray sample, bool isExtrapolated, bool isEs)
        {
            int nbData = sample.size();

            double[] w = weights(nbData);
            /* Sorting data and keeping weight information. The arrays are modified */
            double[] s = sample.toArray();
            DoubleArrayMath.sortPairs(s, w);

            double[] s2    = sample.toArray();
            double[] order = new double[s2.Length];
            for (int i = 0; i < s2.Length; i++)
            {
                order[i] = i;
            }
            DoubleArrayMath.sortPairs(s2, order);
            /* Find the index. */
            double runningWeight = 0.0d;
            int    index         = nbData;

            while (runningWeight < 1.0d - level)
            {
                index--;
                runningWeight += w[index];
            }
            if (isEs)
            {
                return(esFromIndexRunningWeight(index, runningWeight, s2, w, order, level));
            }
            return(quantileFromIndexRunningWeight(index, runningWeight, isExtrapolated, s2, w, order, level));
        }
Exemplo n.º 2
0
        /// <param name="xValues"> X values of data </param>
        /// <param name="yValues"> Y values of data </param>
        /// <returns> <seealso cref="PiecewisePolynomialResult"/> containing knots, coefficients of piecewise polynomials, number of intervals, degree of polynomials, dimension of spline </returns>
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: @Override public PiecewisePolynomialResult interpolate(final double[] xValues, final double[] yValues)
        public override PiecewisePolynomialResult interpolate(double[] xValues, double[] yValues)
        {
            ArgChecker.notNull(xValues, "xValues");
            ArgChecker.notNull(yValues, "yValues");

            ArgChecker.isTrue(xValues.Length == yValues.Length, "xValues length = yValues length");
            ArgChecker.isTrue(xValues.Length > 1, "Data points should be more than 1");

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

            for (int i = 0; i < nDataPts; ++i)
            {
                ArgChecker.isFalse(double.IsNaN(xValues[i]), "xData containing NaN");
                ArgChecker.isFalse(double.IsInfinity(xValues[i]), "xData containing Infinity");
                ArgChecker.isFalse(double.IsNaN(yValues[i]), "yData containing NaN");
                ArgChecker.isFalse(double.IsInfinity(yValues[i]), "yData containing Infinity");
            }

            for (int i = 0; i < nDataPts; ++i)
            {
                for (int j = i + 1; j < nDataPts; ++j)
                {
                    ArgChecker.isFalse(xValues[i] == xValues[j], "Data should be distinct");
                }
            }

            double[] xValuesSrt = new double[nDataPts];
            double[] yValuesSrt = new double[nDataPts];

            xValuesSrt = Arrays.copyOf(xValues, nDataPts);
            yValuesSrt = Arrays.copyOf(yValues, nDataPts);
            DoubleArrayMath.sortPairs(xValuesSrt, yValuesSrt);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix coefMatrix = this._solver.solve(xValuesSrt, yValuesSrt);
            DoubleMatrix coefMatrix = this._solver.solve(xValuesSrt, yValuesSrt);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nCoefs = coefMatrix.columnCount();
            int nCoefs = coefMatrix.columnCount();

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nInts = this._solver.getKnotsMat1D(xValuesSrt).size() - 1;
            int nInts = this._solver.getKnotsMat1D(xValuesSrt).size() - 1;

            for (int i = 0; i < nInts; ++i)
            {
                for (int j = 0; j < nCoefs; ++j)
                {
                    ArgChecker.isFalse(double.IsNaN(coefMatrix.get(i, j)), "Too large input");
                    ArgChecker.isFalse(double.IsInfinity(coefMatrix.get(i, j)), "Too large input");
                }
            }

            return(new PiecewisePolynomialResult(this._solver.getKnotsMat1D(xValuesSrt), coefMatrix, nCoefs, 1));
        }
        public override PiecewisePolynomialResult interpolate(double[] xValues, double[] yValues)
        {
            ArgChecker.notEmpty(xValues, "xValues");
            ArgChecker.notEmpty(yValues, "yValues");
            int nDataPts = xValues.Length;

            ArgChecker.isTrue(nDataPts > 1, "at least two data points required");
            ArgChecker.isTrue(nDataPts == yValues.Length, "xValues length = yValues length");
            for (int i = 0; i < nDataPts; ++i)
            {
                ArgChecker.isFalse(double.IsNaN(xValues[i]), "xData containing NaN");
                ArgChecker.isFalse(double.IsInfinity(xValues[i]), "xData containing Infinity");
                ArgChecker.isFalse(double.IsNaN(yValues[i]), "yData containing NaN");
                ArgChecker.isFalse(double.IsInfinity(yValues[i]), "yData containing Infinity");
            }
            if (nDataPts == 1)
            {
                return(new PiecewisePolynomialResult(DoubleArray.copyOf(xValues), DoubleMatrix.filled(1, 1, yValues[0]), 1, 1));
            }

            for (int i = 0; i < nDataPts; ++i)
            {
                for (int j = i + 1; j < nDataPts; ++j)
                {
                    ArgChecker.isFalse(xValues[i] == xValues[j], "xValues should be distinct");
                }
            }

            double[] xValuesSrt = Arrays.copyOf(xValues, nDataPts);
            double[] yValuesSrt = Arrays.copyOf(yValues, nDataPts);
            DoubleArrayMath.sortPairs(xValuesSrt, yValuesSrt);

            DoubleMatrix coefMatrix = solve(xValuesSrt, yValuesSrt);

            for (int i = 0; i < coefMatrix.rowCount(); ++i)
            {
                for (int j = 0; j < coefMatrix.columnCount(); ++j)
                {
                    ArgChecker.isFalse(double.IsNaN(coefMatrix.get(i, j)), "Too large input");
                    ArgChecker.isFalse(double.IsInfinity(coefMatrix.get(i, j)), "Too large input");
                }
                double @ref     = 0.0;
                double interval = xValuesSrt[i + 1] - xValuesSrt[i];
                for (int j = 0; j < 2; ++j)
                {
                    @ref += coefMatrix.get(i, j) * Math.Pow(interval, 1 - j);
                    ArgChecker.isFalse(double.IsNaN(coefMatrix.get(i, j)), "Too large input");
                    ArgChecker.isFalse(double.IsInfinity(coefMatrix.get(i, j)), "Too large input");
                }
                double bound = Math.Max(Math.Abs(@ref) + Math.Abs(yValuesSrt[i + 1]), 1.e-1);
                ArgChecker.isTrue(Math.Abs(@ref - yValuesSrt[i + 1]) < ERROR * bound, "Input is too large/small or data are not distinct enough");
            }

            return(new PiecewisePolynomialResult(DoubleArray.copyOf(xValuesSrt), coefMatrix, coefMatrix.columnCount(), 1));
        }
        public override PiecewisePolynomialResult interpolate(double[] xValues, double[] yValues)
        {
            ArgChecker.notNull(xValues, "xValues");
            ArgChecker.notNull(yValues, "yValues");

            ArgChecker.isTrue(xValues.Length == yValues.Length, "(xValues length = yValues length) should be true");
            ArgChecker.isTrue(xValues.Length > 2, "Data points should be >= 3");

            int nDataPts = xValues.Length;

            for (int i = 0; i < nDataPts; ++i)
            {
                ArgChecker.isFalse(double.IsNaN(xValues[i]), "xValues containing NaN");
                ArgChecker.isFalse(double.IsInfinity(xValues[i]), "xValues containing Infinity");
                ArgChecker.isFalse(double.IsNaN(yValues[i]), "yValues containing NaN");
                ArgChecker.isFalse(double.IsInfinity(yValues[i]), "yValues containing Infinity");
            }

            for (int i = 0; i < nDataPts - 1; ++i)
            {
                for (int j = i + 1; j < nDataPts; ++j)
                {
                    ArgChecker.isFalse(xValues[i] == xValues[j], "xValues should be distinct");
                }
            }

            double[] xValuesSrt = Arrays.copyOf(xValues, nDataPts);
            double[] yValuesSrt = Arrays.copyOf(yValues, nDataPts);
            DoubleArrayMath.sortPairs(xValuesSrt, yValuesSrt);

            double[]   intervals = _solver.intervalsCalculator(xValuesSrt);
            double[]   slopes    = _solver.slopesCalculator(yValuesSrt, intervals);
            double[]   first     = firstDerivativeCalculator(slopes);
            double[][] coefs     = _solver.solve(yValuesSrt, intervals, slopes, first);

            for (int i = 0; i < nDataPts - 1; ++i)
            {
                double @ref = 0.0;
                for (int j = 0; j < 4; ++j)
                {
                    @ref += coefs[i][j] * Math.Pow(intervals[i], 3 - j);
                    ArgChecker.isFalse(double.IsNaN(coefs[i][j]), "Too large input");
                    ArgChecker.isFalse(double.IsInfinity(coefs[i][j]), "Too large input");
                }
                double bound = Math.Max(Math.Abs(@ref) + Math.Abs(yValuesSrt[i + 1]), 1.e-1);
                ArgChecker.isTrue(Math.Abs(@ref - yValuesSrt[i + 1]) < ERROR * bound, "Input is too large/small or data points are too close");
            }

            return(new PiecewisePolynomialResult(DoubleArray.copyOf(xValuesSrt), DoubleMatrix.copyOf(coefs), 4, 1));
        }
        private double[][] getDataTotal(double[] xData, double[] yData)
        {
            int nExtraPoints = _xValuesClamped.Length;
            int nData        = xData.Length;
            int nTotal       = nExtraPoints + nData;

            double[] xValuesTotal = new double[nTotal];
            double[] yValuesTotal = new double[nTotal];
            Array.Copy(xData, 0, xValuesTotal, 0, nData);
            Array.Copy(yData, 0, yValuesTotal, 0, nData);
            Array.Copy(_xValuesClamped, 0, xValuesTotal, nData, nExtraPoints);
            Array.Copy(_yValuesClamped, 0, yValuesTotal, nData, nExtraPoints);
            DoubleArrayMath.sortPairs(xValuesTotal, yValuesTotal);
            return(new double[][] { xValuesTotal, yValuesTotal });
        }
        protected internal override QuantileResult expectedShortfall(double level, DoubleArray sample)
        {
            ArgChecker.isTrue(level > 0, "Quantile should be above 0.");
            ArgChecker.isTrue(level < 1, "Quantile should be below 1.");
            int    sampleSize      = sampleCorrection(sample.size());
            double fractionalIndex = level * sampleSize + indexCorrection();
            double adjustedLevel   = checkIndex(fractionalIndex, sample.size(), true);

            double[] order = createIndexArray(sample.size());
            double[] s     = sample.toArray();
            DoubleArrayMath.sortPairs(s, order);
            int lowerIndex = (int)Math.Floor(adjustedLevel);
            int upperIndex = (int)Math.Ceiling(adjustedLevel);

            int[]    indices  = new int[upperIndex];
            double[] weights  = new double[upperIndex];
            double   interval = 1d / (double)sampleSize;

            weights[0] = interval * (Math.Min(fractionalIndex, 1d) - indexCorrection());
            double losses = s[0] * weights[0];

            for (int i = 0; i < lowerIndex - 1; i++)
            {
                losses         += 0.5 * (s[i] + s[i + 1]) * interval;
                indices[i]      = (int)order[i];
                weights[i]     += 0.5 * interval;
                weights[i + 1] += 0.5 * interval;
            }
            if (lowerIndex != upperIndex)
            {
                double lowerWeight = upperIndex - adjustedLevel;
                double upperWeight = 1d - lowerWeight;
                double quantile    = lowerWeight * s[lowerIndex - 1] + upperWeight * s[upperIndex - 1];
                losses += 0.5 * (s[lowerIndex - 1] + quantile) * interval * upperWeight;
                indices[lowerIndex - 1]  = (int)order[lowerIndex - 1];
                indices[upperIndex - 1]  = (int)order[upperIndex - 1];
                weights[lowerIndex - 1] += 0.5 * (1d + lowerWeight) * interval * upperWeight;
                weights[upperIndex - 1]  = 0.5 * upperWeight * interval * upperWeight;
            }
            if (fractionalIndex > sample.size())
            {
                losses += s[sample.size() - 1] * (fractionalIndex - sample.size()) * interval;
                indices[sample.size() - 1]  = (int)order[sample.size() - 1];
                weights[sample.size() - 1] += (fractionalIndex - sample.size()) * interval;
            }
            return(QuantileResult.of(losses / level, indices, DoubleArray.ofUnsafe(weights).dividedBy(level)));
        }
Exemplo n.º 7
0
        public override PiecewisePolynomialResultsWithSensitivity interpolateWithSensitivity(double[] xValues, double[] yValues)
        {
            ArgChecker.notNull(xValues, "xValues");
            ArgChecker.notNull(yValues, "yValues");
            ArgChecker.isTrue(xValues.Length == yValues.Length, "xValues length = yValues length");
            ArgChecker.isTrue(xValues.Length > 1, "Data points should be more than 1");
            int nDataPts = xValues.Length;

            for (int i = 0; i < nDataPts; ++i)
            {
                ArgChecker.isTrue(Double.isFinite(xValues[i]), "xData is not finite");
                ArgChecker.isTrue(Double.isFinite(yValues[i]), "yData is not finite");
            }
            double[] xValuesSrt = Arrays.copyOf(xValues, nDataPts);
            double[] yValuesSrt = Arrays.copyOf(yValues, nDataPts);
            DoubleArrayMath.sortPairs(xValuesSrt, yValuesSrt);
            for (int i = 1; i < nDataPts; ++i)
            {
                ArgChecker.isFalse(xValuesSrt[i - 1] == xValuesSrt[i], "xValues should be distinct");
            }

            DoubleMatrix[] temp = solve(xValuesSrt, yValuesSrt);
            // check the matrices
            int n = temp.Length;

            for (int k = 0; k < n; k++)
            {
                DoubleMatrix m    = temp[k];
                int          rows = m.rowCount();
                int          cols = m.columnCount();
                for (int i = 0; i < rows; ++i)
                {
                    for (int j = 0; j < cols; ++j)
                    {
                        ArgChecker.isTrue(Doubles.isFinite(m.get(i, j)), "Matrix contains a NaN or infinite");
                    }
                }
            }
            // copy
            DoubleMatrix coefMatrix = temp[0];

            DoubleMatrix[] coefMatrixSense = new DoubleMatrix[n - 1];
            Array.Copy(temp, 1, coefMatrixSense, 0, n - 1);

            return(new PiecewisePolynomialResultsWithSensitivity(DoubleArray.copyOf(xValuesSrt), coefMatrix, 4, 1, coefMatrixSense));
        }
        protected internal override QuantileResult quantile(double level, DoubleArray sample, bool isExtrapolated)
        {
            ArgChecker.isTrue(level > 0, "Quantile should be above 0.");
            ArgChecker.isTrue(level < 1, "Quantile should be below 1.");
            int    sampleSize    = sampleCorrection(sample.size());
            double adjustedLevel = checkIndex(level * sampleSize + indexCorrection(), sample.size(), isExtrapolated);

            double[] order = createIndexArray(sample.size());
            double[] s     = sample.toArray();
            DoubleArrayMath.sortPairs(s, order);
            int    lowerIndex  = (int)Math.Floor(adjustedLevel);
            int    upperIndex  = (int)Math.Ceiling(adjustedLevel);
            double lowerWeight = upperIndex - adjustedLevel;
            double upperWeight = 1d - lowerWeight;

            return(QuantileResult.of(lowerWeight * s[lowerIndex - 1] + upperWeight * s[upperIndex - 1], new int[] { (int)order[lowerIndex - 1], (int)order[upperIndex - 1] }, DoubleArray.of(lowerWeight, upperWeight)));
        }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: @Override public PiecewisePolynomialResult interpolate(final double[] xValues, final double[][] yValuesMatrix)
        public override PiecewisePolynomialResult interpolate(double[] xValues, double[][] yValuesMatrix)
        {
            ArgChecker.notNull(xValues, "xValues");
            ArgChecker.notNull(yValuesMatrix, "yValuesMatrix");

            ArgChecker.isTrue(xValues.Length == yValuesMatrix[0].Length, "(xValues length = yValuesMatrix's row vector length) should be true");
            ArgChecker.isTrue(xValues.Length > 1, "Data points should be >= 2");

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nDataPts = xValues.length;
            int nDataPts = xValues.Length;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int yValuesLen = yValuesMatrix[0].length;
            int yValuesLen = yValuesMatrix[0].Length;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int dim = yValuesMatrix.length;
            int dim = yValuesMatrix.Length;

            for (int i = 0; i < nDataPts; ++i)
            {
                ArgChecker.isFalse(double.IsNaN(xValues[i]), "xValues containing NaN");
                ArgChecker.isFalse(double.IsInfinity(xValues[i]), "xValues containing Infinity");
            }
            for (int i = 0; i < yValuesLen; ++i)
            {
                for (int j = 0; j < dim; ++j)
                {
                    ArgChecker.isFalse(double.IsNaN(yValuesMatrix[j][i]), "yValuesMatrix containing NaN");
                    ArgChecker.isFalse(double.IsInfinity(yValuesMatrix[j][i]), "yValuesMatrix containing Infinity");
                }
            }
            for (int i = 0; i < nDataPts; ++i)
            {
                for (int j = i + 1; j < nDataPts; ++j)
                {
                    ArgChecker.isFalse(xValues[i] == xValues[j], "xValues should be distinct");
                }
            }

            double[]       xValuesSrt = new double[nDataPts];
            DoubleMatrix[] coefMatrix = new DoubleMatrix[dim];

            for (int i = 0; i < dim; ++i)
            {
                xValuesSrt = Arrays.copyOf(xValues, nDataPts);
                double[] yValuesSrt = Arrays.copyOf(yValuesMatrix[i], nDataPts);
                DoubleArrayMath.sortPairs(xValuesSrt, yValuesSrt);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] intervals = _solver.intervalsCalculator(xValuesSrt);
                double[] intervals = _solver.intervalsCalculator(xValuesSrt);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] slopes = _solver.slopesCalculator(yValuesSrt, intervals);
                double[] slopes = _solver.slopesCalculator(yValuesSrt, intervals);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] first = firstDerivativeCalculator(slopes);
                double[] first = firstDerivativeCalculator(slopes);

                coefMatrix[i] = DoubleMatrix.copyOf(_solver.solve(yValuesSrt, intervals, slopes, first));

                for (int k = 0; k < intervals.Length; ++k)
                {
                    double @ref = 0.0;
                    for (int j = 0; j < 4; ++j)
                    {
                        @ref += coefMatrix[i].get(k, j) * Math.Pow(intervals[k], 3 - j);
                        ArgChecker.isFalse(double.IsNaN(coefMatrix[i].get(k, j)), "Too large input");
                        ArgChecker.isFalse(double.IsInfinity(coefMatrix[i].get(k, j)), "Too large input");
                    }
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double bound = Math.max(Math.abs(ref) + Math.abs(yValuesSrt[k + 1]), 1.e-1);
                    double bound = Math.Max(Math.Abs(@ref) + Math.Abs(yValuesSrt[k + 1]), 1.e-1);
                    ArgChecker.isTrue(Math.Abs(@ref - yValuesSrt[k + 1]) < ERROR * bound, "Input is too large/small or data points are too close");
                }
            }

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nIntervals = coefMatrix[0].rowCount();
            int nIntervals = coefMatrix[0].rowCount();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nCoefs = coefMatrix[0].columnCount();
            int nCoefs = coefMatrix[0].columnCount();

//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] resMatrix = new double[dim * nIntervals][nCoefs];
            double[][] resMatrix = RectangularArrays.ReturnRectangularDoubleArray(dim * nIntervals, nCoefs);

            for (int i = 0; i < nIntervals; ++i)
            {
                for (int j = 0; j < dim; ++j)
                {
                    resMatrix[dim * i + j] = coefMatrix[j].row(i).toArray();
                }
            }

            return(new PiecewisePolynomialResult(DoubleArray.copyOf(xValuesSrt), DoubleMatrix.copyOf(resMatrix), nCoefs, dim));
        }
Exemplo n.º 10
0
        /// <param name="xValues"> X values of data </param>
        /// <param name="yValuesMatrix"> Y values of data, where NumberOfRow defines dimension of the spline </param>
        /// <returns> <seealso cref="PiecewisePolynomialResult"/> containing knots, coefficients of piecewise polynomials, number of intervals, degree of polynomials, dimension of spline </returns>
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: @Override public PiecewisePolynomialResult interpolate(final double[] xValues, final double[][] yValuesMatrix)
        public override PiecewisePolynomialResult interpolate(double[] xValues, double[][] yValuesMatrix)
        {
            ArgChecker.notNull(xValues, "xValues");
            ArgChecker.notNull(yValuesMatrix, "yValuesMatrix");

            ArgChecker.isTrue(xValues.Length == yValuesMatrix[0].Length, "(xValues length = yValuesMatrix's row vector length)");
            ArgChecker.isTrue(xValues.Length > 1, "Data points should be more than 1");

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nDataPts = xValues.length;
            int nDataPts = xValues.Length;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int dim = yValuesMatrix.length;
            int dim = yValuesMatrix.Length;

            for (int i = 0; i < nDataPts; ++i)
            {
                ArgChecker.isFalse(double.IsNaN(xValues[i]), "xData containing NaN");
                ArgChecker.isFalse(double.IsInfinity(xValues[i]), "xData containing Infinity");
                for (int j = 0; j < dim; ++j)
                {
                    ArgChecker.isFalse(double.IsNaN(yValuesMatrix[j][i]), "yValuesMatrix containing NaN");
                    ArgChecker.isFalse(double.IsInfinity(yValuesMatrix[j][i]), "yValuesMatrix containing Infinity");
                }
            }

            for (int k = 0; k < dim; ++k)
            {
                for (int i = 0; i < nDataPts; ++i)
                {
                    for (int j = i + 1; j < nDataPts; ++j)
                    {
                        ArgChecker.isFalse(xValues[i] == xValues[j], "Data should be distinct");
                    }
                }
            }

            double[] xValuesSrt = new double[nDataPts];
//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] yValuesMatrixSrt = new double[dim][nDataPts];
            double[][] yValuesMatrixSrt = RectangularArrays.ReturnRectangularDoubleArray(dim, nDataPts);

            for (int i = 0; i < dim; ++i)
            {
                xValuesSrt = Arrays.copyOf(xValues, nDataPts);
                double[] yValuesSrt = Arrays.copyOf(yValuesMatrix[i], nDataPts);
                DoubleArrayMath.sortPairs(xValuesSrt, yValuesSrt);

                yValuesMatrixSrt[i] = Arrays.copyOf(yValuesSrt, nDataPts);
            }

            DoubleMatrix[] coefMatrix = this._solver.solveMultiDim(xValuesSrt, DoubleMatrix.copyOf(yValuesMatrixSrt));

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nIntervals = coefMatrix[0].rowCount();
            int nIntervals = coefMatrix[0].rowCount();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nCoefs = coefMatrix[0].columnCount();
            int nCoefs = coefMatrix[0].columnCount();

//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] resMatrix = new double[dim * nIntervals][nCoefs];
            double[][] resMatrix = RectangularArrays.ReturnRectangularDoubleArray(dim * nIntervals, nCoefs);

            for (int i = 0; i < nIntervals; ++i)
            {
                for (int j = 0; j < dim; ++j)
                {
                    resMatrix[dim * i + j] = coefMatrix[j].row(i).toArray();
                }
            }

            for (int i = 0; i < dim * nIntervals; ++i)
            {
                for (int j = 0; j < nCoefs; ++j)
                {
                    ArgChecker.isFalse(double.IsNaN(resMatrix[i][j]), "Too large input");
                    ArgChecker.isFalse(double.IsInfinity(resMatrix[i][j]), "Too large input");
                }
            }

            return(new PiecewisePolynomialResult(this._solver.getKnotsMat1D(xValuesSrt), DoubleMatrix.copyOf(resMatrix), nCoefs, dim));
        }
Exemplo n.º 11
0
        /// <summary>
        /// Clamped points
        /// </summary>
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void clampedTest()
        public virtual void clampedTest()
        {
            double[]   xValues           = new double[] { -5.0, -1.4, 3.2, 3.5, 7.6 };
            double[]   yValues           = new double[] { -2.2, 1.1, 1.9, 2.3, -0.1 };
            double[][] xValuesClampedSet = new double[][]
            {
                new double[] { 0.0 },
                new double[] { -7.2, -2.5, 8.45 },
                new double[] {}
            };
            double[][] yValuesClampedSet = new double[][]
            {
                new double[] { 0.0 },
                new double[] { -1.2, -1.4, 2.2 },
                new double[] {}
            };

            for (int k = 0; k < xValuesClampedSet.Length; ++k)
            {
                double[] xValuesClamped = Arrays.copyOf(xValuesClampedSet[k], xValuesClampedSet[k].Length);
                double[] yValuesClamped = Arrays.copyOf(yValuesClampedSet[k], yValuesClampedSet[k].Length);
                int      nData          = xValues.Length;
                int      nClamped       = xValuesClamped.Length;
                int      nTotal         = nData + nClamped;
                double[] xValuesForBase = new double[nTotal];
                double[] yValuesForBase = new double[nTotal];
                Array.Copy(xValues, 0, xValuesForBase, 0, nData);
                Array.Copy(yValues, 0, yValuesForBase, 0, nData);
                Array.Copy(xValuesClamped, 0, xValuesForBase, nData, nClamped);
                Array.Copy(yValuesClamped, 0, yValuesForBase, nData, nClamped);
                DoubleArrayMath.sortPairs(xValuesForBase, yValuesForBase);

                double[] xyValuesBase = new double[nTotal];
                for (int j = 0; j < nTotal; ++j)
                {
                    xyValuesBase[j] = xValuesForBase[j] * yValuesForBase[j];
                }
                int    nKeys    = 100;
                double interval = (xValues[nData - 1] - xValues[0]) / (nKeys - 1.0);

                int n = INTERP.Length;
                for (int i = 0; i < n; ++i)
                {
                    ProductPiecewisePolynomialInterpolator interp = new ProductPiecewisePolynomialInterpolator(INTERP[i], xValuesClamped, yValuesClamped);
                    for (int j = 0; j < nKeys; ++j)
                    {
                        double key = xValues[0] + interval * j;
                        InterpolatorTestUtil.assertRelative("clampedTest", INTERP[i].interpolate(xValuesForBase, xyValuesBase, key), interp.interpolate(xValues, yValues, key), EPS);
                    }
                }
                n = INTERP_SENSE.Length;
                for (int i = 0; i < n; ++i)
                {
                    ProductPiecewisePolynomialInterpolator    interp     = new ProductPiecewisePolynomialInterpolator(INTERP_SENSE[i], xValuesClamped, yValuesClamped);
                    PiecewisePolynomialResultsWithSensitivity result     = interp.interpolateWithSensitivity(xValues, yValues);
                    PiecewisePolynomialResultsWithSensitivity resultBase = INTERP_SENSE[i].interpolateWithSensitivity(xValuesForBase, xyValuesBase);
                    for (int j = 0; j < nKeys; ++j)
                    {
                        double key = xValues[0] + interval * j;
                        InterpolatorTestUtil.assertRelative("clampedTest", FUNC.evaluate(resultBase, key).get(0), FUNC.evaluate(result, key).get(0), EPS);
                        InterpolatorTestUtil.assertArrayRelative("clampedTest", FUNC.nodeSensitivity(resultBase, key).toArray(), FUNC.nodeSensitivity(result, key).toArray(), EPS);
                    }
                }
            }
        }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: @Override public PiecewisePolynomialResult interpolate(final double[] xValues, final double[] yValues)
        public override PiecewisePolynomialResult interpolate(double[] xValues, double[] yValues)
        {
            ArgChecker.notNull(xValues, "xValues");
            ArgChecker.notNull(yValues, "yValues");

            ArgChecker.isTrue(xValues.Length == yValues.Length | xValues.Length + 2 == yValues.Length, "(xValues length = yValues length) or (xValues length + 2 = yValues length)");
            ArgChecker.isTrue(xValues.Length > 2, "Data points should be more than 2");

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nDataPts = xValues.length;
            int nDataPts = xValues.Length;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int yValuesLen = yValues.length;
            int yValuesLen = yValues.Length;

            for (int i = 0; i < nDataPts; ++i)
            {
                ArgChecker.isFalse(double.IsNaN(xValues[i]), "xValues containing NaN");
                ArgChecker.isFalse(double.IsInfinity(xValues[i]), "xValues containing Infinity");
            }
            for (int i = 0; i < yValuesLen; ++i)
            {
                ArgChecker.isFalse(double.IsNaN(yValues[i]), "yValues containing NaN");
                ArgChecker.isFalse(double.IsInfinity(yValues[i]), "yValues containing Infinity");
            }

            for (int i = 0; i < nDataPts - 1; ++i)
            {
                for (int j = i + 1; j < nDataPts; ++j)
                {
                    ArgChecker.isFalse(xValues[i] == xValues[j], "xValues should be distinct");
                }
            }

            double[] xValuesSrt = Arrays.copyOf(xValues, nDataPts);
            double[] yValuesSrt = new double[nDataPts];
            if (nDataPts == yValuesLen)
            {
                yValuesSrt = Arrays.copyOf(yValues, nDataPts);
            }
            else
            {
                yValuesSrt = Arrays.copyOfRange(yValues, 1, nDataPts + 1);
            }
            DoubleArrayMath.sortPairs(xValuesSrt, yValuesSrt);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] intervals = _solver.intervalsCalculator(xValuesSrt);
            double[] intervals = _solver.intervalsCalculator(xValuesSrt);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] slopes = _solver.slopesCalculator(yValuesSrt, intervals);
            double[] slopes = _solver.slopesCalculator(yValuesSrt, intervals);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final PiecewisePolynomialResult result = _method.interpolate(xValues, yValues);
            PiecewisePolynomialResult result = _method.interpolate(xValues, yValues);

            ArgChecker.isTrue(result.Order == 4, "Primary interpolant is not cubic");

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] initialFirst = _function.differentiate(result, xValuesSrt).rowArray(0);
            double[] initialFirst = _function.differentiate(result, xValuesSrt).rowArray(0);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] first = firstDerivativeCalculator(yValuesSrt, intervals, slopes, initialFirst);
            double[] first = firstDerivativeCalculator(yValuesSrt, intervals, slopes, initialFirst);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[][] coefs = _solver.solve(yValuesSrt, intervals, slopes, first);
            double[][] coefs = _solver.solve(yValuesSrt, intervals, slopes, first);

            for (int i = 0; i < nDataPts - 1; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    ArgChecker.isFalse(double.IsNaN(coefs[i][j]), "Too large input");
                    ArgChecker.isFalse(double.IsInfinity(coefs[i][j]), "Too large input");
                }
            }

            return(new PiecewisePolynomialResult(DoubleArray.copyOf(xValuesSrt), DoubleMatrix.copyOf(coefs), 4, 1));
        }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: @Override public PiecewisePolynomialResult interpolate(final double[] xValues, final double[][] yValuesMatrix)
        public override PiecewisePolynomialResult interpolate(double[] xValues, double[][] yValuesMatrix)
        {
            ArgChecker.notNull(xValues, "xValues");
            ArgChecker.notNull(yValuesMatrix, "yValuesMatrix");

            ArgChecker.isTrue(xValues.Length == yValuesMatrix[0].Length | xValues.Length + 2 == yValuesMatrix[0].Length, "(xValues length = yValuesMatrix's row vector length) or (xValues length + 2 = yValuesMatrix's row vector length)");
            ArgChecker.isTrue(xValues.Length > 2, "Data points should be more than 2");

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nDataPts = xValues.length;
            int nDataPts = xValues.Length;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int yValuesLen = yValuesMatrix[0].length;
            int yValuesLen = yValuesMatrix[0].Length;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int dim = yValuesMatrix.length;
            int dim = yValuesMatrix.Length;

            for (int i = 0; i < nDataPts; ++i)
            {
                ArgChecker.isFalse(double.IsNaN(xValues[i]), "xValues containing NaN");
                ArgChecker.isFalse(double.IsInfinity(xValues[i]), "xValues containing Infinity");
            }
            for (int i = 0; i < yValuesLen; ++i)
            {
                for (int j = 0; j < dim; ++j)
                {
                    ArgChecker.isFalse(double.IsNaN(yValuesMatrix[j][i]), "yValuesMatrix containing NaN");
                    ArgChecker.isFalse(double.IsInfinity(yValuesMatrix[j][i]), "yValuesMatrix containing Infinity");
                }
            }
            for (int i = 0; i < nDataPts; ++i)
            {
                for (int j = i + 1; j < nDataPts; ++j)
                {
                    ArgChecker.isFalse(xValues[i] == xValues[j], "xValues should be distinct");
                }
            }

            double[]       xValuesSrt = new double[nDataPts];
            DoubleMatrix[] coefMatrix = new DoubleMatrix[dim];

            for (int i = 0; i < dim; ++i)
            {
                xValuesSrt = Arrays.copyOf(xValues, nDataPts);
                double[] yValuesSrt = new double[nDataPts];
                if (nDataPts == yValuesLen)
                {
                    yValuesSrt = Arrays.copyOf(yValuesMatrix[i], nDataPts);
                }
                else
                {
                    yValuesSrt = Arrays.copyOfRange(yValuesMatrix[i], 1, nDataPts + 1);
                }
                DoubleArrayMath.sortPairs(xValuesSrt, yValuesSrt);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] intervals = _solver.intervalsCalculator(xValuesSrt);
                double[] intervals = _solver.intervalsCalculator(xValuesSrt);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] slopes = _solver.slopesCalculator(yValuesSrt, intervals);
                double[] slopes = _solver.slopesCalculator(yValuesSrt, intervals);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final PiecewisePolynomialResult result = _method.interpolate(xValues, yValuesMatrix[i]);
                PiecewisePolynomialResult result = _method.interpolate(xValues, yValuesMatrix[i]);

                ArgChecker.isTrue(result.Order == 4, "Primary interpolant is not cubic");

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] initialFirst = _function.differentiate(result, xValuesSrt).rowArray(0);
                double[] initialFirst = _function.differentiate(result, xValuesSrt).rowArray(0);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] first = firstDerivativeCalculator(yValuesSrt, intervals, slopes, initialFirst);
                double[] first = firstDerivativeCalculator(yValuesSrt, intervals, slopes, initialFirst);

                coefMatrix[i] = DoubleMatrix.copyOf(_solver.solve(yValuesSrt, intervals, slopes, first));
            }

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nIntervals = coefMatrix[0].rowCount();
            int nIntervals = coefMatrix[0].rowCount();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nCoefs = coefMatrix[0].columnCount();
            int nCoefs = coefMatrix[0].columnCount();

//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] resMatrix = new double[dim * nIntervals][nCoefs];
            double[][] resMatrix = RectangularArrays.ReturnRectangularDoubleArray(dim * nIntervals, nCoefs);

            for (int i = 0; i < nIntervals; ++i)
            {
                for (int j = 0; j < dim; ++j)
                {
                    resMatrix[dim * i + j] = coefMatrix[j].row(i).toArray();
                }
            }

            for (int i = 0; i < (nIntervals * dim); ++i)
            {
                for (int j = 0; j < nCoefs; ++j)
                {
                    ArgChecker.isFalse(double.IsNaN(resMatrix[i][j]), "Too large input");
                    ArgChecker.isFalse(double.IsInfinity(resMatrix[i][j]), "Too large input");
                }
            }

            return(new PiecewisePolynomialResult(DoubleArray.copyOf(xValuesSrt), DoubleMatrix.copyOf(resMatrix), nCoefs, dim));
        }
        public override PiecewisePolynomialResult interpolate(double[] xValues, double[][] yValuesMatrix)
        {
            ArgChecker.notEmpty(xValues, "xValues");
            ArgChecker.notEmpty(yValuesMatrix, "yValuesMatrix");

            int nDataPts = xValues.Length;

            ArgChecker.isTrue(nDataPts > 1, "at least two data points required");
            ArgChecker.isTrue(nDataPts == yValuesMatrix[0].Length, "(xValues length = yValuesMatrix's row vector length)");
            int dim = yValuesMatrix.Length;

            for (int i = 0; i < nDataPts; ++i)
            {
                ArgChecker.isFalse(double.IsNaN(xValues[i]), "xData containing NaN");
                ArgChecker.isFalse(double.IsInfinity(xValues[i]), "xData containing Infinity");
                for (int j = 0; j < dim; ++j)
                {
                    ArgChecker.isFalse(double.IsNaN(yValuesMatrix[j][i]), "yValuesMatrix containing NaN");
                    ArgChecker.isFalse(double.IsInfinity(yValuesMatrix[j][i]), "yValuesMatrix containing Infinity");
                }
            }
            for (int k = 0; k < dim; ++k)
            {
                for (int i = 0; i < nDataPts; ++i)
                {
                    for (int j = i + 1; j < nDataPts; ++j)
                    {
                        ArgChecker.isFalse(xValues[i] == xValues[j], "xValues should be distinct");
                    }
                }
            }

            double[]       xValuesSrt = new double[nDataPts];
            DoubleMatrix[] coefMatrix = new DoubleMatrix[dim];

            for (int i = 0; i < dim; ++i)
            {
                xValuesSrt = Arrays.copyOf(xValues, nDataPts);
                double[] yValuesSrt = Arrays.copyOf(yValuesMatrix[i], nDataPts);
                DoubleArrayMath.sortPairs(xValuesSrt, yValuesSrt);

                coefMatrix[i] = solve(xValuesSrt, yValuesSrt);

                for (int k = 0; k < xValuesSrt.Length - 1; ++k)
                {
                    double @ref     = 0.0;
                    double interval = xValuesSrt[k + 1] - xValuesSrt[k];
                    for (int j = 0; j < 2; ++j)
                    {
                        @ref += coefMatrix[i].get(k, j) * Math.Pow(interval, 1 - j);
                        ArgChecker.isFalse(double.IsNaN(coefMatrix[i].get(k, j)), "Too large input");
                        ArgChecker.isFalse(double.IsInfinity(coefMatrix[i].get(k, j)), "Too large input");
                    }
                    double bound = Math.Max(Math.Abs(@ref) + Math.Abs(yValuesSrt[k + 1]), 1.e-1);
                    ArgChecker.isTrue(Math.Abs(@ref - yValuesSrt[k + 1]) < ERROR * bound, "Input is too large/small or data points are too close");
                }
            }

            int nIntervals = coefMatrix[0].rowCount();
            int nCoefs     = coefMatrix[0].columnCount();

//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] resMatrix = new double[dim * nIntervals][nCoefs];
            double[][] resMatrix = RectangularArrays.ReturnRectangularDoubleArray(dim * nIntervals, nCoefs);

            for (int i = 0; i < nIntervals; ++i)
            {
                for (int j = 0; j < dim; ++j)
                {
                    resMatrix[dim * i + j] = coefMatrix[j].row(i).toArray();
                }
            }

            return(new PiecewisePolynomialResult(DoubleArray.copyOf(xValuesSrt), DoubleMatrix.copyOf(resMatrix), nCoefs, dim));
        }