public virtual void bucketedCs01SingleNodeCurveTest()
        {
            ImmutableCreditRatesProvider ratesProviderNoCredit = ImmutableCreditRatesProvider.builder().valuationDate(VALUATION_DATE).recoveryRateCurves(ImmutableMap.of(LEGAL_ENTITY, RECOVERY_CURVE)).discountCurves(ImmutableMap.of(USD, YIELD_CURVE)).build();
            QuoteId quoteId = QuoteId.of(StandardId.of("OG", END2.ToString()));
            CdsIsdaCreditCurveNode           node          = CdsIsdaCreditCurveNode.ofParSpread(DatesCdsTemplate.of(START, END2, CDS_CONV), quoteId, LEGAL_ENTITY);
            ImmutableMarketData              marketData    = ImmutableMarketData.builder(VALUATION_DATE).addValue(quoteId, DEAL_SPREAD * ONE_BP).build();
            IsdaCreditCurveDefinition        definition    = IsdaCreditCurveDefinition.of(CREDIT_CURVE_NAME, USD, VALUATION_DATE, ACT_365F, ImmutableList.of(node), true, false);
            LegalEntitySurvivalProbabilities creditCurve   = BUILDER.calibrate(definition, marketData, ratesProviderNoCredit, REF_DATA);
            ImmutableCreditRatesProvider     ratesProvider = ImmutableCreditRatesProvider.builder().valuationDate(VALUATION_DATE).recoveryRateCurves(ImmutableMap.of(LEGAL_ENTITY, RECOVERY_CURVE)).discountCurves(ImmutableMap.of(USD, YIELD_CURVE)).creditCurves(ImmutableMap.of(Pair.of(LEGAL_ENTITY, USD), creditCurve)).build();

            double[] expectedFd = new double[] { -6.876275937539589E-4, 1.1832215762730414E-4, 0.0012340982402658796, 0.002784985575488008, 0.005287295115619095, 2429.636217554099, 3101.303324461041 };
            CurrencyParameterSensitivity analytic = CS01_AN.bucketedCs01(CDS2, ImmutableList.copyOf(MARKET_CDS), ratesProvider, REF_DATA);
            CurrencyParameterSensitivity fd       = CS01_FD.bucketedCs01(CDS2, ImmutableList.copyOf(MARKET_CDS), ratesProvider, REF_DATA);

            assertEquals(fd.Currency, USD);
            assertEquals(fd.MarketDataName, CurveName.of("impliedSpreads"));
            assertEquals(fd.ParameterCount, NUM_MARKET_CDS);
            assertEquals(fd.ParameterMetadata, CDS_METADATA);
            assertTrue(DoubleArrayMath.fuzzyEquals(fd.Sensitivity.multipliedBy(ONE_BP).toArray(), expectedFd, NOTIONAL * TOL));
            assertEquals(analytic.Currency, USD);
            assertEquals(analytic.MarketDataName, CurveName.of("impliedSpreads"));
            assertEquals(analytic.ParameterCount, NUM_MARKET_CDS);
            assertEquals(analytic.ParameterMetadata, CDS_METADATA);
            assertTrue(DoubleArrayMath.fuzzyEquals(analytic.Sensitivity.toArray(), fd.Sensitivity.toArray(), NOTIONAL * ONE_BP * 10d));
        }
