/** * Returns a new array containing the values from this array with the values from the amount added. * <p> * The amount is added to each element in this array. * * @param amount the amount to add * @return a new array containing the values from this array added to the values in the other array */ public MultiCurrencyAmountArray plus(MultiCurrencyAmount amount) { ImmutableMap.Builder<Currency, DoubleArray> builder = ImmutableMap.builder(); for (Currency currency : Sets.union(values.keySet(), amount.getCurrencies())) { DoubleArray array = values.get(currency); if (array == null) { builder.put(currency, DoubleArray.filled(size, amount.getAmount(currency).getAmount())); } else if (!amount.contains(currency)) { builder.put(currency, array); } else { builder.put(currency, array.plus(amount.getAmount(currency).getAmount())); } } return MultiCurrencyAmountArray.of(builder.build()); }
/** * Returns a new array containing the values from this array with the values from the amount subtracted. * <p> * The amount is subtracted from each element in this array. * * @param amount the amount to subtract * @return a new array containing the values from this array with the values from the amount subtracted */ public MultiCurrencyAmountArray minus(MultiCurrencyAmount amount) { ImmutableMap.Builder<Currency, DoubleArray> builder = ImmutableMap.builder(); for (Currency currency : Sets.union(values.keySet(), amount.getCurrencies())) { DoubleArray array = values.get(currency); if (array == null) { builder.put(currency, DoubleArray.filled(size, -amount.getAmount(currency).getAmount())); } else if (!amount.contains(currency)) { builder.put(currency, array); } else { builder.put(currency, array.minus(amount.getAmount(currency).getAmount())); } } return MultiCurrencyAmountArray.of(builder.build()); }
public void test_presentValue_inflation() { DiscountingSwapLegPricer pricerLeg = DiscountingSwapLegPricer.DEFAULT; DiscountingSwapProductPricer pricerSwap = new DiscountingSwapProductPricer(pricerLeg); LocalDate paymentDate = SWAP_INFLATION.getLegs().get(0).getPaymentPeriods().get(0).getPaymentDate(); double fixedRate = INFLATION_FIXED_SWAP_LEG_PAY_GBP_FIXED_RATE; MultiCurrencyAmount pvComputed = pricerSwap.presentValue(SWAP_INFLATION, RATES_GBP_INFLATION); double pvExpected = (-(INDEX_VALUES[0] / START_INDEX - 1.0) + Math.pow(1 + fixedRate, 5) - 1.0) * NOTIONAL * RATES_GBP_INFLATION.discountFactor(GBP, paymentDate); assertTrue(pvComputed.getCurrencies().size() == 1); assertEquals(pvComputed.getAmount(GBP).getAmount(), pvExpected, NOTIONAL * TOLERANCE_RATE); }
public void test_forecastValue_brl_swap() { DiscountingSwapLegPricer pricerLeg = DiscountingSwapLegPricer.DEFAULT; DiscountingSwapProductPricer pricerSwap = new DiscountingSwapProductPricer(pricerLeg); LocalDate paymentDate = BRL_SWAP.getLegs().get(0).getPaymentPeriods().get(0).getPaymentDate(); LocalDate startDate = BRL_SWAP.getLegs().get(0).getPaymentPeriods().get(0).getStartDate(); double af = BUS_252.yearFraction(startDate, paymentDate); MultiCurrencyAmount forecastComputed = pricerSwap.forecastValue(BRL_SWAP, BRL_DSCON); double forecastExpected = (-Math.pow((1 + COUPON), af) + BRL_DSCON.discountFactor(BRL, startDate) / BRL_DSCON.discountFactor(BRL, paymentDate)) * NOTIONAL; assertTrue(forecastComputed.getCurrencies().size() == 1); assertEquals(forecastComputed.getAmount(BRL).getAmount(), forecastExpected, NOTIONAL * TOLERANCE_RATE); }
public void test_presentValue_brl_swap() { DiscountingSwapLegPricer pricerLeg = DiscountingSwapLegPricer.DEFAULT; DiscountingSwapProductPricer pricerSwap = new DiscountingSwapProductPricer(pricerLeg); LocalDate paymentDate = BRL_SWAP.getLegs().get(0).getPaymentPeriods().get(0).getPaymentDate(); LocalDate startDate = BRL_SWAP.getLegs().get(0).getPaymentPeriods().get(0).getStartDate(); double af = BUS_252.yearFraction(startDate, paymentDate); MultiCurrencyAmount pvComputed = pricerSwap.presentValue(BRL_SWAP, BRL_DSCON); double pvExpected = (-Math.pow((1 + COUPON), af)*BRL_DSCON.discountFactor(BRL, paymentDate) + BRL_DSCON.discountFactor(BRL, startDate)) * NOTIONAL; assertTrue(pvComputed.getCurrencies().size() == 1); assertEquals(pvComputed.getAmount(BRL).getAmount(), pvExpected, NOTIONAL * TOLERANCE_RATE); }
private void assertMCA(MultiCurrencyAmount actual, CurrencyAmount... expected) { assertEquals(actual.size(), expected.length); assertEquals(actual.getAmounts().size(), expected.length); assertEquals(actual.getAmounts(), ImmutableSet.copyOf(expected)); Set<Currency> currencies = new HashSet<>(); for (CurrencyAmount expectedAmount : expected) { currencies.add(expectedAmount.getCurrency()); assertEquals(actual.contains(expectedAmount.getCurrency()), true); assertEquals(actual.getAmount(expectedAmount.getCurrency()), expectedAmount); assertEquals(actual.getAmountOrZero(expectedAmount.getCurrency()), expectedAmount); } assertEquals(actual.getCurrencies(), currencies); Currency nonExisting = Currency.of("FRZ"); assertEquals(actual.contains(nonExisting), false); assertThrowsIllegalArg(() -> actual.getAmount(nonExisting)); assertEquals(actual.getAmountOrZero(nonExisting), CurrencyAmount.zero(nonExisting)); }
public void test_presentValueZSpread_settle_before_val() { CurrencyAmount pvComputed = PRICER_TRADE .presentValueWithZSpread(BILL_TRADE_SETTLE_BEFORE_VAL, PROVIDER, Z_SPREAD, CompoundedRateType.CONTINUOUS, 0); CurrencyAmount pvExpected = PRICER_PRODUCT .presentValueWithZSpread(BILL_PRODUCT.resolve(REF_DATA), PROVIDER, Z_SPREAD, CompoundedRateType.CONTINUOUS, 0) .multipliedBy(QUANTITY); assertEquals(pvComputed.getCurrency(), EUR); assertEquals(pvComputed.getAmount(), pvExpected.getAmount(), TOLERANCE_PV); MultiCurrencyAmount ceComputed = PRICER_TRADE .currencyExposureWithZSpread(BILL_TRADE_SETTLE_BEFORE_VAL, PROVIDER, Z_SPREAD, CompoundedRateType.CONTINUOUS, 0); assertEquals(ceComputed.getCurrencies().size(), 1); assertTrue(ceComputed.contains(EUR)); assertEquals(ceComputed.getAmount(EUR).getAmount(), pvExpected.getAmount(), TOLERANCE_PV); }
public void test_presentValueZSpread_settle_on_val() { CurrencyAmount pvComputed = PRICER_TRADE .presentValueWithZSpread(BILL_TRADE_SETTLE_ON_VAL, PROVIDER, Z_SPREAD, CompoundedRateType.CONTINUOUS, 0); CurrencyAmount pvExpected = PRICER_PRODUCT .presentValueWithZSpread(BILL_PRODUCT.resolve(REF_DATA), PROVIDER, Z_SPREAD, CompoundedRateType.CONTINUOUS, 0) .plus(-PRICE * NOTIONAL_AMOUNT) .multipliedBy(QUANTITY); assertEquals(pvComputed.getCurrency(), EUR); assertEquals(pvComputed.getAmount(), pvExpected.getAmount(), TOLERANCE_PV); MultiCurrencyAmount ceComputed = PRICER_TRADE .currencyExposureWithZSpread(BILL_TRADE_SETTLE_ON_VAL, PROVIDER, Z_SPREAD, CompoundedRateType.CONTINUOUS, 0); assertEquals(ceComputed.getCurrencies().size(), 1); assertTrue(ceComputed.contains(EUR)); assertEquals(ceComputed.getAmount(EUR).getAmount(), pvExpected.getAmount(), TOLERANCE_PV); }
public void test_presentValue_settle_before_val() { CurrencyAmount pvComputed = PRICER_TRADE.presentValue(BILL_TRADE_SETTLE_BEFORE_VAL, PROVIDER); CurrencyAmount pvExpected = PRICER_PRODUCT.presentValue(BILL_PRODUCT.resolve(REF_DATA), PROVIDER) .multipliedBy(QUANTITY); assertEquals(pvComputed.getCurrency(), EUR); assertEquals(pvComputed.getAmount(), pvExpected.getAmount(), TOLERANCE_PV); MultiCurrencyAmount ceComputed = PRICER_TRADE.currencyExposure(BILL_TRADE_SETTLE_BEFORE_VAL, PROVIDER); assertEquals(ceComputed.getCurrencies().size(), 1); assertTrue(ceComputed.contains(EUR)); assertEquals(ceComputed.getAmount(EUR).getAmount(), pvExpected.getAmount(), TOLERANCE_PV); CurrencyAmount cashComputed = PRICER_TRADE.currentCash(BILL_TRADE_SETTLE_BEFORE_VAL, VAL_DATE); assertEquals(cashComputed.getCurrency(), EUR); assertEquals(cashComputed.getAmount(), 0, TOLERANCE_PV); }
public void test_presentValue_settle_on_val() { CurrencyAmount pvComputed = PRICER_TRADE.presentValue(BILL_TRADE_SETTLE_ON_VAL, PROVIDER); CurrencyAmount pvExpected = PRICER_PRODUCT.presentValue(BILL_PRODUCT.resolve(REF_DATA), PROVIDER) .plus(-PRICE * NOTIONAL_AMOUNT) .multipliedBy(QUANTITY); assertEquals(pvComputed.getCurrency(), EUR); assertEquals(pvComputed.getAmount(), pvExpected.getAmount(), TOLERANCE_PV); MultiCurrencyAmount ceComputed = PRICER_TRADE.currencyExposure(BILL_TRADE_SETTLE_ON_VAL, PROVIDER); assertEquals(ceComputed.getCurrencies().size(), 1); assertTrue(ceComputed.contains(EUR)); assertEquals(ceComputed.getAmount(EUR).getAmount(), pvExpected.getAmount(), TOLERANCE_PV); CurrencyAmount cashComputed = PRICER_TRADE.currentCash(BILL_TRADE_SETTLE_ON_VAL, VAL_DATE); assertEquals(cashComputed.getCurrency(), EUR); assertEquals(cashComputed.getAmount(), -PRICE * NOTIONAL_AMOUNT * QUANTITY, TOLERANCE_PV); }
public void test_forecastValue_inflation() { DiscountingSwapLegPricer pricerLeg = DiscountingSwapLegPricer.DEFAULT; DiscountingSwapProductPricer pricerSwap = new DiscountingSwapProductPricer(pricerLeg); ImmutableRatesProvider prov = ImmutableRatesProvider.builder(VAL_DATE_INFLATION) .discountCurves(RATES_GBP_INFLATION.getDiscountCurves()) .priceIndexCurve(GB_RPI, PRICE_CURVE) .timeSeries(GB_RPI, TS_INFLATION) .build(); MultiCurrencyAmount fvComputed = pricerSwap.forecastValue(SWAP_INFLATION, prov); double fixedRate = INFLATION_FIXED_SWAP_LEG_PAY_GBP_FIXED_RATE; double fvExpected = (-(INDEX_VALUES[0] / START_INDEX - 1.0) + Math.pow(1.0 + fixedRate, 5) - 1.0) * NOTIONAL; assertTrue(fvComputed.getCurrencies().size() == 1); assertEquals(fvComputed.getAmount(GBP).getAmount(), fvExpected, NOTIONAL * TOLERANCE_RATE); }
public void test_currencyExposure() { MultiCurrencyAmount computed = PRICER.currencyExposure(PRODUCT, RATES_PROVIDER, ISSUER_RATES_PROVIDER, VALUATION); PointSensitivities point = PRICER.presentValueSensitivity(PRODUCT, RATES_PROVIDER, ISSUER_RATES_PROVIDER).build(); MultiCurrencyAmount expected = RATES_PROVIDER.currencyExposure(point) .plus(PRICER.presentValue(PRODUCT, RATES_PROVIDER, ISSUER_RATES_PROVIDER)); assertEquals(computed.getCurrencies().size(), 1); assertEquals(computed.getAmount(USD).getAmount(), expected.getAmount(USD).getAmount(), NOTIONAL * TOL); }
public void test_currencyExposure_exCoupon() { MultiCurrencyAmount computed = PRICER.currencyExposure(PRODUCT_EX_COUPON, RATES_PROVIDER, ISSUER_RATES_PROVIDER, VALUATION); PointSensitivities point = PRICER.presentValueSensitivity(PRODUCT_EX_COUPON, RATES_PROVIDER, ISSUER_RATES_PROVIDER).build(); MultiCurrencyAmount expected = RATES_PROVIDER.currencyExposure(point) .plus(PRICER.presentValue(PRODUCT_EX_COUPON, RATES_PROVIDER, ISSUER_RATES_PROVIDER)); assertEquals(computed.getCurrencies().size(), 1); assertEquals(computed.getAmount(USD).getAmount(), expected.getAmount(USD).getAmount(), NOTIONAL * TOL); }
public void test_currencyExposureWithZSpread() { MultiCurrencyAmount computed = PRICER.currencyExposureWithZSpread( PRODUCT, RATES_PROVIDER, ISSUER_RATES_PROVIDER, VALUATION, Z_SPREAD, CONTINUOUS, 0); PointSensitivities point = PRICER.presentValueSensitivityWithZSpread( PRODUCT, RATES_PROVIDER, ISSUER_RATES_PROVIDER, Z_SPREAD, CONTINUOUS, 0).build(); MultiCurrencyAmount expected = RATES_PROVIDER.currencyExposure(point).plus( PRICER.presentValueWithZSpread(PRODUCT, RATES_PROVIDER, ISSUER_RATES_PROVIDER, Z_SPREAD, CONTINUOUS, 0)); assertEquals(computed.getCurrencies().size(), 1); assertEquals(computed.getAmount(USD).getAmount(), expected.getAmount(USD).getAmount(), NOTIONAL * TOL); }
public void test_currencyExposureWithZSpread_exCoupon() { MultiCurrencyAmount computed = PRICER.currencyExposureWithZSpread( PRODUCT_EX_COUPON, RATES_PROVIDER, ISSUER_RATES_PROVIDER, VALUATION, Z_SPREAD, PERIODIC, PERIOD_PER_YEAR); PointSensitivities point = PRICER.presentValueSensitivityWithZSpread( PRODUCT_EX_COUPON, RATES_PROVIDER, ISSUER_RATES_PROVIDER, Z_SPREAD, PERIODIC, PERIOD_PER_YEAR).build(); MultiCurrencyAmount expected = RATES_PROVIDER.currencyExposure(point).plus(PRICER.presentValueWithZSpread( PRODUCT_EX_COUPON, RATES_PROVIDER, ISSUER_RATES_PROVIDER, Z_SPREAD, PERIODIC, PERIOD_PER_YEAR)); assertEquals(computed.getCurrencies().size(), 1); assertEquals(computed.getAmount(USD).getAmount(), expected.getAmount(USD).getAmount(), NOTIONAL * TOL); }
public void test_presentValue_settle_after_val() { CurrencyAmount pvComputed = PRICER_TRADE.presentValue(BILL_TRADE_SETTLE_AFTER_VAL, PROVIDER); CurrencyAmount pvExpected = PRICER_PRODUCT.presentValue(BILL_PRODUCT.resolve(REF_DATA), PROVIDER) .multipliedBy(QUANTITY) .plus(PRICER_PAYMENT.presentValue(BILL_TRADE_SETTLE_AFTER_VAL.getSettlement().get(), PROVIDER.repoCurveDiscountFactors(BILL_PRODUCT.getSecurityId(), BILL_PRODUCT.getLegalEntityId(), BILL_PRODUCT.getCurrency()) .getDiscountFactors())); assertEquals(pvComputed.getCurrency(), EUR); assertEquals(pvComputed.getAmount(), pvExpected.getAmount(), TOLERANCE_PV); MultiCurrencyAmount ceComputed = PRICER_TRADE.currencyExposure(BILL_TRADE_SETTLE_AFTER_VAL, PROVIDER); assertEquals(ceComputed.getCurrencies().size(), 1); assertTrue(ceComputed.contains(EUR)); assertEquals(ceComputed.getAmount(EUR).getAmount(), pvExpected.getAmount(), TOLERANCE_PV); CurrencyAmount cashComputed = PRICER_TRADE.currentCash(BILL_TRADE_SETTLE_AFTER_VAL, VAL_DATE); assertEquals(cashComputed.getCurrency(), EUR); assertEquals(cashComputed.getAmount(), 0, TOLERANCE_PV); }
public void test_presentValueZSpread_settle_after_val() { CurrencyAmount pvComputed = PRICER_TRADE .presentValueWithZSpread(BILL_TRADE_SETTLE_AFTER_VAL, PROVIDER, Z_SPREAD, CompoundedRateType.CONTINUOUS, 0); CurrencyAmount pvExpected = PRICER_PRODUCT .presentValueWithZSpread(BILL_PRODUCT.resolve(REF_DATA), PROVIDER, Z_SPREAD, CompoundedRateType.CONTINUOUS, 0) .multipliedBy(QUANTITY) .plus(PRICER_PAYMENT.presentValue(BILL_TRADE_SETTLE_AFTER_VAL.getSettlement().get(), PROVIDER.repoCurveDiscountFactors(BILL_PRODUCT.getSecurityId(), BILL_PRODUCT.getLegalEntityId(), BILL_PRODUCT.getCurrency()) .getDiscountFactors())); assertEquals(pvComputed.getCurrency(), EUR); assertEquals(pvComputed.getAmount(), pvExpected.getAmount(), TOLERANCE_PV); MultiCurrencyAmount ceComputed = PRICER_TRADE .currencyExposureWithZSpread(BILL_TRADE_SETTLE_AFTER_VAL, PROVIDER, Z_SPREAD, CompoundedRateType.CONTINUOUS, 0); assertEquals(ceComputed.getCurrencies().size(), 1); assertTrue(ceComputed.contains(EUR)); assertEquals(ceComputed.getAmount(EUR).getAmount(), pvExpected.getAmount(), TOLERANCE_PV); }