コード例 #1
0
/**
 * Build of curve in several blocks with relevant Jacobian matrices. EUR: discounting/ON forward;
 * USD: discounting/ON forward. Standard test data set: 2014-03-10
 */
public class StandardDataSetsEURUSDForex {

  private static final ZonedDateTime NOW = DateUtils.getUTCDate(2014, 3, 10);

  private static final Interpolator1D INTERPOLATOR_LINEAR =
      CombinedInterpolatorExtrapolatorFactory.getInterpolator(
          Interpolator1DFactory.LINEAR,
          Interpolator1DFactory.FLAT_EXTRAPOLATOR,
          Interpolator1DFactory.FLAT_EXTRAPOLATOR);

  private static final LastTimeCalculator MATURITY_CALCULATOR = LastTimeCalculator.getInstance();
  private static final double TOLERANCE_ROOT = 1.0E-10;
  private static final int STEP_MAX = 100;

  private static final HolidayCalendar TARGET = HolidayCalendars.EUTA;
  private static final HolidayCalendar NYC = HolidayCalendars.SAT_SUN;
  private static final Currency EUR = Currency.EUR;
  private static final Currency USD = Currency.USD;
  private static final double FX_EURUSD = 1.38775;
  private static final FxMatrix FX_MATRIX = FxMatrix.builder().addRate(EUR, USD, FX_EURUSD).build();

  private static final double NOTIONAL = 1.0;

  private static final GeneratorSwapFixedON GENERATOR_OIS_EUR =
      GeneratorSwapFixedONMaster.getInstance().getGenerator("EUR1YEONIA", TARGET);
  private static final IndexON EUREONIA = GENERATOR_OIS_EUR.getIndex();
  private static final GeneratorSwapFixedON GENERATOR_OIS_USD =
      GeneratorSwapFixedONMaster.getInstance().getGenerator("USD1YFEDFUND", NYC);
  private static final IndexON USDFEDFUND = GENERATOR_OIS_USD.getIndex();
  private static final IndexON INDEX_ON_EUR = GENERATOR_OIS_EUR.getIndex();
  private static final IndexON INDEX_ON_USD = GENERATOR_OIS_USD.getIndex();
  private static final GeneratorDepositON GENERATOR_DEPOSIT_ON_EUR =
      new GeneratorDepositON("EUR Deposit ON", EUR, TARGET, INDEX_ON_EUR.getDayCount());
  private static final GeneratorDepositON GENERATOR_DEPOSIT_ON_USD =
      new GeneratorDepositON("USD Deposit ON", USD, TARGET, INDEX_ON_USD.getDayCount());
  private static final GeneratorForexSwap GENERATOR_FX_EURUSD =
      new GeneratorForexSwap(
          "EURUSD", EUR, USD, TARGET, 2, GENERATOR_OIS_EUR.getBusinessDayConvention(), true);

  private static final ZonedDateTimeDoubleTimeSeries TS_EMPTY =
      ImmutableZonedDateTimeDoubleTimeSeries.ofEmptyUTC();
  private static final ZonedDateTimeDoubleTimeSeries TS_ON_USD_WITH_TODAY =
      ImmutableZonedDateTimeDoubleTimeSeries.ofUTC(
          new ZonedDateTime[] {
            DateUtils.getUTCDate(2011, 9, 27), DateUtils.getUTCDate(2011, 9, 28)
          },
          new double[] {0.07, 0.08});
  private static final ZonedDateTimeDoubleTimeSeries TS_ON_USD_WITHOUT_TODAY =
      ImmutableZonedDateTimeDoubleTimeSeries.ofUTC(
          new ZonedDateTime[] {
            DateUtils.getUTCDate(2011, 9, 27), DateUtils.getUTCDate(2011, 9, 28)
          },
          new double[] {0.07, 0.08});
  private static final ZonedDateTimeDoubleTimeSeries[] TS_FIXED_OIS_USD_WITH_TODAY =
      new ZonedDateTimeDoubleTimeSeries[] {TS_EMPTY, TS_ON_USD_WITH_TODAY};
  private static final ZonedDateTimeDoubleTimeSeries[] TS_FIXED_OIS_USD_WITHOUT_TODAY =
      new ZonedDateTimeDoubleTimeSeries[] {TS_EMPTY, TS_ON_USD_WITHOUT_TODAY};

  private static final String CURVE_NAME_DSC_EUR = "EUR Dsc";
  private static final String CURVE_NAME_DSC_USD = "USD Dsc";

  /** Market values for the dsc USD curve */
  private static final double[] DSC_USD_MARKET_QUOTES =
      new double[] {
        0.0015, 0.0015, 7.9E-4, 7.8E-4, 8.3E-4, 0.0009, 0.0010, 0.00112, 0.0030525, 0.00686, 0.0109,
        0.01465, 0.01782, 0.02048, 0.02264, 0.02445, 0.02597
      };
  /** Generators for the dsc USD curve */
  private static final GeneratorInstrument<? extends GeneratorAttribute>[] DSC_USD_GENERATORS =
      new GeneratorInstrument<?>[] {
        GENERATOR_DEPOSIT_ON_USD,
        GENERATOR_DEPOSIT_ON_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD
      };
  /** Tenors for the dsc USD curve */
  private static final Period[] DSC_USD_TENOR =
      new Period[] {
        Period.ofDays(0),
        Period.ofDays(1),
        Period.ofMonths(1),
        Period.ofMonths(2),
        Period.ofMonths(3),
        Period.ofMonths(6),
        Period.ofMonths(9),
        Period.ofYears(1),
        Period.ofYears(2),
        Period.ofYears(3),
        Period.ofYears(4),
        Period.ofYears(5),
        Period.ofYears(6),
        Period.ofYears(7),
        Period.ofYears(8),
        Period.ofYears(9),
        Period.ofYears(10)
      };

  private static final GeneratorAttributeIR[] DSC_USD_ATTR =
      new GeneratorAttributeIR[DSC_USD_TENOR.length];

  static {
    for (int loopins = 0; loopins < 2; loopins++) {
      DSC_USD_ATTR[loopins] = new GeneratorAttributeIR(DSC_USD_TENOR[loopins], Period.ZERO);
    }
    for (int loopins = 2; loopins < DSC_USD_TENOR.length; loopins++) {
      DSC_USD_ATTR[loopins] = new GeneratorAttributeIR(DSC_USD_TENOR[loopins]);
    }
  }

  /** Market values for the dsc EUR curve - calibrated on OIS */
  private static final double[] DSC_EUR_MARKET_QUOTES =
      new double[] {
        0.001725, 0.00170, 0.00196, 0.00193, 0.00186, 0.00181, 0.00172, 0.00174, 0.002015, 0.00321,
        0.00491, 0.0068, 0.01061, 0.01539
      };
  /** Generators for the dsc EUR curve */
  private static final GeneratorInstrument<? extends GeneratorAttribute>[] DSC_EUR_GENERATORS =
      new GeneratorInstrument<?>[] {
        GENERATOR_DEPOSIT_ON_EUR,
        GENERATOR_DEPOSIT_ON_EUR,
        GENERATOR_OIS_EUR,
        GENERATOR_OIS_EUR,
        GENERATOR_OIS_EUR,
        GENERATOR_OIS_EUR,
        GENERATOR_OIS_EUR,
        GENERATOR_OIS_EUR,
        GENERATOR_OIS_EUR,
        GENERATOR_OIS_EUR,
        GENERATOR_OIS_EUR,
        GENERATOR_OIS_EUR,
        GENERATOR_OIS_EUR,
        GENERATOR_OIS_EUR
      };
  /** Tenors for the dsc EUR curve */
  private static final Period[] DSC_EUR_TENOR =
      new Period[] {
        Period.ofDays(0),
        Period.ofDays(1),
        Period.ofMonths(1),
        Period.ofMonths(2),
        Period.ofMonths(3),
        Period.ofMonths(6),
        Period.ofMonths(9),
        Period.ofYears(1),
        Period.ofYears(2),
        Period.ofYears(3),
        Period.ofYears(4),
        Period.ofYears(5),
        Period.ofYears(7),
        Period.ofYears(10)
      };

  private static final GeneratorAttributeIR[] DSC_EUR_ATTR =
      new GeneratorAttributeIR[DSC_EUR_TENOR.length];

  static {
    for (int loopins = 0; loopins < 2; loopins++) {
      DSC_EUR_ATTR[loopins] = new GeneratorAttributeIR(DSC_EUR_TENOR[loopins], Period.ZERO);
    }
    for (int loopins = 2; loopins < DSC_EUR_TENOR.length; loopins++) {
      DSC_EUR_ATTR[loopins] = new GeneratorAttributeIR(DSC_EUR_TENOR[loopins]);
    }
  }

  /** Market values for the FX EUR USD FX swaps */
  private static final double[] DSC_EURUSD_MARKET_FORWARD =
      new double[] {
        1.387673, 1.387625, 1.3875895, 1.38755, 1.387566,
        1.387777, 1.39303, 1.406789, 1.427726, 1.4525105
      };

  private static final int NB_DSC_EURUSD_QUOTES = DSC_EURUSD_MARKET_FORWARD.length;
  private static final double[] DSC_EURUSD_MARKET_QUOTES = new double[NB_DSC_EURUSD_QUOTES];

  static {
    for (int loopquote = 0; loopquote < NB_DSC_EURUSD_QUOTES; loopquote++) {
      DSC_EURUSD_MARKET_QUOTES[loopquote] = DSC_EURUSD_MARKET_FORWARD[loopquote] - FX_EURUSD;
    }
  }
  /** Generators for the dsc FX curve */
  private static final GeneratorInstrument<? extends GeneratorAttribute>[] DSC_EURUSD_GENERATORS =
      new GeneratorInstrument<?>[] {
        GENERATOR_FX_EURUSD, GENERATOR_FX_EURUSD, GENERATOR_FX_EURUSD, GENERATOR_FX_EURUSD,
            GENERATOR_FX_EURUSD,
        GENERATOR_FX_EURUSD, GENERATOR_FX_EURUSD, GENERATOR_FX_EURUSD, GENERATOR_FX_EURUSD,
            GENERATOR_FX_EURUSD
      };
  /** Tenors for the dsc FX curve */
  private static final Period[] DSC_EURUSD_TENOR =
      new Period[] {
        Period.ofMonths(1), Period.ofMonths(2), Period.ofMonths(3), Period.ofMonths(6),
            Period.ofMonths(9),
        Period.ofYears(1), Period.ofYears(2), Period.ofYears(3), Period.ofYears(4),
            Period.ofYears(5)
      };