Exemplo n.º 2
0
        public virtual void negativeDataTest()
        {
            DoubleArray xValues = DoubleArray.of(-34.5, -27.0, -22.5, -14.2, -10.0, -5.0, -0.3);
            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), -27.7, -21.2, -17.8, -9.99, -1.52, -0.35, 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 void presentValueSensitivity()
        {
            // March 2016
            PointSensitivities             pointMar = TRADE_PRICER.presentValueSensitivity(FUTURE_TRADE_MAR, RATES_PROVIDER).multipliedBy(ONE_BASIS_POINT);
            CurrencyParameterSensitivities sensiMar = RATES_PROVIDER.parameterSensitivity(pointMar);

            double[] sensiFwdMar = new double[] { 935.8275650652985, -3282.5265925099943, -113.19057215551703, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
            assertTrue(DoubleArrayMath.fuzzyEquals(sensiMar.getSensitivity(NAME_FWD, JPY).Sensitivity.toArray(), sensiFwdMar, TOL));
            // June 2016
            PointSensitivities             pointJun = TRADE_PRICER.presentValueSensitivity(FUTURE_TRADE_JUN, RATES_PROVIDER).multipliedBy(ONE_BASIS_POINT);
            CurrencyParameterSensitivities sensiJun = RATES_PROVIDER.parameterSensitivity(pointJun);

            double[] sensiFwdJun = new double[] { 0.0, 3367.914558311612, 0.0, 0.0, -5827.0267754915185, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
            assertTrue(DoubleArrayMath.fuzzyEquals(sensiJun.getSensitivity(NAME_FWD, JPY).Sensitivity.toArray(), sensiFwdJun, TOL));
            // September 2016
            PointSensitivities             pointSep = TRADE_PRICER.presentValueSensitivity(FUTURE_TRADE_SEP, RATES_PROVIDER).multipliedBy(ONE_BASIS_POINT);
            CurrencyParameterSensitivities sensiSep = RATES_PROVIDER.parameterSensitivity(pointSep);

            double[] sensiFwdSep = new double[] { 0.0, 0.0, 0.0, 0.0, 4841.731284141179, 1210.432821035297, 0.0, -6865.62899713775, -1645.2267757666687, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
            assertTrue(DoubleArrayMath.fuzzyEquals(sensiSep.getSensitivity(NAME_FWD, JPY).Sensitivity.toArray(), sensiFwdSep, TOL));
            // June 2017
            PointSensitivities             pointJunMid = TRADE_PRICER.presentValueSensitivity(FUTURE_TRADE_JUN_MID, RATES_PROVIDER).multipliedBy(ONE_BASIS_POINT);
            CurrencyParameterSensitivities sensiJunMid = RATES_PROVIDER.parameterSensitivity(pointJunMid);

            double[] sensiFwdJunMid = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4038.520213502244, -3335.0044730456357, -3168.128056647536, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
            assertTrue(DoubleArrayMath.fuzzyEquals(sensiJunMid.getSensitivity(NAME_FWD, JPY).Sensitivity.toArray(), sensiFwdJunMid, TOL));
            // March 2020
            PointSensitivities             pointMarLong = TRADE_PRICER.presentValueSensitivity(FUTURE_TRADE_MAR_LONG, RATES_PROVIDER).multipliedBy(ONE_BASIS_POINT);
            CurrencyParameterSensitivities sensiMarLong = RATES_PROVIDER.parameterSensitivity(pointMarLong);

            double[] sensiFwdMarLong = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 8455.972826379962, -10915.251436694207, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
            assertTrue(DoubleArrayMath.fuzzyEquals(sensiMarLong.getSensitivity(NAME_FWD, JPY).Sensitivity.toArray(), sensiFwdMarLong, TOL));
        }
        public virtual void parSpreadSensitivity()
        {
            // March 2016
            PointSensitivities             pointMar = TRADE_PRICER.parSpreadSensitivity(FUTURE_TRADE_MAR, RATES_PROVIDER).multipliedBy(HUNDRED * ONE_BASIS_POINT);
            CurrencyParameterSensitivities sensiMar = RATES_PROVIDER.parameterSensitivity(pointMar);

            double[] sensiFwdMar = new double[] { 0.003743310260261194, -0.01313010637003998, -4.527622886220682E-4, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
            assertTrue(DoubleArrayMath.fuzzyEquals(sensiMar.getSensitivity(NAME_FWD, JPY).Sensitivity.toArray(), sensiFwdMar, TOL));
            // June 2016
            PointSensitivities             pointJun = TRADE_PRICER.parSpreadSensitivity(FUTURE_TRADE_JUN, RATES_PROVIDER).multipliedBy(HUNDRED * ONE_BASIS_POINT);
            CurrencyParameterSensitivities sensiJun = RATES_PROVIDER.parameterSensitivity(pointJun);

            double[] sensiFwdJun = new double[] { 0.0, 0.01347165823324645, 0.0, 0.0, -0.023308107101966076, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
            assertTrue(DoubleArrayMath.fuzzyEquals(sensiJun.getSensitivity(NAME_FWD, JPY).Sensitivity.toArray(), sensiFwdJun, TOL));
            // September 2016
            PointSensitivities             pointSep = TRADE_PRICER.parSpreadSensitivity(FUTURE_TRADE_SEP, RATES_PROVIDER).multipliedBy(HUNDRED * ONE_BASIS_POINT);
            CurrencyParameterSensitivities sensiSep = RATES_PROVIDER.parameterSensitivity(pointSep);

            double[] sensiFwdSep = new double[] { 0.0, 0.0, 0.0, 0.0, 0.01936692513656471, 0.0048417312841411864, 0.0, -0.027462515988551, -0.006580907103066675, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
            assertTrue(DoubleArrayMath.fuzzyEquals(sensiSep.getSensitivity(NAME_FWD, JPY).Sensitivity.toArray(), sensiFwdSep, TOL));
            // June 2017
            PointSensitivities             pointJunMid = TRADE_PRICER.parSpreadSensitivity(FUTURE_TRADE_JUN_MID, RATES_PROVIDER).multipliedBy(HUNDRED * ONE_BASIS_POINT);
            CurrencyParameterSensitivities sensiJunMid = RATES_PROVIDER.parameterSensitivity(pointJunMid);

            double[] sensiFwdJunMid = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.016154080854008976, -0.013340017892182532, -0.012672512226590141, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
            assertTrue(DoubleArrayMath.fuzzyEquals(sensiJunMid.getSensitivity(NAME_FWD, JPY).Sensitivity.toArray(), sensiFwdJunMid, TOL));
            // March 2020
            PointSensitivities             pointMarLong = TRADE_PRICER.parSpreadSensitivity(FUTURE_TRADE_MAR_LONG, RATES_PROVIDER).multipliedBy(HUNDRED * ONE_BASIS_POINT);
            CurrencyParameterSensitivities sensiMarLong = RATES_PROVIDER.parameterSensitivity(pointMarLong);

            double[] sensiFwdMarLong = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.03382389130551987, -0.043661005746776824, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
            assertTrue(DoubleArrayMath.fuzzyEquals(sensiMarLong.getSensitivity(NAME_FWD, JPY).Sensitivity.toArray(), sensiFwdMarLong, TOL));
        }
Exemplo n.º 5
0
        protected internal override QuantileResult expectedShortfall(double level, DoubleArray sample)
        {
            ArgChecker.isTrue(level > 0, "Quantile should be above 0.");
            ArgChecker.isTrue(level < 1, "Quantile should be below 1.");
            int sampleSize = sampleCorrection(sample.size());

            double[] order = createIndexArray(sample.size());
            double[] s     = sample.toArray();
            DoubleArrayMath.sortPairs(s, order);
            double fractionalIndex = level * sampleSize;
            int    index           = (int)checkIndex(this.index(fractionalIndex), sample.size(), true);

            int[]    indices  = new int[index];
            double[] weights  = new double[index];
            double   interval = 1d / (double)sampleSize;
            double   losses   = s[0] * interval *indexShift();

            for (int i = 0; i < index - 1; i++)
            {
                losses    += s[i] * interval;
                indices[i] = (int)order[i];
                weights[i] = interval;
            }
            losses            += s[index - 1] * (fractionalIndex - index + 1 - indexShift()) * interval;
            indices[index - 1] = (int)order[index - 1];
            weights[0]        += interval * indexShift();
            weights[index - 1] = (fractionalIndex - index + 1 - indexShift()) * interval;
            return(QuantileResult.of(losses / level, indices, DoubleArray.ofUnsafe(weights).dividedBy(level)));
        }
        //-------------------------------------------------------------------------
        public virtual 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));
        }
Exemplo n.º 7
0
        public virtual void sampleDataTest()
        {
            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, 10.0, 17.52, 25.0, xValues.get(nData - 1));
            int                    nKeys = keys.size();
            BoundCurveInterpolator bound = INTERP.bind(xValues, yValues);
            PiecewisePolynomialResultsWithSensitivity ppRes = BASE_INTERP.interpolateWithSensitivity(xValues.toArray(), pValues.toArray());

            System.Func <double, double> funcDeriv = x => bound.interpolate(x.Value);
            for (int i = 0; i < nKeys; ++i)
            {
                // interpolate
                assertEquals(bound.interpolate(keys.get(i)), FUNCTION.evaluate(ppRes, keys.get(i)).get(0) / 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));
            }
        }
        private void check_quantile(double level)
        {
            double[] w         = METHOD.weights(DATA_123.size());
            double   qComputed = METHOD.quantileFromUnsorted(level, DATA_123);
            double   WI1       = 0.0d;
            int      nbW       = 0;

            for (int i = 0; i < DATA_123.size(); i++)
            {
                if (DATA_123.get(i) > qComputed)
                {
                    WI1 += w[i];
                    nbW++;
                }
            }
            assertTrue(WI1 < 1.0d - level, "Weight of tail lower than level");
            double[] w2   = w.Clone();
            double[] data = DATA_123.toArray();
            DoubleArrayMath.sortPairs(data, w2);
            double WI = WI1 + w2[w.Length - 1 - nbW];

            assertTrue(WI > 1.0d - level, "Weight of tail+1 larger than level");
            double alpha     = (WI - (1 - level)) / (WI - WI1);
            double qExpected = (1 - alpha) * data[w.Length - 1 - nbW] + alpha * data[w.Length - 1 - nbW + 1];

            assertEquals(qComputed, qExpected, TOLERANCE_WEIGHT, "Quantile.");
        }
Exemplo n.º 9
0
        public virtual 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).Sensitivity.toArray(), expected, TOL));
        }
        public virtual 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).Sensitivity.toArray(), sensiDsc, NOTIONAL * TOL));
            assertTrue(DoubleArrayMath.fuzzyEquals(sensi.getSensitivity(IborCapletFloorletDataSet.FWD3_NAME, EUR).Sensitivity.toArray(), sensiFwd, NOTIONAL * TOL));
        }
        public virtual 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 virtual void getIntegrationsPointsTest()
        {
            double start = 0.1;
            double end   = 2.0;

            DoubleArray setA0 = DoubleArray.of(0.5, 0.9, 1.4);
            DoubleArray setB0 = DoubleArray.of(0.3, 0.4, 1.5, 1.6);
            DoubleArray exp0  = DoubleArray.of(0.1, 0.3, 0.4, 0.5, 0.9, 1.4, 1.5, 1.6, 2.0);
            DoubleArray res0  = DoublesScheduleGenerator.getIntegrationsPoints(start, end, setA0, setB0);

            assertTrue(DoubleArrayMath.fuzzyEquals(exp0.toArray(), res0.toArray(), 0d));

            DoubleArray setA1 = DoubleArray.of(0.5, 0.9, 1.4);
            DoubleArray setB1 = DoubleArray.of(0.3, 0.4, 0.5, 1.5, 1.6);
            DoubleArray exp1  = DoubleArray.of(0.1, 0.3, 0.4, 0.5, 0.9, 1.4, 1.5, 1.6, 2.0);
            DoubleArray res1  = DoublesScheduleGenerator.getIntegrationsPoints(start, end, setA1, setB1);

            assertTrue(DoubleArrayMath.fuzzyEquals(exp1.toArray(), res1.toArray(), 0d));

            /*
             * different(temp[pos], end) == false
             */
            DoubleArray setA2 = DoubleArray.of(0.2, 0.9, 1.4);
            DoubleArray setB2 = DoubleArray.of(0.3, 0.4, 0.5, 1.5, 2.0 - 1.e-3);
            DoubleArray exp2  = DoubleArray.of(0.1, 0.2, 0.3, 0.4, 0.5, 0.9, 1.4, 1.5, 2.0);
            DoubleArray res2  = DoublesScheduleGenerator.getIntegrationsPoints(start, end, setA2, setB2);

            assertTrue(DoubleArrayMath.fuzzyEquals(exp2.toArray(), res2.toArray(), 0d));

            DoubleArray setA3 = DoubleArray.of(0.05, 0.07);
            DoubleArray setB3 = DoubleArray.of(0.03, 0.04);
            DoubleArray exp3  = DoubleArray.of(0.1, 2.0);
            DoubleArray res3  = DoublesScheduleGenerator.getIntegrationsPoints(start, end, setA3, setB3);

            assertTrue(DoubleArrayMath.fuzzyEquals(exp3.toArray(), res3.toArray(), 0d));

            DoubleArray setA4 = DoubleArray.of(2.2, 2.7);
            DoubleArray setB4 = DoubleArray.of(2.3, 2.4);
            DoubleArray exp4  = DoubleArray.of(0.1, 2.0);
            DoubleArray res4  = DoublesScheduleGenerator.getIntegrationsPoints(start, end, setA4, setB4);

            assertTrue(DoubleArrayMath.fuzzyEquals(exp4.toArray(), res4.toArray(), 0d));

            DoubleArray setA5 = DoubleArray.of(-0.5, 0.0, 1.2);
            DoubleArray setB5 = DoubleArray.of(-0.2, -0.0, 1.2);
            DoubleArray exp5  = DoubleArray.of(-0.3, -0.2, 0.0, 1.2, 2.0);
            DoubleArray res5  = DoublesScheduleGenerator.getIntegrationsPoints(-0.3, end, setA5, setB5);

            assertTrue(DoubleArrayMath.fuzzyEquals(exp5.toArray(), res5.toArray(), 0d));
        }
