MonotoneConvexSplineInterpolator extends PiecewisePolynomialInterpolator { public PiecewisePolynomialResult interpolateFwds(final double[] xValues, final double[] yValues) { ArgumentChecker.notNull(xValues, "xValues"); ArgumentChecker.notNull(yValues, "yValues"); ArgumentChecker.isTrue(xValues.length == yValues.length, " xValues length = yValues length"); ArgumentChecker.isTrue(xValues.length > 1, "Data points should be more than 1"); final int nDataPts = xValues.length; for (int i = 0; i < nDataPts; ++i) { ArgumentChecker.isFalse(Double.isNaN(xValues[i]), "xData containing NaN"); ArgumentChecker.isFalse(Double.isInfinite(xValues[i]), "xData containing Infinity"); ArgumentChecker.isFalse(Double.isNaN(yValues[i]), "yData containing NaN"); ArgumentChecker.isFalse(Double.isInfinite(yValues[i]), "yData containing Infinity"); } for (int i = 0; i < nDataPts; ++i) { for (int j = i + 1; j < nDataPts; ++j) { ArgumentChecker.isFalse(xValues[i] == xValues[j], "xValues should be distinct"); } } for (int i = 0; i < nDataPts; ++i) { if (xValues[i] == 0.) { ArgumentChecker.isTrue(yValues[i] == 0., "r_i * t_i = 0 if t_i =0"); } } double[] spotTmp = new double[nDataPts]; for (int i = 0; i < nDataPts; ++i) { spotTmp[i] = xValues[i] == 0. ? 0. : yValues[i] / xValues[i]; } _time = Arrays.copyOf(xValues, nDataPts); _spotRates = Arrays.copyOf(spotTmp, nDataPts); ParallelArrayBinarySort.parallelBinarySort(_time, _spotRates); final DoubleMatrix2D coefMatrix = solve(_time, _spotRates); for (int i = 0; i < coefMatrix.getNumberOfRows(); ++i) { for (int j = 0; j < coefMatrix.getNumberOfColumns(); ++j) { ArgumentChecker.isFalse(Double.isNaN(coefMatrix.getData()[i][j]), "Too large input"); ArgumentChecker.isFalse(Double.isInfinite(coefMatrix.getData()[i][j]), "Too large input"); } } return new PiecewisePolynomialResult(new DoubleMatrix1D(_time), coefMatrix, coefMatrix.getNumberOfColumns(), 1); } MonotoneConvexSplineInterpolator(); @Override PiecewisePolynomialResult interpolate(final double[] xValues, final double[] yValues); @Override PiecewisePolynomialResult interpolate(final double[] xValues, final double[][] yValuesMatrix); @Override double interpolate(final double[] xValues, final double[] yValues, final double x); @Override DoubleMatrix1D interpolate(final double[] xValues, final double[] yValues, final double[] x); @Override DoubleMatrix2D interpolate(final double[] xValues, final double[] yValues, final double[][] xMatrix); @Override PiecewisePolynomialResultsWithSensitivity interpolateWithSensitivity(final double[] xValues, final double[] yValues); PiecewisePolynomialResult interpolateFwds(final double[] xValues, final double[] yValues); double interpolateFwds(final double[] xValues, final double[] yValues, final double x); DoubleMatrix1D interpolateFwds(final double[] xValues, final double[] yValues, final double[] x); DoubleMatrix2D interpolateFwds(final double[] xValues, final double[] yValues, final double[][] xMatrix);  }
@Test(enabled = false) public void discontTest() { final double[] xValues = new double[] {0., 0.1, 4., 10., 20., 30. }; final double[] yValues = new double[] {0., 5., 5., 5., 5., 4.5 }; final double[] fwdsExp = new double[] {0., 5., 5., 5., 4.25, 3.125 }; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); final int nPts = xValues.length; for (int i = 1; i < nPts; ++i) { final double key = xValues[i]; final double ref = yValues[i] == 0. ? 1. : Math.abs(yValues[i]); assertEquals(interpolator.interpolateFwds(xValues, yValues, key), yValues[i], ref * EPS); } System.out.println("\n"); for (int i = 1; i < nPts; ++i) { final double key = xValues[i]; final double ref = fwdsExp[i] == 0. ? 1. : Math.abs(fwdsExp[i]); assertEquals(interpolator.interpolateFwds(xValues, yValues, key), fwdsExp[i], ref * EPS); } }
@Test(expectedExceptions = IllegalArgumentException.class) public void infxFwdsTest() { final double[] xValues = new double[] {1., 2., 3., 4. }; final double[] yValues = new double[] {1., 3., 2., 1. }; xValues[3] = INF; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolateFwds(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void infyFwdsTest() { final double[] xValues = new double[] {1., 2., 3., 4. }; final double[] yValues = new double[] {1., 3., 2., 1. }; yValues[3] = INF; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolateFwds(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nanxFwdsTest() { final double[] xValues = new double[] {1., 2., 3., 4. }; final double[] yValues = new double[] {1., 3., 2., 1. }; xValues[3] = Double.NaN; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolateFwds(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nanyFwdsTest() { final double[] xValues = new double[] {1., 2., 3., 4. }; final double[] yValues = new double[] {1., 3., 2., 1. }; yValues[3] = Double.NaN; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolateFwds(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void infKeyFwdsTest() { double[] xValues = new double[] {1., 2., 3., 4. }; double[] yValues = new double[] {1., 3., 2., 1. }; double key = INF; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolateFwds(xValues, yValues, key); }
@Test(expectedExceptions = IllegalArgumentException.class) public void largeKeyFwdsTest() { double[] xValues = new double[] {1., 2., 3., 4. }; double[] yValues = new double[] {1., 3., 2., 1. }; double key = 1.e308; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolateFwds(xValues, yValues, key); }
@Test(expectedExceptions = IllegalArgumentException.class) public void shortDataLengthFwdsTest() { final double[] xValues = new double[] {1. }; final double[] yValues = new double[] {1. }; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolateFwds(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void wrongDataLengthFwdsTest() { final double[] xValues = new double[] {1., 2., 3., 4. }; final double[] yValues = new double[] {1., 3., 2., 1., 2. }; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolateFwds(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void repeatedDataFwdsTest() { final double[] xValues = new double[] {1., 2., 1., 4. }; final double[] yValues = new double[] {1., 3., 2., 1. }; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolateFwds(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void zeroDataFwdsTest() { final double[] xValues = new double[] {0., 2., 1., 4. }; final double[] yValues = new double[] {1., 3., 2., 1. }; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolateFwds(xValues, yValues); }