  private static final GeneratorAttribute[] DSC_EURUSD_ATTR =
      new GeneratorAttribute[DSC_EUR_TENOR.length];

  static {
    for (int loopins = 0; loopins < DSC_EURUSD_TENOR.length; loopins++) {
      DSC_EURUSD_ATTR[loopins] = new GeneratorAttributeFX(DSC_EURUSD_TENOR[loopins], FX_MATRIX);
    }
  }

  /** Standard USD discounting curve instrument definitions */
  private static final InstrumentDefinition<?>[] DEFINITIONS_DSC_USD;
  /** Standard EUR discounting curve instrument definitions */
  private static final InstrumentDefinition<?>[] DEFINITIONS_DSC_EUR;
  /** Standard EUR discounting curve instrument definitions */
  private static final InstrumentDefinition<?>[] DEFINITIONS_DSC_FX;

  /** Units of curves */
  private static final int[] NB_UNITS = new int[] {2, 2, 2};

  private static final int NB_BLOCKS = NB_UNITS.length;
  private static final InstrumentDefinition<?>[][][][] DEFINITIONS_UNITS =
      new InstrumentDefinition<?>[NB_BLOCKS][][][];
  private static final GeneratorYDCurve[][][] GENERATORS_UNITS =
      new GeneratorYDCurve[NB_BLOCKS][][];
  private static final String[][][] NAMES_UNITS = new String[NB_BLOCKS][][];

  private static final MulticurveProviderDiscount MULTICURVE_KNOWN_DATA =
      new MulticurveProviderDiscount(FX_MATRIX);

  private static final LinkedHashMap<String, Currency> DSC_MAP = new LinkedHashMap<>();
  private static final LinkedHashMap<String, IndexON[]> FWD_ON_MAP = new LinkedHashMap<>();
  private static final LinkedHashMap<String, IborIndex[]> FWD_IBOR_MAP = new LinkedHashMap<>();

  static {
    DEFINITIONS_DSC_USD = getDefinitions(DSC_USD_MARKET_QUOTES, DSC_USD_GENERATORS, DSC_USD_ATTR);
    DEFINITIONS_DSC_EUR = getDefinitions(DSC_EUR_MARKET_QUOTES, DSC_EUR_GENERATORS, DSC_EUR_ATTR);
    DEFINITIONS_DSC_FX =
        getDefinitions(DSC_EURUSD_MARKET_QUOTES, DSC_EURUSD_GENERATORS, DSC_EURUSD_ATTR);
    for (int loopblock = 0; loopblock < NB_BLOCKS; loopblock++) {
      DEFINITIONS_UNITS[loopblock] = new InstrumentDefinition<?>[NB_UNITS[loopblock]][][];
      GENERATORS_UNITS[loopblock] = new GeneratorYDCurve[NB_UNITS[loopblock]][];
      NAMES_UNITS[loopblock] = new String[NB_UNITS[loopblock]][];
    }
    DEFINITIONS_UNITS[0] = new InstrumentDefinition<?>[NB_UNITS[0]][][];
    DEFINITIONS_UNITS[0][0] = new InstrumentDefinition<?>[][] {DEFINITIONS_DSC_USD};
    DEFINITIONS_UNITS[0][1] = new InstrumentDefinition<?>[][] {DEFINITIONS_DSC_EUR};
    DEFINITIONS_UNITS[1] = new InstrumentDefinition<?>[NB_UNITS[0]][][];
    DEFINITIONS_UNITS[1][0] = new InstrumentDefinition<?>[][] {DEFINITIONS_DSC_USD};
    DEFINITIONS_UNITS[1][1] = new InstrumentDefinition<?>[][] {DEFINITIONS_DSC_FX};
    DEFINITIONS_UNITS[2] = new InstrumentDefinition<?>[NB_UNITS[0]][][];
    DEFINITIONS_UNITS[2][0] = new InstrumentDefinition<?>[][] {DEFINITIONS_DSC_EUR};
    DEFINITIONS_UNITS[2][1] = new InstrumentDefinition<?>[][] {DEFINITIONS_DSC_FX};
    final GeneratorYDCurve genIntLin =
        new GeneratorCurveYieldInterpolated(MATURITY_CALCULATOR, INTERPOLATOR_LINEAR);
    GENERATORS_UNITS[0] = new GeneratorYDCurve[NB_UNITS[0]][];
    GENERATORS_UNITS[0][0] = new GeneratorYDCurve[] {genIntLin};
    GENERATORS_UNITS[0][1] = new GeneratorYDCurve[] {genIntLin};
    GENERATORS_UNITS[1] = new GeneratorYDCurve[NB_UNITS[0]][];
    GENERATORS_UNITS[1][0] = new GeneratorYDCurve[] {genIntLin};
    GENERATORS_UNITS[1][1] = new GeneratorYDCurve[] {genIntLin};
    GENERATORS_UNITS[2] = new GeneratorYDCurve[NB_UNITS[0]][];
    GENERATORS_UNITS[2][0] = new GeneratorYDCurve[] {genIntLin};
    GENERATORS_UNITS[2][1] = new GeneratorYDCurve[] {genIntLin};
    NAMES_UNITS[0] = new String[NB_UNITS[0]][];
    NAMES_UNITS[0][0] = new String[] {CURVE_NAME_DSC_USD};
    NAMES_UNITS[0][1] = new String[] {CURVE_NAME_DSC_EUR};
    NAMES_UNITS[1] = new String[NB_UNITS[0]][];
    NAMES_UNITS[1][0] = new String[] {CURVE_NAME_DSC_USD};
    NAMES_UNITS[1][1] = new String[] {CURVE_NAME_DSC_EUR};
    NAMES_UNITS[2] = new String[NB_UNITS[0]][];
    NAMES_UNITS[2][0] = new String[] {CURVE_NAME_DSC_EUR};
    NAMES_UNITS[2][1] = new String[] {CURVE_NAME_DSC_USD};
    DSC_MAP.put(CURVE_NAME_DSC_USD, USD);
    DSC_MAP.put(CURVE_NAME_DSC_EUR, EUR);
    FWD_ON_MAP.put(CURVE_NAME_DSC_USD, new IndexON[] {INDEX_ON_USD});
    FWD_ON_MAP.put(CURVE_NAME_DSC_EUR, new IndexON[] {INDEX_ON_EUR});
  }

  @SuppressWarnings("unchecked")
  public static InstrumentDefinition<?>[] getDefinitions(
      final double[] marketQuotes,
      @SuppressWarnings("rawtypes") final GeneratorInstrument[] generators,
      final GeneratorAttribute[] attribute) {
    final InstrumentDefinition<?>[] definitions = new InstrumentDefinition<?>[marketQuotes.length];
    for (int loopmv = 0; loopmv < marketQuotes.length; loopmv++) {
      definitions[loopmv] =
          generators[loopmv].generateInstrument(
              NOW, marketQuotes[loopmv], NOTIONAL, attribute[loopmv]);
    }
    return definitions;
  }

  private static List<Pair<MulticurveProviderDiscount, CurveBuildingBlockBundle>>
      CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK = new ArrayList<>();

  // Calculators
  private static final ParSpreadMarketQuoteDiscountingCalculator PSMQDC =
      ParSpreadMarketQuoteDiscountingCalculator.getInstance();
  private static final ParSpreadMarketQuoteCurveSensitivityDiscountingCalculator PSMQCSDC =
      ParSpreadMarketQuoteCurveSensitivityDiscountingCalculator.getInstance();

  private static final MulticurveDiscountBuildingRepository CURVE_BUILDING_REPOSITORY =
      new MulticurveDiscountBuildingRepository(TOLERANCE_ROOT, TOLERANCE_ROOT, STEP_MAX);

  static {
    for (int loopblock = 0; loopblock < NB_BLOCKS; loopblock++) {
      CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK.add(
          makeCurvesFromDefinitions(
              DEFINITIONS_UNITS[loopblock],
              GENERATORS_UNITS[loopblock],
              NAMES_UNITS[loopblock],
              MULTICURVE_KNOWN_DATA,
              PSMQDC,
              PSMQCSDC,
              false));
    }
  }

  public static Pair<MulticurveProviderDiscount, CurveBuildingBlockBundle> getCurvesEUROisUSDOis() {
    return CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK.get(0);
  }

  public static Pair<MulticurveProviderDiscount, CurveBuildingBlockBundle> getCurvesUSDOisEURFx() {
    return CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK.get(1);
  }

  public static Pair<MulticurveProviderDiscount, CurveBuildingBlockBundle> getCurvesEUROisUSDFx() {
    return CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK.get(2);
  }

  /**
   * Returns the array of overnight index used in the curve data set.
   *
   * @return The array: USDFEDFUND, EUREOINIA
   */
  public static IndexON[] indexONArray() {
    return new IndexON[] {USDFEDFUND, EUREONIA};
  }

  /**
   * Returns the array of calendars used in the curve data set.
   *
   * @return The array: NYC, TARGET
   */
  public static HolidayCalendar[] calendarArray() {
    return new HolidayCalendar[] {NYC, TARGET};
  }

  @SuppressWarnings("unchecked")
  private static Pair<MulticurveProviderDiscount, CurveBuildingBlockBundle>
      makeCurvesFromDefinitions(
          final InstrumentDefinition<?>[][][] definitions,
          final GeneratorYDCurve[][] curveGenerators,
          final String[][] curveNames,
          final MulticurveProviderDiscount knownData,
          final InstrumentDerivativeVisitor<ParameterProviderInterface, Double> calculator,
          final InstrumentDerivativeVisitor<ParameterProviderInterface, MulticurveSensitivity>
              sensitivityCalculator,
          final boolean withToday) {
    final int nUnits = definitions.length;
    final MultiCurveBundle<GeneratorYDCurve>[] curveBundles = new MultiCurveBundle[nUnits];
    for (int i = 0; i < nUnits; i++) {
      final int nCurves = definitions[i].length;
      final SingleCurveBundle<GeneratorYDCurve>[] singleCurves = new SingleCurveBundle[nCurves];
      for (int j = 0; j < nCurves; j++) {
        final int nInstruments = definitions[i][j].length;
        final InstrumentDerivative[] derivatives = new InstrumentDerivative[nInstruments];
        final double[] rates = new double[nInstruments];
        for (int k = 0; k < nInstruments; k++) {
          derivatives[k] = convert(definitions[i][j][k], withToday);
          rates[k] = initialGuess(definitions[i][j][k]);
        }
        final GeneratorYDCurve generator = curveGenerators[i][j].finalGenerator(derivatives);
        final double[] initialGuess = generator.initialGuess(rates);
        singleCurves[j] =
            new SingleCurveBundle<>(curveNames[i][j], derivatives, initialGuess, generator);
      }
      curveBundles[i] = new MultiCurveBundle<>(singleCurves);
    }
    return CURVE_BUILDING_REPOSITORY.makeCurvesFromDerivatives(
        curveBundles,
        knownData,
        DSC_MAP,
        FWD_IBOR_MAP,
        FWD_ON_MAP,
        calculator,
        sensitivityCalculator);
  }