Exemplo n.º 13
0
        protected internal override QuantileResult quantile(double level, DoubleArray sample, bool isExtrapolated)
        {
            ArgChecker.isTrue(level > 0, "Quantile should be above 0.");
            ArgChecker.isTrue(level < 1, "Quantile should be below 1.");
            int sampleSize = sampleCorrection(sample.size());

            double[] order = createIndexArray(sample.size());
            double[] s     = sample.toArray();
            DoubleArrayMath.sortPairs(s, order);
            int index = (int)checkIndex(this.index(level * sampleSize), sample.size(), isExtrapolated);

            int[] ind = new int[1];
            ind[0] = (int)order[index - 1];
            return(QuantileResult.of(s[index - 1], ind, DoubleArray.of(1)));
        }
Exemplo n.º 14
0
        // regression to ISDA curve
        public virtual void curveRegressionTest()
        {
            double[]               xValues = new double[] { 0.08767123287671233, 0.1726027397260274, 0.2602739726027397, 0.5095890410958904, 1.010958904109589, 2.010958904109589, 3.0136986301369864, 4.0191780821917815, 5.016438356164384, 6.013698630136987, 7.016438356164384, 8.016438356164384, 9.016438356164384, 10.021917808219179, 12.01917808219178, 15.027397260273974, 20.024657534246575, 25.027397260273972, 30.030136986301372 };
            double[]               yValues = new double[] { 0.0015967771993938666, 0.002000101499768777, 0.002363431670279865, 0.003338175293899776, 0.005634608399714134, 0.00440326902435394, 0.007809961130263494, 0.011941089607974827, 0.015908558015433557, 0.019426790989545677, 0.022365655212981644, 0.02480329609280203, 0.02681632723967965, 0.028566047406753222, 0.031343018999443514, 0.03409375145707815, 0.036451406286344155, 0.0374228389649933, 0.037841116301420584 };
            CurveExtrapolator      left    = CurveExtrapolators.FLAT;
            CurveExtrapolator      right   = CurveExtrapolators.PRODUCT_LINEAR;
            BoundCurveInterpolator interp  = INTERP.bind(DoubleArray.ofUnsafe(xValues), DoubleArray.ofUnsafe(yValues), left, right);

            double[] keys = new double[] { 1d / 365d, 17d / 365d, 30d / 365d, 98d / 365d, 1d, 2.35, 6d, 10d, 12.6, 28d, 32d, 39d };
            // interpolate
            double[] expected = new double[] { 0.0015967771993938666, 0.0015967771993938666, 0.0015967771993938666, 0.0024244214596922794, 0.005609029445739646, 0.005880433874305883, 0.01938638577242741, 0.02853165777399691, 0.031976443846372445, 0.03768939749254428, 0.037969928846136244, 0.038322391316033835 };
            for (int i = 0; i < keys.Length; ++i)
            {
                double computed = interp.interpolate(keys[i]);
                assertEquals(computed, expected[i], EPS);
            }
            // sensitivity
            double[][] sensiExp = new double[][]
            {
                new double[] { 1d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.0 },
                new double[] { 1d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.0 },
                new double[] { 1.0, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.0 },
                new double[] { 0d, 0d, 0.9374299170217537, 0.0625700829782464, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.0 },
                new double[] { 0d, 0d, 0d, 0.011138558275319966, 0.98886144172468, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.0 },
                new double[] { 0d, 0d, 0d, 0d, 0d, 0.5663932037211347, 0.43360679627886545, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.0 },
                new double[] { 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.011484520046164301, 0.9885154799538357, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.0 },
                new double[] { 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.01965436153932362, 0.9803456384606765, 0d, 0d, 0d, 0d, 0.0 },
                new double[] { 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.7697250253579322, 0.23027497464206784, 0d, 0d, 0.0 },
                new double[] { 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.3627229965940976, 0.6372770034059024 },
                new double[] { 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, -0.3079596561838882, 1.3079596561838882 },
                new double[] { 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, -1.1506122199305884, 2.1506122199305886 }
            };
            for (int i = 0; i < keys.Length; ++i)
            {
                DoubleArray computed = interp.parameterSensitivity(keys[i]);
                assertTrue(DoubleArrayMath.fuzzyEquals(computed.toArray(), sensiExp[i], EPS));
            }
            // fwd rate
            double[] fwdExp = new double[] { 0.0015967771993938666, 0.0015967771993938666, 0.0015967771993938666, 0.004355764791085397, 0.007968687949886104, 0.01464196114594003, 0.037124276087345934, 0.04425631735181895, 0.04508415518352911, 0.03993364832127998, 0.03993364832127998, 0.03993364832127998 };
            for (int i = 0; i < keys.Length; ++i)
            {
                double value    = interp.interpolate(keys[i]);
                double deriv    = interp.firstDerivative(keys[i]);
                double computed = deriv * keys[i] + value;
                assertEquals(computed, fwdExp[i], EPS);
            }
        }
