Ejemplo n.º 1
0
        public virtual void sameIntervalsTest()
        {
            DoubleArray xValues = DoubleArray.of(-1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);

            DoubleArray[] yValues = new DoubleArray[] { DoubleArray.of(1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001), DoubleArray.of(11.0, 11.0, 8.0, 5.0, 1.001, 1.001, 5.0, 8.0, 11.0, 11.0), DoubleArray.of(1.001, 1.001, 5.0, 8.0, 9.0, 9.0, 11.0, 12.0, 18.0, 18.0) };
            int           nKeys   = 100;

            double[] keys     = new double[nKeys];
            double   interval = 0.061;

            for (int i = 0; i < nKeys; ++i)
            {
                keys[i] = xValues.get(0) + interval * i;
            }

            CurveExtrapolator extrap = InterpolatorCurveExtrapolator.INSTANCE;
            int yDim = yValues.Length;

            for (int k = 0; k < yDim; ++k)
            {
                BoundCurveInterpolator         boundInterp = CurveInterpolators.SQUARE_LINEAR.bind(xValues, yValues[k], extrap, extrap);
                AbstractBoundCurveInterpolator baseInterp  = (AbstractBoundCurveInterpolator)boundInterp;
                for (int j = 0; j < nKeys; ++j)
                {
                    // value
                    assertEquals(boundInterp.interpolate(keys[j]), baseInterp.doInterpolate(keys[j]), TOL);
                    // derivative
                    assertEquals(boundInterp.firstDerivative(keys[j]), baseInterp.doFirstDerivative(keys[j]), TOL);
                    // sensitivity
                    assertTrue(boundInterp.parameterSensitivity(keys[j]).equalWithTolerance(baseInterp.doParameterSensitivity(keys[j]), TOL));
                }
            }
        }
Ejemplo n.º 2
0
        //-------------------------------------------------------------------------
        public virtual void test_firstNode()
        {
            BoundCurveInterpolator bci = DQ_INTERPOLATOR.bind(X_DATA, Y_DATA, FLAT_EXTRAPOLATOR, FLAT_EXTRAPOLATOR);

            assertEquals(bci.interpolate(0.0), 3.0, TOL);
            assertEquals(bci.firstDerivative(0.0), bci.firstDerivative(0.00000001), 1e-6);
        }
Ejemplo n.º 3
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 test_lastNode()
        {
            BoundCurveInterpolator bci = LL_INTERPOLATOR.bind(X_DATA, Y_DATA, FLAT_EXTRAPOLATOR, FLAT_EXTRAPOLATOR);

            assertEquals(bci.interpolate(5.0), 2.0, TOL);
            assertEquals(bci.firstDerivative(5.0), bci.firstDerivative(4.99999999), 1e-6);
        }
Ejemplo n.º 5
0
        public virtual void differentIntervalsTest()
        {
            DoubleArray xValues = DoubleArray.of(1.0328724558967068, 1.2692381049172323, 2.8611430465380905, 4.296118458251132, 7.011992052151352, 7.293354144919639, 8.557971037612713, 8.77306861567384, 10.572470371584489, 12.96945799507056);

            DoubleArray[] yValues = new DoubleArray[] { DoubleArray.of(1.1593075755231343, 2.794957672828094, 4.674733634811079, 5.517689918508841, 6.138447304104604, 6.264375977142906, 6.581666492568779, 8.378685055774037, 10.005246918325483, 10.468304334744241), DoubleArray.of(9.95780079114617, 8.733013195721913, 8.192165283188197, 6.539369493529048, 6.3868683960757515, 4.700471352238411, 4.555354921077598, 3.780781869340659, 2.299369456202763, 0.9182441378327986) };
            int           nKeys   = 100;

            double[] keys     = new double[nKeys];
            double   interval = 0.061;

            for (int i = 0; i < nKeys; ++i)
            {
                keys[i] = xValues.get(0) + interval * i;
            }

            CurveExtrapolator extrap = InterpolatorCurveExtrapolator.INSTANCE;
            int yDim = yValues.Length;

            for (int k = 0; k < yDim; ++k)
            {
                BoundCurveInterpolator         boundInterp = CurveInterpolators.SQUARE_LINEAR.bind(xValues, yValues[k], extrap, extrap);
                AbstractBoundCurveInterpolator baseInterp  = (AbstractBoundCurveInterpolator)boundInterp;
                for (int j = 0; j < nKeys; ++j)
                {
                    // value
                    assertEquals(boundInterp.interpolate(keys[j]), baseInterp.doInterpolate(keys[j]), TOL);
                    // derivative
                    assertEquals(boundInterp.firstDerivative(keys[j]), baseInterp.doFirstDerivative(keys[j]), TOL);
                    // sensitivity
                    assertTrue(boundInterp.parameterSensitivity(keys[j]).equalWithTolerance(baseInterp.doParameterSensitivity(keys[j]), TOL));
                }
            }
        }
