Esempio n. 1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void test()
        public virtual void test()
        {
            /// <summary>
            /// Take the form $y = a\sin(bx + cy) + cos(y)$
            /// </summary>
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final ParameterizedSurface testSurface = new ParameterizedSurface()
            ParameterizedSurface testSurface = new ParameterizedSurfaceAnonymousInnerClass(this);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final ParameterizedFunction<com.opengamma.strata.collect.tuple.DoublesPair, com.opengamma.strata.collect.array.DoubleArray, com.opengamma.strata.collect.array.DoubleArray> parmSense = new ParameterizedFunction<com.opengamma.strata.collect.tuple.DoublesPair, com.opengamma.strata.collect.array.DoubleArray, com.opengamma.strata.collect.array.DoubleArray>()
            ParameterizedFunction <DoublesPair, DoubleArray, DoubleArray> parmSense = new ParameterizedFunctionAnonymousInnerClass(this);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleArray params = com.opengamma.strata.collect.array.DoubleArray.of(0.7, -0.3, 1.2);
            DoubleArray @params = DoubleArray.of(0.7, -0.3, 1.2);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.function.Function<com.opengamma.strata.collect.tuple.DoublesPair, com.opengamma.strata.collect.array.DoubleArray> paramsSenseFD = testSurface.getZParameterSensitivity(params);
            System.Func <DoublesPair, DoubleArray> paramsSenseFD = testSurface.getZParameterSensitivity(@params);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.function.Function<com.opengamma.strata.collect.tuple.DoublesPair, com.opengamma.strata.collect.array.DoubleArray> paramsSenseAnal = parmSense.asFunctionOfArguments(params);
            System.Func <DoublesPair, DoubleArray> paramsSenseAnal = parmSense.asFunctionOfArguments(@params);

            for (int i = 0; i < 20; i++)
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double x = Math.PI * (-0.5 + i / 19.0);
                double x = Math.PI * (-0.5 + i / 19.0);
                for (int j = 0; j < 20; j++)
                {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double y = Math.PI * (-0.5 + j / 19.0);
                    double y = Math.PI * (-0.5 + j / 19.0);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.tuple.DoublesPair xy = com.opengamma.strata.collect.tuple.DoublesPair.of(x, y);
                    DoublesPair xy = DoublesPair.of(x, y);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleArray s1 = paramsSenseAnal.apply(xy);
                    DoubleArray s1 = paramsSenseAnal(xy);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleArray s2 = paramsSenseFD.apply(xy);
                    DoubleArray s2 = paramsSenseFD(xy);
                    for (int k = 0; k < 3; k++)
                    {
                        assertEquals(s1.get(k), s2.get(k), 1e-10);
                    }
                }
            }
        }
Esempio n. 2
0
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: @Override public System.Nullable<double> evaluate(final com.opengamma.strata.collect.tuple.DoublesPair xy, final com.opengamma.strata.collect.array.DoubleArray parameters)
            public override double?evaluate(DoublesPair xy, DoubleArray parameters)
            {
                assertEquals(3, parameters.size());
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double a = parameters.get(0);
                double a = parameters.get(0);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double b = parameters.get(1);
                double b = parameters.get(1);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double c = parameters.get(2);
                double c = parameters.get(2);

                return(a * Math.Sin(b * xy.First + c * xy.Second) + Math.Cos(xy.Second));
            }