  private static InstrumentDerivative convert(
      final InstrumentDefinition<?> instrument, final boolean withToday) {
    InstrumentDerivative ird;
    if (instrument instanceof SwapFixedONDefinition) {
      ird = ((SwapFixedONDefinition) instrument).toDerivative(NOW, getTSSwapFixedON(withToday));
    } else {
      ird = instrument.toDerivative(NOW);
    }
    return ird;
  }

  private static ZonedDateTimeDoubleTimeSeries[] getTSSwapFixedON(final Boolean withToday) {
    return withToday ? TS_FIXED_OIS_USD_WITH_TODAY : TS_FIXED_OIS_USD_WITHOUT_TODAY;
  }

  private static double initialGuess(final InstrumentDefinition<?> instrument) {
    if (instrument instanceof SwapFixedONDefinition) {
      return ((SwapFixedONDefinition) instrument).getFixedLeg().getNthPayment(0).getRate();
    }
    if (instrument instanceof SwapFixedIborDefinition) {
      return ((SwapFixedIborDefinition) instrument).getFixedLeg().getNthPayment(0).getRate();
    }
    if (instrument instanceof ForwardRateAgreementDefinition) {
      return ((ForwardRateAgreementDefinition) instrument).getRate();
    }
    if (instrument instanceof CashDefinition) {
      return ((CashDefinition) instrument).getRate();
    }
    if (instrument instanceof InterestRateFutureTransactionDefinition) {
      return 1 - ((InterestRateFutureTransactionDefinition) instrument).getTradePrice();
    }
    return 0.01;
  }
}
/**
 * Build of inflation curve and discount curve simultaneously in several blocks with relevant
 * Jacobian matrices.
 */
@Test(groups = TestGroup.UNIT)
public class InflationBuildingCurveWithDiscountTestUSD {

  private static final Interpolator1D INTERPOLATOR_LINEAR =
      CombinedInterpolatorExtrapolatorFactory.getInterpolator(
          Interpolator1DFactory.LOG_LINEAR,
          Interpolator1DFactory.FLAT_EXTRAPOLATOR,
          Interpolator1DFactory.FLAT_EXTRAPOLATOR);

  private static final LastTimeCalculator MATURITY_CALCULATOR = LastTimeCalculator.getInstance();
  private static final double TOLERANCE_ROOT = 1.0E-10;
  private static final int STEP_MAX = 100;

  private static final Currency USD = Currency.USD;
  private static final Calendar NYC = new MondayToFridayCalendar("NYC");
  private static final FXMatrix FX_MATRIX = new FXMatrix(USD);

  private static final double NOTIONAL = 1.0;

  private static final GeneratorSwapFixedON GENERATOR_OIS_USD =
      GeneratorSwapFixedONMaster.getInstance().getGenerator("USD1YFEDFUND", NYC);
  private static final IndexON INDEX_ON_USD = GENERATOR_OIS_USD.getIndex();
  private static final GeneratorDepositON GENERATOR_DEPOSIT_ON_USD =
      new GeneratorDepositON("USD Deposit ON", USD, NYC, INDEX_ON_USD.getDayCount());

  private static final GeneratorSwapFixedInflationZeroCoupon GENERATOR_INFALTION_SWAP =
      GeneratorSwapFixedInflationMaster.getInstance().getGenerator("USCPI");
  private static final IndexPrice US_CPI = GENERATOR_INFALTION_SWAP.getIndexPrice();

  private static final ZonedDateTime NOW = DateUtils.getUTCDate(2012, 9, 28);

  private static final ZonedDateTimeDoubleTimeSeries TS_PRICE_INDEX_USD_WITH_TODAY =
      ImmutableZonedDateTimeDoubleTimeSeries.ofUTC(
          new ZonedDateTime[] {
            DateUtils.getUTCDate(2011, 9, 27),
            DateUtils.getUTCDate(2011, 9, 28),
            DateUtils.getUTCDate(2012, 6, 30),
            DateUtils.getUTCDate(2012, 7, 31)
          },
          new double[] {200, 200, 200, 200});
  private static final String CURVE_NAME_DSC_USD = "USD Dsc";
  private static final String CURVE_NAME_CPI_USD = "USD CPI";

  /** Market values for the dsc USD curve */
  private static final double[] DSC_USD_MARKET_QUOTES =
      new double[] {
        0.0400, 0.0400, 0.0400, 0.0400, 0.0400, 0.0400, 0.0400, 0.0400, 0.0400, 0.0400, 0.0400,
        0.0400
      };
  /** Generators for the dsc USD curve */
  private static final GeneratorInstrument<? extends GeneratorAttribute>[] DSC_USD_GENERATORS =
      new GeneratorInstrument<?>[] {
        GENERATOR_DEPOSIT_ON_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD,
        GENERATOR_OIS_USD
      };
  /** Tenors for the dsc USD curve */
  private static final Period[] DSC_USD_TENOR =
      new Period[] {
        Period.ofDays(0),
        Period.ofMonths(1),
        Period.ofMonths(2),
        Period.ofMonths(3),
        Period.ofMonths(6),
        Period.ofMonths(9),
        Period.ofYears(1),
        Period.ofYears(2),
        Period.ofYears(3),
        Period.ofYears(4),
        Period.ofYears(5),
        Period.ofYears(10)
      };

  private static final GeneratorAttributeIR[] DSC_USD_ATTR =
      new GeneratorAttributeIR[DSC_USD_TENOR.length];

  static {
    for (int loopins = 0; loopins < DSC_USD_TENOR.length; loopins++) {
      DSC_USD_ATTR[loopins] = new GeneratorAttributeIR(DSC_USD_TENOR[loopins]);
    }
  }

  /** Market values for the CPI USD curve */
  public static final double[] CPI_USD_MARKET_QUOTES =
      new double[] {
        0.0200, 0.0200, 0.0200, 0.0200, 0.0200, 0.0200, 0.0200, 0.0200, 0.0200, 0.0200, 0.0200,
        0.0200, 0.0200, 0.0200, 0.0200
      };
  /** Generators for the CPI USD curve */
  public static final GeneratorInstrument<? extends GeneratorAttribute>[] CPI_USD_GENERATORS =
      new GeneratorInstrument<?>[] {
        GENERATOR_INFALTION_SWAP,
        GENERATOR_INFALTION_SWAP,
        GENERATOR_INFALTION_SWAP,
        GENERATOR_INFALTION_SWAP,
        GENERATOR_INFALTION_SWAP,
        GENERATOR_INFALTION_SWAP,
        GENERATOR_INFALTION_SWAP,
        GENERATOR_INFALTION_SWAP,
        GENERATOR_INFALTION_SWAP,
        GENERATOR_INFALTION_SWAP,
        GENERATOR_INFALTION_SWAP,
        GENERATOR_INFALTION_SWAP,
        GENERATOR_INFALTION_SWAP,
        GENERATOR_INFALTION_SWAP,
        GENERATOR_INFALTION_SWAP
      };
  /** Tenors for the CPI USD curve */
  public static final Period[] CPI_USD_TENOR =
      new Period[] {
        Period.ofYears(1),
        Period.ofYears(2),
        Period.ofYears(3),
        Period.ofYears(4),
        Period.ofYears(5),
        Period.ofYears(6),
        Period.ofYears(7),
        Period.ofYears(8),
        Period.ofYears(9),
        Period.ofYears(10),
        Period.ofYears(12),
        Period.ofYears(15),
        Period.ofYears(20),
        Period.ofYears(25),
        Period.ofYears(30)
      };

  public static final GeneratorAttributeIR[] CPI_USD_ATTR =
      new GeneratorAttributeIR[CPI_USD_TENOR.length];

  static {
    for (int loopins = 0; loopins < CPI_USD_TENOR.length; loopins++) {
      CPI_USD_ATTR[loopins] = new GeneratorAttributeIR(CPI_USD_TENOR[loopins]);
    }
  }

  /** Standard USD discounting curve instrument definitions */
  private static final InstrumentDefinition<?>[] DEFINITIONS_DSC_USD;

  /** Standard USD CPI curve instrument definitions */
  public static final InstrumentDefinition<?>[] DEFINITIONS_CPI_USD;

  /** Units of curves */
  public static final int[] NB_UNITS = new int[] {2, 1};

  public static final int NB_BLOCKS = NB_UNITS.length;
  public static final InstrumentDefinition<?>[][][][] DEFINITIONS_UNITS =
      new InstrumentDefinition<?>[NB_BLOCKS][][][];
  public static final GeneratorCurve[][][] GENERATORS_UNITS = new GeneratorCurve[NB_BLOCKS][][];
  public static final String[][][] NAMES_UNITS = new String[NB_BLOCKS][][];

  private static final InflationProviderDiscount KNOWN_DATA =
      new InflationProviderDiscount(FX_MATRIX);

  private static final LinkedHashMap<String, Currency> DSC_MAP = new LinkedHashMap<>();
  private static final LinkedHashMap<String, IndexON[]> FWD_ON_MAP = new LinkedHashMap<>();
  public static final LinkedHashMap<String, IndexPrice[]> US_CPI_MAP = new LinkedHashMap<>();

