/** * Checks if this array equals another within the specified tolerance. * <p> * This returns true if the two instances have {@code double} values that are * equal within the specified tolerance. * * @param other the other array * @param tolerance the tolerance * @return true if equal up to the tolerance */ public boolean equalWithTolerance(DoubleArray other, double tolerance) { return DoubleArrayMath.fuzzyEquals(array, other.array, tolerance); }
public void test_fuzzyEquals() { assertThat(DoubleArrayMath.fuzzyEquals(DoubleArrayMath.EMPTY_DOUBLE_ARRAY, ARRAY_0_0, 1e-2)).isFalse(); assertThat(DoubleArrayMath.fuzzyEquals(ARRAY_0_0, ARRAY_0_0, 1e-2)).isTrue(); assertThat(DoubleArrayMath.fuzzyEquals(ARRAY_1_2, ARRAY_1_2, 1e-2)).isTrue(); assertThat(DoubleArrayMath.fuzzyEquals(ARRAY_1_2, ARRAY_1_2B, 1e-2)).isTrue(); assertThat(DoubleArrayMath.fuzzyEquals(ARRAY_1_2, ARRAY_1_2B, 1e-3)).isTrue(); assertThat(DoubleArrayMath.fuzzyEquals(ARRAY_1_2, ARRAY_1_2B, 1e-4)).isFalse(); }
public void test_formula() { CoxRossRubinsteinLatticeSpecification test = new CoxRossRubinsteinLatticeSpecification(); DoubleArray computed = test.getParametersTrinomial(VOL, RATE, DT); double u = Math.exp(VOL * Math.sqrt(2.0 * DT)); double d = Math.exp(-VOL * Math.sqrt(2.0 * DT)); double up = Math.pow((Math.exp(0.5 * RATE * DT) - Math.exp(-VOL * Math.sqrt(0.5 * DT))) / (Math.exp(VOL * Math.sqrt(0.5 * DT)) - Math.exp(-VOL * Math.sqrt(0.5 * DT))), 2); double dp = Math.pow((Math.exp(VOL * Math.sqrt(0.5 * DT)) - Math.exp(0.5 * RATE * DT)) / (Math.exp(VOL * Math.sqrt(0.5 * DT)) - Math.exp(-VOL * Math.sqrt(0.5 * DT))), 2); DoubleArray expected = DoubleArray.of(u, 1d, d, up, 1d - up - dp, dp); assertTrue(DoubleArrayMath.fuzzyEquals(computed.toArray(), expected.toArray(), 1.0e-14)); }
public void regression_hullWhiteSensitivity() { DoubleArray computed = PRICER.presentValueSensitivityModelParamsHullWhite(SWAPTION_PAY_LONG, RATE_PROVIDER, HW_PROVIDER); double[] expected = new double[] { 2.9365484063149095E7, 3.262667329294093E7, 7.226220286364576E7, 2.4446925038968167E8, 120476.73820821749}; assertTrue(DoubleArrayMath.fuzzyEquals(computed.toArray(), expected, NOTIONAL * TOL)); } }
public void test_formula() { TrigeorgisLatticeSpecification test = new TrigeorgisLatticeSpecification(); DoubleArray computed = test.getParametersTrinomial(VOL, RATE, DT); double dx = VOL * Math.sqrt(3d * DT); double nu = RATE - 0.5 * VOL * VOL; double u = Math.exp(dx); double d = Math.exp(-dx); double up = 0.5 * ((VOL * VOL * DT + nu * nu * DT * DT) / (dx * dx) + nu * DT / dx); double dm = 1d - (VOL * VOL * DT + nu * nu * DT * DT) / (dx * dx); double dp = 0.5 * ((VOL * VOL * DT + nu * nu * DT * DT) / (dx * dx) - nu * DT / dx); DoubleArray expected = DoubleArray.of(u, 1d, d, up, dm, dp); assertTrue(DoubleArrayMath.fuzzyEquals(computed.toArray(), expected.toArray(), 1.0e-14)); }
public void test_volatilityMaturityPart() { double u = 5.001332435062505; DoubleMatrix v = DoubleMatrix.copyOf(new double[][] {{5.012261396811139, 5.515068493150685, 6.010958904109589, 6.515068493150685, 7.010958904109589, 7.515068493150685, 8.01095890410959, 8.520458118122614, 9.017725877685455, 9.515068493150684, 10.013698630136986 } }); DoubleMatrix computed = MODEL.volatilityMaturityPart(MODEL_PARAMETERS, u, v); double[] expected = new double[] {0.010395243419747402, 0.48742124221025085, 0.9555417903726049, 1.4290478001940943, 1.8925104710768026, 2.361305017379811, 2.8201561576361778, 3.289235677728508, 3.7447552766260217, 4.198083407732067, 4.650327387669373 }; assertTrue(DoubleArrayMath.fuzzyEquals(computed.row(0).toArray(), expected, TOLERANCE_RATE)); }
public void present_value_hw_param_sensitivity_premium_forward() { DoubleArray hwTrade = PRICER_TRADE .presentValueSensitivityModelParamsHullWhite(SWAPTION_PREFWD_LONG_REC, MULTI_USD, HW_PROVIDER); DoubleArray hwProduct = PRICER_PRODUCT .presentValueSensitivityModelParamsHullWhite(SWAPTION_LONG_REC, MULTI_USD, HW_PROVIDER); assertTrue(DoubleArrayMath.fuzzyEquals(hwTrade.toArray(), hwProduct.toArray(), TOL * NOTIONAL)); }
public void regression_pvSensi() { PointSensitivityBuilder point = PRICER.presentValueSensitivityRates(CAPLET_REG, RATES, VOLS); CurrencyParameterSensitivities sensi = RATES.parameterSensitivity(point.build()); double[] sensiDsc = new double[] {0.0, 0.0, 0.0, -7.148360371957523, -1.8968344850148018, 0.0}; // 2.x double[] sensiFwd = new double[] {0.0, 0.0, 0.0, -3999.714444844649, 5987.977558683395, 0.0, 0.0, 0.0}; // 2.x assertTrue(DoubleArrayMath.fuzzyEquals( sensi.getSensitivity(IborCapletFloorletDataSet.DSC_NAME, EUR).getSensitivity().toArray(), sensiDsc, NOTIONAL * TOL)); assertTrue(DoubleArrayMath.fuzzyEquals( sensi.getSensitivity(IborCapletFloorletDataSet.FWD3_NAME, EUR).getSensitivity().toArray(), sensiFwd, NOTIONAL * TOL)); }
public void regression_sensitivity() { PointSensitivities point = PRICER.priceSensitivityRates(FUTURE, RATE_PROVIDER, HW_PROVIDER); CurrencyParameterSensitivities computed = RATE_PROVIDER.parameterSensitivity(point); double[] expected = new double[] {0.0, 0.0, 0.9514709785770106, -1.9399920741192112, 0.0, 0.0, 0.0, 0.0 }; assertEquals(computed.size(), 1); assertTrue(DoubleArrayMath.fuzzyEquals(computed.getSensitivity(HullWhiteIborFutureDataSet.FWD3_NAME, EUR) .getSensitivity().toArray(), expected, TOL)); } }
public void regression_pvSensi() { PointSensitivities point = PRICER.presentValueSensitivityRates(FUTURE_TRADE, RATE_PROVIDER, HW_PROVIDER); CurrencyParameterSensitivities computed = RATE_PROVIDER.parameterSensitivity(point); double[] expected = new double[] {0.0, 0.0, 9.514709785770103E7, -1.939992074119211E8, 0.0, 0.0, 0.0, 0.0 }; assertEquals(computed.size(), 1); assertTrue(DoubleArrayMath.fuzzyEquals(computed.getSensitivity(HullWhiteIborFutureDataSet.FWD3_NAME, EUR) .getSensitivity().toArray(), expected, NOTIONAL * QUANTITY * TOL)); } }
public void regression_curveSensitivity() { PointSensitivities point = PRICER.presentValueSensitivityRates(SWAPTION_PAY_LONG, RATE_PROVIDER, HW_PROVIDER).build(); CurrencyParameterSensitivities computed = RATE_PROVIDER.parameterSensitivity(point); double[] dscExp = new double[] {0.0, 0.0, 0.0, 0.0, -1.4127023229222856E7, -1.744958350376594E7}; double[] fwdExp = new double[] {0.0, 0.0, 0.0, 0.0, -2.0295973516660026E8, 4.12336887967829E8}; assertTrue(DoubleArrayMath.fuzzyEquals(computed.getSensitivity(HullWhiteIborFutureDataSet.DSC_NAME, EUR) .getSensitivity().toArray(), dscExp, NOTIONAL * TOL)); assertTrue(DoubleArrayMath.fuzzyEquals(computed.getSensitivity(HullWhiteIborFutureDataSet.FWD6_NAME, EUR) .getSensitivity().toArray(), fwdExp, NOTIONAL * TOL)); }
public void test_regression_single() { double[] expectedTimes = new double[] {4.852054794520548}; double[] expectedRates = new double[] {0.04666754810728295}; ImmutableList<CdsIndexIsdaCreditCurveNode> singleNode = CURVE_NODES.subList(1, 2); IsdaCreditCurveDefinition curveDefinition = IsdaCreditCurveDefinition.of( CURVE_NAME, EUR, VALUATION_DATE, ACT_365F, singleNode, true, false); LegalEntitySurvivalProbabilities creditCurve = CALIBRATOR.calibrate(curveDefinition, MARKET_DATA, RATES_PROVIDER, REF_DATA); NodalCurve curve = (NodalCurve) creditCurve.getSurvivalProbabilities().findData(CURVE_NAME).get(); assertTrue(DoubleArrayMath.fuzzyEquals(curve.getXValues().toArray(), expectedTimes, TOL)); assertTrue(DoubleArrayMath.fuzzyEquals(curve.getYValues().toArray(), expectedRates, TOL)); assertTrue(curve.getParameterMetadata(0) instanceof DatedParameterMetadata); double computedIndex = curve.getMetadata().getInfo(CurveInfoType.CDS_INDEX_FACTOR); assertEquals(computedIndex, 93.0 / 97.0, TOL); testJacobian(creditCurve, RATES_PROVIDER, singleNode, PUF_QUOTES); }
public void test_regression() { double[] expectedTimes = new double[] {2.852054794520548, 4.852054794520548, 6.854794520547945, 9.854794520547944}; double[] expectedRates = new double[] {0.03240798261187516, 0.04858422754375164, 0.0616141083562273, 0.06235460926516589}; IsdaCreditCurveDefinition curveDefinition = IsdaCreditCurveDefinition.of( CURVE_NAME, EUR, VALUATION_DATE, ACT_365F, CURVE_NODES, true, false); LegalEntitySurvivalProbabilities creditCurve = CALIBRATOR.calibrate(curveDefinition, MARKET_DATA, RATES_PROVIDER, REF_DATA); NodalCurve curve = (NodalCurve) creditCurve.getSurvivalProbabilities().findData(CURVE_NAME).get(); assertTrue(DoubleArrayMath.fuzzyEquals(curve.getXValues().toArray(), expectedTimes, TOL)); assertTrue(DoubleArrayMath.fuzzyEquals(curve.getYValues().toArray(), expectedRates, TOL)); assertTrue(curve.getParameterMetadata(0) instanceof DatedParameterMetadata); assertTrue(curve.getParameterMetadata(1) instanceof DatedParameterMetadata); assertTrue(curve.getParameterMetadata(2) instanceof DatedParameterMetadata); assertTrue(curve.getParameterMetadata(3) instanceof DatedParameterMetadata); double computedIndex = curve.getMetadata().getInfo(CurveInfoType.CDS_INDEX_FACTOR); assertEquals(computedIndex, 93.0 / 97.0, TOL); testJacobian(creditCurve, RATES_PROVIDER, CURVE_NODES, PUF_QUOTES); }
public void regressionCurveSensitivity() { double[] sensiDscExp = new double[] {0.0, 0.0, 0.0, 0.0, -1.1942174487944763E7, -1565567.6976298545}; double[] sensiFwdExp = new double[] {0.0, 0.0, 0.0, 0.0, -2.3978768078237808E8, 4.8392987803482056E8}; PointSensitivityBuilder point = PRICER.presentValueSensitivityRatesStickyModel(SWAPTION_PAY_LONG, RATE_PROVIDER, VOLS_REG); CurrencyParameterSensitivities sensi = RATE_PROVIDER.parameterSensitivity(point.build()); double[] sensiDscCmp = sensi.getSensitivity(SwaptionSabrRateVolatilityDataSet.META_DSC_EUR.getCurveName(), EUR) .getSensitivity().toArray(); double[] sensiFwdCmp = sensi.getSensitivity(SwaptionSabrRateVolatilityDataSet.META_FWD_EUR.getCurveName(), EUR) .getSensitivity().toArray(); assertTrue(DoubleArrayMath.fuzzyEquals(sensiDscCmp, sensiDscExp, TOL * NOTIONAL)); assertTrue(DoubleArrayMath.fuzzyEquals(sensiFwdCmp, sensiFwdExp, TOL * NOTIONAL)); }
public void test_zValue() { double tol = 1.0e-14; double x = 2.5; double y = 1.44; DeformedSurface test = DeformedSurface.of(METADATA, SURFACE_ORG, FUNCTION); double computedValue1 = test.zValue(x, y); double computedValue2 = test.zValue(DoublesPair.of(x, y)); UnitParameterSensitivity computedSensi1 = test.zValueParameterSensitivity(x, y); UnitParameterSensitivity computedSensi2 = test.zValueParameterSensitivity(DoublesPair.of(x, y)); ValueDerivatives expected = FUNCTION.apply(DoublesPair.of(x, y)); assertEquals(computedValue1, expected.getValue()); assertEquals(computedValue2, expected.getValue()); assertTrue(DoubleArrayMath.fuzzyEquals( computedSensi1.getSensitivity().toArray(), expected.getDerivatives().toArray(), tol)); assertTrue(DoubleArrayMath.fuzzyEquals( computedSensi2.getSensitivity().toArray(), expected.getDerivatives().toArray(), tol)); }
public void test_parameterSensitivity() { BoundCurveInterpolator bci = STEP_UPPER_INTERPOLATOR.bind(X_DATA, Y_DATA, FLAT_EXTRAPOLATOR, FLAT_EXTRAPOLATOR); for (int i = 0; i < X_DATA.size(); i++) { assertTrue(DoubleArrayMath.fuzzyEquals( bci.parameterSensitivity(X_DATA.get(i)).toArray(), DoubleArray.filled(SIZE).with(i, 1d).toArray(), TOL)); } for (int i = 0; i < X_TEST.size(); i++) { assertTrue(DoubleArrayMath.fuzzyEquals( bci.parameterSensitivity(X_TEST.get(i)).toArray(), DoubleArray.filled(SIZE).with(INDEX_TEST.get(i), 1d).toArray(), TOL)); } }
public void test_volatility() { SabrParametersIborCapletFloorletVolatilities prov = SabrParametersIborCapletFloorletVolatilities.of(NAME, EUR_EURIBOR_3M, DATE_TIME, PARAM); for (int i = 0; i < NB_TEST; i++) { for (int j = 0; j < NB_STRIKE; ++j) { double expiryTime = prov.relativeTime(TEST_OPTION_EXPIRY[i]); double volExpected = PARAM.volatility(expiryTime, TEST_STRIKE[j], TEST_FORWARD); double volComputed = prov.volatility(TEST_OPTION_EXPIRY[i], TEST_STRIKE[j], TEST_FORWARD); assertEquals(volComputed, volExpected, TOLERANCE_VOL); ValueDerivatives volAdjExpected = PARAM.volatilityAdjoint(expiryTime, TEST_STRIKE[j], TEST_FORWARD); ValueDerivatives volAdjComputed = prov.volatilityAdjoint(expiryTime, TEST_STRIKE[j], TEST_FORWARD); assertEquals(volAdjComputed.getValue(), volExpected, TOLERANCE_VOL); assertTrue(DoubleArrayMath.fuzzyEquals( volAdjComputed.getDerivatives().toArray(), volAdjExpected.getDerivatives().toArray(), TOLERANCE_VOL)); } } }
public void limitingTest() { BoundCurveInterpolator bci = LINEAR.bind( X_DATA, Y_DATA, DISCOUNT_FACTOR_QUADRATIC_LEFT_ZERO_RATE, PRODUCT_LINEAR); double small = 1.0e-8; assertEquals(bci.interpolate(small), bci.interpolate(10d * small), Y_DATA.get(0) * 10d * small); double derivative = bci.firstDerivative(small); double derivativeExp = (bci.interpolate(small + 0.5 * small) - bci.interpolate(small - 0.5 * small)) / small; assertEquals(derivative, derivativeExp, Y_DATA.get(0) * small); DoubleArray sensi = bci.parameterSensitivity(small); DoubleArray sensiS = bci.parameterSensitivity(small * 5d); assertTrue(DoubleArrayMath.fuzzyEquals( sensi.toArray(), sensiS.toArray(), Y_DATA.get(0) * 10d * small)); }
public void sensitivityTest() { int nExamples = Y.length; int n = XX.length; int nData = X.length; for (int example = 0; example < nExamples; example++) { PiecewisePolynomialResultsWithSensitivity pp = BASE.interpolateWithSensitivity(X, Y[example]); BoundCurveInterpolator bound = PCHIP.bind( DoubleArray.ofUnsafe(X), DoubleArray.ofUnsafe(Y[example]), INTERPOLATOR, INTERPOLATOR); for (int i = 0; i < n; i++) { DoubleArray computed = bound.parameterSensitivity(XX[i]); DoubleArray expected = PPVAL.nodeSensitivity(pp, XX[i]); for (int j = 0; j < nData; j++) { assertTrue(DoubleArrayMath.fuzzyEquals(computed.toArray(), expected.toArray(), TOL)); } } } }
public void test_presentValueSensitivityVolatility() { PointSensitivityBuilder capComputed = PRICER.presentValueSensitivityModelParamsVolatility(CAP, RATES, VOLS); PointSensitivityBuilder floorComputed = PRICER.presentValueSensitivityModelParamsVolatility(FLOOR, RATES, VOLS); CurrencyParameterSensitivities capExpected = CurrencyParameterSensitivities.empty(); CurrencyParameterSensitivities floorExpected = CurrencyParameterSensitivities.empty(); int nPeriods = CAP.getCapletFloorletPeriods().size(); for (int i = 0; i < nPeriods; ++i) { capExpected = capExpected.combinedWith(VOLS.parameterSensitivity(PRICER_PERIOD .presentValueSensitivityModelParamsVolatility(CAP.getCapletFloorletPeriods().get(i), RATES, VOLS).build())); floorExpected = floorExpected.combinedWith(VOLS.parameterSensitivity(PRICER_PERIOD .presentValueSensitivityModelParamsVolatility(FLOOR.getCapletFloorletPeriods().get(i), RATES, VOLS).build())); } CurrencyParameterSensitivities capSensiComputed = VOLS.parameterSensitivity(capComputed.build()); CurrencyParameterSensitivities floorSensiComputed = VOLS.parameterSensitivity(floorComputed.build()); CurrencyParameterSensitivity capSensiExpected = capExpected.getSensitivities().get(0); CurrencyParameterSensitivity floorSensiExpected = floorExpected.getSensitivities().get(0); assertTrue(DoubleArrayMath.fuzzyEquals(capSensiComputed.getSensitivities().get(0).getSensitivity().toArray(), capSensiExpected.getSensitivity().toArray(), TOL * NOTIONAL_VALUE)); assertTrue(DoubleArrayMath.fuzzyEquals(floorSensiComputed.getSensitivities().get(0).getSensitivity().toArray(), floorSensiExpected.getSensitivity().toArray(), TOL * NOTIONAL_VALUE)); }