MultipleYieldCurveFinderJacobian extends Function1D<DoubleMatrix1D, DoubleMatrix2D> { @Override public DoubleMatrix2D evaluate(final DoubleMatrix1D x) { final YieldCurveBundle curves = _curveBuilderFunction.evaluate(x); final YieldCurveBundle knownCurves = _data.getKnownCurves(); if (knownCurves != null) { curves.addAll(knownCurves); } final int totalNodes = _data.getTotalNodes(); final List<String> curveNames = _data.getCurveNames(); final double[][] res = new double[_data.getNumInstruments()][totalNodes]; for (int i = 0; i < _data.getNumInstruments(); i++) { final InstrumentDerivative deriv = _data.getDerivative(i); final Map<String, List<DoublesPair>> senseMap = deriv.accept(_calculator, curves); int offset = 0; for (final String name : curveNames) { if (senseMap.containsKey(name)) { final YieldAndDiscountCurve curve = curves.getCurve(name); final List<DoublesPair> senseList = senseMap.get(name); if (senseList.size() != 0) { final double[][] sensitivity = new double[senseList.size()][]; int k = 0; for (final DoublesPair timeAndDF : senseList) { sensitivity[k++] = curve.getInterestRateParameterSensitivity(timeAndDF.first); } for (int j = 0; j < sensitivity[0].length; j++) { double temp = 0.0; k = 0; for (final DoublesPair timeAndDF : senseList) { temp += timeAndDF.getSecond() * sensitivity[k++][j]; } res[i][j + offset] = temp; } } } offset += _data.getNumberOfPointsForCurve(name); } } return new DoubleMatrix2D(res); } MultipleYieldCurveFinderJacobian(final MultipleYieldCurveFinderDataBundle data, final InstrumentDerivativeVisitor<YieldCurveBundle, Map<String, List<DoublesPair>>> calculator); @Override DoubleMatrix2D evaluate(final DoubleMatrix1D x);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullVector() { CASH_ONLY.evaluate((DoubleMatrix1D) null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testWrongNumberOfElements() { CASH_ONLY.evaluate(XN); }
@Test public void testCashOnly() { final DoubleMatrix2D jacobian = CASH_ONLY.evaluate(XM); assertEquals(M, jacobian.getNumberOfRows()); assertEquals(M, jacobian.getNumberOfColumns()); for (int i = 0; i < M; i++) { for (int j = 0; j < M; j++) { if (i == j) { assertEquals(Math.exp(XM.getEntry(i) * i), jacobian.getEntry(i, i), 1e-8); } else { assertEquals(0.0, jacobian.getEntry(i, j), 0); } } } }
@Test public void testFRAOnly() { final DoubleMatrix2D jacobian = FRA_ONLY.evaluate(XN); assertEquals(N, jacobian.getNumberOfRows()); assertEquals(N, jacobian.getNumberOfColumns()); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (i == j) { assertTrue(jacobian.getEntry(i, j) > 0.0); } else if (i == (j + 1)) { assertTrue(jacobian.getEntry(i, j) < 0.0); } else { assertEquals(0.0, jacobian.getEntry(i, j), 0.0); } } } }
@Test public void testMixed() { final DoubleMatrix2D jacobian = MIXED.evaluate(XNM); assertEquals(N + M, jacobian.getNumberOfRows()); assertEquals(N + M, jacobian.getNumberOfColumns()); for (int i = 0; i < N; i++) { for (int j = 0; j < N + M; j++) { if (i == j) { assertTrue(jacobian.getEntry(i, j) > 0.0); } else if (i == (j + 1)) { assertTrue(jacobian.getEntry(i, j) < 0.0); } else { assertEquals(0.0, jacobian.getEntry(i, j), 0.0); } } } for (int i = N; i < N + M; i++) { for (int j = 0; j < N + M; j++) { if (i == j) { assertEquals(Math.exp(XNM.getEntry(i) * (i - N)), jacobian.getEntry(i, i), 1e-8); } else { assertEquals(0.0, jacobian.getEntry(i, j), 0.0); } } } }