  static {
    DEFINITIONS_DSC_USD = getDefinitions(DSC_USD_MARKET_QUOTES, DSC_USD_GENERATORS, DSC_USD_ATTR);
    DEFINITIONS_CPI_USD = getDefinitions(CPI_USD_MARKET_QUOTES, CPI_USD_GENERATORS, CPI_USD_ATTR);

    for (int loopblock = 0; loopblock < NB_BLOCKS; loopblock++) {
      DEFINITIONS_UNITS[loopblock] = new InstrumentDefinition<?>[NB_UNITS[loopblock]][][];
      GENERATORS_UNITS[loopblock] = new GeneratorCurve[NB_UNITS[loopblock]][];
      NAMES_UNITS[loopblock] = new String[NB_UNITS[loopblock]][];
    }

    DEFINITIONS_UNITS[0][0] = new InstrumentDefinition<?>[][] {DEFINITIONS_DSC_USD};
    DEFINITIONS_UNITS[0][1] = new InstrumentDefinition<?>[][] {DEFINITIONS_CPI_USD};
    DEFINITIONS_UNITS[1][0] =
        new InstrumentDefinition<?>[][] {DEFINITIONS_DSC_USD, DEFINITIONS_CPI_USD};

    final GeneratorYDCurve genIntLinDiscount =
        new GeneratorCurveYieldInterpolated(MATURITY_CALCULATOR, INTERPOLATOR_LINEAR);
    final GeneratorPriceIndexCurve genIntLinInflation =
        new GeneratorPriceIndexCurveInterpolated(MATURITY_CALCULATOR, INTERPOLATOR_LINEAR);

    GENERATORS_UNITS[0][0] = new GeneratorYDCurve[] {genIntLinDiscount};
    GENERATORS_UNITS[0][1] = new GeneratorPriceIndexCurve[] {genIntLinInflation};
    GENERATORS_UNITS[1][0] = new GeneratorCurve[] {genIntLinDiscount, genIntLinInflation};

    NAMES_UNITS[0][0] = new String[] {CURVE_NAME_DSC_USD};
    NAMES_UNITS[0][1] = new String[] {CURVE_NAME_CPI_USD};
    NAMES_UNITS[1][0] = new String[] {CURVE_NAME_DSC_USD, CURVE_NAME_CPI_USD};

    DSC_MAP.put(CURVE_NAME_DSC_USD, USD);
    FWD_ON_MAP.put(CURVE_NAME_DSC_USD, new IndexON[] {INDEX_ON_USD});
    US_CPI_MAP.put(CURVE_NAME_CPI_USD, new IndexPrice[] {US_CPI});
  }

  @SuppressWarnings({"unchecked", "rawtypes"})
  public static InstrumentDefinition<?>[] getDefinitions(
      final double[] marketQuotes,
      final GeneratorInstrument[] generators,
      final GeneratorAttribute[] attribute) {
    final InstrumentDefinition<?>[] definitions = new InstrumentDefinition<?>[marketQuotes.length];
    for (int loopmv = 0; loopmv < marketQuotes.length; loopmv++) {
      definitions[loopmv] =
          generators[loopmv].generateInstrument(
              NOW, marketQuotes[loopmv], NOTIONAL, attribute[loopmv]);
    }
    return definitions;
  }

  private static List<Pair<InflationProviderDiscount, CurveBuildingBlockBundle>>
      CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK = new ArrayList<>();

  // Calculator
  private static final PresentValueDiscountingInflationCalculator PVIC =
      PresentValueDiscountingInflationCalculator.getInstance();
  private static final PresentValueCurveSensitivityDiscountingInflationCalculator PVCSDIC =
      PresentValueCurveSensitivityDiscountingInflationCalculator.getInstance();
  private static final ParSpreadInflationMarketQuoteDiscountingCalculator PSIMQC =
      ParSpreadInflationMarketQuoteDiscountingCalculator.getInstance();
  private static final ParSpreadInflationMarketQuoteCurveSensitivityDiscountingCalculator PSIMQCSC =
      ParSpreadInflationMarketQuoteCurveSensitivityDiscountingCalculator.getInstance();

  private static final InflationDiscountBuildingRepositoryWithDiscount CURVE_BUILDING_REPOSITORY =
      new InflationDiscountBuildingRepositoryWithDiscount(TOLERANCE_ROOT, TOLERANCE_ROOT, STEP_MAX);

  private static final double TOLERANCE_CAL = 1.0E-9;

  @BeforeSuite
  static void initClass() {
    for (int loopblock = 0; loopblock < NB_BLOCKS; loopblock++) {
      CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK.add(
          makeCurvesFromDefinitions(
              DEFINITIONS_UNITS[loopblock],
              GENERATORS_UNITS[loopblock],
              NAMES_UNITS[loopblock],
              KNOWN_DATA,
              PSIMQC,
              PSIMQCSC));
    }
  }

  public List<Pair<InflationProviderDiscount, CurveBuildingBlockBundle>> getCurvesWithBlock() {
    initClass();
    return CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK;
  }

  @Test(enabled = false)
  public void comparison1Unit2Units() {
    final InflationProviderDiscount[] units = new InflationProviderDiscount[2];
    final CurveBuildingBlockBundle[] bb = new CurveBuildingBlockBundle[2];
    final YieldAndDiscountCurve[] curveDsc = new YieldAndDiscountCurve[2];
    final PriceIndexCurve[] curveInflation = new PriceIndexCurve[2];

    for (int loopblock = 0; loopblock < 2; loopblock++) {
      units[loopblock] = CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK.get(loopblock).getFirst();
      bb[loopblock] = CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK.get(loopblock).getSecond();
      curveDsc[loopblock] = units[loopblock].getCurve(USD);
      curveInflation[loopblock] = units[loopblock].getCurve(US_CPI);
    }
    assertEquals(
        "Curve construction: 1 unit / 3 units ",
        curveDsc[0].getNumberOfParameters(),
        curveDsc[1].getNumberOfParameters());
    assertEquals(
        "Curve construction: 1 unit / 3 units ",
        curveInflation[0].getNumberOfParameters(),
        curveInflation[1].getNumberOfParameters());

    assertArrayEquals(
        "Curve construction: 1 unit / 3 units ",
        ArrayUtils.toPrimitive(((YieldCurve) curveDsc[0]).getCurve().getXData()),
        ArrayUtils.toPrimitive(((YieldCurve) curveDsc[1]).getCurve().getXData()),
        TOLERANCE_CAL);
    assertArrayEquals(
        "Curve construction: 1 unit / 3 units ",
        ArrayUtils.toPrimitive(((YieldCurve) curveDsc[0]).getCurve().getYData()),
        ArrayUtils.toPrimitive(((YieldCurve) curveDsc[1]).getCurve().getYData()),
        TOLERANCE_CAL);
    assertArrayEquals(
        "Curve construction: 1 unit / 3 units ",
        ArrayUtils.toPrimitive(((PriceIndexCurveSimple) curveInflation[0]).getCurve().getXData()),
        ArrayUtils.toPrimitive(((PriceIndexCurveSimple) curveInflation[1]).getCurve().getXData()),
        TOLERANCE_CAL);
    assertArrayEquals(
        "Curve construction: 1 unit / 3 units ",
        ArrayUtils.toPrimitive(((PriceIndexCurveSimple) curveInflation[0]).getCurve().getYData()),
        ArrayUtils.toPrimitive(((PriceIndexCurveSimple) curveInflation[1]).getCurve().getYData()),
        TOLERANCE_CAL);
  }

  @Test(enabled = false)
  public void performance() {
    long startTime, endTime;
    final int nbTest = 1000;

    startTime = System.currentTimeMillis();
    for (int looptest = 0; looptest < nbTest; looptest++) {
      makeCurvesFromDefinitions(
          DEFINITIONS_UNITS[0], GENERATORS_UNITS[0], NAMES_UNITS[0], KNOWN_DATA, PSIMQC, PSIMQCSC);
    }
    endTime = System.currentTimeMillis();
    System.out.println(
        "MulticurveBuildingDiscountingDiscountXCcyTest - "
            + nbTest
            + " curve construction / USD/EUR 3 units: "
            + (endTime - startTime)
            + " ms");
    // Performance note: curve construction Price index EUR and discount EUR 1 units: 27-Mar-13: On
    // Dell Precision T1850 3.5 GHz Quad-Core Intel Xeon: 5869 ms for 1000 sets.

    startTime = System.currentTimeMillis();
    for (int looptest = 0; looptest < nbTest; looptest++) {
      makeCurvesFromDefinitions(
          DEFINITIONS_UNITS[1], GENERATORS_UNITS[1], NAMES_UNITS[1], KNOWN_DATA, PSIMQC, PSIMQCSC);
    }
    endTime = System.currentTimeMillis();
    System.out.println(nbTest + " curve construction / 1 unit: " + (endTime - startTime) + " ms");
    // Performance note: curve construction Price index EUR and discount EUR 1 units: 27-Mar-13: On
    // Dell Precision T1850 3.5 GHz Quad-Core Intel Xeon: 9153 ms for 1000 sets.

  }

  @Test
  public void curveConstructionGeneratorOtherBlocks() {
    for (int loopblock = 0; loopblock < NB_BLOCKS; loopblock++) {
      curveConstructionTest(
          DEFINITIONS_UNITS[loopblock],
          CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK.get(loopblock).getFirst(),
          loopblock);
    }
  }

  @Test(enabled = false)
  /** Analyzes the shape of the forward curve. */
  public void marketQuoteSensitivityAnalysis() {

    final InflationProviderDiscount multicurves7 =
        CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK.get(1).getFirst();
    multicurves7.setAll(CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK.get(0).getFirst());
    final CurveBuildingBlockBundle blocks7 =
        CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK.get(1).getSecond();
    blocks7.addAll(CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK.get(0).getSecond());
    final double spreadJPYEUR = 0.0010; // 10bps
    final double notional = 100000;
    final GeneratorAttributeIR swapAttribute = new GeneratorAttributeIR(Period.ofYears(4));
    final SwapFixedInflationZeroCouponDefinition swapDefinition =
        GENERATOR_INFALTION_SWAP.generateInstrument(NOW, spreadJPYEUR, notional, swapAttribute);
    final InstrumentDerivative swap =
        swapDefinition.toDerivative(
            NOW,
            new ZonedDateTimeDoubleTimeSeries[] {
              TS_PRICE_INDEX_USD_WITH_TODAY, TS_PRICE_INDEX_USD_WITH_TODAY
            });
    final ParameterSensitivityInflationParameterCalculator<ParameterInflationProviderInterface>
        PSC = new ParameterSensitivityInflationParameterCalculator<>(PVCSDIC);
    final MarketQuoteInflationSensitivityBlockCalculator<ParameterInflationProviderInterface> MQSC =
        new MarketQuoteInflationSensitivityBlockCalculator<>(PSC);
    @SuppressWarnings("unused")
    final MultipleCurrencyParameterSensitivity mqs =
        MQSC.fromInstrument(swap, multicurves7, blocks7);
  }

