<commit_before>/**
 * Copyright (C) 2013 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */
package com.opengamma.analytics.math.interpolation;

import static org.testng.Assert.assertEquals;

import org.testng.annotations.Test;

import com.opengamma.analytics.math.function.PiecewisePolynomialWithSensitivityFunction1D;
import com.opengamma.analytics.math.interpolation.data.Interpolator1DDataBundle;
import com.opengamma.analytics.math.interpolation.data.Interpolator1DPiecewisePoynomialDataBundle;
import com.opengamma.analytics.math.matrix.DoubleMatrix2D;

/**
 * 
 */
public class LogNaturalCubicMonotonicityPreservingInterpolator1DTest {

  /**
   * Parameter for centered finite difference method
   */
  private static final double EPS = 1.e-7;

  /**
   * 
   */
  @Test
  public void sampleDiscountFactorTest() {
    final double[] xValues = new double[] {0.0027378507871300e0, 0.0054757015742600e0, 0.0082135523614000e0, 0.2491444216290200e0, 0.5010266940451800e0, 0.7501711156742000e0, 0.9993155373032200e0,
        1.2511978097193699e0, 1.5003422313483901e0, 1.7494866529774100e0, 2.0013689253935700e0, 3.0006844626967801e0, 4.0000000000000000e0, 4.9993155373032199e0, 6.0013689253935700e0,
        7.0006844626967801e0, 8.0000000000000000e0, 8.9993155373032199e0, 10.0013689253935993e0, 12.0000000000000000e0, 15.0006844626967997e0, 20.0000000000000000e0, 24.9993155373032003e0,
        30.0013689253935993e0, 40.0000000000000000e0, 50.0013689253935993e0 };
    final double[] yValues = new double[] {0.99999593708369e0, 0.99999187419186e0, 0.99998781131373e0, 0.99963871457804e0, 0.99931197383134e0, 0.99899586019624e0, 0.99866966149844e0,
        0.99832564422180e0, 0.99792999296885e0, 0.99743454286751e0, 0.99682495767659e0, 0.99292937367859e0, 0.98496305698586e0, 0.97175664897203e0, 0.95354966017410e0, 0.93180043033936e0,
        0.90782313592531e0, 0.88229750227640e0, 0.85563390241246e0, 0.80140694953051e0, 0.72507946553451e0, 0.61947164354046e0, 0.53291256117173e0, 0.45861337697437e0, 0.35127110211518e0,
        0.28213093243876e0 };
    final int nData = 26;
    final double[] logY = new double[nData];

    for (int i = 0; i < nData; ++i) {
      logY[i] = Math.log(yValues[i]);
    }

    final double[] xKeys = new double[] {0.005063344512662158, 0.005139627715404907, 0.007960760570640704, 0.008179170667708428, 0.22910692625871368, 0.23266742356895445, 0.47859017867010933,
        0.49392862228176115, 0.6904378202261836, 0.7263996219722134, 0.8199005224855304, 0.945873126790612, 1.2498749135827558, 1.2507661649174142, 1.3539547623621575, 1.460371621341975,
        1.6308673887369953, 1.6774521371423725, 1.812262913758786, 1.8443590023112892, 2.512207186635973, 2.7902874895903347, 3.5970425685297793, 3.741479211510104, 4.954414808553191,
        4.95841711559786, 5.777693163699214, 5.966957943112449, 6.7619078149733625, 6.797340882922661, 7.755712878276715, 7.761246168446458, 8.264202963790074, 8.957012101380226, 9.670583873712175,
        9.9744531730438, 11.744410440279495, 11.98851589537167, 13.757067713855939, 13.85816318412105, 19.330436883319244, 19.705909739712666, 21.99262439001881, 24.38460449420516,
        27.179508865117416, 27.53957839017066, 30.222135785652547, 37.33113813086212, 46.96368200755808, 47.53142371305861 };
    final int nKeys = xKeys.length;
    final double[] expected = new double[] {0.99999248611457103, 0.99999237291298715, 0.99998818645318877, 0.99998786233578885, 0.99966645224297979, 0.99966149753818778, 0.99934021998070011,
        0.99932090829766274, 0.99907242015512665, 0.9990264143643216, 0.99890554044171165, 0.99874030223560051, 0.99832755119482808, 0.99832626661226365, 0.99817218413513975, 0.99799939541010563,
        0.99768395031950818, 0.99758892295068913, 0.99729267363216323, 0.9972175285149506, 0.99520743791165955, 0.99401887684461598, 0.9887707042535494, 0.9875037479908445, 0.97246312858285799,
        0.97240057799605961, 0.95798143421057969, 0.95424357758140332, 0.93724127896579301, 0.93644213223383377, 0.913846914817395, 0.91371154774487628, 0.90120456534821014, 0.88340289900659885,
        0.86452578663030977, 0.85636016639709245, 0.80828492265546992, 0.80171503936611166, 0.7555875393367949, 0.7530419913565255, 0.63227742511869778, 0.62505304217145563, 0.58326729332102834,
        0.54286498363894087, 0.49892233087124682, 0.49352999629538352, 0.45565965848843298, 0.37515433502472273, 0.30073974985150431, 0.29714840245998997 };

    final PiecewisePolynomialInterpolator prim = new LogNaturalSplineHelper();
    final PiecewisePolynomialInterpolator primOriginal = new LogNaturalSplineHelper();
    final PiecewisePolynomialInterpolator interp = new MonotonicityPreservingCubicSplineInterpolator(prim);
    final PiecewisePolynomialInterpolator interpOriginal = new MonotonicityPreservingCubicSplineInterpolator(primOriginal);
    final PiecewisePolynomialWithSensitivityFunction1D func = new PiecewisePolynomialWithSensitivityFunction1D();

    final PiecewisePolynomialResultsWithSensitivity result = interp.interpolateWithSensitivity(xValues, logY);
    final PiecewisePolynomialResultsWithSensitivity resultOriginal = interpOriginal.interpolateWithSensitivity(xValues, logY);

    final DoubleMatrix2D resCoef = result.getCoefMatrix();
    final DoubleMatrix2D resCoefOriginal = resultOriginal.getCoefMatrix();
    final DoubleMatrix2D[] resSense = result.getCoefficientSensitivityAll();
    final DoubleMatrix2D[] resOriginal = resultOriginal.getCoefficientSensitivityAll();
    for (int i = 0; i < nData - 1; ++i) {
      for (int j = 0; j < 4; ++j) {
        assertEquals(resCoef.getData()[i][j], resCoefOriginal.getData()[i][j], 1.e-15);
        for (int k = 0; k < nData; ++k) {
          assertEquals(resSense[i].getData()[j][k], resOriginal[i].getData()[j][k], 1.e-15);
        }
      }
    }

    for (int i = 0; i < nKeys; ++i) {
      final double res = Math.exp(func.evaluate(result, xKeys[i]).getEntry(0));
      assertEquals(res, expected[i], 1.e-15);
    }

    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(expected[i], res, 1.e-15);
    }

