HullWhiteOneFactorPiecewiseConstantInterestRateModel { public double alpha(final HullWhiteOneFactorPiecewiseConstantParameters data, final double startExpiry, final double endExpiry, final double numeraireTime, final double bondMaturity) { double factor1 = Math.exp(-data.getMeanReversion() * numeraireTime) - Math.exp(-data.getMeanReversion() * bondMaturity); double numerator = 2 * data.getMeanReversion() * data.getMeanReversion() * data.getMeanReversion(); int indexStart = Math.abs(Arrays.binarySearch(data.getVolatilityTime(), startExpiry) + 1); int indexEnd = Math.abs(Arrays.binarySearch(data.getVolatilityTime(), endExpiry) + 1); int sLen = indexEnd - indexStart + 1; double[] s = new double[sLen + 1]; s[0] = startExpiry; System.arraycopy(data.getVolatilityTime(), indexStart, s, 1, sLen - 1); s[sLen] = endExpiry; double factor2 = 0.0; double[] exp2as = new double[sLen + 1]; for (int loopperiod = 0; loopperiod < sLen + 1; loopperiod++) { exp2as[loopperiod] = Math.exp(2 * data.getMeanReversion() * s[loopperiod]); } for (int loopperiod = 0; loopperiod < sLen; loopperiod++) { factor2 += data.getVolatility()[loopperiod + indexStart - 1] * data.getVolatility()[loopperiod + indexStart - 1] * (exp2as[loopperiod + 1] - exp2as[loopperiod]); } return factor1 * Math.sqrt(factor2 / numerator); }  double futuresConvexityFactor(final HullWhiteOneFactorPiecewiseConstantParameters data, double t0, double t1, double t2); double futuresConvexityFactor(final HullWhiteOneFactorPiecewiseConstantParameters data, final double t0, final double t1, final double t2, final double[] derivatives); double paymentDelayConvexityFactor(final HullWhiteOneFactorPiecewiseConstantParameters parameters, final double startExpiry, final double endExpiry,
      final double u, final double v, final double tp); double alpha(final HullWhiteOneFactorPiecewiseConstantParameters data, final double startExpiry, final double endExpiry, final double numeraireTime, final double bondMaturity); double alpha(final HullWhiteOneFactorPiecewiseConstantParameters data, final double startExpiry, final double endExpiry, final double numeraireTime, final double bondMaturity,
      double[] derivatives); double kappa(final double[] discountedCashFlow, final double[] alpha); double beta(final HullWhiteOneFactorPiecewiseConstantParameters data, final double startExpiry, final double endExpiry); double lambda(final double[] discountedCashFlow, final double[] alpha2, final double[] hwH); double[][] volatilityMaturityPart(final HullWhiteOneFactorPiecewiseConstantParameters hwParameters, double u, double[][] v); double gamma(final HullWhiteOneFactorPiecewiseConstantParameters hwParameters, double theta0, double theta1); double swapRate(final double x, final double[] discountedCashFlowFixed, final double[] alphaFixed, final double[] discountedCashFlowIbor, final double[] alphaIbor); double swapRateDx1(final double x, final double[] discountedCashFlowFixed, final double[] alphaFixed, final double[] discountedCashFlowIbor, final double[] alphaIbor); double swapRateDx2(final double x, final double[] discountedCashFlowFixed, final double[] alphaFixed, final double[] discountedCashFlowIbor, final double[] alphaIbor); double[] swapRateDdcfi1(final double x, final double[] discountedCashFlowFixed, final double[] alphaFixed, final double[] discountedCashFlowIbor, final double[] alphaIbor); double[] swapRateDdcff1(final double x, final double[] discountedCashFlowFixed, final double[] alphaFixed, final double[] discountedCashFlowIbor, final double[] alphaIbor); double[] swapRateDai1(final double x, final double[] discountedCashFlowFixed, final double[] alphaFixed, final double[] discountedCashFlowIbor, final double[] alphaIbor); double[] swapRateDaf1(final double x, final double[] discountedCashFlowFixed, final double[] alphaFixed, final double[] discountedCashFlowIbor, final double[] alphaIbor); Pair<double[], double[]> swapRateDx2Ddcf1(final double x, final double[] discountedCashFlowFixed, final double[] alphaFixed, final double[] discountedCashFlowIbor, final double[] alphaIbor); Pair<double[], double[]> swapRateDx2Da1(final double x, final double[] discountedCashFlowFixed, final double[] alphaFixed, final double[] discountedCashFlowIbor, final double[] alphaIbor);  }
