@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);
       }
     }
   }
 }
 @Test
 public void testFirstOrderOnly() {
   final ParametricVaRDataBundle result = CALC.evaluate(DATA_FIRST_ORDER).get(1);
   final List<String> names = result.getNames();
   assertEquals(
       names,
       Arrays.asList(
           "A_IMPLIED_VOLATILITY",
           "A_SPOT_PRICE",
           "A_INTEREST_RATE",
           "B_INTEREST_RATE",
           "B_SPOT_PRICE",
           "B_IMPLIED_VOLATILITY",
           "C_SPOT_PRICE",
           "C_IMPLIED_VOLATILITY",
           "C_IMPLIED_VARIANCE"));
   final Matrix<?> sensitivities = result.getSensitivities();
   assertEquals(sensitivities.getClass(), DoubleMatrix1D.class);
   final DoubleMatrix1D v = (DoubleMatrix1D) sensitivities;
   assertEquals(v.getNumberOfElements(), 9);
   assertArrayEquals(
       v.getData(),
       new double[] {
         VEGA_A_1,
         DELTA_A_1 + DELTA_A_2,
         RHO_A_1 + RHO_A_2,
         RHO_B,
         DELTA_B,
         VEGA_B,
         DELTA_C,
         VEGA_C,
         VARIANCE_VEGA_C
       },
       EPS);
   final Matrix<?> covariance = result.getCovarianceMatrix();
   assertEquals(covariance.getClass(), DoubleMatrix2D.class);
   final DoubleMatrix2D m = (DoubleMatrix2D) covariance;
   assertEquals(m.getNumberOfRows(), 9);
   assertEquals(m.getNumberOfColumns(), 9);
   for (int i = 0; i < 9; i++) {
     assertArrayEquals(m.getData()[i], C2[i], EPS);
   }
 }
 @Test
 public void testFirstOrderOnlyNoDuplicateGreeks() {
   final ParametricVaRDataBundle result = CALC.evaluate(A_DATA_FIRST_ORDER_1).get(1);
   final List<String> names = result.getNames();
   assertEquals(names, Arrays.asList("A_IMPLIED_VOLATILITY", "A_SPOT_PRICE", "A_INTEREST_RATE"));
   final Matrix<?> sensitivities = result.getSensitivities();
   assertEquals(sensitivities.getClass(), DoubleMatrix1D.class);
   final DoubleMatrix1D v = (DoubleMatrix1D) sensitivities;
   assertEquals(v.getNumberOfElements(), 3);
   assertArrayEquals(v.getData(), new double[] {VEGA_A_1, DELTA_A_1, RHO_A_1}, EPS);
   final Matrix<?> covariance = result.getCovarianceMatrix();
   assertEquals(covariance.getClass(), DoubleMatrix2D.class);
   final DoubleMatrix2D m = (DoubleMatrix2D) covariance;
   assertEquals(m.getNumberOfRows(), 3);
   assertEquals(m.getNumberOfColumns(), 3);
   assertArrayEquals(m.getData()[0], C1[0], EPS);
   assertArrayEquals(m.getData()[1], C1[1], EPS);
   assertArrayEquals(m.getData()[2], C1[2], EPS);
 }
 @Override
 public DoubleMatrix2D getInitializedMatrix(
     final Function1D<DoubleMatrix1D, DoubleMatrix2D> jacobianFunction, final DoubleMatrix1D x) {
   Validate.notNull(jacobianFunction);
   Validate.notNull(x);
   final DoubleMatrix2D estimate = jacobianFunction.evaluate(x);
   final DecompositionResult decompositionResult = _decomposition.evaluate(estimate);
   return decompositionResult.solve(
       DoubleMatrixUtils.getIdentityMatrix2D(x.getNumberOfElements()));
 }
 @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 test() {
   ParametricVaRDataBundle result = CALC.evaluate(DATA).get(1);
   List<String> names = result.getNames();
   assertEquals(
       names,
       Arrays.asList(
           "A_IMPLIED_VOLATILITY",
           "A_SPOT_PRICE",
           "A_INTEREST_RATE",
           "B_INTEREST_RATE",
           "B_SPOT_PRICE",
           "B_IMPLIED_VOLATILITY",
           "C_SPOT_PRICE",
           "C_IMPLIED_VOLATILITY",
           "C_IMPLIED_VARIANCE"));
   Matrix<?> sensitivities = result.getSensitivities();
   assertEquals(sensitivities.getClass(), DoubleMatrix1D.class);
   final DoubleMatrix1D v = (DoubleMatrix1D) sensitivities;
   assertEquals(v.getNumberOfElements(), 9);
   assertArrayEquals(
       v.getData(),
       new double[] {
         VEGA_A_1,
         DELTA_A_1 + DELTA_A_2,
         RHO_A_1 + RHO_A_2,
         RHO_B,
         DELTA_B,
         VEGA_B,
         DELTA_C,
         VEGA_C,
         VARIANCE_VEGA_C
       },
       EPS);
   Matrix<?> covariance = result.getCovarianceMatrix();
   assertEquals(covariance.getClass(), DoubleMatrix2D.class);
   final DoubleMatrix2D m = (DoubleMatrix2D) covariance;
   assertEquals(m.getNumberOfRows(), 9);
   assertEquals(m.getNumberOfColumns(), 9);
   for (int i = 0; i < 9; i++) {
     assertArrayEquals(m.getData()[i], C2[i], EPS);
   }
   result = CALC.evaluate(DATA).get(2);
   names = result.getNames();
   assertEquals(
       names,
       Arrays.asList(
           "A_SPOT_PRICE",
           "A_IMPLIED_VOLATILITY",
           "A_STRIKE",
           "A_TIME",
           "B_SPOT_PRICE",
           "B_IMPLIED_VOLATILITY",
           "C_SPOT_PRICE",
           "C_IMPLIED_VOLATILITY",
           "A_YIELD"));
   sensitivities = result.getSensitivities();
   assertEquals(sensitivities.getClass(), DoubleMatrix2D.class);
   final DoubleMatrix2D m1 = (DoubleMatrix2D) sensitivities;
   assertEquals(m1.getNumberOfRows(), 9);
   assertEquals(m1.getNumberOfColumns(), 9);
   final double[][] s =
       new double[][] {
         new double[] {GAMMA_A_1 + GAMMA_A_2, VANNA_A, 0, DELTA_BLEED_A, 0, 0, 0, 0, DUMMY_A},
         new double[] {VANNA_A, VOMMA_A_1 + VOMMA_A_2, 0, 0, 0, 0, 0, 0, 0},
         new double[] {0, 0, STRIKE_GAMMA_A_1, 0, 0, 0, 0, 0, 0},
         new double[] {DELTA_BLEED_A, 0, 0, 0, 0, 0, 0, 0, 0},
         new double[] {0, 0, 0, 0, GAMMA_B, 0, 0, 0, 0},
         new double[] {0, 0, 0, 0, 0, VOMMA_B, 0, 0, 0},
         new double[] {0, 0, 0, 0, 0, 0, GAMMA_C, VANNA_C, 0},
         new double[] {0, 0, 0, 0, 0, 0, VANNA_C, 0, 0},
         new double[] {DUMMY_A, 0, 0, 0, 0, 0, 0, 0, 0}
       };
   for (int i = 0; i < 9; i++) {
     assertArrayEquals(m1.getData()[i], s[i], EPS);
   }
   covariance = result.getCovarianceMatrix();
   assertEquals(covariance.getClass(), DoubleMatrix2D.class);
   final DoubleMatrix2D m2 = (DoubleMatrix2D) covariance;
   assertEquals(m2.getNumberOfRows(), 9);
   assertEquals(m2.getNumberOfColumns(), 9);
   for (int i = 0; i < 9; i++) {
     assertArrayEquals(m2.getData()[i], C5[i], EPS);
   }
 }
 @Override
 public DoubleMatrix2D evaluate(DoubleMatrix1D v) {
   double[] x = v.getData();
   return new DoubleMatrix2D(
       new double[][] {{x[0] * x[0], x[0] * x[1]}, {x[0] - x[1], x[1] * x[1]}});
 }