Package com.opengamma.analytics.math.interpolation.data

Examples of com.opengamma.analytics.math.interpolation.data.Interpolator1DDataBundle


  @Test
  public void testSingleDerivativeData() {
    final double x = 1.4;
    final double y = 0.34;
    Interpolator1DDataBundle dataBundle = INTERPOLATOR.getDataBundleFromSortedArrays(new double[] {x }, new double[] {y });
    double value = INTERPOLATOR.firstDerivative(dataBundle, x);
    assertEquals(0., value, 0.0);
  }
View Full Code Here


  @Test
  public void testTwoDerivativeData() {
    final double[] x = new double[] {1.4, 1.8 };
    final double[] y = new double[] {0.34, 0.56 };
    Interpolator1DDataBundle dataBundle = INTERPOLATOR.getDataBundleFromSortedArrays(x, y);
    double value = INTERPOLATOR.firstDerivative(dataBundle, 1.5);
    assertEquals((y[1] - y[0]) / (x[1] - x[0]), value, 0.0);
  }
View Full Code Here

    final double[] yData = new double[] {0.4, 0.5, 0.3, 0.8, 0.7, 1.0 };
    final double eps = 1.e-6;
    final double[] xKeys = new double[] {0.5, 0.77, 0.92, 1.13, 1.5 };
    final int nKeys = xKeys.length;

    final Interpolator1DDataBundle data = INTERPOLATOR.getDataBundle(xData, yData);
    for (int i = 0; i < nKeys; ++i) {
      double y = INTERPOLATOR.firstDerivative(data, xKeys[i]);
      assertEquals(0.5 * (INTERPOLATOR.interpolate(data, xKeys[i] + eps) - INTERPOLATOR.interpolate(data, xKeys[i] - eps)) / eps, y, eps);
    }
  }
