// parses the base SecurityPosition
        internal static SecurityPosition parseSecurityPosition(CsvRow row, PositionInfo info, PositionCsvInfoResolver resolver)
        {
            string           securityIdScheme = row.findValue(SECURITY_ID_SCHEME_FIELD).orElse(DEFAULT_SECURITY_SCHEME);
            string           securityIdValue  = row.getValue(SECURITY_ID_FIELD);
            SecurityId       securityId       = SecurityId.of(securityIdScheme, securityIdValue);
            DoublesPair      quantity         = CsvLoaderUtils.parseQuantity(row);
            SecurityPosition position         = SecurityPosition.ofLongShort(info, securityId, quantity.First, quantity.Second);

            return(resolver.completePosition(row, position));
        }
        public virtual void test_lookup_byPair()
        {
            ConstantSurface test = ConstantSurface.of(SURFACE_NAME, VALUE);

            assertThat(test.zValue(DoublesPair.of(0d, 0d))).isEqualTo(VALUE);
            assertThat(test.zValue(DoublesPair.of(-10d, 10d))).isEqualTo(VALUE);
            assertThat(test.zValue(DoublesPair.of(100d, -100d))).isEqualTo(VALUE);

            assertThat(test.zValueParameterSensitivity(DoublesPair.of(0d, 0d)).Sensitivity.get(0)).isEqualTo(1d);
            assertThat(test.zValueParameterSensitivity(DoublesPair.of(-10d, 10d)).Sensitivity.get(0)).isEqualTo(1d);
            assertThat(test.zValueParameterSensitivity(DoublesPair.of(100d, -100d)).Sensitivity.get(0)).isEqualTo(1d);
        }
        // computes the weights related to the two indices
        private DoublesPair weights(IborIndexObservation obs1, IborIndexObservation obs2, LocalDate endDate)
        {
            // weights: linear interpolation on the number of days between the fixing date and the maturity dates of the
            //   actual coupons on one side and the maturity dates of the underlying deposit on the other side.
            long   fixingEpochDay = obs1.FixingDate.toEpochDay();
            double days1          = obs1.MaturityDate.toEpochDay() - fixingEpochDay;
            double days2          = obs2.MaturityDate.toEpochDay() - fixingEpochDay;
            double daysN          = endDate.toEpochDay() - fixingEpochDay;
            double weight1        = (days2 - daysN) / (days2 - days1);
            double weight2        = (daysN - days1) / (days2 - days1);

            return(DoublesPair.of(weight1, weight2));
        }
        //-------------------------------------------------------------------------
        public virtual double rate(IborInterpolatedRateComputation computation, LocalDate startDate, LocalDate endDate, RatesProvider provider)
        {
            IborIndexObservation obs1   = computation.ShortObservation;
            IborIndexObservation obs2   = computation.LongObservation;
            IborIndexRates       rates1 = provider.iborIndexRates(obs1.Index);
            IborIndexRates       rates2 = provider.iborIndexRates(obs2.Index);

            double      rate1   = rates1.rate(obs1);
            double      rate2   = rates2.rate(obs2);
            DoublesPair weights = this.weights(obs1, obs2, endDate);

            return(((rate1 * weights.First) + (rate2 * weights.Second)) / (weights.First + weights.Second));
        }
        public virtual double explainRate(IborInterpolatedRateComputation computation, LocalDate startDate, LocalDate endDate, RatesProvider provider, ExplainMapBuilder builder)
        {
            IborIndexObservation obs1    = computation.ShortObservation;
            IborIndexObservation obs2    = computation.LongObservation;
            DoublesPair          weights = this.weights(obs1, obs2, endDate);
            IborIndexRates       rates1  = provider.iborIndexRates(obs1.Index);
            IborIndexRates       rates2  = provider.iborIndexRates(obs2.Index);

            rates1.explainRate(obs1, builder, child => child.put(ExplainKey.WEIGHT, weights.First));
            rates2.explainRate(obs2, builder, child => child.put(ExplainKey.WEIGHT, weights.Second));
            double rate = this.rate(computation, startDate, endDate, provider);

            builder.put(ExplainKey.COMBINED_RATE, rate);
            return(rate);
        }
        public virtual PointSensitivityBuilder rateSensitivity(IborInterpolatedRateComputation computation, LocalDate startDate, LocalDate endDate, RatesProvider provider)
        {
            // computes the dates related to the underlying deposits associated to the indices
            IborIndexObservation obs1    = computation.ShortObservation;
            IborIndexObservation obs2    = computation.LongObservation;
            DoublesPair          weights = this.weights(obs1, obs2, endDate);
            double totalWeight           = weights.First + weights.Second;

            IborIndexRates          ratesIndex1 = provider.iborIndexRates(obs1.Index);
            PointSensitivityBuilder sens1       = ratesIndex1.ratePointSensitivity(obs1).multipliedBy(weights.First / totalWeight);
            IborIndexRates          ratesIndex2 = provider.iborIndexRates(obs2.Index);
            PointSensitivityBuilder sens2       = ratesIndex2.ratePointSensitivity(obs2).multipliedBy(weights.Second / totalWeight);

            return(sens1.combinedWith(sens2));
        }
Exemplo n.º 7
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));
        }
        //-------------------------------------------------------------------------
        /// <summary>
        /// Parses the quantity.
        /// </summary>
        /// <param name="row">  the CSV row to parse </param>
        /// <returns> the quantity, long first, short second </returns>
        /// <exception cref="IllegalArgumentException"> if the row cannot be parsed </exception>
        public static DoublesPair parseQuantity(CsvRow row)
        {
            double?quantityOpt = row.findValue(QUANTITY_FIELD).map(s => LoaderUtils.parseDouble(s));

            if (quantityOpt.HasValue)
            {
                double quantity = quantityOpt.Value;
                return(DoublesPair.of(quantity >= 0 ? quantity : 0, quantity >= 0 ? 0 : -quantity));
            }
            double?longQuantityOpt  = row.findValue(LONG_QUANTITY_FIELD).map(s => LoaderUtils.parseDouble(s));
            double?shortQuantityOpt = row.findValue(SHORT_QUANTITY_FIELD).map(s => LoaderUtils.parseDouble(s));

            if (!longQuantityOpt.HasValue && !shortQuantityOpt.HasValue)
            {
                throw new System.ArgumentException(Messages.format("Security must contain a quantity column, either '{}' or '{}' and '{}'", QUANTITY_FIELD, LONG_QUANTITY_FIELD, SHORT_QUANTITY_FIELD));
            }
            double longQuantity  = ArgChecker.notNegative(longQuantityOpt.GetValueOrDefault(0d), LONG_QUANTITY_FIELD);
            double shortQuantity = ArgChecker.notNegative(shortQuantityOpt.GetValueOrDefault(0d), SHORT_QUANTITY_FIELD);

            return(DoublesPair.of(longQuantity, shortQuantity));
        }