Ejemplo n.º 6
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));
            }
        }
        public virtual void noRightTest()
        {
            BoundCurveInterpolator bci = LINEAR.bind(X_DATA, Y_DATA, DISCOUNT_FACTOR_QUADRATIC_LEFT_ZERO_RATE, DISCOUNT_FACTOR_QUADRATIC_LEFT_ZERO_RATE);

            assertThrowsIllegalArg(() => bci.interpolate(10d));
            assertThrowsIllegalArg(() => bci.firstDerivative(10d));
            assertThrowsIllegalArg(() => bci.parameterSensitivity(10d));
        }
        //-------------------------------------------------------------------------
        public virtual void test_firstNode()
        {
            BoundCurveInterpolator bci = INTERPOLATOR.bind(X_DATA, Y_DATA, FLAT_EXTRAPOLATOR, FLAT_EXTRAPOLATOR);

            assertEquals(bci.interpolate(0.0), 3.0, TOL);
            assertEquals(bci.parameterSensitivity(0.0).get(0), 1d, TOL);
            assertEquals(bci.parameterSensitivity(0.0).get(1), 0d, TOL);
        }
Ejemplo n.º 9
0
        public virtual void test_lastNode()
        {
            BoundCurveInterpolator bci = NATURAL_CUBLIC_SPLINE_INTERPOLATOR.bind(X_DATA, Y_DATA, FLAT_EXTRAPOLATOR, FLAT_EXTRAPOLATOR);

            assertEquals(bci.interpolate(5.0), 2.0, TOL);
            assertEquals(bci.parameterSensitivity(5.0).get(X_DATA.size() - 2), 0d, TOL);
            assertEquals(bci.parameterSensitivity(5.0).get(X_DATA.size() - 1), 1d, TOL);
        }
Ejemplo n.º 10
0
        //-------------------------------------------------------------------------
        public virtual void noLeftTest()
        {
            BoundCurveInterpolator bci = PRODUCT_LINEAR.bind(X_VALUES, Y_VALUES, DISCOUNT_FACTOR_LINEAR_RIGHT_ZERO_RATE, DISCOUNT_FACTOR_LINEAR_RIGHT_ZERO_RATE);

            assertThrowsIllegalArg(() => bci.interpolate(0.2d));
            assertThrowsIllegalArg(() => bci.firstDerivative(0.3d));
            assertThrowsIllegalArg(() => bci.parameterSensitivity(0.6d));
        }
        public virtual void test_noRight()
        {
            BoundCurveInterpolator bci = CurveInterpolators.LINEAR.bind(X_DATA, Y_DATA, QL_EXTRAPOLATOR, QL_EXTRAPOLATOR);

            assertThrowsIllegalArg(() => bci.interpolate(10d));
            assertThrowsIllegalArg(() => bci.firstDerivative(10d));
            assertThrowsIllegalArg(() => bci.parameterSensitivity(10d));
        }
Ejemplo n.º 12
0
        public virtual void test_allNodes()
        {
            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);
            }
        }
Ejemplo n.º 13
0
        //-------------------------------------------------------------------------
        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_extrapolation()
        {
            BoundCurveInterpolator bci = CurveInterpolators.DOUBLE_QUADRATIC.bind(X_DATA, Y_DATA, LINEAR_EXTRAPOLATOR, LINEAR_EXTRAPOLATOR);

            for (int i = 0; i < X_TEST.size(); i++)
            {
                assertEquals(bci.interpolate(X_TEST.get(i)), Y_TEST.get(i), 1e-6);
            }
        }
        public virtual void test_lastNode()
        {
            BoundCurveInterpolator bci = LINEAR_INTERPOLATOR.bind(X_DATA, Y_DATA, FLAT_EXTRAPOLATOR, FLAT_EXTRAPOLATOR);

            assertEquals(bci.interpolate(5.0), 2.0, TOL);
            assertEquals(bci.firstDerivative(5.0), bci.firstDerivative(4.99), TOL);
            assertEquals(bci.parameterSensitivity(5.0).get(X_DATA.size() - 2), 0d, TOL);
            assertEquals(bci.parameterSensitivity(5.0).get(X_DATA.size() - 1), 1d, TOL);
        }
Ejemplo n.º 16
0
        public virtual void test_firstDerivative()
        {
            BoundCurveInterpolator bci = NATURAL_CUBLIC_SPLINE_INTERPOLATOR.bind(X_DATA, Y_DATA, FLAT_EXTRAPOLATOR, FLAT_EXTRAPOLATOR);
            double eps   = 1e-8;
            double lo    = bci.interpolate(0.2);
            double hi    = bci.interpolate(0.2 + eps);
            double deriv = (hi - lo) / eps;

            assertEquals(bci.firstDerivative(0.2), deriv, 1e-6);
        }