Exemplo n.º 15
0
        // regression to ISDA curve
        public virtual void curveNegativeRateRegressionTest()
        {
            double[]               xValues = new double[] { 0.09589041095890412, 0.1726027397260274, 0.2547945205479452, 0.5041095890410959, 0.7561643835616438, 1.0082191780821919, 2.0136986301369864, 3.0109589041095894, 4.008219178082192, 5.010958904109589, 6.010958904109589, 7.010958904109589, 8.016438356164384, 9.013698630136986, 10.013698630136986, 12.013698630136986, 15.016438356164384, 20.021917808219175, 30.032876712328765 };
            double[]               yValues = new double[] { -0.0020786675364765166, -0.0016860241245632032, -0.0013445488774423426, -4.237821212705129E-4, 2.5198253623336676E-5, 5.935456094577134E-4, -3.2426565772714425E-4, 6.147334333200949E-4, 0.0019060366773708986, 0.0033107384678633615, 0.004774430364382846, 0.006237401212672876, 0.007639615209817064, 0.00896830709117619, 0.010164859928720184, 0.012196812821300893, 0.014410766977011871, 0.01623618497051232, 0.016522578536714926 };
            CurveExtrapolator      left    = CurveExtrapolators.FLAT;
            CurveExtrapolator      right   = CurveExtrapolators.PRODUCT_LINEAR;
            BoundCurveInterpolator interp  = INTERP.bind(DoubleArray.ofUnsafe(xValues), DoubleArray.ofUnsafe(yValues), left, right);

            double[] keys = new double[] { 1d / 365d, 17d / 365d, 30d / 365d, 98d / 365d, 1d, 2.35, 6d, 10d, 12.6, 28d, 32d, 39d };
            // interpolate
            double[] expected = new double[] { -0.0020786675364765166, -0.0020786675364765166, -0.0020786675364765166, -0.0012495606047715551, 5.795315650672766E-4, 8.145045403416393E-5, 0.004761034017457297, 0.010150085453826136, 0.01271200378309255, 0.016480992621622493, 0.016557789252559695, 0.016654277327326956 };
            for (int i = 0; i < keys.Length; ++i)
            {
                double computed = interp.interpolate(keys[i]);
                assertEquals(computed, expected[i], EPS);
            }
            // sensitivity
            double[][] sensiExp = new double[][]
            {
                new double[] { 1d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.0 },
                new double[] { 1d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.0 },
                new double[] { 1d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.0 },
                new double[] { 0d, 0d, 0.8968378560215295, 0.1031621439784705, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.0 },
                new double[] { 0d, 0d, 0d, 0d, 0.024657534246575567, 0.9753424657534244, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.0 },
                new double[] { 0d, 0d, 0d, 0d, 0d, 0d, 0.5679270452660136, 0.43207295473398644, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.0 },
                new double[] { 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.009152436354538397, 0.9908475636454616, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.0 },
                new double[] { 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.012347532370050315, 0.9876524676299496, 0d, 0d, 0d, 0.0 },
                new double[] { 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.7672982701729827, 0.23270172982701726, 0d, 0.0 },
                new double[] { 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0.1452054794520546, 0.8547945205479454 },
                new double[] { 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, -0.12294520547945219, 1.1229452054794522 },
                new double[] { 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, -0.4598524762908325, 1.4598524762908325 }
            };
            for (int i = 0; i < keys.Length; ++i)
            {
                DoubleArray computed = interp.parameterSensitivity(keys[i]);
                assertTrue(DoubleArrayMath.fuzzyEquals(computed.toArray(), sensiExp[i], EPS));
            }
            // fwd rate
            double[] fwdExp = new double[] { -0.0020786675364765166, -0.0020786675364765166, -0.0020786675364765166, 5.172212669050933E-4, 0.0022985876769608432, 0.0025107892902424037, 0.012108930306120206, 0.02095022660137702, 0.023268603631019753, 0.017095365669120136, 0.017095365669120136, 0.017095365669120136 };
            for (int i = 0; i < keys.Length; ++i)
            {
                double value    = interp.interpolate(keys[i]);
                double deriv    = interp.firstDerivative(keys[i]);
                double computed = deriv * keys[i] + value;
                assertEquals(computed, fwdExp[i], EPS);
            }
        }
Exemplo n.º 16
0
        public virtual 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.Value);
            assertEquals(computedValue2, expected.Value);
            assertTrue(DoubleArrayMath.fuzzyEquals(computedSensi1.Sensitivity.toArray(), expected.Derivatives.toArray(), tol));
            assertTrue(DoubleArrayMath.fuzzyEquals(computedSensi2.Sensitivity.toArray(), expected.Derivatives.toArray(), tol));
        }
        public virtual 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.Value, volExpected, TOLERANCE_VOL);
                    assertTrue(DoubleArrayMath.fuzzyEquals(volAdjComputed.Derivatives.toArray(), volAdjExpected.Derivatives.toArray(), TOLERANCE_VOL));
                }
            }
        }
        public virtual void truncateSetInclusiveTest()
        {
            double lower = 0.1;
            double upper = 2.5;

            DoubleArray set0 = DoubleArray.of(-0.2, 1.5, 2.9);
            DoubleArray exp0 = DoubleArray.of(0.1, 1.5, 2.5);
            DoubleArray res0 = DoublesScheduleGenerator.truncateSetInclusive(lower, upper, set0);

            assertTrue(DoubleArrayMath.fuzzyEquals(exp0.toArray(), res0.toArray(), 0d));

            DoubleArray set1 = DoubleArray.of(-0.2, -0.1);
            DoubleArray exp1 = DoubleArray.of(0.1, 2.5);
            DoubleArray res1 = DoublesScheduleGenerator.truncateSetInclusive(lower, upper, set1);

            assertTrue(DoubleArrayMath.fuzzyEquals(exp1.toArray(), res1.toArray(), 0d));

            DoubleArray set2 = DoubleArray.of(0.1 + 1.e-3, 1.5, 2.8);
            DoubleArray exp2 = DoubleArray.of(0.1, 1.5, 2.5);
            DoubleArray res2 = DoublesScheduleGenerator.truncateSetInclusive(lower, upper, set2);

            assertTrue(DoubleArrayMath.fuzzyEquals(exp2.toArray(), res2.toArray(), 0d));

            DoubleArray set3 = DoubleArray.of(0.0, 1.5, 2.5 + 1.e-3);
            DoubleArray exp3 = DoubleArray.of(0.1, 1.5, 2.5);
            DoubleArray res3 = DoublesScheduleGenerator.truncateSetInclusive(lower, upper, set3);

            assertTrue(DoubleArrayMath.fuzzyEquals(exp3.toArray(), res3.toArray(), 0d));

            DoubleArray set4 = DoubleArray.of(0.1 - 1.e-4, 1.5, 2.5 - 1.e-4);
            DoubleArray exp4 = DoubleArray.of(0.1, 1.5, 2.5);
            DoubleArray res4 = DoublesScheduleGenerator.truncateSetInclusive(lower, upper, set4);

            assertTrue(DoubleArrayMath.fuzzyEquals(exp4.toArray(), res4.toArray(), 0d));

            DoubleArray set5 = DoubleArray.of(lower + 1.e-4, lower + 2.e-4);
            DoubleArray exp5 = DoubleArray.of(lower, lower + 1.e-3);
            DoubleArray res5 = DoublesScheduleGenerator.truncateSetInclusive(lower, lower + 1.e-3, set5);

            assertTrue(DoubleArrayMath.fuzzyEquals(exp5.toArray(), res5.toArray(), 0d));
        }