Esempio n. 3
0
        // constructor that sorts (artificial boolean flag)
        private InterpolatedNodalSurface(SurfaceMetadata metadata, DoubleArray xValues, DoubleArray yValues, DoubleArray zValues, SurfaceInterpolator interpolator, bool sort)
        {
            validateInputs(metadata, xValues, yValues, zValues, interpolator);
            // sort inputs
            IDictionary <DoublesPair, ObjDoublePair <ParameterMetadata> > sorted = new SortedDictionary <DoublesPair, ObjDoublePair <ParameterMetadata> >();

            for (int i = 0; i < xValues.size(); i++)
            {
                ParameterMetadata pm = metadata.getParameterMetadata(i);
                sorted[DoublesPair.of(xValues.get(i), yValues.get(i))] = ObjDoublePair.of(pm, zValues.get(i));
            }
            double[]            sortedX  = new double[sorted.Count];
            double[]            sortedY  = new double[sorted.Count];
            double[]            sortedZ  = new double[sorted.Count];
            ParameterMetadata[] sortedPm = new ParameterMetadata[sorted.Count];
            int pos = 0;

            foreach (KeyValuePair <DoublesPair, ObjDoublePair <ParameterMetadata> > entry in sorted.SetOfKeyValuePairs())
            {
                sortedX[pos]  = entry.Key.First;
                sortedY[pos]  = entry.Key.Second;
                sortedZ[pos]  = entry.Value.Second;
                sortedPm[pos] = entry.Value.First;
                pos++;
            }
            // assign
            SurfaceMetadata sortedMetadata = metadata.withParameterMetadata(Arrays.asList(sortedPm));

            this.metadata = sortedMetadata;
            this.xValues  = DoubleArray.ofUnsafe(sortedX);
            this.yValues  = DoubleArray.ofUnsafe(sortedY);
            this.zValues  = DoubleArray.ofUnsafe(sortedZ);
            IDictionary <DoublesPair, double> pairs = new Dictionary <DoublesPair, double>();

            for (int i = 0; i < xValues.size(); i++)
            {
                pairs[DoublesPair.of(xValues.get(i), yValues.get(i))] = zValues.get(i);
            }
            this.interpolator      = interpolator;
            this.boundInterpolator = interpolator.bind(this.xValues, this.yValues, this.zValues);
            this.parameterMetadata = IntStream.range(0, ParameterCount).mapToObj(i => sortedMetadata.getParameterMetadata(i)).collect(toImmutableList());
        }
        public virtual void test_volatility_sensitivity()
        {
            double eps   = 1.0e-6;
            int    nData = TIME.size();

            for (int i = 0; i < NB_TEST; i++)
            {
                double expiryTime = VOLS.relativeTime(TEST_OPTION_EXPIRY[i]);
                SwaptionSensitivity            point      = SwaptionSensitivity.of(VOLS.Name, expiryTime, TEST_TENOR, TEST_STRIKE[i], TEST_FORWARD, GBP, TEST_SENSITIVITY[i]);
                CurrencyParameterSensitivities sensActual = VOLS.parameterSensitivity(point);
                CurrencyParameterSensitivity   sensi      = sensActual.getSensitivity(SURFACE.Name, GBP);
                DoubleArray computed = sensi.Sensitivity;

                IDictionary <DoublesPair, double> map = new Dictionary <DoublesPair, double>();
                for (int j = 0; j < nData; ++j)
                {
                    DoubleArray volDataUp            = VOL.subArray(0, nData).with(j, VOL.get(j) + eps);
                    DoubleArray volDataDw            = VOL.subArray(0, nData).with(j, VOL.get(j) - eps);
                    InterpolatedNodalSurface paramUp = InterpolatedNodalSurface.of(METADATA, TIME, STRIKE, volDataUp, INTERPOLATOR_2D);
                    InterpolatedNodalSurface paramDw = InterpolatedNodalSurface.of(METADATA, TIME, STRIKE, volDataDw, INTERPOLATOR_2D);
                    NormalSwaptionExpiryStrikeVolatilities provUp = NormalSwaptionExpiryStrikeVolatilities.of(CONVENTION, VAL_DATE_TIME, paramUp);
                    NormalSwaptionExpiryStrikeVolatilities provDw = NormalSwaptionExpiryStrikeVolatilities.of(CONVENTION, VAL_DATE_TIME, paramDw);
                    double volUp = provUp.volatility(TEST_OPTION_EXPIRY[i], TEST_TENOR, TEST_STRIKE[i], TEST_FORWARD);
                    double volDw = provDw.volatility(TEST_OPTION_EXPIRY[i], TEST_TENOR, TEST_STRIKE[i], TEST_FORWARD);
                    double fd    = 0.5 * (volUp - volDw) / eps;
                    map[DoublesPair.of(TIME.get(j), STRIKE.get(j))] = fd;
                }
                IList <ParameterMetadata> list = sensi.ParameterMetadata;
                assertEquals(computed.size(), nData);
                for (int j = 0; j < list.Count; ++j)
                {
                    SwaptionSurfaceExpiryStrikeParameterMetadata metadata = (SwaptionSurfaceExpiryStrikeParameterMetadata)list[i];
                    double expected = map[DoublesPair.of(metadata.YearFraction, metadata.Strike)];
                    assertEquals(computed.get(i), expected, eps);
                }
            }
        }
