예제 #1
0
        /// <summary>
        /// Test that the box always returns the same value for any non-negative scenario index.
        /// </summary>
        public virtual void getValue()
        {
            MarketDataBox <int> box = MarketDataBox.ofSingleValue(27);

            assertThat(box.getValue(0)).isEqualTo(27);
            assertThat(box.getValue(int.MaxValue)).isEqualTo(27);
            assertThrows(() => box.getValue(-1), typeof(System.ArgumentException));
        }
예제 #2
0
        public virtual void getValue()
        {
            MarketDataBox <int> box = MarketDataBox.ofScenarioValues(27, 28, 29);

            assertThat(box.getValue(0)).isEqualTo(27);
            assertThat(box.getValue(1)).isEqualTo(28);
            assertThat(box.getValue(2)).isEqualTo(29);
            assertThrows(() => box.getValue(-1), typeof(System.ArgumentException), "Expected 0 <= 'scenarioIndex' < 3, but found -1");
            assertThrows(() => box.getValue(3), typeof(System.ArgumentException), "Expected 0 <= 'scenarioIndex' < 3, but found 3");
        }
예제 #3
0
        /// <summary>
        /// Tests that applying a function multiple times to the value creates a box of scenario values.
        /// </summary>
        public virtual void mapWithIndex()
        {
            MarketDataBox <int> box         = MarketDataBox.ofScenarioValues(27, 28, 29);
            MarketDataBox <int> scenarioBox = box.mapWithIndex(3, (v, idx) => v + idx);

            assertThat(scenarioBox.ScenarioValue).True;
            assertThat(scenarioBox.ScenarioCount).isEqualTo(3);
            assertThat(scenarioBox.getValue(0)).isEqualTo(27);
            assertThat(scenarioBox.getValue(1)).isEqualTo(29);
            assertThat(scenarioBox.getValue(2)).isEqualTo(31);
        }
예제 #4
0
        public virtual void combineWithScenarioBox()
        {
            MarketDataBox <int> box       = MarketDataBox.ofScenarioValues(27, 28, 29);
            MarketDataBox <int> otherBox  = MarketDataBox.ofScenarioValues(15, 16, 17);
            MarketDataBox <int> resultBox = box.combineWith(otherBox, (v1, v2) => v1 + v2);

            assertThat(resultBox.ScenarioValue).True;
            assertThat(resultBox.ScenarioCount).isEqualTo(3);
            assertThat(resultBox.getValue(0)).isEqualTo(42);
            assertThat(resultBox.getValue(1)).isEqualTo(44);
            assertThat(resultBox.getValue(2)).isEqualTo(46);
        }
예제 #5
0
        public virtual void buildScenario()
        {
            FxRateMarketDataFunction function   = new FxRateMarketDataFunction();
            MarketDataBox <double>   quoteBox   = MarketDataBox.ofScenarioValues(1.1d, 1.2d, 1.3d);
            ScenarioMarketData       marketData = ImmutableScenarioMarketData.builder(LocalDate.of(2011, 3, 8)).addBox(QUOTE_ID, quoteBox).build();
            MarketDataBox <FxRate>   rateBox    = function.build(RATE_ID, config(), marketData, REF_DATA);

            assertThat(rateBox.SingleValue).False;
            assertThat(rateBox.ScenarioCount).isEqualTo(3);
            assertThat(rateBox.getValue(0)).isEqualTo(FxRate.of(CURRENCY_PAIR, 1.1d));
            assertThat(rateBox.getValue(1)).isEqualTo(FxRate.of(CURRENCY_PAIR, 1.2d));
            assertThat(rateBox.getValue(2)).isEqualTo(FxRate.of(CURRENCY_PAIR, 1.3d));
        }
