@Override protected Object propertyGet(Bean bean, String propertyName, boolean quiet) { switch (propertyName.hashCode()) { case 97440432: // first return ((Pair<?, ?>) bean).getFirst(); case -906279820: // second return ((Pair<?, ?>) bean).getSecond(); } return super.propertyGet(bean, propertyName, quiet); }
@ImmutableValidator private void validate() { for (Entry<Pair<RepoGroup, Currency>, DiscountFactors> entry : repoCurves.entrySet()) { if (!entry.getValue().getValuationDate().isEqual(valuationDate)) { throw new IllegalArgumentException("Invalid valuation date for the repo curve: " + entry.getValue()); } RepoGroup group = entry.getKey().getFirst(); if (!repoCurveGroups.containsValue(group) && !repoCurveSecurityGroups.containsValue(group)) { throw new IllegalArgumentException("No map to the repo group from ID: " + group); } } for (Entry<Pair<LegalEntityGroup, Currency>, DiscountFactors> entry : issuerCurves.entrySet()) { if (!entry.getValue().getValuationDate().isEqual(valuationDate)) { throw new IllegalArgumentException("Invalid valuation date for the issuer curve: " + entry.getValue()); } if (!issuerCurveGroups.containsValue(entry.getKey().getFirst())) { throw new IllegalArgumentException("No map to the legal entity group from ID: " + entry.getKey().getFirst()); } } }
/** * Obtains an instance from a {@code Pair}. * * @param <A> the first element type * @param pair the pair to convert * @return a pair formed by extracting values from the pair */ public static <A> ObjIntPair<A> ofPair(Pair<A, Integer> pair) { ArgChecker.notNull(pair, "pair"); return new ObjIntPair<A>(pair.getFirst(), pair.getSecond()); }
private Pair<Integer, List<? extends ParameterMetadata>> findStartIndexAndMetadata(MarketDataName<?> name) { int startIndex = 0; for (Pair<MarketDataName<?>, List<? extends ParameterMetadata>> entry : order) { if (entry.getFirst().equals(name)) { return Pair.of(startIndex, entry.getSecond()); } startIndex += entry.getSecond().size(); } throw new IllegalArgumentException(Messages.format("Unable to find sensitivity: {} and {}", marketDataName, name)); }
/** * Creates a {@code Collector} that allows a collection of pairs each containing * a currency pair and a rate to be streamed and collected into a new {@code FxMatrix}. * * @return a collector for creating an {@code FxMatrix} from a stream */ public static Collector<? super Pair<CurrencyPair, Double>, FxMatrixBuilder, FxMatrix> pairsToFxMatrix() { return collector((builder, pair) -> builder.addRate(pair.getFirst(), pair.getSecond())); }
/** * Obtains an instance from a {@code Pair}. * * @param pair the pair to convert * @return a pair formed by extracting values from the pair */ public static IntDoublePair ofPair(Pair<Integer, Double> pair) { ArgChecker.notNull(pair, "pair"); return new IntDoublePair(pair.getFirst(), pair.getSecond()); }
/** * Obtains an instance from a {@code Pair}. * * @param <A> the first element type * @param pair the pair to convert * @return a pair formed by extracting values from the pair */ public static <A> ObjDoublePair<A> ofPair(Pair<A, Double> pair) { ArgChecker.notNull(pair, "pair"); return new ObjDoublePair<A>(pair.getFirst(), pair.getSecond()); }
/** * Obtains an instance from a {@code Pair}. * * @param pair the pair to convert * @return a pair formed by extracting values from the pair */ public static LongDoublePair ofPair(Pair<Long, Double> pair) { ArgChecker.notNull(pair, "pair"); return new LongDoublePair(pair.getFirst(), pair.getSecond()); }
/** * Obtains an instance from a {@code Pair}. * * @param pair the pair to convert * @return a pair formed by extracting values from the pair */ public static DoublesPair ofPair(Pair<Double, Double> pair) { ArgChecker.notNull(pair, "pair"); return new DoublesPair(pair.getFirst(), pair.getSecond()); }
@Test public void testAlpha2() { final int n = 14; final Pair<DoubleFunction1D, DoubleFunction1D>[] polynomialAndDerivative1 = LAGUERRE.getPolynomialsAndFirstDerivative(n); final Pair<DoubleFunction1D, DoubleFunction1D>[] polynomialAndDerivative2 = LAGUERRE.getPolynomialsAndFirstDerivative(n, 0); for (int i = 0; i < n; i++) { assertTrue(polynomialAndDerivative1[i].getFirst() instanceof RealPolynomialFunction1D); assertTrue(polynomialAndDerivative2[i].getFirst() instanceof RealPolynomialFunction1D); final RealPolynomialFunction1D first = (RealPolynomialFunction1D) polynomialAndDerivative1[i].getFirst(); final RealPolynomialFunction1D second = (RealPolynomialFunction1D) polynomialAndDerivative2[i].getFirst(); assertEquals(first, second); } } }
@Test(dataProvider = "factory") public void test_of_getters(Object first, Object second) { Pair<Object, Object> test = Pair.of(first, second); assertEquals(test.getFirst(), first); assertEquals(test.getSecond(), second); }
public void partitionByValueEmptySeries() { Pair<LocalDateDoubleTimeSeries, LocalDateDoubleTimeSeries> partitioned = LocalDateDoubleTimeSeries.empty().partitionByValue(d -> d > 10); assertThat(partitioned.getFirst()).isEqualTo(LocalDateDoubleTimeSeries.empty()); assertThat(partitioned.getSecond()).isEqualTo(LocalDateDoubleTimeSeries.empty()); }
public void partitionEmptySeries() { Pair<LocalDateDoubleTimeSeries, LocalDateDoubleTimeSeries> partitioned = LocalDateDoubleTimeSeries.empty().partition((ld, d) -> ld.getYear() == 2015); assertThat(partitioned.getFirst()).isEqualTo(LocalDateDoubleTimeSeries.empty()); assertThat(partitioned.getSecond()).isEqualTo(LocalDateDoubleTimeSeries.empty()); }
double price( ResolvedCds cds, CreditRatesProvider ratesProvider, double fractionalSpread, LocalDate referenceDate, PriceType priceType, ReferenceData refData) { if (!cds.getProtectionEndDate().isAfter(ratesProvider.getValuationDate())) { //short cut already expired CDSs return 0d; } LocalDate stepinDate = cds.getStepinDateOffset().adjust(ratesProvider.getValuationDate(), refData); LocalDate effectiveStartDate = cds.calculateEffectiveStartDate(stepinDate); double recoveryRate = recoveryRate(cds, ratesProvider); Pair<CreditDiscountFactors, LegalEntitySurvivalProbabilities> rates = reduceDiscountFactors(cds, ratesProvider); double protectionLeg = protectionLeg(cds, rates.getFirst(), rates.getSecond(), referenceDate, effectiveStartDate, recoveryRate); double rpv01 = riskyAnnuity( cds, rates.getFirst(), rates.getSecond(), referenceDate, stepinDate, effectiveStartDate, priceType); return protectionLeg - rpv01 * fractionalSpread; }
@Override public Pair<DoubleFunction1D, DoubleFunction1D>[] getPolynomialsAndFirstDerivative(int n) { ArgChecker.isTrue(n >= 0); @SuppressWarnings("unchecked") Pair<DoubleFunction1D, DoubleFunction1D>[] polynomials = new Pair[n + 1]; DoubleFunction1D p, dp; for (int i = 0; i <= n; i++) { if (i == 0) { polynomials[i] = Pair.of(getOne(), getZero()); } else if (i == 1) { polynomials[i] = Pair.of(getX(), getOne()); } else { p = (polynomials[i - 1].getFirst() .multiply(getX()) .multiply(2 * i - 1) .subtract(polynomials[i - 2].getFirst().multiply(i - 1))).multiply(1. / i); dp = p.derivative(); polynomials[i] = Pair.of(p, dp); } } return polynomials; }
public void partitionByValue() { List<LocalDate> dates = dates(DATE_2010_01_01, DATE_2011_06_01, DATE_2012_01_01, DATE_2013_06_01, DATE_2014_01_01); LocalDateDoubleTimeSeries series = LocalDateDoubleTimeSeries.builder().putAll(dates, VALUES_10_14).build(); Pair<LocalDateDoubleTimeSeries, LocalDateDoubleTimeSeries> partition = series.partitionByValue(d -> d > 10 && d < 14); LocalDateDoubleTimeSeries mid = partition.getFirst(); LocalDateDoubleTimeSeries extreme = partition.getSecond(); assertThat(mid.size()).isEqualTo(3); assertThat(extreme.size()).isEqualTo(2); assertThat(mid.get(DATE_2011_06_01)).hasValue(11); assertThat(mid.get(DATE_2012_01_01)).hasValue(12); assertThat(mid.get(DATE_2013_06_01)).hasValue(13); assertThat(extreme.get(DATE_2010_01_01)).hasValue(10); assertThat(extreme.get(DATE_2014_01_01)).hasValue(14); }
public void available_smile_at_expiry() { double shift = 0.0075; RawOptionData test = RawOptionData.ofBlackVolatility(EXPIRIES, STRIKES, ValueType.STRIKE, DATA_SPARSE, shift); DoubleArray[] strikesAvailable = new DoubleArray[3]; strikesAvailable[0] = DoubleArray.EMPTY; strikesAvailable[1] = DoubleArray.of(0.0050, 0.0150, 0.0250); strikesAvailable[2] = DoubleArray.of(-0.0050, 0.0050, 0.0150, 0.0250); DoubleArray[] volAvailable = new DoubleArray[3]; volAvailable[0] = DoubleArray.EMPTY; volAvailable[1] = DoubleArray.of(0.10, 0.11, 0.12); volAvailable[2] = DoubleArray.of(0.10, 0.11, 0.12, 0.13); for (int i = 0; i < DATA_SPARSE.rowCount(); i++) { Pair<DoubleArray, DoubleArray> smile = test.availableSmileAtExpiry(EXPIRIES.get(i)); assertEquals(smile.getFirst(), strikesAvailable[i]); } }
public void partition() { List<LocalDate> dates = dates(DATE_2010_01_01, DATE_2011_06_01, DATE_2012_01_01, DATE_2013_06_01, DATE_2014_01_01); LocalDateDoubleTimeSeries series = LocalDateDoubleTimeSeries.builder().putAll(dates, VALUES_10_14).build(); Pair<LocalDateDoubleTimeSeries, LocalDateDoubleTimeSeries> partition = series.partition((ld, d) -> ld.getYear() % 2 == 0); LocalDateDoubleTimeSeries even = partition.getFirst(); LocalDateDoubleTimeSeries odd = partition.getSecond(); assertThat(even.size()).isEqualTo(3); assertThat(odd.size()).isEqualTo(2); assertThat(even.get(DATE_2010_01_01)).hasValue(10); assertThat(even.get(DATE_2012_01_01)).hasValue(12); assertThat(even.get(DATE_2014_01_01)).hasValue(14); assertThat(odd.get(DATE_2011_06_01)).hasValue(11); assertThat(odd.get(DATE_2013_06_01)).hasValue(13); }
/** * Parses an ETD future position from the CSV row. * <p> * This is intended to use reference data to find the ETD future security, * returning it as an instance of {@link EtdFuturePosition}. * The reference data lookup uses {@link #parseEtdContractSpec(CsvRow, EtdType)} by default, * however it could be overridden to lookup the security directly in reference data. * * @param row the CSV row to parse * @param info the position information * @return the parsed position * @throws IllegalArgumentException if the row cannot be parsed */ public default Position parseEtdFuturePosition(CsvRow row, PositionInfo info) { EtdContractSpec contract = parseEtdContractSpec(row, EtdType.FUTURE); Pair<YearMonth, EtdVariant> variant = CsvLoaderUtils.parseEtdVariant(row, EtdType.FUTURE); EtdFutureSecurity security = contract.createFuture(variant.getFirst(), variant.getSecond()); DoublesPair quantity = CsvLoaderUtils.parseQuantity(row); EtdFuturePosition position = EtdFuturePosition.ofLongShort(info, security, quantity.getFirst(), quantity.getSecond()); return completePosition(row, position, contract); }
/** * Parses an ETD future position from the CSV row without using reference data. * <p> * This returns a {@link SecurityPosition} based on a standard ETD identifier from {@link EtdIdUtils}. * * @param row the CSV row to parse * @param info the position information * @return the loaded positions, position-level errors are captured in the result * @throws IllegalArgumentException if the row cannot be parsed */ public default SecurityPosition parseEtdFutureSecurityPosition(CsvRow row, PositionInfo info) { ExchangeId exchangeId = ExchangeId.of(row.getValue(EXCHANGE_FIELD)); EtdContractCode contractCode = EtdContractCode.of(row.getValue(CONTRACT_CODE_FIELD)); Pair<YearMonth, EtdVariant> variant = CsvLoaderUtils.parseEtdVariant(row, EtdType.FUTURE); SecurityId securityId = EtdIdUtils.futureId(exchangeId, contractCode, variant.getFirst(), variant.getSecond()); DoublesPair quantity = CsvLoaderUtils.parseQuantity(row); SecurityPosition position = SecurityPosition.ofLongShort(info, securityId, quantity.getFirst(), quantity.getSecond()); return completePosition(row, position); }