Esempio n. 5
0
 //-------------------------------------------------------------------------
 public double zValue(double x, double y)
 {
     return(deformationFunction.apply(DoublesPair.of(x, y)).Value);
 }
Esempio n. 6
0
 public UnitParameterSensitivity zValueParameterSensitivity(double x, double y)
 {
     return(Metadata.ParameterMetadata.Present ? UnitParameterSensitivity.of(Metadata.SurfaceName, Metadata.ParameterMetadata.get(), deformationFunction.apply(DoublesPair.of(x, y)).Derivatives) : UnitParameterSensitivity.of(Metadata.SurfaceName, deformationFunction.apply(DoublesPair.of(x, y)).Derivatives));
 }
        public virtual void regressionPvSurfaceSensi()
        {
            PointSensitivities pointComputed = SWAPTION_PRICER.presentValueSensitivityModelParamsSabr(SWAPTION_PAY_LONG, RATE_PROVIDER, VOLS_REGRESSION).build();

            assertSensitivity(pointComputed, SabrParameterType.ALPHA, 6.5786313367554754E7, REGRESSION_TOL);
            assertSensitivity(pointComputed, SabrParameterType.BETA, -1.2044275797229866E7, REGRESSION_TOL);
            assertSensitivity(pointComputed, SabrParameterType.RHO, 266223.51118849067, REGRESSION_TOL);
            assertSensitivity(pointComputed, SabrParameterType.NU, 400285.5505271345, REGRESSION_TOL);
            CurrencyParameterSensitivities sensiComputed = VOLS_REGRESSION.parameterSensitivity(pointComputed);

            double[][] alphaExp = new double[][]
            {
                new double[] { 0.0, 1.0, 0.0 },
                new double[] { 0.5, 1.0, 0.0 },
                new double[] { 1.0, 1.0, 0.0 },
                new double[] { 2.0, 1.0, 0.0 },
                new double[] { 5.0, 1.0, 0.0 },
                new double[] { 10.0, 1.0, 0.0 },
                new double[] { 0.0, 5.0, 0.0 },
                new double[] { 0.5, 5.0, 0.0 },
                new double[] { 1.0, 5.0, 6204.475194599179 },
                new double[] { 2.0, 5.0, 3.94631212984123E7 },
                new double[] { 5.0, 5.0, 0.0 },
                new double[] { 10.0, 5.0, 0.0 },
                new double[] { 0.0, 10.0, 0.0 },
                new double[] { 0.5, 10.0, 0.0 },
                new double[] { 1.0, 10.0, 4136.961894403858 },
                new double[] { 2.0, 10.0, 2.631285063205345E7 },
                new double[] { 5.0, 10.0, 0.0 },
                new double[] { 10.0, 10.0, 0.0 }
            };
            double[][] betaExp = new double[][]
            {
                new double[] { 0.0, 1.0, -0.0 },
                new double[] { 0.5, 1.0, -0.0 },
                new double[] { 1.0, 1.0, -0.0 },
                new double[] { 2.0, 1.0, -0.0 },
                new double[] { 5.0, 1.0, -0.0 },
                new double[] { 10.0, 1.0, -0.0 },
                new double[] { 0.0, 5.0, -0.0 },
                new double[] { 0.5, 5.0, -0.0 },
                new double[] { 1.0, 5.0, -1135.926404680998 },
                new double[] { 2.0, 5.0, -7224978.759366533 },
                new double[] { 5.0, 5.0, -0.0 },
                new double[] { 10.0, 5.0, -0.0 },
                new double[] { 0.0, 10.0, -0.0 },
                new double[] { 0.5, 10.0, -0.0 },
                new double[] { 1.0, 10.0, -757.402375482629 },
                new double[] { 2.0, 10.0, -4817403.70908317 },
                new double[] { 5.0, 10.0, -0.0 },
                new double[] { 10.0, 10.0, -0.0 }
            };
            double[][] rhoExp = new double[][]
            {
                new double[] { 0.0, 1.0, 0.0 },
                new double[] { 0.5, 1.0, 0.0 },
                new double[] { 1.0, 1.0, 0.0 },
                new double[] { 2.0, 1.0, 0.0 },
                new double[] { 5.0, 1.0, 0.0 },
                new double[] { 10.0, 1.0, 0.0 },
                new double[] { 0.0, 5.0, 0.0 },
                new double[] { 0.5, 5.0, 0.0 },
                new double[] { 1.0, 5.0, 25.10821912392996 },
                new double[] { 2.0, 5.0, 159699.03429338703 },
                new double[] { 5.0, 5.0, 0.0 },
                new double[] { 10.0, 5.0, 0.0 },
                new double[] { 0.0, 10.0, 0.0 },
                new double[] { 0.5, 10.0, 0.0 },
                new double[] { 1.0, 10.0, 16.741423326578513 },
                new double[] { 2.0, 10.0, 106482.62725265314 },
                new double[] { 5.0, 10.0, 0.0 },
                new double[] { 10.0, 10.0, 0.0 }
            };
            double[][] nuExp = new double[][]
            {
                new double[] { 0.0, 1.0, 0.0 },
                new double[] { 0.5, 1.0, 0.0 },
                new double[] { 1.0, 1.0, 0.0 },
                new double[] { 2.0, 1.0, 0.0 },
                new double[] { 5.0, 1.0, 0.0 },
                new double[] { 10.0, 1.0, 0.0 },
                new double[] { 0.0, 5.0, 0.0 },
                new double[] { 0.5, 5.0, 0.0 },
                new double[] { 1.0, 5.0, 37.751952372314484 },
                new double[] { 2.0, 5.0, 240118.59649585965 },
                new double[] { 5.0, 5.0, 0.0 },
                new double[] { 10.0, 5.0, 0.0 },
                new double[] { 0.0, 10.0, 0.0 },
                new double[] { 0.5, 10.0, 0.0 },
                new double[] { 1.0, 10.0, 25.171893432592533 },
                new double[] { 2.0, 10.0, 160104.03018547 },
                new double[] { 5.0, 10.0, 0.0 },
                new double[] { 10.0, 10.0, 0.0 }
            };
            double[][][]      exps     = new double[][][] { alphaExp, betaExp, rhoExp, nuExp };
            SurfaceMetadata[] metadata = new SurfaceMetadata[] { SwaptionSabrRateVolatilityDataSet.META_ALPHA, SwaptionSabrRateVolatilityDataSet.META_BETA_USD, SwaptionSabrRateVolatilityDataSet.META_RHO, SwaptionSabrRateVolatilityDataSet.META_NU };
            // x-y-value order does not match sorted order in surface, thus sort it
            CurrencyParameterSensitivities sensiExpected = CurrencyParameterSensitivities.empty();

            for (int i = 0; i < exps.Length; ++i)
            {
                int size = exps[i].Length;
                IDictionary <DoublesPair, double> sensiMap = new SortedDictionary <DoublesPair, double>();
                for (int j = 0; j < size; ++j)
                {
                    sensiMap[DoublesPair.of(exps[i][j][0], exps[i][j][1])] = exps[i][j][2];
                }
                IList <ParameterMetadata> paramMetadata = new List <ParameterMetadata>(size);
                IList <double>            sensi         = new List <double>();
                foreach (KeyValuePair <DoublesPair, double> entry in sensiMap.SetOfKeyValuePairs())
                {
                    paramMetadata.Add(SwaptionSurfaceExpiryTenorParameterMetadata.of(entry.Key.First, entry.Key.Second));
                    sensi.Add(entry.Value);
                }
                SurfaceMetadata surfaceMetadata = metadata[i].withParameterMetadata(paramMetadata);
                sensiExpected = sensiExpected.combinedWith(CurrencyParameterSensitivity.of(surfaceMetadata.SurfaceName, surfaceMetadata.ParameterMetadata.get(), USD, DoubleArray.copyOf(sensi)));
            }
            testSurfaceParameterSensitivities(sensiComputed, sensiExpected, REGRESSION_TOL * NOTIONAL);
        }