  private void curveConstructionTest(
      final InstrumentDefinition<?>[][][] definitions,
      final InflationProviderDiscount curves,
      final int block) {
    final int nbBlocks = definitions.length;
    for (int loopblock = 0; loopblock < nbBlocks; loopblock++) {
      final InstrumentDerivative[][] instruments = convert(definitions[loopblock]);
      final double[][] pv = new double[instruments.length][];
      for (int loopcurve = 0; loopcurve < instruments.length; loopcurve++) {
        pv[loopcurve] = new double[instruments[loopcurve].length];
        for (int loopins = 0; loopins < instruments[loopcurve].length; loopins++) {
          pv[loopcurve][loopins] =
              curves
                  .getFxRates()
                  .convert(instruments[loopcurve][loopins].accept(PVIC, curves), USD)
                  .getAmount();
          assertEquals(
              "Curve construction: block "
                  + block
                  + ", unit "
                  + loopblock
                  + " - instrument "
                  + loopins,
              0,
              pv[loopcurve][loopins],
              TOLERANCE_CAL);
        }
      }
    }
  }

  @SuppressWarnings("unchecked")
  private static Pair<InflationProviderDiscount, CurveBuildingBlockBundle>
      makeCurvesFromDefinitions(
          final InstrumentDefinition<?>[][][] definitions,
          final GeneratorCurve[][] curveGenerators,
          final String[][] curveNames,
          final InflationProviderDiscount knownData,
          final InstrumentDerivativeVisitor<ParameterInflationProviderInterface, Double> calculator,
          final InstrumentDerivativeVisitor<
                  ParameterInflationProviderInterface, InflationSensitivity>
              sensitivityCalculator) {
    final int nUnits = definitions.length;
    final MultiCurveBundle<GeneratorCurve>[] curveBundles = new MultiCurveBundle[nUnits];
    for (int i = 0; i < nUnits; i++) {
      final int nCurves = definitions[i].length;
      final SingleCurveBundle<GeneratorCurve>[] singleCurves = new SingleCurveBundle[nCurves];
      for (int j = 0; j < nCurves; j++) {
        final int nInstruments = definitions[i][j].length;
        final InstrumentDerivative[] derivatives = new InstrumentDerivative[nInstruments];
        final double[] initialGuess = new double[nInstruments];
        for (int k = 0; k < nInstruments; k++) {
          derivatives[k] = convert(definitions[i][j][k]);
          initialGuess[k] = initialGuess(definitions[i][j][k]);
        }
        final GeneratorCurve generator = curveGenerators[i][j].finalGenerator(derivatives);
        singleCurves[j] =
            new SingleCurveBundle<>(curveNames[i][j], derivatives, initialGuess, generator);
      }
      curveBundles[i] = new MultiCurveBundle<>(singleCurves);
    }
    return CURVE_BUILDING_REPOSITORY.makeCurvesFromDerivatives(
        curveBundles,
        knownData,
        DSC_MAP,
        FWD_ON_MAP,
        US_CPI_MAP,
        calculator,
        sensitivityCalculator);
  }

  private static InstrumentDerivative[][] convert(final InstrumentDefinition<?>[][] definitions) {
    final InstrumentDerivative[][] instruments = new InstrumentDerivative[definitions.length][];
    for (int loopcurve = 0; loopcurve < definitions.length; loopcurve++) {
      instruments[loopcurve] = new InstrumentDerivative[definitions[loopcurve].length];
      int loopins = 0;
      for (final InstrumentDefinition<?> instrument : definitions[loopcurve]) {
        InstrumentDerivative ird;
        if (instrument instanceof SwapFixedInflationZeroCouponDefinition) {
          final Annuity<? extends Payment> ird1 =
              ((SwapFixedInflationZeroCouponDefinition) instrument).getFirstLeg().toDerivative(NOW);
          final Annuity<? extends Payment> ird2 =
              ((SwapFixedInflationZeroCouponDefinition) instrument)
                  .getSecondLeg()
                  .toDerivative(NOW, TS_PRICE_INDEX_USD_WITH_TODAY);
          ird = new Swap<>(ird1, ird2);
        } else {
          ird = instrument.toDerivative(NOW);
        }
        instruments[loopcurve][loopins++] = ird;
      }
    }
    return instruments;
  }

  private static InstrumentDerivative convert(final InstrumentDefinition<?> instrument) {
    InstrumentDerivative ird;
    if (instrument instanceof SwapFixedInflationZeroCouponDefinition) {
      final Annuity<? extends Payment> ird1 =
          ((SwapFixedInflationZeroCouponDefinition) instrument).getFirstLeg().toDerivative(NOW);
      final Annuity<? extends Payment> ird2 =
          ((SwapFixedInflationZeroCouponDefinition) instrument)
              .getSecondLeg()
              .toDerivative(NOW, TS_PRICE_INDEX_USD_WITH_TODAY);
      ird = new Swap<>(ird1, ird2);
    } else {
      ird = instrument.toDerivative(NOW);
    }
    return ird;
  }

  private static double initialGuess(final InstrumentDefinition<?> instrument) {
    if (instrument instanceof SwapFixedONDefinition) {
      return ((SwapFixedONDefinition) instrument).getFixedLeg().getNthPayment(0).getRate();
    }
    if (instrument instanceof SwapFixedIborDefinition) {
      return ((SwapFixedIborDefinition) instrument).getFixedLeg().getNthPayment(0).getRate();
    }
    if (instrument instanceof SwapFixedInflationZeroCouponDefinition) {

      if (((SwapFixedInflationZeroCouponDefinition) instrument).getFirstLeg().getNthPayment(0)
          instanceof CouponInflationZeroCouponMonthlyDefinition) {
        return 100.0;
      }
      if (((SwapFixedInflationZeroCouponDefinition) instrument).getFirstLeg().getNthPayment(0)
          instanceof CouponInflationZeroCouponInterpolationDefinition) {
        return 100.0;
      }
      return 100;
    }
    if (instrument instanceof ForwardRateAgreementDefinition) {
      return ((ForwardRateAgreementDefinition) instrument).getRate();
    }
    if (instrument instanceof CashDefinition) {
      return ((CashDefinition) instrument).getRate();
    }
    return 100;
  }
}
/** Build of curve in several blocks with relevant Jacobian matrices. */
public class MulticurveBuildingDiscountingDiscountAUDTest {

  private static final Interpolator1D INTERPOLATOR_LINEAR =
      CombinedInterpolatorExtrapolatorFactory.getInterpolator(
          Interpolator1DFactory.LINEAR,
          Interpolator1DFactory.FLAT_EXTRAPOLATOR,
          Interpolator1DFactory.FLAT_EXTRAPOLATOR);

  private static final LastTimeCalculator MATURITY_CALCULATOR = LastTimeCalculator.getInstance();
  private static final double TOLERANCE_ROOT = 1.0E-10;
  private static final int STEP_MAX = 100;

  private static final Calendar SYD = new MondayToFridayCalendar("SYD");
  private static final Currency AUD = Currency.AUD;
  private static final FXMatrix FX_MATRIX = new FXMatrix(AUD);

  private static final double NOTIONAL = 1.0;

  private static final GeneratorSwapFixedON GENERATOR_OIS_AUD =
      GeneratorSwapFixedONMaster.getInstance().getGenerator("AUD1YRBAON", SYD);
  private static final IndexON INDEX_ON_AUD = GENERATOR_OIS_AUD.getIndex();
  private static final GeneratorDepositON GENERATOR_DEPOSIT_ON_AUD =
      new GeneratorDepositON("AUD Deposit ON", AUD, SYD, INDEX_ON_AUD.getDayCount());
  private static final GeneratorSwapFixedIborMaster GENERATOR_SWAP_MASTER =
      GeneratorSwapFixedIborMaster.getInstance();
  private static final GeneratorSwapIborIborMaster GENERATOR_BASIS_MASTER =
      GeneratorSwapIborIborMaster.getInstance();
  private static final GeneratorSwapFixedIbor AUD3MBBSW3M =
      GENERATOR_SWAP_MASTER.getGenerator("AUD3MBBSW3M", SYD);
  private static final GeneratorSwapFixedIbor AUD6MBBSW6M =
      GENERATOR_SWAP_MASTER.getGenerator("AUD6MBBSW6M", SYD);
  private static final GeneratorSwapIborIbor AUDBBSW3MBBSW6M =
      GENERATOR_BASIS_MASTER.getGenerator("AUDBBSW3MBBSW6M", SYD);
  private static final IborIndex AUDBB3M = AUD3MBBSW3M.getIborIndex();
  private static final IborIndex AUDBB6M = AUD6MBBSW6M.getIborIndex();
  private static final GeneratorFRA GENERATOR_FRA_3M =
      new GeneratorFRA("GENERATOR_FRA_3M", AUDBB3M, SYD);
  private static final GeneratorDepositIbor GENERATOR_AUDBB3M =
      new GeneratorDepositIbor("GENERATOR_AUDBB3M", AUDBB3M, SYD);
  private static final GeneratorDepositIbor GENERATOR_AUDBB6M =
      new GeneratorDepositIbor("GENERATOR_AUDBB6M", AUDBB6M, SYD);

  private static final ZonedDateTime NOW = DateUtils.getUTCDate(2011, 9, 28);

  private static final ZonedDateTimeDoubleTimeSeries TS_EMPTY =
      ImmutableZonedDateTimeDoubleTimeSeries.ofEmptyUTC();
  private static final ZonedDateTimeDoubleTimeSeries TS_ON_AUD_WITH_TODAY =
      ImmutableZonedDateTimeDoubleTimeSeries.ofUTC(
          new ZonedDateTime[] {
            DateUtils.getUTCDate(2011, 9, 27), DateUtils.getUTCDate(2011, 9, 28)
          },
          new double[] {0.07, 0.08});
  private static final ZonedDateTimeDoubleTimeSeries TS_ON_AUD_WITHOUT_TODAY =
      ImmutableZonedDateTimeDoubleTimeSeries.ofUTC(
          new ZonedDateTime[] {
            DateUtils.getUTCDate(2011, 9, 27), DateUtils.getUTCDate(2011, 9, 28)
          },
          new double[] {0.07, 0.08});
  private static final ZonedDateTimeDoubleTimeSeries[] TS_FIXED_OIS_AUD_WITH_TODAY =
      new ZonedDateTimeDoubleTimeSeries[] {TS_EMPTY, TS_ON_AUD_WITH_TODAY};
  private static final ZonedDateTimeDoubleTimeSeries[] TS_FIXED_OIS_AUD_WITHOUT_TODAY =
      new ZonedDateTimeDoubleTimeSeries[] {TS_EMPTY, TS_ON_AUD_WITHOUT_TODAY};