Exemplo n.º 19
0
        public virtual void test_presentValueSensitivityVolatility_after()
        {
            PointSensitivityBuilder        capComputed   = PRICER.presentValueSensitivityModelParamsVolatility(CAP, RATES_AFTER, VOLS_AFTER);
            PointSensitivityBuilder        floorComputed = PRICER.presentValueSensitivityModelParamsVolatility(FLOOR, RATES_AFTER, VOLS_AFTER);
            CurrencyParameterSensitivities capExpected   = CurrencyParameterSensitivities.empty();
            CurrencyParameterSensitivities floorExpected = CurrencyParameterSensitivities.empty();
            int nPeriods = CAP.CapletFloorletPeriods.size();

            for (int i = 3; i < nPeriods; ++i)
            {
                capExpected   = capExpected.combinedWith(VOLS_AFTER.parameterSensitivity(PRICER_PERIOD.presentValueSensitivityModelParamsVolatility(CAP.CapletFloorletPeriods.get(i), RATES_AFTER, VOLS_AFTER).build()));
                floorExpected = floorExpected.combinedWith(VOLS_AFTER.parameterSensitivity(PRICER_PERIOD.presentValueSensitivityModelParamsVolatility(FLOOR.CapletFloorletPeriods.get(i), RATES_AFTER, VOLS_AFTER).build()));
            }
            CurrencyParameterSensitivities capSensiComputed   = VOLS_AFTER.parameterSensitivity(capComputed.build());
            CurrencyParameterSensitivities floorSensiComputed = VOLS_AFTER.parameterSensitivity(floorComputed.build());
            CurrencyParameterSensitivity   capSensiExpected   = capExpected.Sensitivities.get(0);
            CurrencyParameterSensitivity   floorSensiExpected = floorExpected.Sensitivities.get(0);

            assertTrue(DoubleArrayMath.fuzzyEquals(capSensiComputed.Sensitivities.get(0).Sensitivity.toArray(), capSensiExpected.Sensitivity.toArray(), TOL * NOTIONAL_VALUE));
            assertTrue(DoubleArrayMath.fuzzyEquals(floorSensiComputed.Sensitivities.get(0).Sensitivity.toArray(), floorSensiExpected.Sensitivity.toArray(), TOL * NOTIONAL_VALUE));
        }
Exemplo n.º 20
0
        public virtual 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 virtual void bucketedCs01Test()
        {
            double[] expectedFd                   = new double[] { 0.02446907003406107, 0.1166137422736746, 0.5196553952424576, 1.4989046391578054, 3.5860718603647483, 4233.77162264947, 0.0 };
            CurrencyParameterSensitivity fd       = CS01_FD.bucketedCs01(CDS1, ImmutableList.copyOf(MARKET_CDS), RATES_PROVIDER, REF_DATA);
            CurrencyParameterSensitivity analytic = CS01_AN.bucketedCs01(CDS1, ImmutableList.copyOf(MARKET_CDS), RATES_PROVIDER, REF_DATA);

            assertEquals(fd.Currency, USD);
            assertEquals(fd.MarketDataName, CurveName.of("impliedSpreads"));
            assertEquals(fd.ParameterCount, NUM_MARKET_CDS);
            assertEquals(fd.ParameterMetadata, CDS_METADATA);
            assertTrue(DoubleArrayMath.fuzzyEquals(fd.Sensitivity.multipliedBy(ONE_BP).toArray(), expectedFd, NOTIONAL * TOL));
            assertEquals(analytic.Currency, USD);
            assertEquals(analytic.MarketDataName, CurveName.of("impliedSpreads"));
            assertEquals(analytic.ParameterCount, NUM_MARKET_CDS);
            assertEquals(analytic.ParameterMetadata, CDS_METADATA);
            assertTrue(DoubleArrayMath.fuzzyEquals(analytic.Sensitivity.toArray(), fd.Sensitivity.toArray(), NOTIONAL * ONE_BP * 10d));
            PointSensitivities             point      = PRICER.presentValueOnSettleSensitivity(CDS1, RATES_PROVIDER, REF_DATA);
            CurrencyParameterSensitivity   paramSensi = RATES_PROVIDER.singleCreditCurveParameterSensitivity(point, LEGAL_ENTITY, USD);
            CurrencyParameterSensitivities quoteSensi = QUOTE_CAL.sensitivity(CurrencyParameterSensitivities.of(paramSensi), RATES_PROVIDER);

            assertTrue(DoubleArrayMath.fuzzyEquals(quoteSensi.Sensitivities.get(0).Sensitivity.toArray(), analytic.Sensitivity.toArray(), NOTIONAL * TOL));
        }
Exemplo n.º 22
0
        //-------------------------------------------------------------------------
        public virtual void test_putCallParity()
        {
            double df = RATES_PROVIDER.discountFactor(USD, PAY);
            PointSensitivityBuilder dfSensi = RATES_PROVIDER.discountFactors(USD).zeroRatePointSensitivity(PAY);

            for (int i = 0; i < NB_STRIKES; ++i)
            {
                CurrencyAmount          pvCall         = PRICER.presentValue(CALLS[i], RATES_PROVIDER, VOLS);
                PointSensitivityBuilder pvSensiCall    = PRICER.presentValueSensitivityRatesStickyStrike(CALLS[i], RATES_PROVIDER, VOLS);
                PointSensitivityBuilder pvSensiVolCall = PRICER.presentValueSensitivityModelParamsVolatility(CALLS[i], RATES_PROVIDER, VOLS);
                CurrencyAmount          pvPut          = PRICER.presentValue(PUTS[i], RATES_PROVIDER, VOLS);
                PointSensitivityBuilder pvSensiPut     = PRICER.presentValueSensitivityRatesStickyStrike(PUTS[i], RATES_PROVIDER, VOLS);
                PointSensitivityBuilder pvSensiVolPut  = PRICER.presentValueSensitivityModelParamsVolatility(PUTS[i], RATES_PROVIDER, VOLS);
                double forward = FX_PRICER.forwardFxRate(UNDERLYING[i], RATES_PROVIDER).fxRate(CURRENCY_PAIR);
                PointSensitivityBuilder forwardSensi = FX_PRICER.forwardFxRatePointSensitivity(UNDERLYING[i], RATES_PROVIDER);
                double strike = CALLS[i].Strike;
                assertEquals(pvCall.Amount + pvPut.Amount, df * (forward - strike) * NOTIONAL, TOL * NOTIONAL);
                assertTrue(pvSensiCall.combinedWith(pvSensiPut).build().normalized().equalWithTolerance(dfSensi.multipliedBy((forward - strike) * NOTIONAL).combinedWith(forwardSensi.multipliedBy(df * NOTIONAL)).build().normalized(), NOTIONAL * TOL));
                DoubleArray sensiVol = VOLS.parameterSensitivity(pvSensiVolCall.combinedWith(pvSensiVolPut).build()).Sensitivities.get(0).Sensitivity;
                assertTrue(DoubleArrayMath.fuzzyEquals(sensiVol.toArray(), new double[sensiVol.size()], NOTIONAL * TOL));
            }
        }