예제 #6
0
        public virtual void relative()
        {
            IList <LabelDateParameterMetadata> nodeMetadata = ImmutableList.of(LabelDateParameterMetadata.of(date(2011, 3, 8), TNR_1M), LabelDateParameterMetadata.of(date(2011, 5, 8), TNR_3M), LabelDateParameterMetadata.of(date(2011, 8, 8), TNR_6M));

            // This should create 4 scenarios. Scenario zero has no shifts and scenario 3 doesn't have shifts on all nodes
            PointShifts shift = PointShifts.builder(ShiftType.RELATIVE).addShift(1, TNR_1W, 0.1).addShift(1, TNR_1M, 0.2).addShift(1, TNR_3M, 0.3).addShift(2, TNR_1M, 0.4).addShift(2, TNR_3M, 0.5).addShift(2, TNR_6M, 0.6).addShift(3, TNR_3M, 0.7).build();

            Curve curve = InterpolatedNodalCurve.of(Curves.zeroRates(CurveName.of("curve"), DayCounts.ACT_365F, nodeMetadata), DoubleArray.of(1, 2, 3), DoubleArray.of(5, 6, 7), INTERPOLATOR);

            MarketDataBox <ParameterizedData> shiftedCurveBox = shift.applyTo(MarketDataBox.ofSingleValue(curve), REF_DATA);

            Curve scenario1Curve = InterpolatedNodalCurve.of(Curves.zeroRates(CurveName.of("curve"), DayCounts.ACT_365F, nodeMetadata), DoubleArray.of(1, 2, 3), DoubleArray.of(6, 7.8, 7), INTERPOLATOR);

            Curve scenario2Curve = InterpolatedNodalCurve.of(Curves.zeroRates(CurveName.of("curve"), DayCounts.ACT_365F, nodeMetadata), DoubleArray.of(1, 2, 3), DoubleArray.of(7, 9, 11.2), INTERPOLATOR);

            Curve scenario3Curve = InterpolatedNodalCurve.of(Curves.zeroRates(CurveName.of("curve"), DayCounts.ACT_365F, nodeMetadata), DoubleArray.of(1, 2, 3), DoubleArray.of(5, 10.2, 7), INTERPOLATOR);

            // Scenario zero has no perturbations so the expected curve is the same as the input
            IList <Curve> expectedCurves = ImmutableList.of(curve, scenario1Curve, scenario2Curve, scenario3Curve);

            for (int scenarioIndex = 0; scenarioIndex < 4; scenarioIndex++)
            {
                // Check every point from 0 to 4 in steps of 0.1 is the same on the bumped curve and the expected curve
                for (int xIndex = 0; xIndex <= 40; xIndex++)
                {
                    double xValue        = xIndex * 0.1;
                    Curve  expectedCurve = expectedCurves[scenarioIndex];
                    Curve  shiftedCurve  = (Curve)shiftedCurveBox.getValue(scenarioIndex);
                    double shiftedY      = shiftedCurve.yValue(xValue);
                    double expectedY     = expectedCurve.yValue(xValue);
                    assertThat(shiftedY).overridingErrorMessage("Curve differed in scenario %d at x value %f, expected %f, actual %f", scenarioIndex, xValue, expectedY, shiftedY).isEqualTo(expectedY);
                }
            }
        }
예제 #7
0
        public virtual void combineWithSingleBox()
        {
            MarketDataBox <int> box       = MarketDataBox.ofSingleValue(27);
            MarketDataBox <int> otherBox  = MarketDataBox.ofSingleValue(15);
            MarketDataBox <int> resultBox = box.combineWith(otherBox, (v1, v2) => v1 + v2);

            assertThat(resultBox.SingleValue).True;
            assertThat(resultBox.getValue(0)).isEqualTo(42);
        }
예제 #8
0
        //-------------------------------------------------------------------------
        public virtual void test_scenarios()
        {
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: java.util.Map<com.opengamma.strata.data.MarketDataId<?>, MarketDataBox<?>> dataMap = com.google.common.collect.ImmutableMap.of(ID1, BOX1);
            IDictionary <MarketDataId <object>, MarketDataBox <object> > dataMap = ImmutableMap.of(ID1, BOX1);
            IDictionary <ObservableId, LocalDateDoubleTimeSeries>        tsMap   = ImmutableMap.of(ID1, TIME_SERIES);
            ScenarioMarketData test = ScenarioMarketData.of(2, VAL_DATE, dataMap, tsMap);

            MarketData scenario0 = test.scenario(0);
            MarketData scenario1 = test.scenario(1);

            assertThat(scenario0.getValue(ID1)).isEqualTo(BOX1.getValue(0));
            assertThat(scenario1.getValue(ID1)).isEqualTo(BOX1.getValue(1));
            IList <double> list = test.scenarios().map(s => s.getValue(ID1)).collect(toImmutableList());

            assertThat(list[0]).isEqualTo(BOX1.getValue(0));
            assertThat(list[1]).isEqualTo(BOX1.getValue(1));
        }