  private static final ZonedDateTimeDoubleTimeSeries TS_IBOR_AUD3M_WITH_TODAY =
      ImmutableZonedDateTimeDoubleTimeSeries.ofUTC(
          new ZonedDateTime[] {
            DateUtils.getUTCDate(2011, 9, 27), DateUtils.getUTCDate(2011, 9, 28)
          },
          new double[] {0.0035, 0.0036});
  private static final ZonedDateTimeDoubleTimeSeries TS_IBOR_AUD3M_WITHOUT_TODAY =
      ImmutableZonedDateTimeDoubleTimeSeries.ofUTC(
          new ZonedDateTime[] {DateUtils.getUTCDate(2011, 9, 27)}, new double[] {0.0035});
  private static final ZonedDateTimeDoubleTimeSeries TS_IBOR_AUD6M_WITH_TODAY =
      ImmutableZonedDateTimeDoubleTimeSeries.ofUTC(
          new ZonedDateTime[] {
            DateUtils.getUTCDate(2011, 9, 27), DateUtils.getUTCDate(2011, 9, 28)
          },
          new double[] {0.0035, 0.0036});
  private static final ZonedDateTimeDoubleTimeSeries TS_IBOR_AUD6M_WITHOUT_TODAY =
      ImmutableZonedDateTimeDoubleTimeSeries.ofUTC(
          new ZonedDateTime[] {DateUtils.getUTCDate(2011, 9, 27)}, new double[] {0.0035});

  private static final ZonedDateTimeDoubleTimeSeries[] TS_FIXED_IBOR_AUD3M_WITH_TODAY =
      new ZonedDateTimeDoubleTimeSeries[] {TS_IBOR_AUD3M_WITH_TODAY};
  private static final ZonedDateTimeDoubleTimeSeries[] TS_FIXED_IBOR_AUD3M_WITHOUT_TODAY =
      new ZonedDateTimeDoubleTimeSeries[] {TS_IBOR_AUD3M_WITHOUT_TODAY};
  private static final ZonedDateTimeDoubleTimeSeries[] TS_FIXED_IBOR_AUD3M6M_WITH_TODAY =
      new ZonedDateTimeDoubleTimeSeries[] {TS_IBOR_AUD3M_WITH_TODAY, TS_IBOR_AUD6M_WITH_TODAY};
  private static final ZonedDateTimeDoubleTimeSeries[] TS_FIXED_IBOR_AUD3M6M_WITHOUT_TODAY =
      new ZonedDateTimeDoubleTimeSeries[] {
        TS_IBOR_AUD3M_WITHOUT_TODAY, TS_IBOR_AUD6M_WITHOUT_TODAY
      };

  private static final String CURVE_NAME_DSC_AUD = "AUD Dsc";
  private static final String CURVE_NAME_FWD3_AUD = "AUD Fwd 3M";
  private static final String CURVE_NAME_FWD6_AUD = "AUD Fwd 6M";

  //  /** Simplified versions for the note */
  //  /** Market values for the dsc USD curve */
  //  private static final double[] DSC_AUD_MARKET_QUOTES = new double[] {0.0400, 0.0400, 0.0400,
  // 0.0400, 0.0400};
  //  /** Generators for the dsc USD curve */
  //  private static final GeneratorInstrument[] DSC_USD_GENERATORS = new GeneratorInstrument[]
  // {GENERATOR_DEPOSIT_ON_AUD, GENERATOR_OIS_AUD, GENERATOR_OIS_AUD, GENERATOR_OIS_AUD,
  // GENERATOR_OIS_AUD};
  //  /** Tenors for the dsc USD curve */
  //  private static final Period[] DSC_AUD_TENOR = new Period[] {Period.ofDays(0),
  // Period.ofMonths(1), Period.ofMonths(6), Period.ofYears(1), Period.ofYears(5)};
  //
  //  /** Market values for the Fwd 3M USD curve */
  //  private static final double[] FWD3_AUD_MARKET_QUOTES = new double[] {0.0420, 0.0420, 0.0470,
  // 0.0020};
  //  /** Generators for the Fwd 3M USD curve */
  //  private static final GeneratorInstrument[] FWD3_AUD_GENERATORS = new GeneratorInstrument[]
  // {GENERATOR_AUDBB3M, GENERATOR_FRA_3M, AUD3MBBSW3M, AUDBBSW3MBBSW6M};
  //  /** Tenors for the Fwd 3M USD curve */
  //  private static final Period[] FWD3_AUD_TENOR = new Period[] {Period.ofMonths(0),
  // Period.ofMonths(6), Period.ofYears(1), Period.ofYears(5)};
  //
  //  /** Market values for the Fwd 3M USD curve */
  //  private static final double[] FWD6_AUD_MARKET_QUOTES = new double[] {0.0440, 0.0020, 0.0560};
  //  /** Generators for the Fwd 3M USD curve */
  //  private static final GeneratorInstrument[] FWD6_AUD_GENERATORS = new GeneratorInstrument[]
  // {GENERATOR_AUDBB6M, AUDBBSW3MBBSW6M, AUD6MBBSW6M};
  //  /** Tenors for the Fwd 3M USD curve */
  //  private static final Period[] FWD6_AUD_TENOR = new Period[] {Period.ofMonths(0),
  // Period.ofYears(1), Period.ofYears(5)};

  /** Market values for the dsc USD curve */
  private static final double[] DSC_AUD_MARKET_QUOTES =
      new double[] {
        0.0400, 0.0400, 0.0400, 0.0400, 0.0400, 0.0400, 0.0400, 0.0400, 0.0400, 0.0400, 0.0400,
        0.0400
      };
  /** Generators for the dsc USD curve */
  private static final GeneratorInstrument<? extends GeneratorAttribute>[] DSC_USD_GENERATORS =
      new GeneratorInstrument<?>[] {
        GENERATOR_DEPOSIT_ON_AUD,
        GENERATOR_OIS_AUD,
        GENERATOR_OIS_AUD,
        GENERATOR_OIS_AUD,
        GENERATOR_OIS_AUD,
        GENERATOR_OIS_AUD,
        GENERATOR_OIS_AUD,
        GENERATOR_OIS_AUD,
        GENERATOR_OIS_AUD,
        GENERATOR_OIS_AUD,
        GENERATOR_OIS_AUD,
        GENERATOR_OIS_AUD
      };
  /** Tenors for the dsc USD curve */
  private static final Period[] DSC_AUD_TENOR =
      new Period[] {
        Period.ofDays(0),
        Period.ofMonths(1),
        Period.ofMonths(2),
        Period.ofMonths(3),
        Period.ofMonths(6),
        Period.ofMonths(9),
        Period.ofYears(1),
        Period.ofYears(2),
        Period.ofYears(3),
        Period.ofYears(4),
        Period.ofYears(5),
        Period.ofYears(10)
      };

  private static final GeneratorAttributeIR[] DSC_AUD_ATTR =
      new GeneratorAttributeIR[DSC_AUD_TENOR.length];

  static {
    for (int loopins = 0; loopins < DSC_AUD_TENOR.length; loopins++) {
      DSC_AUD_ATTR[loopins] = new GeneratorAttributeIR(DSC_AUD_TENOR[loopins]);
    }
  }

  /** Market values for the Fwd 3M USD curve */
  private static final double[] FWD3_AUD_MARKET_QUOTES =
      new double[] {0.0420, 0.0420, 0.0420, 0.0420, 0.0430, 0.0470, 0.0020, 0.0020, 0.0020};
  /** Generators for the Fwd 3M USD curve */
  private static final GeneratorInstrument<? extends GeneratorAttribute>[] FWD3_AUD_GENERATORS =
      new GeneratorInstrument<?>[] {
        GENERATOR_AUDBB3M,
        GENERATOR_FRA_3M,
        GENERATOR_FRA_3M,
        AUD3MBBSW3M,
        AUD3MBBSW3M,
        AUD3MBBSW3M,
        AUDBBSW3MBBSW6M,
        AUDBBSW3MBBSW6M,
        AUDBBSW3MBBSW6M
      };
  /** Tenors for the Fwd 3M USD curve */
  private static final Period[] FWD3_AUD_TENOR =
      new Period[] {
        Period.ofMonths(0),
        Period.ofMonths(6),
        Period.ofMonths(9),
        Period.ofYears(1),
        Period.ofYears(2),
        Period.ofYears(3),
        Period.ofYears(5),
        Period.ofYears(7),
        Period.ofYears(10)
      };

  private static final GeneratorAttributeIR[] FWD3_AUD_ATTR =
      new GeneratorAttributeIR[FWD3_AUD_TENOR.length];

  static {
    for (int loopins = 0; loopins < FWD3_AUD_TENOR.length; loopins++) {
      FWD3_AUD_ATTR[loopins] = new GeneratorAttributeIR(FWD3_AUD_TENOR[loopins]);
    }
  }

  /** Market values for the Fwd 3M USD curve */
  private static final double[] FWD6_AUD_MARKET_QUOTES =
      new double[] {0.0440, 0.0020, 0.0020, 0.0020, 0.0560, 0.0610, 0.0620};
  /** Generators for the Fwd 3M USD curve */
  private static final GeneratorInstrument<? extends GeneratorAttribute>[] FWD6_AUD_GENERATORS =
      new GeneratorInstrument<?>[] {
        GENERATOR_AUDBB6M,
        AUDBBSW3MBBSW6M,
        AUDBBSW3MBBSW6M,
        AUDBBSW3MBBSW6M,
        AUD6MBBSW6M,
        AUD6MBBSW6M,
        AUD6MBBSW6M
      };
  /** Tenors for the Fwd 3M USD curve */
  private static final Period[] FWD6_AUD_TENOR =
      new Period[] {
        Period.ofMonths(0),
        Period.ofYears(1),
        Period.ofYears(2),
        Period.ofYears(3),
        Period.ofYears(5),
        Period.ofYears(7),
        Period.ofYears(10)
      };

  private static final GeneratorAttributeIR[] FWD6_AUD_ATTR =
      new GeneratorAttributeIR[FWD6_AUD_TENOR.length];

