BondSecurityDiscountingMethod { public double dirtyPriceFromYield(final BondFixedSecurity bond, final double yield) { ArgumentChecker.notNull(bond, "bond"); ArgumentChecker.isTrue(bond.getNominal().getNumberOfPayments() == 1, "Yield: more than one nominal repayment."); final int nbCoupon = bond.getCoupon().getNumberOfPayments(); final double nominal = bond.getNominal().getNthPayment(bond.getNominal().getNumberOfPayments() - 1).getAmount(); final YieldConvention yieldConvention = bond.getYieldConvention(); if (nbCoupon == 1) { if (yieldConvention.equals(US_STREET) || yieldConvention.equals(GERMAN_BOND) || yieldConvention.equals(ITALY_TREASURY_BONDS) || yieldConvention.equals(AUSTRALIA_EX_DIVIDEND) || yieldConvention.equals(MEXICAN_BONOS)) { return (nominal + bond.getCoupon().getNthPayment(0).getAmount()) / (1.0 + bond.getFactorToNextCoupon() * yield / bond.getCouponPerYear()) / nominal; } if (yieldConvention.equals(FRANCE_COMPOUND_METHOD)) { return (nominal + bond.getCoupon().getNthPayment(0).getAmount()) / nominal * Math.pow(1.0 + yield / bond.getCouponPerYear(), -bond.getFactorToNextCoupon()); } } if ((yieldConvention.equals(US_STREET)) || (yieldConvention.equals(UK_BUMP_DMO_METHOD)) || (yieldConvention.equals(GERMAN_BOND)) || (yieldConvention.equals(FRANCE_COMPOUND_METHOD)) || (yieldConvention.equals(AUSTRALIA_EX_DIVIDEND) || yieldConvention.equals(MEXICAN_BONOS))) { return dirtyPriceFromYieldStandard(bond, yield); } if (yieldConvention.equals(ITALY_TREASURY_BONDS)) { final double yieldSemiAnnual = (Math.sqrt(1 + yield) - 1) * 2; return dirtyPriceFromYieldStandard(bond, yieldSemiAnnual); } throw new UnsupportedOperationException("The convention " + yieldConvention.getName() + " is not supported."); } private  BondSecurityDiscountingMethod(); static BondSecurityDiscountingMethod getInstance(); MultipleCurrencyAmount presentValue(final BondSecurity<? extends Payment, ? extends Coupon> bond, final IssuerProviderInterface issuerMulticurves); MultipleCurrencyAmount presentValueFromCleanPrice(final BondSecurity<? extends Payment, ? extends Coupon> bond, final MulticurveProviderInterface multicurves, final double cleanPrice); MultipleCurrencyAmount presentValueFromYield(final BondSecurity<? extends Payment, ? extends Coupon> bond, final MulticurveProviderInterface multicurves, final double yield); MultipleCurrencyAmount presentValueFromZSpread(final BondSecurity<? extends Payment, ? extends Coupon> bond,
      final IssuerProviderInterface issuerMulticurves, final double zSpread); MultipleCurrencyAmount presentValueFromZSpread(BondSecurity<? extends Payment, ? extends Coupon> bond,
      IssuerProviderInterface issuerMulticurves, double zSpread, boolean periodic, int periodPerYear); double presentValueZSpreadSensitivity(final BondSecurity<? extends Payment, ? extends Coupon> bond, final IssuerProviderInterface issuerMulticurves, final double zSpread); double dirtyPriceFromCurves(final BondFixedSecurity bond, final IssuerProviderInterface issuerMulticurves); double dirtyPriceFromCleanPrice(final BondFixedSecurity bond, final double cleanPrice); double dirtyPriceFromYield(final BondFixedSecurity bond, final double yield); MulticurveSensitivity dirtyPriceCurveSensitivity(final BondFixedSecurity bond, final IssuerProviderInterface issuerMulticurves); double cleanPriceFromCurves(final BondFixedSecurity bond, final IssuerProviderInterface issuerMulticurves); double cleanPriceFromDirtyPrice(final BondFixedSecurity bond, final double dirtyPrice); double cleanPriceFromYield(final BondFixedSecurity bond, final double yield); double yieldFromDirtyPrice(final BondFixedSecurity bond, final double dirtyPrice); double yieldFromCurves(final BondFixedSecurity bond, final IssuerProviderInterface issuerMulticurves); double yieldFromCleanPrice(final BondFixedSecurity bond, final double cleanPrice); double modifiedDurationFromYield(final BondFixedSecurity bond, final double yield); double modifiedDurationFromCurves(final BondFixedSecurity bond, final IssuerProviderInterface issuerMulticurves); double modifiedDurationFromDirtyPrice(final BondFixedSecurity bond, final double dirtyPrice); double modifiedDurationFromCleanPrice(final BondFixedSecurity bond, final double cleanPrice); double macaulayDurationFromYield(final BondFixedSecurity bond, final double yield); double macaulayDurationFromCurves(final BondFixedSecurity bond, final IssuerProviderInterface issuerMulticurves); double macaulayDurationFromCleanPrice(final BondFixedSecurity bond, final double cleanPrice); double macaulayDurationFromDirtyPrice(final BondFixedSecurity bond, final double dirtyPrice); double convexityFromYield(final BondFixedSecurity bond, final double yield); double convexityFromCurves(final BondFixedSecurity bond, final IssuerProviderInterface issuerMulticurves); double convexityFromDirtyPrice(final BondFixedSecurity bond, final double dirtyPrice); double convexityFromCleanPrice(final BondFixedSecurity bond, final double cleanPrice); double zSpreadFromCurvesAndPV(final BondSecurity<? extends Payment, ? extends Coupon> bond, final IssuerProviderInterface issuerMulticurves, final MultipleCurrencyAmount pv); double zSpreadFromCurvesAndPV(final BondSecurity<? extends Payment, ? extends Coupon> bond,
      final IssuerProviderInterface issuerMulticurves, final MultipleCurrencyAmount pv, final boolean periodic,
      final int periodPerYear); double presentValueZSpreadSensitivityFromCurvesAndPV(final BondSecurity<? extends Payment, ? extends Coupon> bond, final IssuerProviderInterface issuerMulticurves,
      final MultipleCurrencyAmount pv); double zSpreadFromCurvesAndClean(final BondSecurity<? extends Payment, ? extends Coupon> bond, final IssuerProviderInterface issuerMulticurves, final double cleanPrice); double zSpreadFromCurvesAndClean(BondSecurity<? extends Payment, ? extends Coupon> bond,
      IssuerProviderInterface issuerMulticurves, double cleanPrice, boolean periodic, int periodPerYear); double zSpreadFromCurvesAndYield(final BondSecurity<? extends Payment, ? extends Coupon> bond, final IssuerProviderInterface issuerMulticurves, final double yield); double zSpreadFromCurvesAndYield(BondSecurity<? extends Payment, ? extends Coupon> bond,
      IssuerProviderInterface issuerMulticurves, double yield, boolean periodic, int periodPerYear); double presentValueZSpreadSensitivityFromCurvesAndClean(final BondSecurity<? extends Payment, ? extends Coupon> bond, final IssuerProviderInterface issuerMulticurves,
      final double cleanPrice); MultipleCurrencyMulticurveSensitivity presentValueCurveSensitivity(final BondSecurity<? extends Payment, ? extends Coupon> bond, final IssuerProviderInterface issuerMulticurves); StringAmount presentValueParallelCurveSensitivity(final BondSecurity<? extends Payment, ? extends Coupon> bond, final IssuerProviderInterface issuerMulticurves); double accruedInterestFromYield(final BondFixedSecurity bond, final double yield); double accruedInterestFromDirtyPrice(final BondFixedSecurity bond, final double dirtyPrice); double accruedInterestFromCleanPrice(final BondFixedSecurity bond, final double cleanPrice); double accruedInterestFromCurves(final BondFixedSecurity bond, final IssuerProviderInterface curves);  }