Ejemplo n.º 17
0
        public virtual void value()
        {
            BoundCurveInterpolator bci = CurveInterpolators.LINEAR.bind(X_DATA, Y_DATA, EXP_EXTRAPOLATOR, EXP_EXTRAPOLATOR);

            double mLeft  = Math.Log(Y_DATA.get(0)) / X_DATA.get(0);
            double mRight = Math.Log(Y_DATA.get(X_DATA.size() - 1)) / X_DATA.get(X_DATA.size() - 1);

            assertEquals(bci.interpolate(0.0), 1d, TOLERANCE_VALUE);
            assertEquals(bci.interpolate(-0.2), Math.Exp(mLeft * -0.2), TOLERANCE_VALUE);
            assertEquals(bci.interpolate(6.0), Math.Exp(mRight * 6.0), TOLERANCE_VALUE);
        }
Ejemplo n.º 18
0
        public virtual void test_exceptionThrown()
        {
            BoundCurveInterpolator bci = CurveInterpolators.LINEAR.bind(X_DATA, Y_DATA, EXCEPTION_EXTRAPOLATOR, EXCEPTION_EXTRAPOLATOR);

            assertThrows(() => bci.interpolate(-1d), typeof(System.NotSupportedException));
            assertThrows(() => bci.firstDerivative(-1d), typeof(System.NotSupportedException));
            assertThrows(() => bci.parameterSensitivity(-1d), typeof(System.NotSupportedException));
            assertThrows(() => bci.interpolate(10d), typeof(System.NotSupportedException));
            assertThrows(() => bci.firstDerivative(10d), typeof(System.NotSupportedException));
            assertThrows(() => bci.parameterSensitivity(10d), typeof(System.NotSupportedException));
        }
Ejemplo n.º 19
0
        public virtual void smallKeyTest()
        {
            DoubleArray            xValues = DoubleArray.of(1e-13, 3e-8, 2e-5);
            DoubleArray            yValues = DoubleArray.of(1.0, 13.2, 1.5);
            double                 keyDw   = 1.0e-12;
            BoundCurveInterpolator bound   = INTERP.bind(xValues, yValues);

            assertThrowsIllegalArg(() => bound.interpolate(keyDw));
            assertThrowsIllegalArg(() => bound.firstDerivative(keyDw));
            assertThrowsIllegalArg(() => bound.parameterSensitivity(keyDw));
        }
 internal Bound(DoubleArray xValues, DoubleArray yValues, BoundCurveInterpolator interpolator)
 {
     this.nodeCount      = xValues.size();
     this.lastXValue     = xValues.get(nodeCount - 1);
     this.lastYValue     = yValues.get(nodeCount - 1);
     this.lastDf         = Math.Exp(-lastXValue * lastYValue);
     this.eps            = EPS * (lastXValue - xValues.get(0));
     this.rightYGradient = (lastYValue - interpolator.interpolate(lastXValue - eps)) / eps;
     this.rightYSens     = interpolator.parameterSensitivity(lastXValue - eps).multipliedBy(-1d);
     this.coef1          = -lastYValue * lastDf - lastXValue * lastDf * rightYGradient;
     this.coef0          = lastDf - coef1 * lastXValue;
 }
Ejemplo n.º 21
0
 internal Bound(DoubleArray xValues, DoubleArray yValues, BoundCurveInterpolator interpolator)
 {
     this.nodeCount   = xValues.size();
     this.firstXValue = xValues.get(0);
     this.firstYValue = yValues.get(0);
     this.lastXValue  = xValues.get(nodeCount - 1);
     this.lastYValue  = yValues.get(nodeCount - 1);
     // left
     this.leftGradient = Math.Log(firstYValue) / firstXValue;
     // right
     this.rightGradient = Math.Log(lastYValue) / lastXValue;
 }
Ejemplo n.º 22
0
        public virtual void test_sensitivityEdgeCase()
        {
            BoundCurveInterpolator bci = DQ_INTERPOLATOR.bind(X_SENS, Y_SENS, FLAT_EXTRAPOLATOR, FLAT_EXTRAPOLATOR);
            double      lastXValue     = X_SENS.get(X_SENS.size() - 1);
            DoubleArray sensitivity    = bci.parameterSensitivity(lastXValue);

            for (int i = 0; i < sensitivity.size() - 1; i++)
            {
                assertEquals(0, sensitivity.get(i), EPS);
            }
            assertEquals(1.0, sensitivity.get(sensitivity.size() - 1), EPS);
        }