예제 #9
0
 // checks the value is an instance of the market data type of the id
 internal static void checkType <T1, T2>(MarketDataId <T1> id, MarketDataBox <T2> box, int scenarioCount)
 {
     if (box == null)
     {
         throw new System.ArgumentException(Messages.format("Value for identifier '{}' must not be null", id));
     }
     if (box.ScenarioValue && box.ScenarioCount != scenarioCount)
     {
         throw new System.ArgumentException(Messages.format("Value for identifier '{}' should have had {} scenarios but had {}", id, scenarioCount, box.ScenarioCount));
     }
     if (box.ScenarioCount > 0 && !id.MarketDataType.IsInstanceOfType(box.getValue(0)))
     {
         throw new System.InvalidCastException(Messages.format("Value for identifier '{}' does not implement expected type '{}': '{}'", id, id.MarketDataType.Name, box));
     }
 }
예제 #10
0
        public MarketDataBox <RatesCurveInputs> build(RatesCurveInputsId id, MarketDataConfig marketDataConfig, ScenarioMarketData marketData, ReferenceData refData)
        {
            CurveGroupName             groupName          = id.CurveGroupName;
            CurveName                  curveName          = id.CurveName;
            RatesCurveGroupDefinition  groupDefn          = marketDataConfig.get(typeof(RatesCurveGroupDefinition), groupName);
            Optional <CurveDefinition> optionalDefinition = groupDefn.findCurveDefinition(id.CurveName);

            if (!optionalDefinition.Present)
            {
                throw new System.ArgumentException(Messages.format("No curve named '{}' found in group '{}'", curveName, groupName));
            }
            CurveDefinition configuredDefn = optionalDefinition.get();
            // determine market data needs
            MarketDataBox <LocalDate> valuationDates = marketData.ValuationDate;
            bool multipleValuationDates = valuationDates.ScenarioValue;

            // curve definition can vary for each valuation date
            if (multipleValuationDates)
            {
                IList <CurveDefinition> curveDefns = IntStream.range(0, valuationDates.ScenarioCount).mapToObj(valuationDates.getValue).map((LocalDate valDate) => configuredDefn.filtered(valDate, refData)).collect(toImmutableList());

//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: java.util.Set<com.opengamma.strata.data.MarketDataId<?>> requirements = nodeRequirements(curveDefns);
                ISet <MarketDataId <object> > requirements = nodeRequirements(curveDefns);
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: java.util.Map<com.opengamma.strata.data.MarketDataId<?>, com.opengamma.strata.data.scenario.MarketDataBox<?>> marketDataValues = requirements.stream().collect(toImmutableMap(k -> k, k -> marketData.getValue(k)));
//JAVA TO C# CONVERTER TODO TASK: Most Java stream collectors are not converted by Java to C# Converter:
                IDictionary <MarketDataId <object>, MarketDataBox <object> > marketDataValues = requirements.collect(toImmutableMap(k => k, k => marketData.getValue(k)));
                return(buildMultipleCurveInputs(MarketDataBox.ofScenarioValues(curveDefns), marketDataValues, valuationDates, refData));
            }
            // only one valuation date
            LocalDate       valuationDate = valuationDates.getValue(0);
            CurveDefinition filteredDefn  = configuredDefn.filtered(valuationDate, refData);
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: java.util.Set<com.opengamma.strata.data.MarketDataId<?>> requirements = nodeRequirements(com.google.common.collect.ImmutableList.of(filteredDefn));
            ISet <MarketDataId <object> > requirements = nodeRequirements(ImmutableList.of(filteredDefn));
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: java.util.Map<com.opengamma.strata.data.MarketDataId<?>, com.opengamma.strata.data.scenario.MarketDataBox<?>> marketDataValues = requirements.stream().collect(toImmutableMap(k -> k, k -> marketData.getValue(k)));
//JAVA TO C# CONVERTER TODO TASK: Most Java stream collectors are not converted by Java to C# Converter:
            IDictionary <MarketDataId <object>, MarketDataBox <object> > marketDataValues = requirements.collect(toImmutableMap(k => k, k => marketData.getValue(k)));
            // Do any of the inputs contain values for multiple scenarios, or do they contain 1 value each?
            bool multipleInputValues = marketDataValues.Values.Any(MarketDataBox.isScenarioValue);

            return(multipleInputValues || multipleValuationDates?buildMultipleCurveInputs(MarketDataBox.ofSingleValue(filteredDefn), marketDataValues, valuationDates, refData) : buildSingleCurveInputs(filteredDefn, marketDataValues, valuationDate, refData));
        }