    /*
     * Test sensitivity
     */
    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];
      yValues1Dw[j] = yValues[j];
    }

    /*
     * Test new bundle
     */
    final double[] values = bundle.getValues();
    final double[] breaks = ((Interpolator1DPiecewisePoynomialDataBundle) bundle).getBreakPointsY();
    for (int i = 0; i < nData; ++i) {
      assertEquals(values[i], yValues[i], 1.e-15);
      assertEquals(breaks[i], yValues[i], 1.e-15);
      assertEquals(bundle.get(xValues[i]), yValues[i], 1.e-15);
    }
    assertEquals(values[0], bundle.firstValue(), 1.e-15);
    assertEquals(values[nData - 1], bundle.lastValue(), 1.e-15);
  }

  /**
   * Data contain minima
   */
  @Test
  public void localMinimumTest() {<commit_msg>[PLAT-4093] Sample data test removed<commit_after>/**
 * Copyright (C) 2013 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */
package com.opengamma.analytics.math.interpolation;

import static org.testng.Assert.assertEquals;

import org.testng.annotations.Test;

import com.opengamma.analytics.math.interpolation.data.Interpolator1DDataBundle;

/**
 * 
 */
public class LogNaturalCubicMonotonicityPreservingInterpolator1DTest {

  /**
   * Parameter for centered finite difference method
   */
  private static final double EPS = 1.e-7;

  /**
   * Data contain minima
   */
  @Test
  public void localMinimumTest() {
{'fraction_non_alphanumeric': 0.11125114573785518, 'fraction_numerical': 0.31152612282309805, 'mean_word_length': 4.660830090791181, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 5, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '540707', 'n_tokens_mistral': 5040, 'n_tokens_neox': 3389, 'n_words': 533}