  static {
    for (int loopins = 0; loopins < FWD6_AUD_TENOR.length; loopins++) {
      FWD6_AUD_ATTR[loopins] = new GeneratorAttributeIR(FWD6_AUD_TENOR[loopins]);
    }
  }

  /** Standard USD discounting curve instrument definitions */
  private static final InstrumentDefinition<?>[] DEFINITIONS_DSC_AUD;
  /** Standard USD Forward 3M curve instrument definitions */
  private static final InstrumentDefinition<?>[] DEFINITIONS_FWD3_AUD;
  /** Standard USD Forward 3M curve instrument definitions */
  private static final InstrumentDefinition<?>[] DEFINITIONS_FWD6_AUD;

  /** Units of curves */
  private static final int[] NB_UNITS = new int[] {2, 1};

  private static final int NB_BLOCKS = NB_UNITS.length;
  private static final InstrumentDefinition<?>[][][][] DEFINITIONS_UNITS =
      new InstrumentDefinition<?>[NB_BLOCKS][][][];
  private static final GeneratorYDCurve[][][] GENERATORS_UNITS =
      new GeneratorYDCurve[NB_BLOCKS][][];
  private static final String[][][] NAMES_UNITS = new String[NB_BLOCKS][][];
  private static final MulticurveProviderDiscount KNOWN_DATA =
      new MulticurveProviderDiscount(FX_MATRIX);
  private static final LinkedHashMap<String, Currency> DSC_MAP = new LinkedHashMap<>();
  private static final LinkedHashMap<String, IndexON[]> FWD_ON_MAP = new LinkedHashMap<>();
  private static final LinkedHashMap<String, IborIndex[]> FWD_IBOR_MAP = new LinkedHashMap<>();

  static {
    DEFINITIONS_DSC_AUD = getDefinitions(DSC_AUD_MARKET_QUOTES, DSC_USD_GENERATORS, DSC_AUD_ATTR);
    DEFINITIONS_FWD3_AUD =
        getDefinitions(FWD3_AUD_MARKET_QUOTES, FWD3_AUD_GENERATORS, FWD3_AUD_ATTR);
    DEFINITIONS_FWD6_AUD =
        getDefinitions(FWD6_AUD_MARKET_QUOTES, FWD6_AUD_GENERATORS, FWD6_AUD_ATTR);
    for (int loopblock = 0; loopblock < NB_BLOCKS; loopblock++) {
      DEFINITIONS_UNITS[loopblock] = new InstrumentDefinition<?>[NB_UNITS[loopblock]][][];
      GENERATORS_UNITS[loopblock] = new GeneratorYDCurve[NB_UNITS[loopblock]][];
      NAMES_UNITS[loopblock] = new String[NB_UNITS[loopblock]][];
    }
    DEFINITIONS_UNITS[0][0] = new InstrumentDefinition<?>[][] {DEFINITIONS_DSC_AUD};
    DEFINITIONS_UNITS[0][1] =
        new InstrumentDefinition<?>[][] {DEFINITIONS_FWD3_AUD, DEFINITIONS_FWD6_AUD};
    DEFINITIONS_UNITS[1][0] =
        new InstrumentDefinition<?>[][] {
          DEFINITIONS_DSC_AUD, DEFINITIONS_FWD3_AUD, DEFINITIONS_FWD6_AUD
        };
    final GeneratorYDCurve genIntLin =
        new GeneratorCurveYieldInterpolated(MATURITY_CALCULATOR, INTERPOLATOR_LINEAR);
    GENERATORS_UNITS[0][0] = new GeneratorYDCurve[] {genIntLin};
    GENERATORS_UNITS[0][1] = new GeneratorYDCurve[] {genIntLin, genIntLin};
    GENERATORS_UNITS[1][0] = new GeneratorYDCurve[] {genIntLin, genIntLin, genIntLin};
    NAMES_UNITS[0][0] = new String[] {CURVE_NAME_DSC_AUD};
    NAMES_UNITS[0][1] = new String[] {CURVE_NAME_FWD3_AUD, CURVE_NAME_FWD6_AUD};
    NAMES_UNITS[1][0] = new String[] {CURVE_NAME_DSC_AUD, CURVE_NAME_FWD3_AUD, CURVE_NAME_FWD6_AUD};
    DSC_MAP.put(CURVE_NAME_DSC_AUD, AUD);
    FWD_ON_MAP.put(CURVE_NAME_DSC_AUD, new IndexON[] {INDEX_ON_AUD});
    FWD_IBOR_MAP.put(CURVE_NAME_FWD3_AUD, new IborIndex[] {AUDBB3M});
    FWD_IBOR_MAP.put(CURVE_NAME_FWD6_AUD, new IborIndex[] {AUDBB6M});
  }

  @SuppressWarnings({"rawtypes", "unchecked"})
  public static InstrumentDefinition<?>[] getDefinitions(
      final double[] marketQuotes,
      final GeneratorInstrument[] generators,
      final GeneratorAttribute[] attribute) {
    final InstrumentDefinition<?>[] definitions = new InstrumentDefinition<?>[marketQuotes.length];
    for (int loopmv = 0; loopmv < marketQuotes.length; loopmv++) {
      definitions[loopmv] =
          generators[loopmv].generateInstrument(
              NOW, marketQuotes[loopmv], NOTIONAL, attribute[loopmv]);
    }
    return definitions;
  }

  private static List<Pair<MulticurveProviderDiscount, CurveBuildingBlockBundle>>
      CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK = new ArrayList<>();

  // Calculator
  private static final PresentValueDiscountingCalculator PVC =
      PresentValueDiscountingCalculator.getInstance();
  private static final ParSpreadMarketQuoteDiscountingCalculator PSMQC =
      ParSpreadMarketQuoteDiscountingCalculator.getInstance();
  private static final ParSpreadMarketQuoteCurveSensitivityDiscountingCalculator PSMQCSC =
      ParSpreadMarketQuoteCurveSensitivityDiscountingCalculator.getInstance();

  private static final MulticurveDiscountBuildingRepository CURVE_BUILDING_REPOSITORY =
      new MulticurveDiscountBuildingRepository(TOLERANCE_ROOT, TOLERANCE_ROOT, STEP_MAX);

  private static final double TOLERANCE_CAL = 1.0E-9;

  @BeforeSuite
  static void initClass() {
    for (int loopblock = 0; loopblock < NB_BLOCKS; loopblock++) {
      CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK.add(
          makeCurvesFromDefinitions(
              DEFINITIONS_UNITS[loopblock],
              GENERATORS_UNITS[loopblock],
              NAMES_UNITS[loopblock],
              KNOWN_DATA,
              PSMQC,
              PSMQCSC,
              false));
    }
  }

  @Test
  public void curveConstruction() {
    for (int loopblock = 0; loopblock < NB_BLOCKS; loopblock++) {
      curveConstructionTest(
          DEFINITIONS_UNITS[loopblock],
          CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK.get(loopblock).getFirst(),
          false,
          loopblock);
    }
  }

  @Test
  public void comparison1Unit2Units() {
    final MulticurveProviderDiscount[] units = new MulticurveProviderDiscount[2];
    final CurveBuildingBlockBundle[] bb = new CurveBuildingBlockBundle[2];
    final YieldAndDiscountCurve[] curveDsc = new YieldAndDiscountCurve[2];
    final YieldAndDiscountCurve[] curveFwd = new YieldAndDiscountCurve[2];
    for (int loopblock = 0; loopblock < 2; loopblock++) {
      units[loopblock] = CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK.get(loopblock).getFirst();
      bb[loopblock] = CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK.get(loopblock).getSecond();
      curveDsc[loopblock] = units[loopblock].getCurve(AUD);
      curveFwd[loopblock] = units[loopblock].getCurve(AUDBB3M);
    }
    assertEquals(
        "Curve construction: 1 unit / 2 units ",
        curveDsc[0].getNumberOfParameters(),
        curveDsc[1].getNumberOfParameters());
    assertEquals(
        "Curve construction: 1 unit / 2 units ",
        curveFwd[0].getNumberOfParameters(),
        curveFwd[1].getNumberOfParameters());
    assertArrayEquals(
        "Curve construction: 1 unit / 2 units ",
        ArrayUtils.toPrimitive(((YieldCurve) curveDsc[0]).getCurve().getXData()),
        ArrayUtils.toPrimitive(((YieldCurve) curveDsc[1]).getCurve().getXData()),
        TOLERANCE_CAL);
    assertArrayEquals(
        "Curve construction: 1 unit / 2 units ",
        ArrayUtils.toPrimitive(((YieldCurve) curveDsc[0]).getCurve().getYData()),
        ArrayUtils.toPrimitive(((YieldCurve) curveDsc[1]).getCurve().getYData()),
        TOLERANCE_CAL);
    assertArrayEquals(
        "Curve construction: 1 unit / 2 units ",
        ArrayUtils.toPrimitive(((YieldCurve) curveFwd[0]).getCurve().getXData()),
        ArrayUtils.toPrimitive(((YieldCurve) curveFwd[1]).getCurve().getXData()),
        TOLERANCE_CAL);
    assertArrayEquals(
        "Curve construction: 1 unit / 2 units ",
        ArrayUtils.toPrimitive(((YieldCurve) curveFwd[0]).getCurve().getYData()),
        ArrayUtils.toPrimitive(((YieldCurve) curveFwd[1]).getCurve().getYData()),
        TOLERANCE_CAL);

    assertEquals(
        "Curve construction: 1 unit / 2 units ",
        bb[0].getBlock(CURVE_NAME_FWD3_AUD).getFirst(),
        bb[1].getBlock(CURVE_NAME_FWD3_AUD).getFirst());
    // Test note: the discounting curve building blocks are not the same; in one case both curves
    // are build together in the other one after the other.
    final int nbLineDsc = bb[0].getBlock(CURVE_NAME_DSC_AUD).getSecond().getNumberOfRows();
    final int nbLineFwd3 = bb[0].getBlock(CURVE_NAME_FWD3_AUD).getSecond().getNumberOfRows();
    final int nbLineFwd6 = bb[0].getBlock(CURVE_NAME_FWD6_AUD).getSecond().getNumberOfRows();
    assertEquals(
        "Curve construction: 1 unit / 2 units ",
        bb[1].getBlock(CURVE_NAME_DSC_AUD).getSecond().getNumberOfRows(),
        nbLineDsc);
    assertEquals(
        "Curve construction: 1 unit / 2 units ",
        bb[1].getBlock(CURVE_NAME_FWD3_AUD).getSecond().getNumberOfRows(),
        nbLineFwd3);
    assertEquals(
        "Curve construction: 1 unit / 2 units ",
        bb[1].getBlock(CURVE_NAME_FWD6_AUD).getSecond().getNumberOfRows(),
        nbLineFwd6);
  }