예제 #11
0
        // calibrates when there are multiple groups
        private MarketDataBox <RatesCurveGroup> buildMultipleCurveGroups(RatesCurveGroupDefinition configuredGroup, RatesCurveCalibrator calibrator, MarketDataBox <LocalDate> valuationDateBox, IList <MarketDataBox <RatesCurveInputs> > inputBoxes, IDictionary <ObservableId, LocalDateDoubleTimeSeries> fixings, ReferenceData refData)
        {
            int scenarioCount = RatesCurveGroupMarketDataFunction.scenarioCount(valuationDateBox, inputBoxes);

            ImmutableList.Builder <RatesCurveGroup> builder = ImmutableList.builder();

            for (int i = 0; i < scenarioCount; i++)
            {
                LocalDate valuationDate = valuationDateBox.getValue(i);
                RatesCurveGroupDefinition filteredGroup   = configuredGroup.filtered(valuationDate, refData);
                IList <RatesCurveInputs>  curveInputsList = inputsForScenario(inputBoxes, i);
                MarketData inputs = inputsByKey(valuationDate, curveInputsList, fixings);
                builder.add(buildGroup(filteredGroup, calibrator, inputs, refData));
            }
            ImmutableList <RatesCurveGroup> curveGroups = builder.build();

            return(MarketDataBox.ofScenarioValues(curveGroups));
        }
예제 #12
0
        // one valuation date, scenario market data
        private MarketDataBox <RatesCurveInputs> buildMultipleCurveInputs <T1>(MarketDataBox <CurveDefinition> filteredDefns, IDictionary <T1> marketData, MarketDataBox <LocalDate> valuationDates, ReferenceData refData) where T1 : com.opengamma.strata.data.MarketDataId <T1>
        {
            // If there are multiple values for any of the input data values or for the valuation
            // dates then we need to create multiple sets of inputs
            int scenarioCount = RatesCurveInputsMarketDataFunction.scenarioCount(valuationDates, marketData);

            ImmutableList.Builder <CurveMetadata> curveMetadataBuilder = ImmutableList.builder();
            for (int i = 0; i < scenarioCount; i++)
            {
                LocalDate       valDate = valuationDates.getValue(i);
                CurveDefinition defn    = filteredDefns.getValue(i);
                curveMetadataBuilder.add(defn.metadata(valDate, refData));
            }
            IList <CurveMetadata> curveMetadata = curveMetadataBuilder.build();

//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: java.util.List<java.util.Map<? extends com.opengamma.strata.data.MarketDataId<?>, ?>> scenarioValues = java.util.stream.IntStream.range(0, scenarioCount).mapToObj(i -> buildScenarioValues(marketData, i)).collect(toImmutableList());
            IList <IDictionary <MarketDataId <object>, ?> > scenarioValues = IntStream.range(0, scenarioCount).mapToObj(i => buildScenarioValues(marketData, i)).collect(toImmutableList());

            IList <RatesCurveInputs> curveInputs = zip(scenarioValues.stream(), curveMetadata.stream()).map(pair => RatesCurveInputs.of(pair.First, pair.Second)).collect(toImmutableList());

            return(MarketDataBox.ofScenarioValues(curveInputs));
        }
