final MultiCurveCalculationConfig curveCalculationConfig = new ConfigDBCurveCalculationConfigSource(configSource).getConfig(curveCalculationConfigName);
if (curveCalculationConfig == null) {
throw new OpenGammaRuntimeException("Could not find curve calculation configuration named " + curveCalculationConfigName);
}
final List<InstrumentDerivative> derivatives = new ArrayList<>();
final DoubleArrayList marketValues = new DoubleArrayList();
final DoubleArrayList initialRatesGuess = new DoubleArrayList();
final LinkedHashSet<String> curveNames = new LinkedHashSet<>();
for (final String curveName : curveCalculationConfig.getYieldCurveNames()) {
curveNames.add(curveName);
}
final LinkedHashMap<String, double[]> curveNodes = new LinkedHashMap<>();
final LinkedHashMap<String, Interpolator1D> interpolators = new LinkedHashMap<>();
final ComputationTargetSpecification targetSpec = target.toSpecification();
final Map<String, Integer> nodesPerCurve = new HashMap<>();
final HistoricalTimeSeriesBundle timeSeries = getTimeSeriesBundle(inputs, targetSpec, curveCalculationConfigName);
for (final String curveName : curveNames) {
final InterpolatedYieldCurveSpecificationWithSecurities spec = getYieldCurveSpecification(inputs, targetSpec, curveName);
if (spec == null) {
continue;
}
int nInstruments = 0;
final Interpolator1D interpolator = spec.getInterpolator();
final SnapshotDataBundle marketData = getMarketData(inputs, targetSpec, curveName);
final DoubleArrayList nodeTimes = new DoubleArrayList();
FixedIncomeStripWithSecurity previousStrip = null;
for (final FixedIncomeStripWithSecurity strip : spec.getStrips()) {
final Double marketValue = marketData.getDataPoint(strip.getSecurityIdentifier());
if (marketValue == null) {
throw new OpenGammaRuntimeException("Could not get market data for " + strip);
}
final Security security = strip.getSecurity();
final String[] curveNamesForSecurity = curveCalculationConfig.getCurveExposureForInstrument(curveName, strip.getInstrumentType());
final InstrumentDefinition<?> definition = _securityConverter.visit(security);
final InstrumentDerivative derivative = _definitionConverter.convert(security, definition, now, curveNamesForSecurity, timeSeries);
if (derivative != null) {
if (strip.getInstrumentType() == StripInstrumentType.FUTURE) {
InstrumentDefinition<?> unitNotional;
if (definition instanceof InterestRateFutureSecurityDefinition) {
final InterestRateFutureSecurityDefinition securityDefinition = (InterestRateFutureSecurityDefinition) definition;
unitNotional = new InterestRateFutureTransactionDefinition(securityDefinition, now, marketValue, 1);
} else {
unitNotional = ((InterestRateFutureTransactionDefinition) definition).withNewNotionalAndTransactionPrice(1, marketValue);
// Implementation note: to have the same notional for OTC and futures (and thus not near-singular Jacobian)
}
final InstrumentDerivative unitNotionalDerivative = _definitionConverter.convert(security, unitNotional, now, curveNamesForSecurity, timeSeries);
derivatives.add(unitNotionalDerivative);
initialRatesGuess.add(1 - marketValue);
} else {
derivatives.add(derivative);
initialRatesGuess.add(marketValue);
}
final double t = derivative.accept(LAST_TIME_CALCULATOR);
if (nInstruments > 0 && CompareUtils.closeEquals(nodeTimes.get(nInstruments - 1), t, 1e-12)) {
throw new OpenGammaRuntimeException("Strip " + strip + " has same maturity as one already added (" + previousStrip + ") - will lead to" +
"equal nodes in the curve. Remove one of these strips.");
}
nodeTimes.add(t);
marketValues.add(0.0);
previousStrip = strip;
nInstruments++;
}
}
nodesPerCurve.put(curveName, nInstruments);
curveNodes.put(curveName, nodeTimes.toDoubleArray());
interpolators.put(curveName, interpolator);
}
final YieldCurveBundle knownCurves = getKnownCurves(curveCalculationConfig, targetSpec, inputs);
final double absoluteTolerance = Double.parseDouble(absoluteToleranceName);
final double relativeTolerance = Double.parseDouble(relativeToleranceName);