public virtual void positiveDataTest() { DoubleArray xValues = DoubleArray.of(0.5, 1.0, 2.5, 4.2, 10.0, 15.0, 30.0); DoubleArray yValues = DoubleArray.of(4.0, 2.0, 1.0, 5.0, 10.0, 3.5, -2.0); int nData = yValues.size(); DoubleArray pValues = DoubleArray.of(nData, i => xValues.get(i) * yValues.get(i)); System.Func <double, bool> domain = (double?x) => { return(x.Value >= xValues.get(0) && x.Value <= xValues.get(nData - 1)); }; DoubleArray keys = DoubleArray.of(xValues.get(0), 0.7, 1.2, 7.8, 9.99, 17.52, 25.0, xValues.get(nData - 1)); int nKeys = keys.size(); BoundCurveInterpolator bound = INTERP.bind(xValues, yValues); BoundCurveInterpolator boundBase = BASE_INTERP.bind(xValues, pValues); System.Func <double, double> funcDeriv = x => bound.interpolate(x.Value); for (int i = 0; i < nKeys; ++i) { // interpolate assertEquals(bound.interpolate(keys.get(i)), boundBase.interpolate(keys.get(i)) / keys.get(i), TOL); // first derivative double firstExp = DIFF_CALC.differentiate(funcDeriv, domain).apply(keys.get(i)); assertEquals(bound.firstDerivative(keys.get(i)), firstExp, EPS); // parameter sensitivity int index = i; System.Func <DoubleArray, double> funcSensi = x => INTERP.bind(xValues, x).interpolate(keys.get(index)); DoubleArray sensExp = SENS_CALC.differentiate(funcSensi).apply(yValues); assertTrue(DoubleArrayMath.fuzzyEquals(bound.parameterSensitivity(keys.get(i)).toArray(), sensExp.toArray(), EPS)); } }
//------------------------------------------------------------------------- public virtual double interpolate(double x, double y) { // use each y-interpolator to find the z-value for each unique x DoubleArray zValuesEffective = DoubleArray.of(yInterpolators.Length, i => yInterpolators[i].interpolate(y)); // interpolate unique x-values against derived z-values return(xInterpolator.bind(xValuesUnique, zValuesEffective, xExtrapolatorLeft, xExtrapolatorRight).interpolate(x)); }
//------------------------------------------------------------------------- public virtual void test_interpolation() { BoundCurveInterpolator bci = DQ_INTERPOLATOR.bind(X_DATA, Y_DATA, FLAT_EXTRAPOLATOR, FLAT_EXTRAPOLATOR); for (int i = 0; i < X_TEST.Length; i++) { assertEquals(bci.interpolate(X_TEST[i]), Y_TEST[i], 1e-8); } }
//------------------------------------------------------------------------- public virtual void test_interpolation() { BoundCurveInterpolator bci = NATURAL_CUBLIC_SPLINE_INTERPOLATOR.bind(X_DATA, Y_DATA, FLAT_EXTRAPOLATOR, FLAT_EXTRAPOLATOR); for (int i = 0; i < X_DATA.size(); i++) { assertEquals(bci.interpolate(X_DATA.get(i)), Y_DATA.get(i), TOL); } for (int i = 0; i < X_TEST.size(); i++) { assertEquals(bci.interpolate(X_TEST.get(i)), Y_TEST.get(i), TOL); } }
/// <summary> /// Tests the extrapolation below the first expiry. /// </summary> public virtual void volatilityBelowFirstExpiry() { double forward = 1.40; double timeToExpiry = 0.05; double strike = 1.45; SmileDeltaParameters smile = SmileDeltaParameters.of(timeToExpiry, ATM.get(0), DELTA, DoubleArray.copyOf(RISK_REVERSAL.toArray()[0]), DoubleArray.copyOf(STRANGLE.toArray()[0])); DoubleArray strikes = smile.strike(forward); DoubleArray vol = smile.Volatility; double volExpected = INTERPOLATOR_STRIKE.bind(strikes, vol, FLAT, FLAT).interpolate(strike); double volComputed = SMILE_TERM.volatility(timeToExpiry, strike, forward); assertEquals(volComputed, volExpected, TOLERANCE_VOL, "Smile by delta term structure: vol interpolation on strike"); }
//------------------------------------------------------------------------- public virtual void test_lookup() { InterpolatedNodalCurve test = InterpolatedNodalCurve.of(METADATA, XVALUES, YVALUES, INTERPOLATOR); BoundCurveInterpolator interp = INTERPOLATOR.bind(XVALUES, YVALUES, FLAT_EXTRAPOLATOR, FLAT_EXTRAPOLATOR); assertThat(test.yValue(XVALUES.get(0))).isEqualTo(YVALUES.get(0)); assertThat(test.yValue(XVALUES.get(1))).isEqualTo(YVALUES.get(1)); assertThat(test.yValue(XVALUES.get(2))).isEqualTo(YVALUES.get(2)); assertThat(test.yValue(10d)).isEqualTo(interp.interpolate(10d)); assertThat(test.yValueParameterSensitivity(10d).MarketDataName).isEqualTo(CURVE_NAME); assertThat(test.yValueParameterSensitivity(10d).Sensitivity).isEqualTo(interp.parameterSensitivity(10d)); assertThat(test.firstDerivative(10d)).isEqualTo(interp.firstDerivative(10d)); }
//------------------------------------------------------------------------- // restricted constructor //JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @ImmutableConstructor private InterpolatedNodalCurve(CurveMetadata metadata, com.opengamma.strata.collect.array.DoubleArray xValues, com.opengamma.strata.collect.array.DoubleArray yValues, com.opengamma.strata.market.curve.interpolator.CurveInterpolator interpolator, com.opengamma.strata.market.curve.interpolator.CurveExtrapolator extrapolatorLeft, com.opengamma.strata.market.curve.interpolator.CurveExtrapolator extrapolatorRight) private InterpolatedNodalCurve(CurveMetadata metadata, DoubleArray xValues, DoubleArray yValues, CurveInterpolator interpolator, CurveExtrapolator extrapolatorLeft, CurveExtrapolator extrapolatorRight) { JodaBeanUtils.notNull(metadata, "metadata"); JodaBeanUtils.notNull(xValues, "times"); JodaBeanUtils.notNull(yValues, "values"); JodaBeanUtils.notNull(interpolator, "interpolator"); JodaBeanUtils.notNull(extrapolatorLeft, "extrapolatorLeft"); JodaBeanUtils.notNull(extrapolatorRight, "extrapolatorRight"); if (xValues.size() < 2) { throw new System.ArgumentException("Length of x-values must be at least 2"); } if (xValues.size() != yValues.size()) { throw new System.ArgumentException("Length of x-values and y-values must match"); } metadata.ParameterMetadata.ifPresent(@params => { if (xValues.size() != @params.size()) { throw new System.ArgumentException("Length of x-values and parameter metadata must match when metadata present"); } }); for (int i = 1; i < xValues.size(); i++) { if (xValues.get(i) <= xValues.get(i - 1)) { throw new System.ArgumentException("Array of x-values must be sorted and unique"); } } this.metadata = metadata; this.xValues = xValues; this.yValues = yValues; this.extrapolatorLeft = extrapolatorLeft; this.interpolator = interpolator; this.extrapolatorRight = extrapolatorRight; this.boundInterpolator = interpolator.bind(xValues, yValues, extrapolatorLeft, extrapolatorRight); this.parameterMetadata = IntStream.range(0, ParameterCount).mapToObj(i => getParameterMetadata(i)).collect(toImmutableList()); }
//------------------------------------------------------------------------- public virtual void test_interpolation() { BoundCurveInterpolator bci = LL_INTERPOLATOR.bind(X_DATA, Y_DATA, FLAT_EXTRAPOLATOR, FLAT_EXTRAPOLATOR); for (int i = 0; i < X_DATA.size(); i++) { assertEquals(bci.interpolate(X_DATA.get(i)), Y_DATA.get(i), TOL); } // log-linear same as linear where y-values have had log applied BoundCurveInterpolator bciLinear = CurveInterpolators.LINEAR.bind(X_DATA, Y_DATA_LOG, FLAT_EXTRAPOLATOR, FLAT_EXTRAPOLATOR); assertEquals(Math.Log(bci.interpolate(0.2)), bciLinear.interpolate(0.2), EPS); assertEquals(Math.Log(bci.interpolate(0.8)), bciLinear.interpolate(0.8), EPS); assertEquals(Math.Log(bci.interpolate(1.1)), bciLinear.interpolate(1.1), EPS); assertEquals(Math.Log(bci.interpolate(2.1)), bciLinear.interpolate(2.1), EPS); assertEquals(Math.Log(bci.interpolate(3.4)), bciLinear.interpolate(3.4), EPS); }