/** * Calibrate trinomial tree to implied volatility surface. * * @param impliedVolatilitySurface the implied volatility surface * @param spot the spot * @param interestRate the interest rate * @param dividendRate the dividend rate * @return the trinomial tree */ public RecombiningTrinomialTreeData calibrateImpliedVolatility( Function<DoublesPair, Double> impliedVolatilitySurface, double spot, Function<Double, Double> interestRate, Function<Double, Double> dividendRate) { return calibrate(impliedVolatilitySurface, spot, interestRate, dividendRate).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)); }
@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() { int col = sensitivity.columnCount(); int row = sensitivity.rowCount(); if (row != parameterMetadata.size()) { throw new IllegalArgumentException("row count of sensitivity and parameter metadata size must match"); } int nParamsTotal = 0; for (Pair<MarketDataName<?>, List<? extends ParameterMetadata>> entry : order) { nParamsTotal += entry.getSecond().size(); } if (col != nParamsTotal) { throw new IllegalArgumentException("column count of sensitivity and total parameter metadata size of order must match"); } }
/** * 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()); }
/** * 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 <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()); }
/** * 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(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); }
/** * Obtains a failure from a reason and message. * <p> * The message is produced using a template that contains zero to many "{}" or "{abc}" placeholders. * Each placeholder is replaced by the next available argument. * If the placeholder has a name, its value is added to the attributes map with the name as a key. * If there are too few arguments, then the message will be left with placeholders. * If there are too many arguments, then the excess arguments are appended to the * end of the message. No attempt is made to format the arguments. * See {@link Messages#formatWithAttributes(String, Object...)} for more details. * <p> * An exception will be created internally to obtain a stack trace. * The cause type will not be present in the resulting failure. * * @param reason the reason * @param message a message explaining the failure, not empty, uses "{}" for inserting {@code messageArgs} * @param messageArgs the arguments for the message * @return the failure */ public static FailureItem of(FailureReason reason, String message, Object... messageArgs) { Pair<String, Map<String, String>> msg = Messages.formatWithAttributes(message, messageArgs); return of(reason, msg.getFirst(), msg.getSecond()); }
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()); }
/** * {@inheritDoc} */ @Override public GaussianQuadratureData generate(int n) { ArgChecker.isTrue(n > 0); int mid = (n + 1) / 2; double[] x = new double[n]; double[] w = new double[n]; Pair<DoubleFunction1D, DoubleFunction1D>[] polynomials = LEGENDRE.getPolynomialsAndFirstDerivative(n); Pair<DoubleFunction1D, DoubleFunction1D> pair = polynomials[n]; DoubleFunction1D function = pair.getFirst(); DoubleFunction1D derivative = pair.getSecond(); for (int i = 0; i < mid; i++) { double root = ROOT_FINDER.getRoot(function, derivative, getInitialRootGuess(i, n)); x[i] = -root; x[n - i - 1] = root; double dp = derivative.applyAsDouble(root); w[i] = 2 / ((1 - root * root) * dp * dp); w[n - i - 1] = w[i]; } return new GaussianQuadratureData(x, w); }
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; }
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 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); }