G2ppPiecewiseConstantModel { public double futuresConvexityFactor(final G2ppPiecewiseConstantParameters g2parameters, final double expiry, final double u, final double v) { final double[] a = g2parameters.getMeanReversion(); final double rho = g2parameters.getCorrelation(); final DoubleArrayList[] eta = g2parameters.getVolatility(); final double factor111 = 0.5 / (a[0] * a[0] * a[0]); final double factor112 = rho / (a[0] * a[1]); final double factor121 = rho / (a[0] * a[1]); final double factor122 = 0.5 / (a[1] * a[1] * a[1]); final double factor21 = Math.exp(-a[0] * u) - Math.exp(-a[0] * v); final double factor22 = Math.exp(-a[1] * u) - Math.exp(-a[1] * v); final double[] s = g2parameters.getVolatilityTime(); int indexexpiry = 1; while (expiry > s[indexexpiry]) { indexexpiry++; } double[] r = new double[indexexpiry + 1]; System.arraycopy(s, 0, r, 0, indexexpiry); r[indexexpiry] = expiry; double factor311 = 0.0; double factor312 = 0.0; double factor321 = 0.0; double factor322 = 0.0; for (int j = 0; j < indexexpiry; j++) { factor311 += eta[0].getDouble(j) * eta[0].getDouble(j) * (Math.exp(a[0] * r[j + 1]) - Math.exp(a[0] * r[j])) * (2 - Math.exp(-a[0] * (v - r[j + 1])) - Math.exp(-a[0] * (v - r[j]))); factor312 += eta[0].getDouble(j) * eta[1].getDouble(j) * ((Math.exp(a[1] * r[j + 1]) - Math.exp(a[1] * r[j])) / a[1] - (Math.exp(-a[0] * (v - r[j + 1]) + a[1] * r[j + 1]) - Math.exp(-a[0] * (v - r[j]) + a[1] * r[j])) / (a[0] + a[1])); factor321 += eta[1].getDouble(j) * eta[0].getDouble(j) * ((Math.exp(a[0] * r[j + 1]) - Math.exp(a[0] * r[j])) / a[0] - (Math.exp(-a[1] * (v - r[j + 1]) + a[0] * r[j + 1]) - Math.exp(-a[1] * (v - r[j]) + a[0] * r[j])) / (a[1] + a[0])); factor322 += eta[1].getDouble(j) * eta[1].getDouble(j) * (Math.exp(a[1] * r[j + 1]) - Math.exp(a[1] * r[j])) * (2 - Math.exp(-a[1] * (v - r[j + 1])) - Math.exp(-a[1] * (v - r[j]))); } double convexity = factor111 * factor21 * factor311 + factor112 * factor22 * factor312 + factor121 * factor21 * factor321 + factor122 * factor22 * factor322; convexity = Math.exp(convexity); return convexity; }  double[] volatilityMaturityPart(final G2ppPiecewiseConstantParameters g2parameters, double u, double v); double[][] volatilityMaturityPart(final G2ppPiecewiseConstantParameters g2parameters, double u, double[] v); double[][][] volatilityMaturityPart(final G2ppPiecewiseConstantParameters g2parameters, double u, double[][] v); double[][] gamma(final G2ppPiecewiseConstantParameters g2parameters, double theta0, double theta1); double swapRate(final double[] x, final double[] discountedCashFlowFixed, final double[][] alphaFixed, final double[] tau2Fixed, final double[] discountedCashFlowIbor,
      final double[][] alphaIbor, final double[] tau2Ibor); double swapRate(final double[] x, final double[] discountedCashFlowFixed, final double[][] alphaFixed, final double[] tau2Fixed, final double[] discountedCashFlowIbor,
      final double[][] alphaIbor, final double[] tau2Ibor, double[] d1); double swapRate(final double[] x, final double[] discountedCashFlowFixed, final double[][] alphaFixed, final double[] tau2Fixed, final double[] discountedCashFlowIbor,
      final double[][] alphaIbor, final double[] tau2Ibor, double[] d1, double[][] d2); double[] swapRateDdcfi1(final double[] x, final double[] discountedCashFlowFixed, final double[][] alphaFixed, final double[] tau2Fixed, final double[] discountedCashFlowIbor,
      final double[][] alphaIbor, final double[] tau2Ibor); double[] swapRateDdcff1(final double[] x, final double[] discountedCashFlowFixed, final double[][] alphaFixed, final double[] tau2Fixed, final double[] discountedCashFlowIbor,
      final double[][] alphaIbor, final double[] tau2Ibor); Pair<double[][][], double[][][]> swapRateDdcfDx2(final double[] x, final double[] discountedCashFlowFixed, final double[][] alphaFixed, final double[] tau2Fixed,
      final double[] discountedCashFlowIbor, final double[][] alphaIbor, final double[] tau2Ibor); double futuresConvexityFactor(final G2ppPiecewiseConstantParameters g2parameters, final double expiry, final double u, final double v);  }
@Test public void futuresConvexityFactor() { final double[] volTime = new double[0]; final double[][] vol10 = new double[][] { {0.01 }, {0.00 } }; final double[][] vol01 = new double[][] { {0.00 }, {0.01 } }; final double expiryTime = 1.0; final double u = 1.02; final double v = 1.27; final HullWhiteOneFactorPiecewiseConstantParameters hw10 = new HullWhiteOneFactorPiecewiseConstantParameters(MEAN_REVERSION[0], vol10[0], volTime); final G2ppPiecewiseConstantParameters g2pp10 = new G2ppPiecewiseConstantParameters(MEAN_REVERSION, vol10, volTime, CORRELATION); final double adjHW10 = MODEL_HW.futuresConvexityFactor(hw10, expiryTime, u, v); final double adjG210 = MODEL_G2PP.futuresConvexityFactor(g2pp10, expiryTime, u, v); assertEquals("G2++: swap rate", adjHW10, adjG210, TOLERANCE_FACTOR); final HullWhiteOneFactorPiecewiseConstantParameters hw01 = new HullWhiteOneFactorPiecewiseConstantParameters(MEAN_REVERSION[1], vol01[1], volTime); final G2ppPiecewiseConstantParameters g2pp01 = new G2ppPiecewiseConstantParameters(MEAN_REVERSION, vol01, volTime, CORRELATION); final double adjHW01 = MODEL_HW.futuresConvexityFactor(hw01, expiryTime, u, v); final double adjG201 = MODEL_G2PP.futuresConvexityFactor(g2pp01, expiryTime, u, v); assertEquals("G2++: swap rate", adjHW01, adjG201, TOLERANCE_FACTOR); }