MonotoneConvexSplineInterpolator extends PiecewisePolynomialInterpolator { @Override public PiecewisePolynomialResult interpolate(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); final DoubleMatrix2D coefMatrixIntegrate = integration(_time, coefMatrix.getData()); for (int i = 0; i < coefMatrixIntegrate.getNumberOfRows(); ++i) { for (int j = 0; j < coefMatrixIntegrate.getNumberOfColumns(); ++j) { ArgumentChecker.isFalse(Double.isNaN(coefMatrixIntegrate.getData()[i][j]), "Too large input"); ArgumentChecker.isFalse(Double.isInfinite(coefMatrixIntegrate.getData()[i][j]), "Too large input"); } } return new PiecewisePolynomialResult(new DoubleMatrix1D(_time), coefMatrixIntegrate, coefMatrixIntegrate.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 public void linearTest() { final double[] xValues = new double[] {2., 3., 4., 5., 6. }; final double[] yValues = new double[] {2., 3., 4., 5., 6. }; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); final int nPts = 301; for (int i = 0; i < nPts; ++i) { final double key = 1. + 6. / (nPts - 1) * i; final double ref = key == 0. ? 1. : Math.abs(key); assertEquals(interpolator.interpolate(xValues, yValues, key), key, ref * EPS); } }
@Test public void monotonicTest() { final boolean print = false; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); final double[] xValues = new double[] {0., 0.3, 0.6, 1.5, 2.7, 3.4, 4.8, 5.9 }; final int nData = xValues.length; final double[] yValuesTmp = new double[] {1.0, 1.2, 1.5, 2.0, 2.1, 3.0, 3.1, 3.3 }; double[] yValues = new double[nData]; for (int i = 0; i < nData; ++i) { yValues[i] = yValuesTmp[i] * xValues[i]; } final int nPts = 300; double old = yValues[0] * xValues[0]; for (int i = 0; i < nPts; ++i) { final double key = .0 + i * 5.9 / (nPts - 1); final double value = interpolator.interpolate(xValues, yValues, key); if (print) { System.out.println(key + "\t" + value); } assertTrue(value >= old); old = value; } }
@Test(expectedExceptions = IllegalArgumentException.class) public void nonSupTest() { final double[] xValues = new double[] {1., 2., 3., 4. }; final double[][] yValues = new double[][] { {1., 3., 2., 1. }, {1., 3., 2., 1. } }; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullyTest() { double[] xValues = new double[] {1., 2., 3., 4. }; double[] yValues = new double[4]; yValues = null; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullxTest() { double[] yValues = new double[] {1., 2., 3., 4. }; double[] xValues = new double[4]; xValues = null; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullkeyVecTest() { double[] xValues = new double[] {1., 2., 3., 4. }; double[] yValues = new double[] {1., 3., 2., 1. }; double[] key = new double[] {2., 2.5 }; key = null; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolate(xValues, yValues, key); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullkeyMatTest() { final double[] xValues = new double[] {1., 2., 3., 4. }; final double[] yValues = new double[] {1., 3., 2., 1. }; double[][] key = new double[][] { {2., 2.5 }, {2., 2.5 } }; key = null; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolate(xValues, yValues, key); }
@Test(expectedExceptions = IllegalArgumentException.class) public void infxTest() { 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.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void infyTest() { 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.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nanxTest() { 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.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nanyTest() { 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.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void infKeyTest() { double[] xValues = new double[] {1., 2., 3., 4. }; double[] yValues = new double[] {1., 3., 2., 1. }; double key = INF; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolate(xValues, yValues, key); }
@Test(expectedExceptions = IllegalArgumentException.class) public void infKeyVecTest() { double[] xValues = new double[] {1., 2., 3., 4. }; double[] yValues = new double[] {1., 3., 2., 1. }; double[] key = new double[] {2., 2.5 }; key[1] = INF; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolate(xValues, yValues, key); }
@Test(expectedExceptions = IllegalArgumentException.class) public void infKeyMatTest() { final double[] xValues = new double[] {1., 2., 3., 4. }; final double[] yValues = new double[] {1., 3., 2., 1. }; double[][] key = new double[][] { {2., 2.5 }, {2., 2.5 } }; key[1][1] = INF; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolate(xValues, yValues, key); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nanKeyVecTest() { double[] xValues = new double[] {1., 2., 3., 4. }; double[] yValues = new double[] {1., 3., 2., 1. }; double[] key = new double[] {2., 2.5 }; key[1] = Double.NaN; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolate(xValues, yValues, key); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nanKeyMatTest() { final double[] xValues = new double[] {1., 2., 3., 4. }; final double[] yValues = new double[] {1., 3., 2., 1. }; double[][] key = new double[][] { {2., 2.5 }, {2., 2.5 } }; key[1][1] = Double.NaN; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolate(xValues, yValues, key); }
@Test(expectedExceptions = IllegalArgumentException.class) public void largeKeyTest() { 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.interpolate(xValues, yValues, key); }
@Test(expectedExceptions = IllegalArgumentException.class) public void largeKeyVecTest() { double[] xValues = new double[] {1., 2., 3., 4. }; double[] yValues = new double[] {1., 3., 2., 1. }; double[] key = new double[] {2., 2.5 }; key[1] = 1.e308; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolate(xValues, yValues, key); }
@Test(expectedExceptions = IllegalArgumentException.class) public void largeKeyMatTest() { final double[] xValues = new double[] {1., 2., 3., 4. }; final double[] yValues = new double[] {1., 3., 2., 1. }; double[][] key = new double[][] { {2., 2.5 }, {2., 2.5 } }; key[1][1] = 1.e308; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolate(xValues, yValues, key); }
@Test(expectedExceptions = IllegalArgumentException.class) public void shortDataLengthTest() { final double[] xValues = new double[] {1. }; final double[] yValues = new double[] {1. }; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void wrongDataLengthTest() { final double[] xValues = new double[] {1., 2., 3., 4. }; final double[] yValues = new double[] {1., 3., 2., 1., 2. }; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void repeatedDataTest() { final double[] xValues = new double[] {1., 2., 1., 4. }; final double[] yValues = new double[] {1., 3., 2., 1. }; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void zeroDataSpotsTest() { final double[] xValues = new double[] {0., 2., 1., 4. }; final double[] yValues = new double[] {1., 3., 2., 1. }; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); interpolator.interpolate(xValues, yValues); }
@Test(enabled = false) public void printTest2() { final double[] xValues = new double[] {0., 1., 2., 3., 4. }; final double[] yValues = new double[] {0., 1., 4., 9., 16. }; MonotoneConvexSplineInterpolator interpolator = new MonotoneConvexSplineInterpolator(); PiecewisePolynomialResult result = interpolator.interpolate(xValues, yValues); System.out.println(result.getCoefMatrix()); System.out.println(result.getKnots()); PiecewisePolynomialFunction1D func = new PiecewisePolynomialFunction1D(); final int nPts = 101; for (int i = 0; i < nPts; ++i) { final double key = 0. + 5. / (nPts - 1) * i; System.out.println(key + "\t" + interpolator.interpolate(xValues, yValues, key) + "\t" + func.integrate(result, 0., key)); } System.out.println("\n"); for (int i = 0; i < nPts; ++i) { final double key = 0. + 5. / (nPts - 1) * i; System.out.println(key + "\t" + func.evaluate(result, key).getData()[0]); } }