View Full Code Here

    final double[] resPrim1 = INTERP.interpolate(xValues, yValues1, xKeys).getData();
    final double[] resPrim2 = INTERP.interpolate(xValues, yValues2, xKeys).getData();
    final double[] resPrim3 = INTERP.interpolate(xValues, yValues3, xKeys).getData();

    Interpolator1DDataBundle dataBund1 = INTERP1D.getDataBundle(xValues, yValues1);
    Interpolator1DDataBundle dataBund2 = INTERP1D.getDataBundle(xValues, yValues2);
    Interpolator1DDataBundle dataBund3 = INTERP1D.getDataBundleFromSortedArrays(xValues, yValues3);
    for (int i = 0; i < 10 * nData; ++i) {
      assertEquals(resPrim1[i], INTERP1D.interpolate(dataBund1, xKeys[i]), 1.e-15);
      assertEquals(resPrim2[i], INTERP1D.interpolate(dataBund2, xKeys[i]), 1.e-15);
      assertEquals(resPrim3[i], INTERP1D.interpolate(dataBund3, xKeys[i]), 1.e-15);
    }

    for (int j = 0; j < nData; ++j) {
      yValues1Up[j] = yValues1[j] == 0. ? EPS : yValues1[j] * (1. + EPS);
      yValues2Up[j] = yValues2[j] == 0. ? EPS : yValues2[j] * (1. + EPS);
      yValues3Up[j] = yValues3[j] == 0. ? EPS : yValues3[j] * (1. + EPS);
      yValues1Dw[j] = yValues1[j] == 0. ? -EPS : yValues1[j] * (1. - EPS);
      yValues2Dw[j] = yValues2[j] == 0. ? -EPS : yValues2[j] * (1. - EPS);
      yValues3Dw[j] = yValues3[j] == 0. ? -EPS : yValues3[j] * (1. - EPS);
      Interpolator1DDataBundle dataBund1Up = INTERP1D.getDataBundleFromSortedArrays(xValues, yValues1Up);
      Interpolator1DDataBundle dataBund2Up = INTERP1D.getDataBundleFromSortedArrays(xValues, yValues2Up);
      Interpolator1DDataBundle dataBund3Up = INTERP1D.getDataBundleFromSortedArrays(xValues, yValues3Up);
      Interpolator1DDataBundle dataBund1Dw = INTERP1D.getDataBundleFromSortedArrays(xValues, yValues1Dw);
      Interpolator1DDataBundle dataBund2Dw = INTERP1D.getDataBundleFromSortedArrays(xValues, yValues2Dw);
      Interpolator1DDataBundle dataBund3Dw = INTERP1D.getDataBundleFromSortedArrays(xValues, yValues3Dw);
      for (int i = 0; i < 10 * nData; ++i) {
        final double ref1 = yValues1[j] == 0. ? EPS : yValues1[j] * EPS;
        final double ref2 = yValues2[j] == 0. ? EPS : yValues2[j] * EPS;
        final double ref3 = yValues3[j] == 0. ? EPS : yValues3[j] * EPS;
        double res1 = 0.5 * (INTERP1D.interpolate(dataBund1Up, xKeys[i]) - INTERP1D.interpolate(dataBund1Dw, xKeys[i])) / ref1;
View Full Code Here

    final double xMax = xValues[nData - 1];
    for (int i = 0; i < 10 * nData; ++i) {
      xKeys[i] = xMin + (xMax - xMin) / (10 * nData - 1) * i;
    }

    Interpolator1DDataBundle dataBund = INTERP1D.getDataBundleFromSortedArrays(xValues, yValues);

    for (int j = 0; j < nData; ++j) {
      yValuesUp[j] = yValues[j] * (1. + EPS);
      yValuesDw[j] = yValues[j] * (1. - EPS);
      Interpolator1DDataBundle dataBundUp = INTERP1D.getDataBundleFromSortedArrays(xValues, yValuesUp);
      Interpolator1DDataBundle dataBundDw = INTERP1D.getDataBundleFromSortedArrays(xValues, yValuesDw);
      for (int i = 0; i < 10 * nData; ++i) {
        double res0 = 0.5 * (INTERP1D.interpolate(dataBundUp, xKeys[i]) - INTERP1D.interpolate(dataBundDw, xKeys[i])) / EPS / yValues[j];
        assertEquals(res0, INTERP1D.getNodeSensitivitiesForValue(dataBund, xKeys[i])[j], Math.max(Math.abs(yValues[j]) * EPS, EPS));
      }
      yValuesUp[j] = yValues[j];
View Full Code Here

    final double xMax = xValues[nData - 1];
    for (int i = 0; i < 10 * nData; ++i) {
      xKeys[i] = xMin + (xMax - xMin) / (10 * nData - 1) * i;
    }

    Interpolator1DDataBundle dataBund = INTERP1D.getDataBundleFromSortedArrays(xValues, yValues);

    for (int j = 0; j < nData; ++j) {
      yValuesUp[j] = yValues[j] * (1. + EPS);
      yValuesDw[j] = yValues[j] * (1. - EPS);
      Interpolator1DDataBundle dataBundUp = INTERP1D.getDataBundle(xValues, yValuesUp);
      Interpolator1DDataBundle dataBundDw = INTERP1D.getDataBundle(xValues, yValuesDw);
      for (int i = 0; i < 10 * nData; ++i) {
        double res0 = 0.5 * (INTERP1D.interpolate(dataBundUp, xKeys[i]) - INTERP1D.interpolate(dataBundDw, xKeys[i])) / EPS / yValues[j];
        assertEquals(res0, INTERP1D.getNodeSensitivitiesForValue(dataBund, xKeys[i])[j], Math.max(Math.abs(yValues[j]) * EPS, EPS));
      }
      yValuesUp[j] = yValues[j];
View Full Code Here

     * Linear Interpolation
     */

    CubicSplineInterpolator interp = new CubicSplineInterpolator();

    Interpolator1DDataBundle dataBundLin1 = INTERP1D.getDataBundleFromSortedArrays(xValuesForLin, yValuesForLin1);
    Interpolator1DDataBundle dataBundLin2 = INTERP1D.getDataBundleFromSortedArrays(xValuesForLin, yValuesForLin2);

    for (int i = 0; i < keyLength; ++i) {
      final double val = interp.interpolate(xValuesForLin, yValuesForLin1, xKeys[i]);
      final double ref = val == 0. ? 1. : Math.abs(val);
      assertEquals(val, INTERP1D.interpolate(dataBundLin1, xKeys[i]), ref * 1.e-15);
    }
    for (int i = 0; i < keyLength; ++i) {
      final double val = interp.interpolate(xValuesForLin, yValuesForLin2, xKeys[i]);
      final double ref = val == 0. ? 1. : Math.abs(val);
      assertEquals(val, INTERP1D.interpolate(dataBundLin2, xKeys[i]), ref * 1.e-15);
    }

    for (int j = 0; j < 2; ++j) {
      yValuesForLin1Up[j] = yValuesForLin1[j] == 0. ? EPS : yValuesForLin1[j] * (1. + EPS);
      yValuesForLin2Up[j] = yValuesForLin2[j] == 0. ? EPS : yValuesForLin2[j] * (1. + EPS);
      yValuesForLin1Dw[j] = yValuesForLin1[j] == 0. ? EPS : yValuesForLin1[j] * (1. - EPS);
      yValuesForLin2Dw[j] = yValuesForLin2[j] == 0. ? EPS : yValuesForLin2[j] * (1. - EPS);
      Interpolator1DDataBundle dataBund1Up = INTERP1D.getDataBundleFromSortedArrays(xValuesForLin, yValuesForLin1Up);
      Interpolator1DDataBundle dataBund2Up = INTERP1D.getDataBundleFromSortedArrays(xValuesForLin, yValuesForLin2Up);
      Interpolator1DDataBundle dataBund1Dw = INTERP1D.getDataBundleFromSortedArrays(xValuesForLin, yValuesForLin1Dw);
      Interpolator1DDataBundle dataBund2Dw = INTERP1D.getDataBundleFromSortedArrays(xValuesForLin, yValuesForLin2Dw);
      for (int i = 0; i < keyLength; ++i) {
        final double ref1 = yValuesForLin1[j] == 0. ? EPS : yValuesForLin1[j] * EPS;
        final double ref2 = yValuesForLin2[j] == 0. ? EPS : yValuesForLin2[j] * EPS;
        double res1 = 0.5 * (INTERP1D.interpolate(dataBund1Up, xKeys[i]) - INTERP1D.interpolate(dataBund1Dw, xKeys[i])) / ref1;
        double res2 = 0.5 * (INTERP1D.interpolate(dataBund2Up, xKeys[i]) - INTERP1D.interpolate(dataBund2Dw, xKeys[i])) / ref2;
        assertEquals(res1, INTERP1D.getNodeSensitivitiesForValue(dataBundLin1, xKeys[i])[j], Math.max(Math.abs(yValuesForLin1[j]) * EPS, EPS));
        assertEquals(res2, INTERP1D.getNodeSensitivitiesForValue(dataBundLin2, xKeys[i])[j], Math.max(Math.abs(yValuesForLin2[j]) * EPS, EPS));
      }
      yValuesForLin1Up[j] = yValuesForLin1[j];
      yValuesForLin2Up[j] = yValuesForLin2[j];
      yValuesForLin1Dw[j] = yValuesForLin1[j];
      yValuesForLin2Dw[j] = yValuesForLin2[j];
    }

    /**
     * Quadratic Interpolation
     */

    Interpolator1DDataBundle dataBundQuad1 = INTERP1D.getDataBundleFromSortedArrays(xValuesForQuad, yValuesForQuad1);
    Interpolator1DDataBundle dataBundQuad2 = INTERP1D.getDataBundleFromSortedArrays(xValuesForQuad, yValuesForQuad2);

    for (int i = 0; i < keyLength; ++i) {
      final double val = interp.interpolate(xValuesForQuad, yValuesForQuad1, xKeys[i]);
      final double ref = val == 0. ? 1. : Math.abs(val);
      assertEquals(val, INTERP1D.interpolate(dataBundQuad1, xKeys[i]), ref * 1.e-15);
    }
    for (int i = 0; i < keyLength; ++i) {
      final double val = interp.interpolate(xValuesForQuad, yValuesForQuad2, xKeys[i]);
      final double ref = val == 0. ? 1. : Math.abs(val);
      assertEquals(val, INTERP1D.interpolate(dataBundQuad2, xKeys[i]), ref * 1.e-15);
    }

    for (int j = 0; j < 3; ++j) {
      yValuesForQuad1Up[j] = yValuesForQuad1[j] == 0. ? EPS : yValuesForQuad1[j] * (1. + EPS);
      yValuesForQuad2Up[j] = yValuesForQuad2[j] == 0. ? EPS : yValuesForQuad2[j] * (1. + EPS);
      yValuesForQuad1Dw[j] = yValuesForQuad1[j] == 0. ? EPS : yValuesForQuad1[j] * (1. - EPS);
      yValuesForQuad2Dw[j] = yValuesForQuad2[j] == 0. ? EPS : yValuesForQuad2[j] * (1. - EPS);
      Interpolator1DDataBundle dataBund1Up = INTERP1D.getDataBundleFromSortedArrays(xValuesForQuad, yValuesForQuad1Up);
      Interpolator1DDataBundle dataBund2Up = INTERP1D.getDataBundleFromSortedArrays(xValuesForQuad, yValuesForQuad2Up);
      Interpolator1DDataBundle dataBund1Dw = INTERP1D.getDataBundleFromSortedArrays(xValuesForQuad, yValuesForQuad1Dw);
      Interpolator1DDataBundle dataBund2Dw = INTERP1D.getDataBundleFromSortedArrays(xValuesForQuad, yValuesForQuad2Dw);
      for (int i = 0; i < keyLength; ++i) {
        final double ref1 = yValuesForQuad1[j] == 0. ? EPS : yValuesForQuad1[j] * EPS;
        final double ref2 = yValuesForQuad2[j] == 0. ? EPS : yValuesForQuad2[j] * EPS;
        double res1 = 0.5 * (INTERP1D.interpolate(dataBund1Up, xKeys[i]) - INTERP1D.interpolate(dataBund1Dw, xKeys[i])) / ref1;
        double res2 = 0.5 * (INTERP1D.interpolate(dataBund2Up, xKeys[i]) - INTERP1D.interpolate(dataBund2Dw, xKeys[i])) / ref2;
View Full Code Here

    final int nKeys = xKeys.length;
    final double[] expected = new double[] {11.000000000000002, 9.8292188543753305, 8.8720987912733342, 7.9999999999999982, 7.5805212645035995, 6.8919800777834626, 4.9999999999999991,
        3.1906863006920174, 1.8676668553236566, 1.0009999999999999, 0.77713354870874862, 0.77713354870874862, 1.0009999999999999, 1.5674583835931175, 3.1906863006920183, 4.9999999999999991,
        6.5303730578459076, 7.5805212645035995, 7.9999999999999982, 8.6084760049832294, 9.8292188543753305, 11.000000000000002 };
    final Interpolator1D interpWrap = Interpolator1DFactory.getInterpolator(Interpolator1DFactory.LOG_NATURAL_CUBIC_MONOTONE);
    final Interpolator1DDataBundle bundle = interpWrap.getDataBundle(xValues, yValues);
    for (int i = 0; i < nKeys; ++i) {
      final double res = interpWrap.interpolate(bundle, xKeys[i]);
      assertEquals(res, expected[i], expected[i] * 1.e-15);
    }

    /*
     * Test sensitivity
     */
    final int nData = 8;
    final double[] yValues1Up = new double[nData];
    final double[] yValues1Dw = new double[nData];
    for (int i = 0; i < nData; ++i) {
      yValues1Up[i] = yValues[i];
      yValues1Dw[i] = yValues[i];
    }
    for (int j = 0; j < nData; ++j) {
      yValues1Up[j] = yValues[j] * (1. + EPS);
      yValues1Dw[j] = yValues[j] * (1. - EPS);
      Interpolator1DDataBundle dataBund1Up = interpWrap.getDataBundleFromSortedArrays(xValues, yValues1Up);
      Interpolator1DDataBundle dataBund1Dw = interpWrap.getDataBundleFromSortedArrays(xValues, yValues1Dw);
      for (int i = 0; i < nKeys; ++i) {
        double res1 = 0.5 * (interpWrap.interpolate(dataBund1Up, xKeys[i]) - interpWrap.interpolate(dataBund1Dw, xKeys[i])) / EPS / yValues[j];
        assertEquals(res1, interpWrap.getNodeSensitivitiesForValue(bundle, xKeys[i])[j], Math.max(Math.abs(yValues[j]) * EPS, EPS));
      }
      yValues1Up[j] = yValues[j];
View Full Code Here

    final int nKeys = xKeys.length;
    final double[] expected = new double[] {1.0009999999999999, 1.0009999999999999, 1.0009999999999999, 1.0009999999999999, 1.3100031054806665, 2.3728322906920676, 4.9999999999999991,
        6.547464969992709, 7.3863523497161641, 7.9999999999999982, 8.7698200360758953, 10.147409335950455, 11.000000000000002, 11.32816743116623, 11.473242089855484, 12, 13.599314796416568,
        16.75801353957614, 17.999999999999996, 17.999999999999996, 17.999999999999996, 17.999999999999996 };
    final PiecewisePolynomialInterpolator1D interpWrap = new LogNaturalCubicMonotonicityPreservingInterpolator1D();
    final Interpolator1DDataBundle bundle = interpWrap.getDataBundle(xValues, yValues);
    for (int i = 0; i < nKeys; ++i) {
      final double res = interpWrap.interpolate(bundle, xKeys[i]);
      assertEquals(res, expected[i], expected[i] * 1.e-15);
    }

    /*
     * Test sensitivity
     */
    final int nData = 8;
    final double[] yValues1Up = new double[nData];
    final double[] yValues1Dw = new double[nData];
    for (int i = 0; i < nData; ++i) {
      yValues1Up[i] = yValues[i];
      yValues1Dw[i] = yValues[i];
    }
    for (int j = 0; j < nData; ++j) {
      yValues1Up[j] = yValues[j] * (1. + EPS);
      yValues1Dw[j] = yValues[j] * (1. - EPS);
      Interpolator1DDataBundle dataBund1Up = interpWrap.getDataBundleFromSortedArrays(xValues, yValues1Up);
      Interpolator1DDataBundle dataBund1Dw = interpWrap.getDataBundleFromSortedArrays(xValues, yValues1Dw);
      for (int i = 0; i < nKeys; ++i) {
        double res1 = 0.5 * (interpWrap.interpolate(dataBund1Up, xKeys[i]) - interpWrap.interpolate(dataBund1Dw, xKeys[i])) / EPS / yValues[j];
        assertEquals(res1, interpWrap.getNodeSensitivitiesForValue(bundle, xKeys[i])[j], Math.max(Math.abs(yValues[j]) * EPS, EPS));
      }
      yValues1Up[j] = yValues[j];
View Full Code Here

    final int nKeys = xKeys.length;
    final double[] expected = new double[] {1.0009999999999999, 1.0009999999999999, 1.0009999999999999, 1.0009999999999999, 1.0009999999999999, 1.0009999999999999, 1.0009999999999999,
        1.0009999999999999, 1.0009999999999999, 1.0009999999999999, 1.0009999999999999, 1.0009999999999999, 1.0009999999999999, 1.0009999999999999, 1.0009999999999999, 1.0009999999999999,
        1.0009999999999999, 1.0009999999999999, 1.0009999999999999, 1.0009999999999999, 1.0009999999999999, 1.0009999999999999 };
    final PiecewisePolynomialInterpolator1D interpWrap = new LogNaturalCubicMonotonicityPreservingInterpolator1D();
    final Interpolator1DDataBundle bundle = interpWrap.getDataBundle(xValues, yValues);
    for (int i = 0; i < nKeys; ++i) {
      final double res = interpWrap.interpolate(bundle, xKeys[i]);
      assertEquals(expected[i], res, expected[i] * 1.e-15);
    }

    /*
     * Test sensitivity
     */
    final int nData = 8;
    final double[] yValues1Up = new double[nData];
    final double[] yValues1Dw = new double[nData];
    for (int i = 0; i < nData; ++i) {
      yValues1Up[i] = yValues[i];
      yValues1Dw[i] = yValues[i];
    }
    for (int j = 0; j < nData; ++j) {
      yValues1Up[j] = yValues[j] * (1. + EPS);
      yValues1Dw[j] = yValues[j] * (1. - EPS);
      Interpolator1DDataBundle dataBund1Up = interpWrap.getDataBundleFromSortedArrays(xValues, yValues1Up);
      Interpolator1DDataBundle dataBund1Dw = interpWrap.getDataBundleFromSortedArrays(xValues, yValues1Dw);
      for (int i = 0; i < nKeys; ++i) {
        double res1 = 0.5 * (interpWrap.interpolate(dataBund1Up, xKeys[i]) - interpWrap.interpolate(dataBund1Dw, xKeys[i])) / EPS / yValues[j];
        assertEquals(res1, interpWrap.getNodeSensitivitiesForValue(bundle, xKeys[i])[j], Math.max(Math.abs(yValues[j]) * EPS, EPS));
      }
      yValues1Up[j] = yValues[j];
View Full Code Here

TOP

Related Classes of com.opengamma.analytics.math.interpolation.data.Interpolator1DDataBundle

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.