Exemplo n.º 23
0
        public virtual void test_parameterSensitivity_multi()
        {
            double[] points1 = new double[] { 2.24, 3.45, -2.12, -0.56 };
            double[] points2 = new double[] { -0.145, 1.01, -5.0, -11.0 };
            double[] points3 = new double[] { 1.3, -4.32, 2.1, -7.18 };
            SabrParametersSwaptionVolatilities prov = SabrParametersSwaptionVolatilities.of(NAME, CONV, DATE_TIME, PARAM);
            double expiryTime0 = prov.relativeTime(TEST_OPTION_EXPIRY[0]);
            double expiryTime3 = prov.relativeTime(TEST_OPTION_EXPIRY[3]);

            for (int i = 0; i < NB_TEST; i++)
            {
                PointSensitivities             sensi1   = PointSensitivities.of(SwaptionSabrSensitivity.of(NAME, expiryTime0, TEST_TENOR[i], ALPHA, USD, points1[0]), SwaptionSabrSensitivity.of(NAME, expiryTime0, TEST_TENOR[i], BETA, USD, points1[1]), SwaptionSabrSensitivity.of(NAME, expiryTime0, TEST_TENOR[i], RHO, USD, points1[2]), SwaptionSabrSensitivity.of(NAME, expiryTime0, TEST_TENOR[i], NU, USD, points1[3]));
                PointSensitivities             sensi2   = PointSensitivities.of(SwaptionSabrSensitivity.of(NAME, expiryTime0, TEST_TENOR[i], ALPHA, USD, points2[0]), SwaptionSabrSensitivity.of(NAME, expiryTime0, TEST_TENOR[i], BETA, USD, points2[1]), SwaptionSabrSensitivity.of(NAME, expiryTime0, TEST_TENOR[i], RHO, USD, points2[2]), SwaptionSabrSensitivity.of(NAME, expiryTime0, TEST_TENOR[i], NU, USD, points2[3]));
                PointSensitivities             sensi3   = PointSensitivities.of(SwaptionSabrSensitivity.of(NAME, expiryTime3, TEST_TENOR[i], ALPHA, USD, points3[0]), SwaptionSabrSensitivity.of(NAME, expiryTime3, TEST_TENOR[i], BETA, USD, points3[1]), SwaptionSabrSensitivity.of(NAME, expiryTime3, TEST_TENOR[i], RHO, USD, points3[2]), SwaptionSabrSensitivity.of(NAME, expiryTime3, TEST_TENOR[i], NU, USD, points3[3]));
                PointSensitivities             sensis   = sensi1.combinedWith(sensi2).combinedWith(sensi3).normalized();
                CurrencyParameterSensitivities computed = prov.parameterSensitivity(sensis);
                CurrencyParameterSensitivities expected = prov.parameterSensitivity(sensi1).combinedWith(prov.parameterSensitivity(sensi2)).combinedWith(prov.parameterSensitivity(sensi3));
                DoubleArrayMath.fuzzyEquals(computed.getSensitivity(PARAM.AlphaSurface.Name, USD).Sensitivity.toArray(), expected.getSensitivity(PARAM.AlphaSurface.Name, USD).Sensitivity.toArray(), TOLERANCE_VOL);
                DoubleArrayMath.fuzzyEquals(computed.getSensitivity(PARAM.BetaSurface.Name, USD).Sensitivity.toArray(), expected.getSensitivity(PARAM.BetaSurface.Name, USD).Sensitivity.toArray(), TOLERANCE_VOL);
                DoubleArrayMath.fuzzyEquals(computed.getSensitivity(PARAM.RhoSurface.Name, USD).Sensitivity.toArray(), expected.getSensitivity(PARAM.RhoSurface.Name, USD).Sensitivity.toArray(), TOLERANCE_VOL);
                DoubleArrayMath.fuzzyEquals(computed.getSensitivity(PARAM.NuSurface.Name, USD).Sensitivity.toArray(), expected.getSensitivity(PARAM.NuSurface.Name, USD).Sensitivity.toArray(), TOLERANCE_VOL);
            }
        }
Exemplo n.º 24
0
        public virtual void test_priceSensitivityHullWhiteParameter()
        {
            DoubleArray computed = PRICER.priceSensitivityModelParamsHullWhite(FUTURE, RATE_PROVIDER, HW_PROVIDER);
            DoubleArray vols     = HW_PROVIDER.Parameters.Volatility;
            int         size     = vols.size();

            double[] expected = new double[size];
            for (int i = 0; i < size; ++i)
            {
                double[] volsUp = vols.toArray();
                double[] volsDw = vols.toArray();
                volsUp[i] += TOL_FD;
                volsDw[i] -= TOL_FD;
                HullWhiteOneFactorPiecewiseConstantParameters         paramsUp = HullWhiteOneFactorPiecewiseConstantParameters.of(HW_PROVIDER.Parameters.MeanReversion, DoubleArray.copyOf(volsUp), HW_PROVIDER.Parameters.VolatilityTime.subArray(1, size));
                HullWhiteOneFactorPiecewiseConstantParameters         paramsDw = HullWhiteOneFactorPiecewiseConstantParameters.of(HW_PROVIDER.Parameters.MeanReversion, DoubleArray.copyOf(volsDw), HW_PROVIDER.Parameters.VolatilityTime.subArray(1, size));
                HullWhiteOneFactorPiecewiseConstantParametersProvider provUp   = HullWhiteOneFactorPiecewiseConstantParametersProvider.of(paramsUp, HW_PROVIDER.DayCount, HW_PROVIDER.ValuationDateTime);
                HullWhiteOneFactorPiecewiseConstantParametersProvider provDw   = HullWhiteOneFactorPiecewiseConstantParametersProvider.of(paramsDw, HW_PROVIDER.DayCount, HW_PROVIDER.ValuationDateTime);
                double priceUp = PRICER.price(FUTURE, RATE_PROVIDER, provUp);
                double priceDw = PRICER.price(FUTURE, RATE_PROVIDER, provDw);
                expected[i] = 0.5 * (priceUp - priceDw) / TOL_FD;
            }
            assertTrue(DoubleArrayMath.fuzzyEquals(computed.toArray(), expected, TOL_FD));
        }
        public virtual void test_parameterSensitivity_multi()
        {
            double[] points1 = new double[] { 2.24, 3.45, -2.12, -0.56 };
            double[] points2 = new double[] { -0.145, 1.01, -5.0, -11.0 };
            double[] points3 = new double[] { 1.3, -4.32, 2.1, -7.18 };
            SabrParametersIborCapletFloorletVolatilities prov = SabrParametersIborCapletFloorletVolatilities.of(NAME, EUR_EURIBOR_3M, DATE_TIME, PARAM);
            double expiryTime0 = prov.relativeTime(TEST_OPTION_EXPIRY[0]);
            double expiryTime3 = prov.relativeTime(TEST_OPTION_EXPIRY[3]);

            for (int i = 0; i < NB_TEST; i++)
            {
                PointSensitivities             sensi1   = PointSensitivities.of(IborCapletFloorletSabrSensitivity.of(NAME, expiryTime0, ALPHA, EUR, points1[0]), IborCapletFloorletSabrSensitivity.of(NAME, expiryTime0, BETA, EUR, points1[1]), IborCapletFloorletSabrSensitivity.of(NAME, expiryTime0, RHO, EUR, points1[2]), IborCapletFloorletSabrSensitivity.of(NAME, expiryTime0, NU, EUR, points1[3]));
                PointSensitivities             sensi2   = PointSensitivities.of(IborCapletFloorletSabrSensitivity.of(NAME, expiryTime0, ALPHA, EUR, points2[0]), IborCapletFloorletSabrSensitivity.of(NAME, expiryTime0, BETA, EUR, points2[1]), IborCapletFloorletSabrSensitivity.of(NAME, expiryTime0, RHO, EUR, points2[2]), IborCapletFloorletSabrSensitivity.of(NAME, expiryTime0, NU, EUR, points2[3]));
                PointSensitivities             sensi3   = PointSensitivities.of(IborCapletFloorletSabrSensitivity.of(NAME, expiryTime3, ALPHA, EUR, points3[0]), IborCapletFloorletSabrSensitivity.of(NAME, expiryTime3, BETA, EUR, points3[1]), IborCapletFloorletSabrSensitivity.of(NAME, expiryTime3, RHO, EUR, points3[2]), IborCapletFloorletSabrSensitivity.of(NAME, expiryTime3, NU, EUR, points3[3]));
                PointSensitivities             sensis   = sensi1.combinedWith(sensi2).combinedWith(sensi3).normalized();
                CurrencyParameterSensitivities computed = prov.parameterSensitivity(sensis);
                CurrencyParameterSensitivities expected = prov.parameterSensitivity(sensi1).combinedWith(prov.parameterSensitivity(sensi2)).combinedWith(prov.parameterSensitivity(sensi3));
                DoubleArrayMath.fuzzyEquals(computed.getSensitivity(PARAM.AlphaCurve.Name, EUR).Sensitivity.toArray(), expected.getSensitivity(PARAM.AlphaCurve.Name, EUR).Sensitivity.toArray(), TOLERANCE_VOL);
                DoubleArrayMath.fuzzyEquals(computed.getSensitivity(PARAM.BetaCurve.Name, EUR).Sensitivity.toArray(), expected.getSensitivity(PARAM.BetaCurve.Name, EUR).Sensitivity.toArray(), TOLERANCE_VOL);
                DoubleArrayMath.fuzzyEquals(computed.getSensitivity(PARAM.RhoCurve.Name, EUR).Sensitivity.toArray(), expected.getSensitivity(PARAM.RhoCurve.Name, EUR).Sensitivity.toArray(), TOLERANCE_VOL);
                DoubleArrayMath.fuzzyEquals(computed.getSensitivity(PARAM.NuCurve.Name, EUR).Sensitivity.toArray(), expected.getSensitivity(PARAM.NuCurve.Name, EUR).Sensitivity.toArray(), TOLERANCE_VOL);
            }
        }
        public virtual void bucketedCs01WithNodesIndexTest()
        {
            CurrencyParameterSensitivity fdSingle       = CS01_FD.bucketedCs01(CDS2, RATES_PROVIDER, REF_DATA);
            CurrencyParameterSensitivity analyticSingle = CS01_AN.bucketedCs01(CDS2, RATES_PROVIDER, REF_DATA);
            CurrencyParameterSensitivity fd             = CS01_FD.bucketedCs01(CDS_INDEX, RATES_PROVIDER, REF_DATA);
            CurrencyParameterSensitivity analytic       = CS01_AN.bucketedCs01(CDS_INDEX, RATES_PROVIDER, REF_DATA);

            assertEquals(fd.Currency, USD);
            assertEquals(fd.MarketDataName, CurveName.of("impliedSpreads"));
            assertEquals(fd.ParameterCount, NUM_MARKET_CDS);
            assertEquals(fd.ParameterMetadata, CDS_INDEX_METADATA);
            assertTrue(DoubleArrayMath.fuzzyEquals(fd.Sensitivity.toArray(), fdSingle.Sensitivity.multipliedBy(INDEX_FACTOR).toArray(), NOTIONAL * TOL));
            assertEquals(analytic.Currency, USD);
            assertEquals(analytic.MarketDataName, CurveName.of("impliedSpreads"));
            assertEquals(analytic.ParameterCount, NUM_MARKET_CDS);
            assertEquals(analytic.ParameterMetadata, CDS_INDEX_METADATA);
            assertTrue(DoubleArrayMath.fuzzyEquals(analytic.Sensitivity.toArray(), analyticSingle.Sensitivity.multipliedBy(INDEX_FACTOR).toArray(), NOTIONAL * TOL));
            PointSensitivities             point      = PRICER_INDEX.presentValueOnSettleSensitivity(CDS_INDEX, RATES_PROVIDER, REF_DATA);
            CurrencyParameterSensitivity   paramSensi = RATES_PROVIDER.singleCreditCurveParameterSensitivity(point, INDEX_ID, USD);
            CurrencyParameterSensitivities quoteSensi = QUOTE_CAL.sensitivity(CurrencyParameterSensitivities.of(paramSensi), RATES_PROVIDER);

            assertTrue(DoubleArrayMath.fuzzyEquals(quoteSensi.Sensitivities.get(0).Sensitivity.toArray(), analytic.Sensitivity.toArray(), NOTIONAL * TOL));
        }
