Exemplo n.º 1
0
        public virtual void test_quote_secenarioDefinition()
        {
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: java.util.List<com.opengamma.strata.calc.marketdata.PerturbationMapping<?>> perturbationMapping = new java.util.ArrayList<>();
            IList <PerturbationMapping <object> > perturbationMapping = new List <PerturbationMapping <object> >();
            int nScenarios = 3;

            foreach (KeyValuePair <QuoteId, double> entry in MARKET_QUOTES.entrySet())
            {
                DoubleArray shifts = DoubleArray.of(nScenarios, n => Math.Pow(0.9, n));
                ScenarioPerturbation <double> perturb = GenericDoubleShifts.of(ShiftType.SCALED, shifts);
                perturbationMapping.Add(PerturbationMapping.of(MarketDataFilter.ofId(entry.Key), perturb));
            }
            ScenarioDefinition    scenarioDefinition   = ScenarioDefinition.ofMappings(perturbationMapping);
            ScenarioMarketData    marketDataCalibrated = StandardComponents.marketDataFactory().createMultiScenario(REQUIREMENTS, SCENARIO_CONFIG, MARKET_DATA, REF_DATA, scenarioDefinition);
            Results               results = CALC_RUNNER.calculateMultiScenario(RULES, TARGETS, COLUMN, marketDataCalibrated, REF_DATA);
            CurrencyScenarioArray pvs     = results.get(0, 0, typeof(CurrencyScenarioArray)).Value;

            for (int i = 0; i < nScenarios; ++i)
            {
                ImmutableMap.Builder <QuoteId, double> builder = ImmutableMap.builder();
                foreach (KeyValuePair <QuoteId, double> entry in MARKET_QUOTES.entrySet())
                {
                    builder.put(entry.Key, entry.Value * Math.Pow(0.9, i));
                }
                ImmutableMarketData shiftedMarketData           = ImmutableMarketData.builder(VALUATION_DATE).addValueMap(builder.build()).addValueMap(MARKET_FX_QUOTES).build();
                MarketData          shiftedMarketDataCalibrated = StandardComponents.marketDataFactory().create(REQUIREMENTS, CONFIG, shiftedMarketData, REF_DATA);
                Results             shiftedResults = CALC_RUNNER.calculate(RULES, TARGETS, COLUMN, shiftedMarketDataCalibrated, REF_DATA);
                CurrencyAmount      pv             = shiftedResults.get(0, 0, typeof(CurrencyAmount)).Value;
                assertEquals(pvs.get(i), pv);
            }
        }
        /// <summary>
        /// Calculates the present value sensitivity of the FX barrier option product.
        /// <para>
        /// The present value sensitivity of the product is the sensitivity of <seealso cref="#presentValue"/> to
        /// the underlying curve parameters.
        /// </para>
        /// <para>
        /// The sensitivity is computed by bump and re-price.
        ///
        /// </para>
        /// </summary>
        /// <param name="option">  the option product </param>
        /// <param name="ratesProvider">  the rates provider </param>
        /// <param name="volatilities">  the Black volatility provider </param>
        /// <param name="baseTreeData">  the trinomial tree data </param>
        /// <returns> the present value of the product </returns>
        public virtual CurrencyParameterSensitivities presentValueSensitivityRates(ResolvedFxSingleBarrierOption option, RatesProvider ratesProvider, BlackFxOptionVolatilities volatilities, RecombiningTrinomialTreeData baseTreeData)
        {
            ArgChecker.isTrue(baseTreeData.NumberOfSteps == calibrator.NumberOfSteps, "the number of steps mismatch between pricer and trinomial tree data");
            double                         shift            = 1.0e-5;
            CurrencyAmount                 pvBase           = presentValue(option, ratesProvider, volatilities, baseTreeData);
            ResolvedFxVanillaOption        underlyingOption = option.UnderlyingOption;
            ResolvedFxSingle               underlyingFx     = underlyingOption.Underlying;
            CurrencyPair                   currencyPair     = underlyingFx.CurrencyPair;
            ImmutableRatesProvider         immRatesProvider = ratesProvider.toImmutableRatesProvider();
            ImmutableMap <Currency, Curve> baseCurves       = immRatesProvider.DiscountCurves;
            CurrencyParameterSensitivities result           = CurrencyParameterSensitivities.empty();

            foreach (KeyValuePair <Currency, Curve> entry in baseCurves.entrySet())
            {
                if (currencyPair.contains(entry.Key))
                {
                    Curve       curve       = entry.Value;
                    int         nParams     = curve.ParameterCount;
                    DoubleArray sensitivity = DoubleArray.of(nParams, i =>
                    {
                        Curve dscBumped = curve.withParameter(i, curve.getParameter(i) + shift);
                        IDictionary <Currency, Curve> mapBumped = new Dictionary <Currency, Curve>(baseCurves);
                        mapBumped[entry.Key] = dscBumped;
                        ImmutableRatesProvider providerDscBumped = immRatesProvider.toBuilder().discountCurves(mapBumped).build();
                        double pvBumped = presentValue(option, providerDscBumped, volatilities).Amount;
                        return((pvBumped - pvBase.Amount) / shift);
                    });
                    result = result.combinedWith(curve.createParameterSensitivity(pvBase.Currency, sensitivity));
                }
            }
            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns a string summary of the element.
        /// <para>
        /// The string form includes the attributes and content, but summarizes the child elements.
        ///
        /// </para>
        /// </summary>
        /// <returns> the string form </returns>
        public override string ToString()
        {
            StringBuilder buf = new StringBuilder(512);

            buf.Append('<').Append(name);
            foreach (KeyValuePair <string, string> entry in attributes.entrySet())
            {
                buf.Append(' ').Append(entry.Key).Append('=').Append('"').Append(entry.Value).Append('"');
            }
            buf.Append('>');
            if (children.Empty)
            {
                buf.Append(content);
            }
            else
            {
                foreach (XmlElement child in children)
                {
                    buf.Append(Environment.NewLine).Append(" <").Append(child.Name).Append(" ... />");
                }
                buf.Append(Environment.NewLine);
            }
            buf.Append("</").Append(name).Append('>');
            return(buf.ToString());
        }
Exemplo n.º 4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @ImmutableValidator private void validate()
        private void validate()
        {
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: for (java.util.Map.Entry<Class, CalculationFunction<?>> entry : functions.entrySet())
            foreach (KeyValuePair <Type, CalculationFunction <object> > entry in functions.entrySet())
            {
                ArgChecker.isTrue(entry.Key.IsAssignableFrom(entry.Value.targetType()), "Invalid map, key and function mismatch: {} and {}", entry.Key, entry.Value.targetType());
            }
        }
            /// <summary>
            /// Looks up the external name given a standard enum instance.
            /// <para>
            /// This searches the map of external names and returns the first matching entry
            /// that maps to the given standard name.
            ///
            /// </para>
            /// </summary>
            /// <param name="namedEnum">  the named enum to find an external name for </param>
            /// <returns> the external name </returns>
            /// <exception cref="IllegalArgumentException"> if there is no external name </exception>
            public string reverseLookup(T namedEnum)
            {
                string name = namedEnum.Name;

                foreach (KeyValuePair <string, string> entry in externalNames_Renamed.entrySet())
                {
                    if (entry.Value.Equals(name))
                    {
                        return(entry.Key);
                    }
                }
                throw new System.ArgumentException(Messages.format("{}:{} external name not found for standard name: {}", extendedEnum.type.Name, group, name));
            }
Exemplo n.º 6
0
        // append the explanation with indent
        private void explanationString(StringBuilder buf, string indent)
        {
            buf.Append("{").Append(Environment.NewLine);
            string entryIndent = indent + "  ";

//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: for (java.util.Map.Entry<ExplainKey<?>, Object> entry : map.entrySet())
            foreach (KeyValuePair <ExplainKey <object>, object> entry in map.entrySet())
            {
                buf.Append(entryIndent).Append(entry.Key).Append(" = ");
                if (entry.Value is System.Collections.IList)
                {
                    // list
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings("unchecked") java.util.List<ExplainMap> list = (java.util.List<ExplainMap>) entry.getValue();
                    IList <ExplainMap> list = (IList <ExplainMap>)entry.Value;
                    explanationString(buf, entryIndent, list);
                }
                else
                {
                    // single entry
                    try
                    {
                        buf.Append(StringConvert.INSTANCE.convertToString(entry.Value));
                    }
                    catch (Exception)
                    {
                        buf.Append(entry.Value);
                    }
                }
                buf.Append(',').Append(Environment.NewLine);
            }
            if (!map.entrySet().Empty)
            {
                buf.Remove(buf.lastIndexOf(","), 1);
            }
            buf.Append(indent).Append("}");
        }
Exemplo n.º 7
0
        private double sumMod(ImmutableRatesProvider provider)
        {
            double result = 0.0;
            // Index
            ImmutableMap <Index, Curve> mapIndex = provider.IndexCurves;

            foreach (KeyValuePair <Index, Curve> entry in mapIndex.entrySet())
            {
                if (entry.Key is IborIndex)
                {
                    InterpolatedNodalCurve curveInt = checkInterpolated(entry.Value);
                    result += sumSingle(curveInt);
                }
            }
            return(result);
        }
Exemplo n.º 8
0
        // modified sensitivity function - sensitivities are computed only for ibor index curves
        private CurrencyParameterSensitivities sensiModFn(ImmutableRatesProvider provider)
        {
            CurrencyParameterSensitivities sensi = CurrencyParameterSensitivities.empty();
            // Index
            ImmutableMap <Index, Curve> mapIndex = provider.IndexCurves;

            foreach (KeyValuePair <Index, Curve> entry in mapIndex.entrySet())
            {
                if (entry.Key is IborIndex)
                {
                    InterpolatedNodalCurve curveInt = checkInterpolated(entry.Value);
                    double sumSqrt = sumMod(provider);
                    sensi = sensi.combinedWith(CurrencyParameterSensitivity.of(curveInt.Name, USD, DoubleArray.of(curveInt.ParameterCount, i => 2d * sumSqrt * curveInt.XValues.get(i))));
                }
            }
            return(sensi);
        }
Exemplo n.º 9
0
        private double sumCombine(ImmutableRatesProvider provider)
        {
            double result = 0.0;
            // Currency
            ImmutableMap <Currency, Curve> mapCurrency = provider.DiscountCurves;

            foreach (KeyValuePair <Currency, Curve> entry in mapCurrency.entrySet())
            {
                if (entry.Value is CombinedCurve)
                {
                    InterpolatedNodalCurve baseCurveInt   = checkInterpolated(entry.Value.Split().get(0));
                    InterpolatedNodalCurve spreadCurveInt = checkInterpolated(entry.Value.Split().get(1));
                    result += 0.25d * sumSingle(baseCurveInt);
                    result += sumSingle(spreadCurveInt);
                }
                else
                {
                    InterpolatedNodalCurve curveInt = checkInterpolated(entry.Value);
                    result += sumSingle(curveInt);
                }
            }
            // Index
            ImmutableMap <Index, Curve> mapIndex = provider.IndexCurves;

            foreach (KeyValuePair <Index, Curve> entry in mapIndex.entrySet())
            {
                if (entry.Value is CombinedCurve)
                {
                    InterpolatedNodalCurve baseCurveInt   = checkInterpolated(entry.Value.Split().get(0));
                    InterpolatedNodalCurve spreadCurveInt = checkInterpolated(entry.Value.Split().get(1));
                    result += 0.25d * sumSingle(baseCurveInt);
                    result += sumSingle(spreadCurveInt);
                }
                else
                {
                    InterpolatedNodalCurve curveInt = checkInterpolated(entry.Value);
                    result += entry.Key.Equals(IborIndices.USD_LIBOR_3M) ? 0.25d * sumSingle(curveInt) : sumSingle(curveInt);
                }
            }
            return(result);
        }
Exemplo n.º 10
0
        private double sum(ImmutableRatesProvider provider)
        {
            double result = 0.0;
            // Currency
            ImmutableMap <Currency, Curve> mapCurrency = provider.DiscountCurves;

            foreach (KeyValuePair <Currency, Curve> entry in mapCurrency.entrySet())
            {
                InterpolatedNodalCurve curveInt = checkInterpolated(entry.Value);
                result += sumSingle(curveInt);
            }
            // Index
            ImmutableMap <Index, Curve> mapIndex = provider.IndexCurves;

            foreach (KeyValuePair <Index, Curve> entry in mapIndex.entrySet())
            {
                InterpolatedNodalCurve curveInt = checkInterpolated(entry.Value);
                result += sumSingle(curveInt);
            }
            return(result);
        }
        // private function for testing. Returns the sum of rates multiplied by time
        private CurrencyAmount creditFunction(ImmutableCreditRatesProvider provider)
        {
            double result = 0.0;
            // credit curve
            ImmutableMap <Pair <StandardId, Currency>, LegalEntitySurvivalProbabilities> mapCredit = provider.metaBean().creditCurves().get(provider);

            foreach (KeyValuePair <Pair <StandardId, Currency>, LegalEntitySurvivalProbabilities> entry in mapCredit.entrySet())
            {
                InterpolatedNodalCurve curveInt = checkInterpolated(checkDiscountFactors(entry.Value.SurvivalProbabilities.toDiscountFactors()));
                result += sumProduct(curveInt);
            }
            // repo curve
            ImmutableMap <Currency, CreditDiscountFactors> mapDiscount = provider.metaBean().discountCurves().get(provider);

            foreach (KeyValuePair <Currency, CreditDiscountFactors> entry in mapDiscount.entrySet())
            {
                InterpolatedNodalCurve curveInt = checkInterpolated(checkDiscountFactors(entry.Value.toDiscountFactors()));
                result += sumProduct(curveInt);
            }
            return(CurrencyAmount.of(USD, result));
        }
Exemplo n.º 12
0
        // modified sensitivity function - CombinedCurve involved
        private CurrencyParameterSensitivities sensiCombinedFn(ImmutableRatesProvider provider)
        {
            CurrencyParameterSensitivities sensi = CurrencyParameterSensitivities.empty();
            double sum = sumCombine(provider);
            // Currency
            ImmutableMap <Currency, Curve> mapCurrency = provider.DiscountCurves;

            foreach (KeyValuePair <Currency, Curve> entry in mapCurrency.entrySet())
            {
                CombinedCurve          curveComb      = (CombinedCurve)entry.Value;
                InterpolatedNodalCurve baseCurveInt   = checkInterpolated(curveComb.BaseCurve);
                InterpolatedNodalCurve spreadCurveInt = checkInterpolated(curveComb.SpreadCurve);
                sensi = sensi.combinedWith(CurrencyParameterSensitivity.of(baseCurveInt.Name, USD, DoubleArray.of(baseCurveInt.ParameterCount, i => 2d * sum * baseCurveInt.XValues.get(i))));
                sensi = sensi.combinedWith(CurrencyParameterSensitivity.of(spreadCurveInt.Name, USD, DoubleArray.of(spreadCurveInt.ParameterCount, i => 2d * sum * spreadCurveInt.XValues.get(i))));
            }
            // Index
            ImmutableMap <Index, Curve> mapIndex = provider.IndexCurves;

            foreach (KeyValuePair <Index, Curve> entry in mapIndex.entrySet())
            {
                if (entry.Value is CombinedCurve)
                {
                    CombinedCurve          curveComb      = (CombinedCurve)entry.Value;
                    InterpolatedNodalCurve baseCurveInt   = checkInterpolated(curveComb.BaseCurve);
                    InterpolatedNodalCurve spreadCurveInt = checkInterpolated(curveComb.SpreadCurve);
                    sensi = sensi.combinedWith(CurrencyParameterSensitivity.of(baseCurveInt.Name, USD, DoubleArray.of(baseCurveInt.ParameterCount, i => 2d * sum * baseCurveInt.XValues.get(i))));
                    sensi = sensi.combinedWith(CurrencyParameterSensitivity.of(spreadCurveInt.Name, USD, DoubleArray.of(spreadCurveInt.ParameterCount, i => 2d * sum * spreadCurveInt.XValues.get(i))));
                }
                else
                {
                    InterpolatedNodalCurve curveInt = checkInterpolated(entry.Value);
                    sensi = sensi.combinedWith(CurrencyParameterSensitivity.of(curveInt.Name, USD, DoubleArray.of(curveInt.ParameterCount, i => 2d * sum * curveInt.XValues.get(i))));
                }
            }
            return(sensi);
        }
Exemplo n.º 13
0
        private double sum(ImmutableLegalEntityDiscountingProvider provider)
        {
            double result = 0d;
            // repo curves
            ImmutableMap <Pair <RepoGroup, Currency>, DiscountFactors> mapIndex = provider.RepoCurves;

            foreach (KeyValuePair <Pair <RepoGroup, Currency>, DiscountFactors> entry in mapIndex.entrySet())
            {
                InterpolatedNodalCurve curve = (InterpolatedNodalCurve)getCurve(entry.Value);
                result += sumSingle(curve);
            }
            // issuer curves
            ImmutableMap <Pair <LegalEntityGroup, Currency>, DiscountFactors> mapCurrency = provider.IssuerCurves;

            foreach (KeyValuePair <Pair <LegalEntityGroup, Currency>, DiscountFactors> entry in mapCurrency.entrySet())
            {
                InterpolatedNodalCurve curve = (InterpolatedNodalCurve)getCurve(entry.Value);
                result += sumSingle(curve);
            }
            return(result);
        }
Exemplo n.º 14
0
        // modified sensitivity function - CombinedCurve involved
        private CurrencyParameterSensitivities sensiCombinedFnBond(ImmutableLegalEntityDiscountingProvider provider)
        {
            CurrencyParameterSensitivities sensi = CurrencyParameterSensitivities.empty();
            double sum = sumCombine(provider);
            // repo curves
            ImmutableMap <Pair <RepoGroup, Currency>, DiscountFactors> mapCurrency = provider.RepoCurves;

            foreach (KeyValuePair <Pair <RepoGroup, Currency>, DiscountFactors> entry in mapCurrency.entrySet())
            {
                CombinedCurve          curveComb      = (CombinedCurve)getCurve(entry.Value);
                InterpolatedNodalCurve baseCurveInt   = checkInterpolated(curveComb.BaseCurve);
                InterpolatedNodalCurve spreadCurveInt = checkInterpolated(curveComb.SpreadCurve);
                sensi = sensi.combinedWith(CurrencyParameterSensitivity.of(baseCurveInt.Name, USD, DoubleArray.of(baseCurveInt.ParameterCount, i => 2d * sum * baseCurveInt.XValues.get(i))));
                sensi = sensi.combinedWith(CurrencyParameterSensitivity.of(spreadCurveInt.Name, USD, DoubleArray.of(spreadCurveInt.ParameterCount, i => 2d * sum * spreadCurveInt.XValues.get(i))));
            }
            // issuer curves
            ImmutableMap <Pair <LegalEntityGroup, Currency>, DiscountFactors> mapIndex = provider.IssuerCurves;

            foreach (KeyValuePair <Pair <LegalEntityGroup, Currency>, DiscountFactors> entry in mapIndex.entrySet())
            {
                CombinedCurve          curveComb      = (CombinedCurve)getCurve(entry.Value);
                InterpolatedNodalCurve baseCurveInt   = checkInterpolated(curveComb.BaseCurve);
                InterpolatedNodalCurve spreadCurveInt = checkInterpolated(curveComb.SpreadCurve);
                sensi = sensi.combinedWith(CurrencyParameterSensitivity.of(baseCurveInt.Name, USD, DoubleArray.of(baseCurveInt.ParameterCount, i => 2d * sum * baseCurveInt.XValues.get(i))));
                sensi = sensi.combinedWith(CurrencyParameterSensitivity.of(spreadCurveInt.Name, USD, DoubleArray.of(spreadCurveInt.ParameterCount, i => 2d * sum * spreadCurveInt.XValues.get(i))));
            }
            return(sensi);
        }
Exemplo n.º 15
0
        //-------------------------------------------------------------------------
        private CurrencyParameterSensitivities sensiFnBond(ImmutableLegalEntityDiscountingProvider provider)
        {
            CurrencyParameterSensitivities sensi = CurrencyParameterSensitivities.empty();
            double sum = this.sum(provider);
            // repo curves
            ImmutableMap <Pair <RepoGroup, Currency>, DiscountFactors> mapRepoCurves = provider.RepoCurves;

            foreach (KeyValuePair <Pair <RepoGroup, Currency>, DiscountFactors> entry in mapRepoCurves.entrySet())
            {
                DiscountFactors        discountFactors = entry.Value;
                InterpolatedNodalCurve curve           = (InterpolatedNodalCurve)getCurve(discountFactors);
                sensi = sensi.combinedWith(CurrencyParameterSensitivity.of(curve.Name, discountFactors.Currency, DoubleArray.of(discountFactors.ParameterCount, i => 2d * curve.XValues.get(i) * sum)));
            }
            // issuer curves
            ImmutableMap <Pair <LegalEntityGroup, Currency>, DiscountFactors> mapIssuerCurves = provider.IssuerCurves;

            foreach (KeyValuePair <Pair <LegalEntityGroup, Currency>, DiscountFactors> entry in mapIssuerCurves.entrySet())
            {
                DiscountFactors        discountFactors = entry.Value;
                InterpolatedNodalCurve curve           = (InterpolatedNodalCurve)getCurve(discountFactors);
                sensi = sensi.combinedWith(CurrencyParameterSensitivity.of(curve.Name, discountFactors.Currency, DoubleArray.of(discountFactors.ParameterCount, i => 2d * curve.XValues.get(i) * sum)));
            }
            return(sensi);
        }
        // private function for testing. Returns the sum of rates multiplied by time
        private CurrencyAmount fn(ImmutableLegalEntityDiscountingProvider provider)
        {
            double result = 0.0;
            // issuer curve
            ImmutableMap <Pair <LegalEntityGroup, Currency>, DiscountFactors> mapLegal = provider.metaBean().issuerCurves().get(provider);

            foreach (KeyValuePair <Pair <LegalEntityGroup, Currency>, DiscountFactors> entry in mapLegal.entrySet())
            {
                InterpolatedNodalCurve curveInt = checkInterpolated(checkDiscountFactors(entry.Value));
                result += sumProduct(curveInt);
            }
            // repo curve
            ImmutableMap <Pair <RepoGroup, Currency>, DiscountFactors> mapRepo = provider.metaBean().repoCurves().get(provider);

            foreach (KeyValuePair <Pair <RepoGroup, Currency>, DiscountFactors> entry in mapRepo.entrySet())
            {
                InterpolatedNodalCurve curveInt = checkInterpolated(checkDiscountFactors(entry.Value));
                result += sumProduct(curveInt);
            }
            return(CurrencyAmount.of(USD, result));
        }