  // TODO: test on the correctness of the Jacobian matrix in the CurveBuildingBlock's.

  @Test(enabled = false)
  public void performance() {
    long startTime, endTime;
    final int nbTest = 100;

    startTime = System.currentTimeMillis();
    for (int looptest = 0; looptest < nbTest; looptest++) {
      makeCurvesFromDefinitions(
          DEFINITIONS_UNITS[0],
          GENERATORS_UNITS[0],
          NAMES_UNITS[0],
          KNOWN_DATA,
          PSMQC,
          PSMQCSC,
          false);
    }
    endTime = System.currentTimeMillis();
    System.out.println(
        nbTest + " x 3 curves construction / 2 units: " + (endTime - startTime) + " ms");
    // Performance note: Curve construction 2 units: 08-Nov-12: On Mac Pro 3.2 GHz Quad-Core Intel
    // Xeon: 810 ms for 100 sets.

    startTime = System.currentTimeMillis();
    for (int looptest = 0; looptest < nbTest; looptest++) {
      makeCurvesFromDefinitions(
          DEFINITIONS_UNITS[1],
          GENERATORS_UNITS[1],
          NAMES_UNITS[1],
          KNOWN_DATA,
          PSMQC,
          PSMQCSC,
          false);
    }
    endTime = System.currentTimeMillis();
    System.out.println(
        nbTest + " x 3 curves construction / 1 unit: " + (endTime - startTime) + " ms");
    // Performance note: Curve construction 1 unit: 08-Nov-12: On Mac Pro 3.2 GHz Quad-Core Intel
    // Xeon: 995 ms for 100 sets.

  }

  public void curveConstructionTest(
      final InstrumentDefinition<?>[][][] definitions,
      final MulticurveProviderDiscount curves,
      final boolean withToday,
      final int block) {
    final int nbBlocks = definitions.length;
    for (int loopblock = 0; loopblock < nbBlocks; loopblock++) {
      final InstrumentDerivative[][] instruments = convert(definitions[loopblock], withToday);
      final double[][] pv = new double[instruments.length][];
      for (int loopcurve = 0; loopcurve < instruments.length; loopcurve++) {
        pv[loopcurve] = new double[instruments[loopcurve].length];
        for (int loopins = 0; loopins < instruments[loopcurve].length; loopins++) {
          pv[loopcurve][loopins] =
              curves
                  .getFxRates()
                  .convert(instruments[loopcurve][loopins].accept(PVC, curves), AUD)
                  .getAmount();
          assertEquals(
              "Curve construction: block "
                  + block
                  + ", unit "
                  + loopblock
                  + " - instrument "
                  + loopins,
              0,
              pv[loopcurve][loopins],
              TOLERANCE_CAL);
        }
      }
    }
  }

  @Test(enabled = false)
  /** Analyzes the shape of the forward curve. */
  public void forwardAnalysis() {
    final MulticurveProviderInterface marketDsc =
        CURVES_PAR_SPREAD_MQ_WITHOUT_TODAY_BLOCK.get(0).getFirst();
    final int jump = 1;
    final int startIndex = 0;
    final int nbDate = 2750;
    ZonedDateTime startDate =
        ScheduleCalculator.getAdjustedDate(NOW, AUDBB3M.getSpotLag() + startIndex * jump, SYD);
    final double[] rateDsc = new double[nbDate];
    final double[] startTime = new double[nbDate];
    try {
      final FileWriter writer = new FileWriter("fwd-dsc.csv");
      for (int loopdate = 0; loopdate < nbDate; loopdate++) {
        startTime[loopdate] = TimeCalculator.getTimeBetween(NOW, startDate);
        final ZonedDateTime endDate = ScheduleCalculator.getAdjustedDate(startDate, AUDBB3M, SYD);
        final double endTime = TimeCalculator.getTimeBetween(NOW, endDate);
        final double accrualFactor = AUDBB3M.getDayCount().getDayCountFraction(startDate, endDate);
        rateDsc[loopdate] =
            marketDsc.getForwardRate(AUDBB3M, startTime[loopdate], endTime, accrualFactor);
        startDate = ScheduleCalculator.getAdjustedDate(startDate, jump, SYD);
        writer.append(0.0 + "," + startTime[loopdate] + "," + rateDsc[loopdate] + "\n");
      }
      writer.flush();
      writer.close();
    } catch (final IOException e) {
      e.printStackTrace();
    }
  }

  @SuppressWarnings("unchecked")
  private static Pair<MulticurveProviderDiscount, CurveBuildingBlockBundle>
      makeCurvesFromDefinitions(
          final InstrumentDefinition<?>[][][] definitions,
          final GeneratorYDCurve[][] curveGenerators,
          final String[][] curveNames,
          final MulticurveProviderDiscount knownData,
          final InstrumentDerivativeVisitor<MulticurveProviderInterface, Double> calculator,
          final InstrumentDerivativeVisitor<MulticurveProviderInterface, MulticurveSensitivity>
              sensitivityCalculator,
          final boolean withToday) {
    final int nUnits = curveGenerators.length;
    final MultiCurveBundle<GeneratorYDCurve>[] curveBundles = new MultiCurveBundle[nUnits];
    for (int i = 0; i < nUnits; i++) {
      final int nCurves = definitions[i].length;
      final SingleCurveBundle<GeneratorYDCurve>[] singleCurves = new SingleCurveBundle[nCurves];
      for (int j = 0; j < nCurves; j++) {
        final int nInstruments = definitions[i][j].length;
        final InstrumentDerivative[] derivatives = new InstrumentDerivative[nInstruments];
        final double[] initialGuess = new double[nInstruments];
        for (int k = 0; k < nInstruments; k++) {
          derivatives[k] = convert(definitions[i][j][k], withToday);
          initialGuess[k] = initialGuess(definitions[i][j][k]);
        }
        final GeneratorYDCurve generator = curveGenerators[i][j].finalGenerator(derivatives);
        singleCurves[j] =
            new SingleCurveBundle<>(curveNames[i][j], derivatives, initialGuess, generator);
      }
      curveBundles[i] = new MultiCurveBundle<>(singleCurves);
    }
    return CURVE_BUILDING_REPOSITORY.makeCurvesFromDerivatives(
        curveBundles,
        knownData,
        DSC_MAP,
        FWD_IBOR_MAP,
        FWD_ON_MAP,
        calculator,
        sensitivityCalculator);
  }

  private static InstrumentDerivative[][] convert(
      final InstrumentDefinition<?>[][] definitions, final boolean withToday) {
    final InstrumentDerivative[][] instruments = new InstrumentDerivative[definitions.length][];
    for (int loopcurve = 0; loopcurve < definitions.length; loopcurve++) {
      instruments[loopcurve] = new InstrumentDerivative[definitions[loopcurve].length];
      int loopins = 0;
      for (final InstrumentDefinition<?> instrument : definitions[loopcurve]) {
        InstrumentDerivative ird;
        if (instrument instanceof SwapFixedONDefinition) {
          ird = ((SwapFixedONDefinition) instrument).toDerivative(NOW, getTSSwapFixedON(withToday));
        } else {
          if (instrument instanceof SwapFixedIborDefinition) {
            ird =
                ((SwapFixedIborDefinition) instrument)
                    .toDerivative(NOW, getTSSwapFixedIbor(withToday));
          } else {
            if (instrument instanceof SwapIborIborDefinition) {
              ird =
                  ((SwapIborIborDefinition) instrument)
                      .toDerivative(NOW, getTSSwapIborIbor(withToday));
            } else {
              ird = instrument.toDerivative(NOW);
            }
          }
        }
        instruments[loopcurve][loopins++] = ird;
      }
    }
    return instruments;
  }

  private static InstrumentDerivative convert(
      final InstrumentDefinition<?> instrument, final boolean withToday) {
    InstrumentDerivative ird;
    if (instrument instanceof SwapFixedONDefinition) {
      ird = ((SwapFixedONDefinition) instrument).toDerivative(NOW, getTSSwapFixedON(withToday));
    } else {
      if (instrument instanceof SwapFixedIborDefinition) {
        ird =
            ((SwapFixedIborDefinition) instrument).toDerivative(NOW, getTSSwapFixedIbor(withToday));
      } else {
        if (instrument instanceof SwapIborIborDefinition) {
          ird =
              ((SwapIborIborDefinition) instrument).toDerivative(NOW, getTSSwapIborIbor(withToday));
        } else {
          ird = instrument.toDerivative(NOW);
        }
      }
    }
    return ird;
  }

  private static ZonedDateTimeDoubleTimeSeries[] getTSSwapFixedON(final Boolean withToday) {
    return withToday ? TS_FIXED_OIS_AUD_WITH_TODAY : TS_FIXED_OIS_AUD_WITHOUT_TODAY;
  }

  private static ZonedDateTimeDoubleTimeSeries[] getTSSwapFixedIbor(final Boolean withToday) {
    return withToday
        ? TS_FIXED_IBOR_AUD3M_WITH_TODAY
        : TS_FIXED_IBOR_AUD3M_WITHOUT_TODAY; // TODO: get the correct fixing
  }

  private static ZonedDateTimeDoubleTimeSeries[] getTSSwapIborIbor(final Boolean withToday) {
    return withToday ? TS_FIXED_IBOR_AUD3M6M_WITH_TODAY : TS_FIXED_IBOR_AUD3M6M_WITHOUT_TODAY;
  }

  private static double initialGuess(final InstrumentDefinition<?> instrument) {
    if (instrument instanceof SwapFixedONDefinition) {
      return ((SwapFixedONDefinition) instrument).getFixedLeg().getNthPayment(0).getRate();
    }
    if (instrument instanceof SwapFixedIborDefinition) {
      return ((SwapFixedIborDefinition) instrument).getFixedLeg().getNthPayment(0).getRate();
    }
    if (instrument instanceof ForwardRateAgreementDefinition) {
      return ((ForwardRateAgreementDefinition) instrument).getRate();
    }
    if (instrument instanceof CashDefinition) {
      return ((CashDefinition) instrument).getRate();
    } // TODO: What about basis swaps?
    return 0.01;
  }
}