Exemplo n.º 27
0
        public virtual void test_function()
        {
            /* Source: EIOPA - European Insurance and Occupational Pensions Authority */
            DoubleArray weights = DoubleArray.of(2.66199573146653, -2.69394271301803, 0.484066828515597, 0.690098457608931, -0.380409008626438, 0.228249649568978, -0.160902407241201);
            DoubleArray nodes   = DoubleArray.of(3d, 5d, 7d, 10d, 15d, 20d, 30d);
            double      alpha   = 0.162009;

            double[] dfExp = new double[] { 1.0, 0.9784491887550640, 0.9542301229696670, 0.9249659993836340, 0.8901581292246130, 0.8554401454007420, 0.8255482242362790, 0.7988075218816840, 0.7726983322166260, 0.7460275162373400, 0.7179860712887310, 0.6881599086848680, 0.6573456807993880, 0.6264650351287230, 0.5962704115599910, 0.5673812725224810, 0.5401743074526890, 0.5144298960128170, 0.4898295198849120, 0.4660960306958390, 0.4429840495494910, 0.4203401447244430, 0.3982807994009170, 0.3769536975722210, 0.3564726090143710, 0.3369247816853740, 0.3183770246642050, 0.3008807334361760, 0.2844760642424350, 0.2691954275764280, 0.2550664411957300, 0.2420825656824480, 0.2301189446273790, 0.2190430997506780, 0.2087446877039010, 0.1991315201776450, 0.1901263120847230, 0.1816640241112870, 0.1736896905038690, 0.1661566430181050, 0.1590250583253540, 0.1522607695344190, 0.1458342933905510, 0.1397200336141430, 0.1338956281057440, 0.1283414136729280, 0.1230399867736360, 0.1179758427202050, 0.1131350790119630, 0.1085051510954450, 0.1040746709988320, 0.0998332410401614, 0.0957713162396188, 0.0918800902340929, 0.0881514004454756, 0.0845776490323465, 0.0811517367898836, 0.0778670076814032, 0.0747172021082445, 0.0716964173703138, 0.0687990740517746, 0.0660198872967402, 0.0633538421279633, 0.0607961721151252, 0.0583423408248006, 0.0559880255866578, 0.0537291031941595, 0.0515616372264417, 0.0494818667339434, 0.0474861960760429, 0.0455711857363243, 0.0437335439716375, 0.0419701191761172, 0.0402778928617874, 0.0386539731741227, 0.0370955888746717, 0.0356000837341027, 0.0341649112882632, 0.0327876299174312, 0.0314658982151756, 0.0301974706183710, 0.0289801932741481, 0.0278120001230513, 0.0266909091805686, 0.0256150190015920, 0.0245825053143602, 0.0235916178120912, 0.0226406770919034, 0.0217280717317838, 0.0208522554973446, 0.0200117446709340, 0.0192051154963766, 0.0184310017332138, 0.0176880923148379, 0.0169751291053533, 0.0162909047503889, 0.0156342606174260, 0.0150040848215014, 0.0143993103324119, 0.0138189131597808, 0.0132619106125611, 0.0127273596297376, 0.0122143551791677, 0.0117220287216560, 0.0112495467375058, 0.0107961093129258, 0.0103609487837926, 0.0099433284343883, 0.0095425412488404, 0.0091579087130933, 0.0087887796653373, 0.0084345291929100, 0.0080945575737721, 0.0077682892607414, 0.0074551719067439, 0.0071546754294138, 0.0068662911134475, 0.0065895307491796, 0.0063239258059133, 0.0060690266385980, 0.0058244017265059 };
            int      years = 121;

            System.Func <double, double> derivFunc = DERIVATIVE.differentiate(x => SW_FUNCTION.value(x, alpha, nodes, weights), x => (x >= 0d));
            double dfPrev = 1d;

            for (int i = 0; i < years; ++i)
            {
                double t = (double)i;
                // value
                double dfCmp = SW_FUNCTION.value(t, alpha, nodes, weights);
                assertEquals(dfCmp, dfExp[i], TOL);
                // first derivative
                double derivCmp = SW_FUNCTION.firstDerivative(t, alpha, nodes, weights);
                double derivExp = derivFunc(t);
                assertEquals(derivCmp, derivExp, EPS);
                // parameter sensitivity
                DoubleArray paramSensiCmp = SW_FUNCTION.parameterSensitivity(t, alpha, nodes);
                System.Func <DoubleArray, DoubleArray> paramSensiFunc = PARAM_SENSI.differentiate(w => SW_FUNCTION.value(t, alpha, nodes, w));
                DoubleArray paramSensiExp = paramSensiFunc(weights);
                assertTrue(DoubleArrayMath.fuzzyEquals(paramSensiCmp.toArray(), paramSensiExp.toArray(), EPS));
                // forward rate
                if (i > 60)
                {
                    double fwd = dfPrev / dfCmp - 1d;
                    assertEquals(fwd, 0.042, ONE_BP);
                }
                dfPrev = dfCmp;
            }
        }