@Test public void dirtyPriceFromYieldUSStreet() { final double yield = 0.04; final double dirtyPrice = METHOD_BOND_SECURITY.dirtyPriceFromYield(BOND_FIXED_SECURITY_NO_ENTITY_1, yield); final double dirtyPriceExpected = 1.04173525; assertEquals("Fixed coupon bond security: dirty price from yield", dirtyPriceExpected, dirtyPrice, TOLERANCE_PRICE); }
@Test public void dirtyPriceFromYieldUSStreetLastPeriod() { final ZonedDateTime referenceDate = DateUtils.getUTCDate(2016, 6, 3); final BondFixedSecurity bondSecurity = BOND_FIXED_SECURITY_DEFINITION_NO_ENTITY.toDerivative(referenceDate); final double yield = 0.04; final double dirtyPrice = METHOD_BOND_SECURITY.dirtyPriceFromYield(bondSecurity, yield); final double dirtyPriceExpected = (1 + RATE_FIXED / COUPON_PER_YEAR) / (1 + bondSecurity.getFactorToNextCoupon() * yield / COUPON_PER_YEAR); assertEquals("Fixed coupon bond security: dirty price from yield US Street - last period", dirtyPriceExpected, dirtyPrice, TOLERANCE_PRICE); }
@Test public void dirtyPriceFromYieldUKExDividend() { final double yield = 0.04; final double dirtyPrice = METHOD_BOND_SECURITY.dirtyPriceFromYield(BOND_FIXED_SECURITY_UK, yield); final double dirtyPriceExpected = 1.0277859038; assertEquals("Fixed coupon bond security: dirty price from yield UK", dirtyPriceExpected, dirtyPrice, 1E-8); }
@Test public void dirtyPriceFromYieldUKLastPeriod() { final ZonedDateTime referenceDate = DateUtils.getUTCDate(2014, 6, 3); final BondFixedSecurity bondSecurity = BOND_FIXED_SECURITY_DEFINITION_UK.toDerivative(referenceDate); final double yield = 0.04; final double dirtyPrice = METHOD_BOND_SECURITY.dirtyPriceFromYield(bondSecurity, yield); final double dirtyPriceExpected = (1 + RATE_UK / COUPON_PER_YEAR_G) * Math.pow(1 + yield / COUPON_PER_YEAR_G, -bondSecurity.getFactorToNextCoupon()); assertEquals("Fixed coupon bond security: dirty price from yield UK - last period", dirtyPriceExpected, dirtyPrice, 1E-8); }