@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); }
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)); }
/** * Converts this pair to an object-based {@code Pair}. * * @return the object-based pair */ public Pair<Long, Double> toPair() { return Pair.of(first, second); }
@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, p1, p2; double sqrt2 = Math.sqrt(2); DoubleFunction1D x = getX(); for (int i = 0; i <= n; i++) { if (i == 0) { polynomials[i] = Pair.of((DoubleFunction1D) F0, getZero()); } else if (i == 1) { polynomials[i] = Pair.of(polynomials[0].getFirst().multiply(sqrt2).multiply(x), (DoubleFunction1D) DF1); } else { p1 = polynomials[i - 1].getFirst(); p2 = polynomials[i - 2].getFirst(); p = p1.multiply(x).multiply(Math.sqrt(2. / i)).subtract(p2.multiply(Math.sqrt((i - 1.) / i))); dp = p1.multiply(Math.sqrt(2 * i)); polynomials[i] = Pair.of(p, dp); } } return polynomials; }
@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()); } } }
/** * 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(); }
/** * Converts this pair to an object-based {@code Pair}. * * @return the object-based pair */ public Pair<Integer, Double> toPair() { return Pair.of(first, second); }
@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; }
@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); } } }
@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 <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()); }
/** * Converts this pair to an object-based {@code Pair}. * * @return the object-based pair */ public Pair<A, Double> toPair() { return Pair.of(first, second); }
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]); } }
/** * Calculates the expected loss of the CDS product. * <p> * The expected loss is the (undiscounted) expected default settlement value paid by the protection seller. * The resulting value is always positive. * * @param cds the product * @param ratesProvider the rates provider * @return the expected loss */ public CurrencyAmount expectedLoss( ResolvedCds cds, CreditRatesProvider ratesProvider) { if (isExpired(cds, ratesProvider)) { return CurrencyAmount.of(cds.getCurrency(), 0d); } double recoveryRate = recoveryRate(cds, ratesProvider); Pair<CreditDiscountFactors, LegalEntitySurvivalProbabilities> rates = reduceDiscountFactors(cds, ratesProvider); double survivalProbability = rates.getSecond().survivalProbability(cds.getProtectionEndDate()); double el = (1d - recoveryRate) * (1d - survivalProbability); return CurrencyAmount.of(cds.getCurrency(), Math.abs(cds.getNotional()) * el); }