Exemplo n.º 28
0
        //-------------------------------------------------------------------------
        public virtual void regression_test()
        {
            double[]     expected      = new double[] { 3.860405407112769E7, 3.0897699603079587E7, 2.3542824458812844E7, 1.6993448607300103E7, 1.1705393621236656E7, 7865881.8260216825, 5312495.846331886, 3680367.6766224853, 2607701.430445888, 1849818.297903138, 1282881.9812227674 };
            double[][][] sensiExpected = new double[][][]
            {
                new double[][]
                {
                    new double[] { 0d, 0d, -1.016834993607875E8, -1.0687281893573801E8, 0d },
                    new double[] { 0d, 0d, 7.321670893786977E7, 7.695325324735151E7, 0d }
                },
                new double[][]
                {
                    new double[] { 0d, 0d, -1.0021953885887374E8, -1.0533414661787288E8, 0d },
                    new double[] { 0d, 0d, 7.743544635059586E7, 8.13872898657015E7, 0d }
                },
                new double[][]
                {
                    new double[] { 0d, 0d, -9.430418338541561E7, -9.911690666813123E7, 0d },
                    new double[] { 0d, 0d, 7.69436064730077E7, 8.087034941308834E7, 0d }
                },
                new double[][]
                {
                    new double[] { 0d, 0d, -8.284596766339977E7, -8.707393192902757E7, 0d },
                    new double[] { 0d, 0d, 7.031492781003796E7, 7.3903382511043E7, 0d }
                },
                new double[][]
                {
                    new double[] { 0d, 0d, -6.764082328040574E7, -7.109280762910862E7, 0d },
                    new double[] { 0d, 0d, 5.900921722111582E7, 6.2020695857797466E7, 0d }
                },
                new double[][]
                {
                    new double[] { 0d, 0d, -5.2035331262043096E7, -5.4690904337366335E7, 0d },
                    new double[] { 0d, 0d, 4.623499720852033E7, 4.859455581508104E7, 0d }
                },
                new double[][]
                {
                    new double[] { 0d, 0d, -3.862682913929568E7, -4.059811220715709E7, 0d },
                    new double[] { 0d, 0d, 3.470937255296122E7, 3.64807319923551E7, 0d }
                },
                new double[][]
                {
                    new double[] { 0d, 0d, -2.8260648102423556E7, -2.970290309286549E7, 0d },
                    new double[] { 0d, 0d, 2.554672963322189E7, 2.6850482405254934E7, 0d }
                },
                new double[][]
                {
                    new double[] { 0d, 0d, -2.0537629799871795E7, -2.1585747980437294E7, 0d },
                    new double[] { 0d, 0d, 1.8614699892839946E7, 1.9564683195371673E7, 0d }
                },
                new double[][]
                {
                    new double[] { 0d, 0d, -1.4728101851302534E7, -1.5479736361515924E7, 0d },
                    new double[] { 0d, 0d, 1.3364038126029937E7, 1.404605895619165E7, 0d }
                },
                new double[][]
                {
                    new double[] { 0d, 0d, -1.0288414551608022E7, -1.0813473891259879E7, 0d },
                    new double[] { 0d, 0d, 9342412.029968219, 9819193.040939828, 0d }
                }
            };
            double[][] sensiVolExpected = new double[][]
            {
                new double[] { -5.026579681006058E7, 1.8086314260527827E7, 3.7857814067085885E7 },
                new double[] { -8.042768121510313E7, 2.6917736783424407E7, 6.828128237717555E7 },
                new double[] { -7.799249393870309E7, 2.3424689020542752E7, 8.3630726080757E7 },
                new double[] { -3.164036884467365E7, 7924431.550466087, 6.934450937795731E7 },
                new double[] { 3.014881927958022E7, -5158819.178957329, 3.45255051012762E7 },
                new double[] { 6.557093411024924E7, -1634314.6028730718, 2572569.1856012754 },
                new double[] { 5.660208041880186E7, 1.8970507395428047E7, -1.03548720023163E7 },
                new double[] { 1.685473267352155E7, 4.502558010977008E7, -4691785.767471665 },
                new double[] { -2.724527388461766E7, 6.375255786622942E7, 9031557.200953793 },
                new double[] { -5.657559310469577E7, 6.925498619398344E7, 2.0671226006236725E7 },
                new double[] { -6.6067357100129634E7, 6.307938934530911E7, 2.5692820222277485E7 }
            };
            CurveName eurName = RatesProviderFxDataSets.getCurveName(EUR);
            CurveName usdName = RatesProviderFxDataSets.getCurveName(USD);

            for (int i = 0; i < NB_STRIKES; ++i)
            {
                // pv
                CurrencyAmount computed = PRICER.presentValue(CALLS[i], RATES_PROVIDER, VOLS);
                assertEquals(computed.Amount, expected[i], NOTIONAL * TOL);
                // curve sensitivity
                PointSensitivityBuilder        point         = PRICER.presentValueSensitivityRatesStickyStrike(CALLS[i], RATES_PROVIDER, VOLS);
                CurrencyParameterSensitivities sensiComputed = RATES_PROVIDER.parameterSensitivity(point.build());
                assertTrue(DoubleArrayMath.fuzzyEquals(sensiComputed.getSensitivity(eurName, USD).Sensitivity.toArray(), sensiExpected[i][0], NOTIONAL * TOL));
                assertTrue(DoubleArrayMath.fuzzyEquals(sensiComputed.getSensitivity(usdName, USD).Sensitivity.toArray(), sensiExpected[i][1], NOTIONAL * TOL));
                // vol sensitivity
                PointSensitivities pointVol = PRICER.presentValueSensitivityModelParamsVolatility(CALLS[i], RATES_PROVIDER, VOLS).build();
                assertEquals(pointVol.Sensitivities.get(0).Sensitivity, sensiVolExpected[i][2], NOTIONAL * TOL);
                assertEquals(pointVol.Sensitivities.get(1).Sensitivity, sensiVolExpected[i][1], NOTIONAL * TOL);
                assertEquals(pointVol.Sensitivities.get(2).Sensitivity, sensiVolExpected[i][0], NOTIONAL * TOL);
            }
        }