Esempio n. 8
0
        //-----------------------------------------------------------------------
        private Pair <ImmutableList <double[]>, RecombiningTrinomialTreeData> calibrate(System.Func <DoublesPair, double> impliedVolatilitySurface, double spot, System.Func <double, double> interestRate, System.Func <double, double> dividendRate)
        {
            double[][]           stateValue  = new double[nSteps + 1][];
            double[]             df          = new double[nSteps];
            double[]             timePrim    = new double[nSteps + 1];
            IList <DoubleMatrix> probability = new List <DoubleMatrix>(nSteps);
            int nTotal = (nSteps - 1) * (nSteps - 1) + 1;

            double[] timeRes = new double[nTotal];
            double[] spotRes = new double[nTotal];
            double[] volRes  = new double[nTotal];
            // uniform grid based on TrigeorgisLatticeSpecification
            double volatility = impliedVolatilitySurface(DoublesPair.of(maxTime, spot));
            double dt         = maxTime / nSteps;
            double dx         = volatility * Math.Sqrt(3d * dt);
            double upFactor   = Math.Exp(dx);
            double downFactor = Math.Exp(-dx);

            double[] adSec      = new double[2 * nSteps + 1];
            double[] assetPrice = new double[2 * nSteps + 1];
            for (int i = nSteps; i > -1; --i)
            {
                timePrim[i] = dt * i;
                if (i == 0)
                {
                    resolveFirstLayer(interestRate, dividendRate, nTotal, dt, spot, adSec, assetPrice, timeRes, spotRes, volRes, df, stateValue, probability);
                }
                else
                {
                    double   zeroRate         = interestRate(timePrim[i]);
                    double   zeroDividendRate = dividendRate(timePrim[i]);
                    double   zeroCostRate     = zeroRate - zeroDividendRate;
                    int      nNodes           = 2 * i + 1;
                    double[] assetPriceLocal  = new double[nNodes];
                    double[] callOptionPrice  = new double[nNodes];
                    double[] putOptionPrice   = new double[nNodes];
                    int      position         = i - 1;
                    double   assetTmp         = spot * Math.Pow(upFactor, i);
                    // call options for upper half nodes
                    for (int j = nNodes - 1; j > position - 1; --j)
                    {
                        assetPriceLocal[j] = assetTmp;
                        double impliedVol = impliedVolatilitySurface(DoublesPair.of(timePrim[i], assetPriceLocal[j]));
                        callOptionPrice[j] = BlackScholesFormulaRepository.price(spot, assetPriceLocal[j], timePrim[i], impliedVol, zeroRate, zeroCostRate, true);
                        assetTmp          *= downFactor;
                    }
                    // put options for lower half nodes
                    assetTmp = spot * Math.Pow(downFactor, i);
                    for (int j = 0; j < position + 2; ++j)
                    {
                        assetPriceLocal[j] = assetTmp;
                        double impliedVol = impliedVolatilitySurface(DoublesPair.of(timePrim[i], assetPriceLocal[j]));
                        putOptionPrice[j] = BlackScholesFormulaRepository.price(spot, assetPriceLocal[j], timePrim[i], impliedVol, zeroRate, zeroCostRate, false);
                        assetTmp         *= upFactor;
                    }
                    resolveLayer(interestRate, dividendRate, i, nTotal, position, dt, zeroRate, zeroDividendRate, callOptionPrice, putOptionPrice, adSec, assetPrice, assetPriceLocal, timeRes, spotRes, volRes, df, stateValue, probability);
                }
            }
            ImmutableList <double[]>     localVolData = ImmutableList.of(timeRes, spotRes, volRes);
            RecombiningTrinomialTreeData treeData     = RecombiningTrinomialTreeData.of(DoubleMatrix.ofUnsafe(stateValue), probability, DoubleArray.ofUnsafe(df), DoubleArray.ofUnsafe(timePrim));

            return(Pair.of(localVolData, treeData));
        }