예제 #13
0
        public virtual MarketDataBox <FxOptionVolatilities> build(FxOptionVolatilitiesId id, MarketDataConfig marketDataConfig, ScenarioMarketData marketData, ReferenceData refData)
        {
            FxOptionVolatilitiesDefinition volatilitiesDefinition = marketDataConfig.get(typeof(FxOptionVolatilitiesDefinition), id.Name.Name);
            ValuationZoneTimeDefinition    zoneTimeDefinition     = marketDataConfig.get(typeof(ValuationZoneTimeDefinition));
            int nScenarios = marketData.ScenarioCount;
            MarketDataBox <LocalDate>     valuationDates     = marketData.ValuationDate;
            MarketDataBox <ZonedDateTime> valuationDateTimes = zoneTimeDefinition.toZonedDateTime(valuationDates);

            int nParameters = volatilitiesDefinition.ParameterCount;
//JAVA TO C# CONVERTER TODO TASK: Most Java stream collectors are not converted by Java to C# Converter:
            ImmutableList <MarketDataBox <double> > inputs = volatilitiesDefinition.volatilitiesInputs().Select(q => marketData.getValue(q)).collect(toImmutableList());
            ImmutableList <FxOptionVolatilities>    vols   = IntStream.range(0, nScenarios).mapToObj(scenarioIndex => volatilitiesDefinition.volatilities(valuationDateTimes.getValue(scenarioIndex), DoubleArray.of(nParameters, paramIndex => inputs.get(paramIndex).getValue(scenarioIndex)), refData)).collect(toImmutableList());

            return(nScenarios > 1 ? MarketDataBox.ofScenarioValues(vols) : MarketDataBox.ofSingleValue(vols.get(0)));
        }
예제 #14
0
        /// <summary>
        /// Creates zoned date time.
        /// <para>
        /// If the scenario size of {@code dates} is greater than the size of {@code localTimes},
        /// {@code defaultLocalTime} is used.
        /// If {@code dates} is single value, {@code defaultLocalTime} is used.
        ///
        /// </para>
        /// </summary>
        /// <param name="dates">  the local date </param>
        /// <returns> the zoned date time </returns>
        public MarketDataBox <ZonedDateTime> toZonedDateTime(MarketDataBox <LocalDate> dates)
        {
            if (dates.ScenarioValue)
            {
                int nScenarios = dates.ScenarioCount;
                int nTimes     = localTimes.size();
                IList <ZonedDateTime> zonedDateTimes = IntStream.range(0, nScenarios).mapToObj(scenarioIndex => zonedDateTime(dates.getValue(scenarioIndex), nTimes, scenarioIndex)).collect(Collectors.toList());
                return(MarketDataBox.ofScenarioValues(zonedDateTimes));
            }
            ZonedDateTime zonedDateTime = dates.SingleValue.atTime(defaultLocalTime).atZone(zoneId);

            return(MarketDataBox.ofSingleValue(zonedDateTime));
        }
예제 #15
0
 public QuoteScenarioArray createScenarioValue(MarketDataBox <double> marketDataBox, int scenarioCount)
 {
     return(QuoteScenarioArray.of(DoubleArray.of(scenarioCount, i => marketDataBox.getValue(i))));
 }
예제 #16
0
        //-------------------------------------------------------------------------

        public MarketDataBox <ParameterizedData> applyTo(MarketDataBox <ParameterizedData> marketData, ReferenceData refData)
        {
            log.debug("Applying {} point shift to ParameterizedData '{}'", shiftType, marketData.getValue(0).ToString());
            return(marketData.mapWithIndex(shifts.rowCount(), (prams, scenarioIndex) => applyShifts(scenarioIndex, prams)));
        }
예제 #17
0
 //-------------------------------------------------------------------------
 public MarketDataBox <FxRate> applyTo(MarketDataBox <FxRate> marketData, ReferenceData refData)
 {
     log.debug("Applying {} shift to FX rate '{}'", shiftType, marketData.getValue(0).Pair.ToString());
     return(marketData.mapWithIndex(ScenarioCount, (fxRate, scenarioIndex) => FxRate.of(currencyPair, shiftType.applyShift(fxRate.fxRate(currencyPair), shiftAmount.get(scenarioIndex)))));
 }
        private MarketDataBox <R> combineWithMultiple <U, R>(MarketDataBox <U> other, System.Func <T, U, R> fn)
        {
            ScenarioArray <U> otherValue = other.ScenarioValue;
            int scenarioCount            = otherValue.ScenarioCount;

            IList <R> values = IntStream.range(0, scenarioCount).mapToObj(i => fn(value, other.getValue(i))).collect(toImmutableList());

            return(MarketDataBox.ofScenarioValues(values));
        }