@Test public void alpha() { final double expiry1 = 0.25; final double expiry2 = 2.25; final double numeraire = 10.0; final double maturity = 9.0; double alphaExpected = -0.015191631; double alpha = MODEL.alpha(MODEL_PARAMETERS, expiry1, expiry2, numeraire, maturity); assertEquals("Hull-White one factor: bond volatility (alpha) - all", alphaExpected, alpha, TOLERANCE_ALPHA); alphaExpected = -0.015859116; alpha = MODEL.alpha(MODEL_PARAMETERS, 0.0, expiry2, numeraire, maturity); assertEquals("Hull-White one factor: bond volatility (alpha)- today", alphaExpected, alpha, TOLERANCE_ALPHA); alphaExpected = 0.111299267; alpha = MODEL.alpha(MODEL_PARAMETERS, 0.0, expiry2, expiry2, maturity); assertEquals("Hull-White one factor: bond volatility (alpha) - today and expiry numeraire", alphaExpected, alpha, TOLERANCE_ALPHA); alpha = MODEL.alpha(MODEL_PARAMETERS, 0.0, 0.0, numeraire, maturity); assertEquals("Hull-White one factor: bond volatility (alpha) - today and expiry numeraire", 0.0d, alpha, TOLERANCE_ALPHA); }
@Test public void alphaDSigma() { final double expiry1 = 0.25; final double expiry2 = 2.25; final double numeraire = 10.0; final double maturity = 9.0; final int nbVolatility = VOLATILITY.length; final double[] alphaDerivatives = new double[nbVolatility]; final double alpha = MODEL.alpha(MODEL_PARAMETERS, expiry1, expiry2, numeraire, maturity, alphaDerivatives); final double alpha2 = MODEL.alpha(MODEL_PARAMETERS, expiry1, expiry2, numeraire, maturity); assertEquals("Alpha adjoint: value", alpha2, alpha, 1.0E-10); final double shiftVol = 1.0E-6; final double[] volatilityBumped = new double[nbVolatility]; System.arraycopy(VOLATILITY, 0, volatilityBumped, 0, nbVolatility); final double[] alphaBumpedPlus = new double[nbVolatility]; final double[] alphaBumpedMinus = new double[nbVolatility]; HullWhiteOneFactorPiecewiseConstantParameters parametersBumped; for (int loopvol = 0; loopvol < nbVolatility; loopvol++) { volatilityBumped[loopvol] += shiftVol; parametersBumped = new HullWhiteOneFactorPiecewiseConstantParameters(MEAN_REVERSION, volatilityBumped, VOLATILITY_TIME); alphaBumpedPlus[loopvol] = MODEL.alpha(parametersBumped, expiry1, expiry2, numeraire, maturity); volatilityBumped[loopvol] -= 2 * shiftVol; parametersBumped = new HullWhiteOneFactorPiecewiseConstantParameters(MEAN_REVERSION, volatilityBumped, VOLATILITY_TIME); alphaBumpedMinus[loopvol] = MODEL.alpha(parametersBumped, expiry1, expiry2, numeraire, maturity); assertEquals("Alpha adjoint: derivative " + loopvol + " - Difference: " + ((alphaBumpedPlus[loopvol] - alphaBumpedMinus[loopvol]) / (2 * shiftVol) - alphaDerivatives[loopvol]), (alphaBumpedPlus[loopvol] - alphaBumpedMinus[loopvol]) / (2 * shiftVol), alphaDerivatives[loopvol], 1.0E-9); volatilityBumped[loopvol] = VOLATILITY[loopvol]; } }
@Test(enabled = false) public void performanceAlphaAdjoint() { final double expiry1 = 0.25; final double expiry2 = 2.25; final double numeraire = 10.0; final double maturity = 9.0; final int nbVolatility = VOLATILITY.length; final double[] alphaDerivatives = new double[nbVolatility]; long startTime, endTime; final int nbTest = 100000; double alpha = 0.0; startTime = System.currentTimeMillis(); for (int looptest = 0; looptest < nbTest; looptest++) { alpha = MODEL.alpha(MODEL_PARAMETERS, expiry1, expiry2, numeraire, maturity); } endTime = System.currentTimeMillis(); System.out.println(nbTest + " alpha Hull-White: " + (endTime - startTime) + " ms"); startTime = System.currentTimeMillis(); for (int looptest = 0; looptest < nbTest; looptest++) { alpha = MODEL.alpha(MODEL_PARAMETERS, expiry1, expiry2, numeraire, maturity, alphaDerivatives); } endTime = System.currentTimeMillis(); System.out.println(nbTest + " alpha Hull-White adjoint (value+" + nbVolatility + " derivatives): " + (endTime - startTime) + " ms"); System.out.println("Alpha: " + alpha); }