@Override public RepoCurveZeroRateSensitivity convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { return (RepoCurveZeroRateSensitivity) PointSensitivity.super.convertedTo(resultCurrency, rateProvider); }
/** * Converts this instance to an equivalent amount in the specified currency. * <p> * The result will be expressed in terms of the given currency. * Any FX conversion that is required will use rates from the provider. * * @param resultCurrency the currency of the result * @param rateProvider the provider of FX rates * @return the converted instance, which should be expressed in the specified currency * @throws RuntimeException if no FX rate could be found */ @Override public default PointSensitivity convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { if (getCurrency().equals(resultCurrency)) { return this; } double fxRate = rateProvider.fxRate(getCurrency(), resultCurrency); return withCurrency(resultCurrency).withSensitivity(fxRate * getSensitivity()); }
/** * Normalizes the point sensitivities by sorting and merging, mutating the internal list. * <p> * The list of sensitivities is sorted and then merged. * Any two entries that represent the same curve query are merged. * For example, if there are two point sensitivities that were created based on the same curve, * currency and fixing date, then the entries are combined, summing the sensitivity value. * <p> * The intention is that normalization occurs after gathering all the point sensitivities. * * @return {@code this}, for method chaining */ @Override public MutablePointSensitivities normalize() { sensitivities.sort(PointSensitivity::compareKey); PointSensitivity previous = sensitivities.get(0); for (int i = 1; i < sensitivities.size(); i++) { PointSensitivity current = sensitivities.get(i); if (current.compareKey(previous) == 0) { sensitivities.set(i - 1, previous.withSensitivity(previous.getSensitivity() + current.getSensitivity())); sensitivities.remove(i); i--; } previous = current; } return this; }
private static void insert(List<PointSensitivity> mutable, PointSensitivity addition) { int index = Collections.binarySearch(mutable, addition, PointSensitivity::compareKey); if (index >= 0) { PointSensitivity base = mutable.get(index); double combined = base.getSensitivity() + addition.getSensitivity(); mutable.set(index, base.withSensitivity(combined)); } else { int insertionPoint = -(index + 1); mutable.add(insertionPoint, addition); } }
private void assertSensitivity( PointSensitivities points1, PointSensitivities points2, SabrParameterType type, int factor) { // use ordinal() as a hack to find correct type assertEquals( points1.getSensitivities().get(type.ordinal()).getSensitivity(), points2.getSensitivities().get(type.ordinal()).getSensitivity() * factor, NOTIONAL * TOL); }
public void test_convertedTo_multipleCurrency() { double rate = 1.5d; FxMatrix matrix = FxMatrix.of(CurrencyPair.of(GBP, USD), rate); PointSensitivities base = PointSensitivities.of(Lists.newArrayList(CS4, CS3, CS1)); PointSensitivities test1 = base.convertedTo(USD, matrix); PointSensitivity c1Conv = CS1.convertedTo(USD, matrix); PointSensitivity c3Conv = CS3.convertedTo(USD, matrix); PointSensitivity c3c4Usd = c3Conv.withSensitivity(c3Conv.getSensitivity() + CS4.getSensitivity()); PointSensitivities expected1 = PointSensitivities.of(Lists.newArrayList(c3c4Usd, c1Conv)); assertEquals(test1.normalized(), expected1.normalized()); PointSensitivities test2 = base.convertedTo(GBP, matrix); PointSensitivity c4Conv = CS4.convertedTo(GBP, matrix); PointSensitivity c3c4GBP = CS3.withSensitivity(CS3.getSensitivity() + c4Conv.getSensitivity()); PointSensitivities expected2 = PointSensitivities.of(Lists.newArrayList(c3c4GBP, CS1)); assertEquals(test2.normalized(), expected2.normalized()); }
/** * Checks if this sensitivity equals another within the specified tolerance. * <p> * This returns true if the two instances have the list of {@code PointSensitivity}, * where the sensitivity {@code double} values are compared within the specified tolerance. * It is expected that this comparator will be used on the normalized version of the sensitivity. * * @param other the other sensitivity * @param tolerance the tolerance * @return true if equal up to the tolerance */ public boolean equalWithTolerance(PointSensitivities other, double tolerance) { ImmutableList<PointSensitivity> list1 = this.getSensitivities(); ImmutableList<PointSensitivity> list2 = other.getSensitivities(); int nbList1 = list1.size(); int nbList2 = list2.size(); if (nbList1 != nbList2) { return false; } for (int i1 = 0; i1 < nbList1; i1++) { if (list1.get(i1).compareKey(list2.get(i1)) == 0) { if (Math.abs(list1.get(i1).getSensitivity() - list2.get(i1).getSensitivity()) > tolerance) { return false; } } else { return false; } } return true; }
public void test_mapSensitivities() { PointSensitivities test = PointSensitivities.of(Lists.newArrayList(CS3, CS2, CS1)); assertEquals( test.mapSensitivities(s -> s / 2).getSensitivities(), ImmutableList.of(CS3.withSensitivity(16d), CS2.withSensitivity(11d), CS1.withSensitivity(6d))); }
@Override public MutablePointSensitivities withCurrency(Currency currency) { sensitivities.replaceAll(ps -> ps.withCurrency(currency)); return this; }
private void assertSensitivity( PointSensitivities points1, PointSensitivities points2, SabrParameterType type, int factor) { // use ordinal() as a hack to find correct type assertEquals( points1.getSensitivities().get(type.ordinal()).getSensitivity(), points2.getSensitivities().get(type.ordinal()).getSensitivity() * factor, NOTIONAL * TOL); }
@Override public MutablePointSensitivities mapSensitivity(DoubleUnaryOperator operator) { sensitivities.replaceAll(cs -> cs.withSensitivity(operator.applyAsDouble(cs.getSensitivity()))); return this; }
public void test_multipliedBy() { PointSensitivities test = PointSensitivities.of(Lists.newArrayList(CS3, CS2, CS1)); assertEquals( test.multipliedBy(2d).getSensitivities(), ImmutableList.of(CS3.withSensitivity(64d), CS2.withSensitivity(44d), CS1.withSensitivity(24d))); }
public void test_withCurrency() { MutablePointSensitivities test = new MutablePointSensitivities(); test.addAll(Lists.newArrayList(CS3, CS2, CS1)); test.withCurrency(USD); assertEquals( test.getSensitivities(), ImmutableList.of(CS3.withCurrency(USD), CS2.withCurrency(USD), CS1.withCurrency(USD))); }
public void test_presentValueSensitivity_afterMaturity() { PointSensitivities pointRec = PRICER.presentValueSensitivityRates( SWAPTION_REC_LONG, RATES_PROVIDER_AFTER_MATURITY, HW_PROVIDER_AFTER_MATURITY).build(); for (PointSensitivity sensi : pointRec.getSensitivities()) { assertEquals(Math.abs(sensi.getSensitivity()), 0d); } PointSensitivities pointPay = PRICER.presentValueSensitivityRates( SWAPTION_PAY_SHORT, RATES_PROVIDER_AFTER_MATURITY, HW_PROVIDER_AFTER_MATURITY).build(); for (PointSensitivity sensi : pointPay.getSensitivities()) { assertEquals(Math.abs(sensi.getSensitivity()), 0d); } }
@Override public IborFutureOptionSensitivity convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { return (IborFutureOptionSensitivity) PointSensitivity.super.convertedTo(resultCurrency, rateProvider); }
/** * Applies an operation to the sensitivities in this instance. * <p> * The result will consist of the same entries, but with the operator applied to each sensitivity value. * This instance is immutable and unaffected by this method. * <p> * This is used to apply a mathematical operation to the sensitivity values. * For example, the operator could multiply the sensitivities by a constant, or take the inverse. * <pre> * inverse = base.mapSensitivities(value -> 1 / value); * </pre> * * @param operator the operator to be applied to the sensitivities * @return a {@code PointSensitivities} based on this one, with the operator applied to the sensitivity values */ public PointSensitivities mapSensitivities(DoubleUnaryOperator operator) { return sensitivities.stream() .map(s -> s.withSensitivity(operator.applyAsDouble(s.getSensitivity()))) .collect( Collectors.collectingAndThen( Guavate.toImmutableList(), PointSensitivities::new)); }
public void test_multiplyBy() { MutablePointSensitivities test = new MutablePointSensitivities(); test.addAll(Lists.newArrayList(CS3, CS2, CS1)); test.multipliedBy(2d); assertEquals( test.getSensitivities(), ImmutableList.of(CS3.withSensitivity(64d), CS2.withSensitivity(44d), CS1.withSensitivity(24d))); }
public void test_presentValueSensitivityRatesStickyModel_afterMaturity() { PointSensitivities pointRec = PRICER.presentValueSensitivityRatesStickyModel( SWAPTION_REC_LONG, RATE_PROVIDER_AFTER_MATURITY, VOLS_AFTER_MATURITY).build(); for (PointSensitivity sensi : pointRec.getSensitivities()) { assertEquals(Math.abs(sensi.getSensitivity()), 0d); } PointSensitivities pointPay = PRICER.presentValueSensitivityRatesStickyModel( SWAPTION_PAY_SHORT, RATE_PROVIDER_AFTER_MATURITY, VOLS_AFTER_MATURITY).build(); for (PointSensitivity sensi : pointPay.getSensitivities()) { assertEquals(Math.abs(sensi.getSensitivity()), 0d); } }
@Override public SwaptionSabrSensitivity convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { return (SwaptionSabrSensitivity) PointSensitivity.super.convertedTo(resultCurrency, rateProvider); }
public void test_mapSensitivities() { MutablePointSensitivities test = new MutablePointSensitivities(); test.addAll(Lists.newArrayList(CS3, CS2, CS1)); test.mapSensitivity(s -> s / 2); assertEquals( test.getSensitivities(), ImmutableList.of(CS3.withSensitivity(16d), CS2.withSensitivity(11d), CS1.withSensitivity(6d))); }