/** * Computes the conventional real yield from the dirty price. * <p> * The input dirty price should be real price or nominal price depending on the yield convention. This is coherent to * the implementation of {@link #dirtyPriceFromRealYield(ResolvedCapitalIndexedBond, RatesProvider, LocalDate, double)}. * <p> * The input price and output are expressed in fraction. * * @param bond the product * @param ratesProvider the rates provider, used to determine price index values * @param settlementDate the settlement date * @param dirtyPrice the bond dirty price * @return the yield of the product */ public double realYieldFromDirtyPrice( ResolvedCapitalIndexedBond bond, RatesProvider ratesProvider, LocalDate settlementDate, double dirtyPrice) { final Function<Double, Double> priceResidual = new Function<Double, Double>() { @Override public Double apply(Double y) { return dirtyPriceFromRealYield(bond, ratesProvider, settlementDate, y) - dirtyPrice; } }; double[] range = ROOT_BRACKETER.getBracketedPoints(priceResidual, -0.05, 0.10); double yield = ROOT_FINDER.getRoot(priceResidual, range[0], range[1]); return yield; }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullFunction() { getRootFinder().checkInputs((DoubleFunction1D) null, 1., 2.); }
return ROOT_FINDER.getRoot(residual, range[0], range[1]);
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullLower() { getRootFinder().checkInputs(F, null, 2.); }
public abstract Double getRoot(Function<Double, Double> function, Double x1, Double x2);
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullUpper() { getRootFinder().checkInputs(F, 1., null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testOutsideRoots() { getRootFinder().getRoot(F, 10., 100.); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testBracketTwoRoots() { getRootFinder().getRoot(F, 1.5, 3.5); }
return ROOT_FINDER.getRoot(residual, range[0], range[1]);
return ROOT_FINDER.getRoot(residual, range[0], range[1]);
func, 0.8 * guess[i], 1.25 * guess[i], Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); double zeroRate = bracket[0] > bracket[1] ? ROOTFINDER.getRoot(func, bracket[1], bracket[0]) : ROOTFINDER.getRoot(func, bracket[0], bracket[1]); //Negative guess handled creditCurve = creditCurve.withParameter(i, zeroRate); } catch (final MathException e) { //handling bracketing failure due to small survival probability double zeroRate = ROOTFINDER.getRoot(func, bracket[0], bracket[1]); creditCurve = creditCurve.withParameter(i, zeroRate); break; final double[] bracket = BRACKETER.getBracketedPoints(func, guess[i], 1.2 * guess[i], minValue, Double.POSITIVE_INFINITY); final double zeroRate = ROOTFINDER.getRoot(func, bracket[0], bracket[1]); creditCurve = creditCurve.withParameter(i, zeroRate);
/** * Calculates the yield of the fixed coupon bond product from dirty price. * <p> * The dirty price must be fractional. * If the analytic formula is not available, the yield is computed by solving * a root-finding problem with {@link #dirtyPriceFromYield(ResolvedFixedCouponBond, LocalDate, double)}. * The result is also expressed in fraction. * * @param bond the product * @param settlementDate the settlement date * @param dirtyPrice the dirty price * @return the yield of the product */ public double yieldFromDirtyPrice(ResolvedFixedCouponBond bond, LocalDate settlementDate, double dirtyPrice) { if (bond.getYieldConvention().equals(JP_SIMPLE)) { double cleanPrice = cleanPriceFromDirtyPrice(bond, settlementDate, dirtyPrice); LocalDate maturityDate = bond.getUnadjustedEndDate(); double maturity = bond.getDayCount().relativeYearFraction(settlementDate, maturityDate); return (bond.getFixedRate() + (1d - cleanPrice) / maturity) / cleanPrice; } final Function<Double, Double> priceResidual = new Function<Double, Double>() { @Override public Double apply(final Double y) { return dirtyPriceFromYield(bond, settlementDate, y) - dirtyPrice; } }; double[] range = ROOT_BRACKETER.getBracketedPoints(priceResidual, 0.00, 0.20); double yield = ROOT_FINDER.getRoot(priceResidual, range[0], range[1]); return yield; }
double[] bracket = BRACKER.getBracketedPoints(func, 0.8 * guess[i], 1.25 * guess[i], 0.0, Double.POSITIVE_INFINITY); double zeroRate = bracket[0] > bracket[1] ? ROOTFINDER.getRoot(func, bracket[1], bracket[0]) : ROOTFINDER.getRoot(func, bracket[0], bracket[1]); //Negative guess handled creditCurve = creditCurve.withParameter(i, zeroRate);