@Test public void presentValueCurveSensitivity() { final InterestRateCurveSensitivity pvcsComputed = METHOD_SECURITY.presentValueCurveSensitivity(BILL_IAM_SEC, CURVE_BUNDLE); assertEquals( "Bill Security: present value curve sensitivity", 1, pvcsComputed.getSensitivities().size()); assertEquals( "Bill Security: present value curve sensitivity", 1, pvcsComputed.getSensitivities().get(NAME_CURVES[1]).size()); final double deltaTolerancePrice = 1.0E+2; // Testing note: Sensitivity is for a movement of 1. 1E+2 = 0.01 unit for a 1 bp move. final double deltaShift = 1.0E-6; // Credit curve sensitivity final String bumpedCurveName = "Bumped Curve"; final BillSecurity billBumped = BILL_IAM_SEC_DEFINITION.toDerivative(REFERENCE_DATE, NAME_CURVES[0], bumpedCurveName); final double[] nodeTimes = new double[] {billBumped.getEndTime()}; final double[] sensi = SensitivityFiniteDifference.curveSensitivity( billBumped, CURVE_BUNDLE, NAME_CURVES[1], bumpedCurveName, nodeTimes, deltaShift, METHOD_SECURITY); final List<DoublesPair> sensiPv = pvcsComputed.getSensitivities().get(NAME_CURVES[1]); for (int loopnode = 0; loopnode < sensi.length; loopnode++) { final DoublesPair pairPv = sensiPv.get(loopnode); assertEquals( "Bill Security: curve sensitivity - Node " + loopnode, nodeTimes[loopnode], pairPv.getFirst(), 1E-8); AssertJUnit.assertEquals( "Bill Security: curve sensitivity", pairPv.second, sensi[loopnode], deltaTolerancePrice); } }
/** * Test related to the description of a bill total return swap with an underlying bill and a funding * leg. */ public class BillTotalReturnSwapTest { private static final Currency EUR = Currency.EUR; private static final ZonedDateTime EFFECTIVE_DATE = DateUtils.getUTCDate(2014, 6, 25); private static final ZonedDateTime TERMINATION_DATE = DateUtils.getUTCDate(2014, 12, 22); private static final ZonedDateTime REFERENCE_DATE_1 = DateUtils.getUTCDate(2014, 6, 23); // Before effective date. private static final double EFFECTIVE_TIME_1_1 = TimeCalculator.getTimeBetween(REFERENCE_DATE_1, EFFECTIVE_DATE); private static final double TERMINATION_TIME_1_1 = TimeCalculator.getTimeBetween(REFERENCE_DATE_1, TERMINATION_DATE); private static final BillSecurityDefinition BELDEC14_DEFINITION = BillDataSets.billBel_20141218(); private static final BillSecurity BELDEC14 = BELDEC14_DEFINITION.toDerivative(REFERENCE_DATE_1, EFFECTIVE_DATE); // Funding: unique fixed coupon in EUR: pay TRS bill, receive funding private static final double NOTIONAL_TRS = 123456000; private static final double NOTIONAL_BILL = 100000000; // Funding: unique fixed coupon in GBP: receive TRS bond, pay funding private static final double RATE = 0.0043; private static final CouponFixedDefinition FUNDING_FIXED_CPN_REC_DEFINITION = new CouponFixedDefinition( EUR, TERMINATION_DATE, EFFECTIVE_DATE, TERMINATION_DATE, 0.50, NOTIONAL_TRS, RATE); private static final AnnuityDefinition<? extends PaymentDefinition> FUNDING_LEG_FIXED_REC_DEFINITION = new AnnuityDefinition<>( new CouponFixedDefinition[] {FUNDING_FIXED_CPN_REC_DEFINITION}, BELDEC14_DEFINITION.getCalendar()); private static final Annuity<? extends Payment> FUNDING_LEG_FIXED_REC_1 = FUNDING_LEG_FIXED_REC_DEFINITION.toDerivative(REFERENCE_DATE_1); private static final BillTotalReturnSwap TRS_PAY_FIXED_REC_1 = new BillTotalReturnSwap( EFFECTIVE_TIME_1_1, TERMINATION_TIME_1_1, FUNDING_LEG_FIXED_REC_1, BELDEC14, -NOTIONAL_BILL); @Test(expectedExceptions = IllegalArgumentException.class) public void nullFundingLeg() { new BillTotalReturnSwap( EFFECTIVE_TIME_1_1, TERMINATION_TIME_1_1, null, BELDEC14, -NOTIONAL_BILL); } @Test(expectedExceptions = IllegalArgumentException.class) public void nullBond() { new BillTotalReturnSwap( EFFECTIVE_TIME_1_1, TERMINATION_TIME_1_1, FUNDING_LEG_FIXED_REC_1, null, -NOTIONAL_BILL); } @Test public void getter() { assertEquals( "BondTotalReturnSwap: getter", EFFECTIVE_TIME_1_1, TRS_PAY_FIXED_REC_1.getEffectiveTime()); assertEquals( "BondTotalReturnSwap: getter", TERMINATION_TIME_1_1, TRS_PAY_FIXED_REC_1.getTerminationTime()); assertEquals( "BondTotalReturnSwap: getter", FUNDING_LEG_FIXED_REC_1, TRS_PAY_FIXED_REC_1.getFundingLeg()); assertEquals("BondTotalReturnSwap: getter", BELDEC14, TRS_PAY_FIXED_REC_1.getAsset()); assertEquals("BondTotalReturnSwap: getter", -NOTIONAL_BILL, TRS_PAY_FIXED_REC_1.getQuantity()); } }
/** * Tests related to the pricing of bills security by discounting. * * @deprecated This class tests deprecated functionality */ @Deprecated public class BillSecurityDiscountingMethodTest { private static final Currency EUR = Currency.EUR; private static final Calendar CALENDAR = new MondayToFridayCalendar("TARGET"); private static final ZonedDateTime REFERENCE_DATE = DateUtils.getUTCDate(2012, 1, 17); private static final DayCount ACT360 = DayCountFactory.INSTANCE.getDayCount("Actual/360"); private static final int SETTLEMENT_DAYS = 2; private static final YieldConvention YIELD_IAM = YieldConventionFactory.INSTANCE.getYieldConvention("INTEREST@MTY"); private static final YieldConvention YIELD_DSC = YieldConventionFactory.INSTANCE.getYieldConvention("DISCOUNT"); // ISIN: BE0312677462 private static final String ISSUER_BEL = "BELGIUM GOVT"; private static final String ISSUER_US = "US GOVT"; private static final ZonedDateTime END_DATE = DateUtils.getUTCDate(2012, 3, 15); private static final double NOTIONAL = 1000; private static final double YIELD = 0.00185; // External source private static final double PRICE = 0.99971; // External source private static final ZonedDateTime SETTLE_DATE = ScheduleCalculator.getAdjustedDate(REFERENCE_DATE, SETTLEMENT_DAYS, CALENDAR); private static final String[] NAME_CURVES = TestsDataSetsSABR.nameCurvesBond3(); private static final BillSecurityDefinition BILL_IAM_SEC_DEFINITION = new BillSecurityDefinition( EUR, END_DATE, NOTIONAL, SETTLEMENT_DAYS, CALENDAR, YIELD_IAM, ACT360, ISSUER_BEL); private static final BillSecurityDefinition BILL_DSC_SEC_DEFINITION = new BillSecurityDefinition( EUR, END_DATE, NOTIONAL, SETTLEMENT_DAYS, CALENDAR, YIELD_DSC, ACT360, ISSUER_US); // Should not be in EUR, but this is only a test private static final BillSecurity BILL_IAM_SEC = BILL_IAM_SEC_DEFINITION.toDerivative(REFERENCE_DATE, SETTLE_DATE, NAME_CURVES); private static final BillSecurity BILL_DSC_SEC = BILL_DSC_SEC_DEFINITION.toDerivative(REFERENCE_DATE, SETTLE_DATE, NAME_CURVES); private static final YieldCurveBundle CURVE_BUNDLE = TestsDataSetsSABR.createCurvesBond3(); private static final BillSecurityDiscountingMethod METHOD_SECURITY = BillSecurityDiscountingMethod.getInstance(); private static final PresentValueCalculator PVC = PresentValueCalculator.getInstance(); private static final PresentValueCurveSensitivityCalculator PVCSC = PresentValueCurveSensitivityCalculator.getInstance(); private static final YieldFromCurvesCalculator YFCC = YieldFromCurvesCalculator.getInstance(); private static final YieldFromPriceCalculator YFPC = YieldFromPriceCalculator.getInstance(); private static final double TOLERANCE_PV = 1.0E-2; private static final double TOLERANCE_PRICE = 1.0E-8; private static final double TOLERANCE_PRICE_EXTERNAL = 1.0E-5; private static final double TOLERANCE_YIELD = 1.0E-8; private static final double TOLERANCE_YIELD_EXTERNAL = 1.0E-4; private static final double TOLERANCE_YIELD_DERIVATIVE = 1.0E-6; @Test /** Tests the present value against explicit computation. */ public void presentValue() { final CurrencyAmount pvComputed = METHOD_SECURITY.presentValue(BILL_IAM_SEC, CURVE_BUNDLE); final double pvExpected = NOTIONAL * CURVE_BUNDLE.getCurve(NAME_CURVES[1]).getDiscountFactor(BILL_IAM_SEC.getEndTime()); assertEquals( "Bill Security: discounting method - present value", pvExpected, pvComputed.getAmount(), TOLERANCE_PV); } @Test /** Tests the present value: Method vs Calculator */ public void presentValueMethodVsCalculator() { final CurrencyAmount pvMethod = METHOD_SECURITY.presentValue(BILL_IAM_SEC, CURVE_BUNDLE); final double pvCalculator = BILL_IAM_SEC.accept(PVC, CURVE_BUNDLE); assertEquals( "Bill Security: discounting method - present value", pvMethod.getAmount(), pvCalculator, TOLERANCE_PV); } @Test public void priceFromYield() { final double[] yields = new double[] {0.0010, 0.0, -0.0010}; for (final double yield2 : yields) { final double priceComputed = METHOD_SECURITY.priceFromYield(BILL_IAM_SEC, yield2); final double priceExpected = 1.0 / (1 + BILL_IAM_SEC.getAccrualFactor() * yield2); assertEquals( "Bill Security: discounting method - price", priceExpected, priceComputed, TOLERANCE_PRICE); } } @Test public void priceFromYieldExternal() { final double priceComputed = METHOD_SECURITY.priceFromYield(BILL_IAM_SEC, YIELD); assertEquals( "Bill Security: discounting method - price", PRICE, priceComputed, TOLERANCE_PRICE_EXTERNAL); } @Test public void yieldFromPrice() { final double yieldComputedIAM = METHOD_SECURITY.yieldFromPrice(BILL_IAM_SEC, PRICE); final double yieldExpectedIAM = (1.0 / PRICE - 1.0) / BILL_IAM_SEC.getAccrualFactor(); assertEquals( "Bill Security: discounting method - yield", yieldExpectedIAM, yieldComputedIAM, TOLERANCE_YIELD); final double yieldComputedDSC = METHOD_SECURITY.yieldFromPrice(BILL_DSC_SEC, PRICE); final double yieldExpectedDSC = (1.0 - PRICE) / BILL_DSC_SEC.getAccrualFactor(); assertEquals( "Bill Security: discounting method - yield", yieldExpectedDSC, yieldComputedDSC, TOLERANCE_YIELD); } @Test public void yieldFromPriceDerivative() { final double shift = 1.0E-8; final double yieldIAM = METHOD_SECURITY.yieldFromPrice(BILL_IAM_SEC, PRICE); final double yieldPIAM = METHOD_SECURITY.yieldFromPrice(BILL_IAM_SEC, PRICE + shift); final double yieldDerivativeExpectedIAM = (yieldPIAM - yieldIAM) / shift; final double yieldDerivativeComputedIAM = METHOD_SECURITY.yieldFromPriceDerivative(BILL_IAM_SEC, PRICE); assertEquals( "Bill Security: discounting method - yield", yieldDerivativeExpectedIAM, yieldDerivativeComputedIAM, TOLERANCE_YIELD_DERIVATIVE); final double yieldDSC = METHOD_SECURITY.yieldFromPrice(BILL_DSC_SEC, PRICE); final double yieldPDSC = METHOD_SECURITY.yieldFromPrice(BILL_DSC_SEC, PRICE + shift); final double yieldDerivativeExpectedDSC = (yieldPDSC - yieldDSC) / shift; final double yieldDerivativeComputedDSC = METHOD_SECURITY.yieldFromPriceDerivative(BILL_DSC_SEC, PRICE); assertEquals( "Bill Security: discounting method - yield", yieldDerivativeExpectedDSC, yieldDerivativeComputedDSC, TOLERANCE_YIELD_DERIVATIVE); } @Test public void yieldFromPriceExternal() { final double yieldComputed = METHOD_SECURITY.yieldFromPrice(BILL_IAM_SEC, PRICE); assertEquals( "Bill Security: discounting method - yield", YIELD, yieldComputed, TOLERANCE_YIELD_EXTERNAL); } @Test public void yieldFromPriceCoherence() { final double priceComputed = METHOD_SECURITY.priceFromYield(BILL_IAM_SEC, YIELD); final double yieldComputed = METHOD_SECURITY.yieldFromPrice(BILL_IAM_SEC, priceComputed); assertEquals( "Bill Security: discounting method - yield", YIELD, yieldComputed, TOLERANCE_YIELD); } @Test public void priceFromYieldCoherence() { final double yieldComputed = METHOD_SECURITY.yieldFromPrice(BILL_IAM_SEC, PRICE); final double priceComputed = METHOD_SECURITY.priceFromYield(BILL_IAM_SEC, yieldComputed); assertEquals( "Bill Security: discounting method - price", PRICE, priceComputed, TOLERANCE_PRICE); } @Test public void presentValueFromPrice() { final CurrencyAmount pvComputed = METHOD_SECURITY.presentValueFromPrice(BILL_IAM_SEC, PRICE, CURVE_BUNDLE); final double pvExpected = NOTIONAL * PRICE * CURVE_BUNDLE .getCurve(NAME_CURVES[0]) .getDiscountFactor(BILL_IAM_SEC.getSettlementTime()); assertEquals( "Bill Security: discounting method - present value", pvExpected, pvComputed.getAmount(), TOLERANCE_PV); } @Test public void presentValueFromYield() { final CurrencyAmount pvComputed = METHOD_SECURITY.presentValueFromYield(BILL_IAM_SEC, YIELD, CURVE_BUNDLE); final double price = METHOD_SECURITY.priceFromYield(BILL_IAM_SEC, YIELD); final double pvExpected = NOTIONAL * price * CURVE_BUNDLE .getCurve(NAME_CURVES[0]) .getDiscountFactor(BILL_IAM_SEC.getSettlementTime()); assertEquals( "Bill Security: discounting method - present value", pvExpected, pvComputed.getAmount(), TOLERANCE_PV); } @Test public void priceFromCurves() { final double priceComputed = METHOD_SECURITY.priceFromCurves(BILL_IAM_SEC, CURVE_BUNDLE); final CurrencyAmount pvComputed = METHOD_SECURITY.presentValue(BILL_IAM_SEC, CURVE_BUNDLE); final double priceExpected = pvComputed.getAmount() / (NOTIONAL * CURVE_BUNDLE .getCurve(NAME_CURVES[0]) .getDiscountFactor(BILL_IAM_SEC.getSettlementTime())); assertEquals( "Bill Security: discounting method - price", priceExpected, priceComputed, TOLERANCE_PRICE); } @Test public void yieldFromCurves() { final double yieldComputed = METHOD_SECURITY.yieldFromCurves(BILL_IAM_SEC, CURVE_BUNDLE); final double priceComputed = METHOD_SECURITY.priceFromCurves(BILL_IAM_SEC, CURVE_BUNDLE); final double yieldExpected = METHOD_SECURITY.yieldFromPrice(BILL_IAM_SEC, priceComputed); assertEquals( "Bill Security: discounting method - yield", yieldExpected, yieldComputed, TOLERANCE_YIELD); } @Test public void presentValueCurveSensitivity() { final InterestRateCurveSensitivity pvcsComputed = METHOD_SECURITY.presentValueCurveSensitivity(BILL_IAM_SEC, CURVE_BUNDLE); assertEquals( "Bill Security: present value curve sensitivity", 1, pvcsComputed.getSensitivities().size()); assertEquals( "Bill Security: present value curve sensitivity", 1, pvcsComputed.getSensitivities().get(NAME_CURVES[1]).size()); final double deltaTolerancePrice = 1.0E+2; // Testing note: Sensitivity is for a movement of 1. 1E+2 = 0.01 unit for a 1 bp move. final double deltaShift = 1.0E-6; // Credit curve sensitivity final String bumpedCurveName = "Bumped Curve"; final BillSecurity billBumped = BILL_IAM_SEC_DEFINITION.toDerivative(REFERENCE_DATE, NAME_CURVES[0], bumpedCurveName); final double[] nodeTimes = new double[] {billBumped.getEndTime()}; final double[] sensi = SensitivityFiniteDifference.curveSensitivity( billBumped, CURVE_BUNDLE, NAME_CURVES[1], bumpedCurveName, nodeTimes, deltaShift, METHOD_SECURITY); final List<DoublesPair> sensiPv = pvcsComputed.getSensitivities().get(NAME_CURVES[1]); for (int loopnode = 0; loopnode < sensi.length; loopnode++) { final DoublesPair pairPv = sensiPv.get(loopnode); assertEquals( "Bill Security: curve sensitivity - Node " + loopnode, nodeTimes[loopnode], pairPv.getFirst(), 1E-8); AssertJUnit.assertEquals( "Bill Security: curve sensitivity", pairPv.second, sensi[loopnode], deltaTolerancePrice); } } @Test public void presentValueCurveSensitivityMethodVsCalculator() { final InterestRateCurveSensitivity pvcsMethod = METHOD_SECURITY.presentValueCurveSensitivity(BILL_IAM_SEC, CURVE_BUNDLE); final InterestRateCurveSensitivity pvcsCalculator = new InterestRateCurveSensitivity(BILL_IAM_SEC.accept(PVCSC, CURVE_BUNDLE)); AssertSensivityObjects.assertEquals( "Bill Security: discounting method - curve sensitivity", pvcsMethod, pvcsCalculator, TOLERANCE_PV); } @Test public void methodVsCalculator() { double yield1 = METHOD_SECURITY.yieldFromCurves(BILL_IAM_SEC, CURVE_BUNDLE); double yield2 = BILL_IAM_SEC.accept(YFCC, CURVE_BUNDLE); assertEquals( "Bill Security: discounting method - yield from curves", yield1, yield2, TOLERANCE_YIELD); yield1 = METHOD_SECURITY.yieldFromPrice(BILL_IAM_SEC, PRICE); yield2 = BILL_IAM_SEC.accept(YFPC, PRICE); assertEquals( "Bill Security: discounting method - yield from price", yield1, yield2, TOLERANCE_YIELD); } }
/** Tests related to the construction of bills transaction. */ public class BillTransactionTest { private static final Currency EUR = Currency.EUR; private static final Calendar CALENDAR = new MondayToFridayCalendar("TARGET"); private static final ZonedDateTime REFERENCE_DATE = DateUtils.getUTCDate(2012, 1, 16); private static final DayCount ACT360 = DayCountFactory.INSTANCE.getDayCount("Actual/360"); private static final int SETTLEMENT_DAYS = 2; private static final YieldConvention YIELD_CONVENTION = YieldConventionFactory.INSTANCE.getYieldConvention("INTEREST@MTY"); private static final String ISSUER_BEL = "BELGIUM GOVT"; private static final ZonedDateTime END_DATE = DateUtils.getUTCDate(2012, 2, 29); private static final double NOTIONAL = 1000; private static final BillSecurityDefinition BILL_SEC_DEFINITION = new BillSecurityDefinition( EUR, END_DATE, NOTIONAL, SETTLEMENT_DAYS, CALENDAR, YIELD_CONVENTION, ACT360, ISSUER_BEL); private static final double QUANTITY = 123456; private static final ZonedDateTime SETTLE_DATE = ScheduleCalculator.getAdjustedDate(REFERENCE_DATE, 3, CALENDAR); private static final double SETTLE_AMOUT = -NOTIONAL * QUANTITY * 99.95; private static final String DSC_NAME = "EUR Discounting"; private static final String CREDIT_NAME = "EUR BELGIUM GOVT"; private static final BillSecurity BILL_PURCHASE = BILL_SEC_DEFINITION.toDerivative(REFERENCE_DATE, SETTLE_DATE, DSC_NAME, CREDIT_NAME); private static final BillSecurity BILL_STANDARD = BILL_SEC_DEFINITION.toDerivative(REFERENCE_DATE, DSC_NAME, CREDIT_NAME); private static final BillTransaction BILL_TRA = new BillTransaction(BILL_PURCHASE, QUANTITY, SETTLE_AMOUT, BILL_STANDARD); @Test(expectedExceptions = IllegalArgumentException.class) public void nullPurchase() { new BillTransaction(null, QUANTITY, SETTLE_AMOUT, BILL_STANDARD); } @Test(expectedExceptions = IllegalArgumentException.class) public void nullStandard() { new BillTransaction(BILL_PURCHASE, QUANTITY, SETTLE_AMOUT, null); } @Test /** Tests the bill getters. */ public void getters() { assertEquals("Bill transation: getter", BILL_PURCHASE, BILL_TRA.getBillPurchased()); assertEquals("Bill transation: getter", QUANTITY, BILL_TRA.getQuantity()); assertEquals("Bill transation: getter", SETTLE_AMOUT, BILL_TRA.getSettlementAmount()); assertEquals("Bill transation: getter", BILL_STANDARD, BILL_TRA.getBillStandard()); } @Test /** Tests the equal and hash-code methods. */ public void equalHash() { assertEquals("Bill transaction: equal-hash code", BILL_TRA, BILL_TRA); BillTransaction other = new BillTransaction(BILL_PURCHASE, QUANTITY, SETTLE_AMOUT, BILL_STANDARD); assertEquals("Bill transaction: equal-hash code", BILL_TRA, other); assertEquals("Bill transaction: equal-hash code", BILL_TRA.hashCode(), other.hashCode()); BillTransaction modified; modified = new BillTransaction(BILL_STANDARD, QUANTITY, SETTLE_AMOUT, BILL_STANDARD); assertFalse("Bill transaction: equal-hash code", BILL_TRA.equals(modified)); modified = new BillTransaction(BILL_PURCHASE, QUANTITY + 1.0, SETTLE_AMOUT, BILL_STANDARD); assertFalse("Bill transaction: equal-hash code", BILL_TRA.equals(modified)); modified = new BillTransaction(BILL_PURCHASE, QUANTITY, SETTLE_AMOUT + 1.0, BILL_STANDARD); assertFalse("Bill transaction: equal-hash code", BILL_TRA.equals(modified)); modified = new BillTransaction(BILL_PURCHASE, QUANTITY, SETTLE_AMOUT, BILL_PURCHASE); assertFalse("Bill transaction: equal-hash code", BILL_TRA.equals(modified)); } }