Ejemplo n.º 23
0
        //-------------------------------------------------------------------------
        public virtual void test_sensitivities()
        {
            BoundCurveInterpolator bci = DQ_INTERPOLATOR.bind(X_SENS, Y_SENS, FLAT_EXTRAPOLATOR, FLAT_EXTRAPOLATOR);
            double lastXValue          = X_SENS.get(X_SENS.size() - 1);

            for (int i = 0; i < 100; i++)
            {
                double      t           = lastXValue * RANDOM.NextDouble();
                DoubleArray sensitivity = bci.parameterSensitivity(t);
                assertEquals(sensitivity.sum(), 1d, TOL);
            }
        }
Ejemplo n.º 24
0
        public virtual void derivativeTest()
        {
            BoundCurveInterpolator bci = PRODUCT_LINEAR.bind(X_VALUES, Y_VALUES, LINEAR, DISCOUNT_FACTOR_LINEAR_RIGHT_ZERO_RATE);

            for (int i = 0; i < NUM_KEYS; ++i)
            {
                double key      = X_KEYS.get(i);
                double computed = bci.firstDerivative(key);
                double expected = 0.5d * (bci.interpolate(key + EPS) - bci.interpolate(key - EPS)) / EPS;
                assertEquals(computed, expected, EPS);
            }
        }
Ejemplo n.º 25
0
        public virtual void interpolateTest()
        {
            BoundCurveInterpolator bci = PRODUCT_LINEAR.bind(X_VALUES, Y_VALUES, LINEAR, DISCOUNT_FACTOR_LINEAR_RIGHT_ZERO_RATE);
            double grad = -Y_VALUES.get(NUM_DATA - 1) * DSC_VALUES.get(NUM_DATA - 1) - X_VALUES.get(NUM_DATA - 1) * DSC_VALUES.get(NUM_DATA - 1) * bci.firstDerivative(X_VALUES.get(NUM_DATA - 1));

            for (int i = 0; i < NUM_KEYS; ++i)
            {
                double key = X_KEYS.get(i);
                double df  = grad * (key - X_VALUES.get(NUM_DATA - 1)) + DSC_VALUES.get(NUM_DATA - 1);
                assertEquals(bci.interpolate(key), -Math.Log(df) / key, TOL);
            }
        }
        //-------------------------------------------------------------------------
        public virtual void test_interpolation()
        {
            BoundCurveInterpolator bci = 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);
            }
        }
Ejemplo n.º 27
0
        public virtual void sensitivity3()
        {
            BoundCurveInterpolator bci = CurveInterpolators.LINEAR.bind(X_DATA, Y_DATA, EXP_EXTRAPOLATOR, EXP_EXTRAPOLATOR);

            double shift = 1e-8;
            double value = 6d;

            double[] yDataShifted = Y_DATA.toArray();
            yDataShifted[Y_DATA.size() - 1] += shift;
            BoundCurveInterpolator bciShifted = CurveInterpolators.LINEAR.bind(X_DATA, DoubleArray.ofUnsafe(yDataShifted), EXP_EXTRAPOLATOR, EXP_EXTRAPOLATOR);

            assertEquals(bci.parameterSensitivity(value).get(Y_DATA.size() - 1), (bciShifted.interpolate(value) - bci.interpolate(value)) / shift, TOLERANCE_SENSI);
        }
            internal Bound(DoubleArray xValues, DoubleArray yValues, BoundCurveInterpolator interpolator)
            {
                this.nodeCount   = xValues.size();
                this.firstXValue = xValues.get(0);
                this.firstYValue = yValues.get(0);
                this.lastXValue  = xValues.get(nodeCount - 1);
                double gradient = interpolator.firstDerivative(firstXValue);

                this.eps          = EPS * (lastXValue - firstXValue);
                this.leftQuadCoef = gradient / firstXValue - (firstYValue - 1d) / firstXValue / firstXValue;
                this.leftLinCoef  = -gradient + 2d * (firstYValue - 1d) / firstXValue;
                this.leftSens     = Suppliers.memoize(() => interpolator.parameterSensitivity(firstXValue + eps));
            }
        /// <summary>
        /// Tests the interpolation in the strike dimension at a time of the grid.
        /// </summary>
        public virtual void volatilityStrikeInterpolation()
        {
            double                 forward      = 1.40;
            double                 timeToExpiry = 0.50;
            double                 strike       = 1.50;
            DoubleArray            strikes      = SMILE_TERM.VolatilityTerm.get(2).strike(forward);
            DoubleArray            vol          = SMILE_TERM.VolatilityTerm.get(2).Volatility;
            BoundCurveInterpolator interpolator = CurveInterpolators.LINEAR.bind(strikes, vol);
            double                 volExpected  = interpolator.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_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));
            }
        }