@Test
 public void testRungeKutta() {
   final RungeKuttaIntegrator1D integrator = new RungeKuttaIntegrator1D();
   final double lower = -1;
   final double upper = 2;
   assertEquals(
       F1.evaluate(upper) - F1.evaluate(lower), integrator.integrate(DF1, lower, upper), EPS);
 }
 @Test
 public void testGaussJacobi() {
   final double upper = 12;
   final double lower = -1;
   final Integrator1D<Double, Double> integrator = new GaussJacobiQuadratureIntegrator1D(7);
   assertEquals(
       F1.evaluate(upper) - F1.evaluate(lower), integrator.integrate(DF1, lower, upper), EPS);
 }
 @Test
 public void testGaussLegendre() {
   double upper = 2;
   double lower = -6;
   final Integrator1D<Double, Double> integrator = new GaussLegendreQuadratureIntegrator1D(6);
   assertEquals(
       F1.evaluate(upper) - F1.evaluate(lower), integrator.integrate(DF1, lower, upper), EPS);
   lower = -0.56;
   upper = 1.4;
   assertEquals(
       F1.evaluate(upper) - F1.evaluate(lower), integrator.integrate(DF1, lower, upper), EPS);
 }
 @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);
       }
     }
   }
 }
 /** {@inheritDoc} */
 @Override
 public Double integrate(
     final Function1D<Double, Double> function, final Double lower, final Double upper) {
   Validate.notNull(function);
   Validate.notNull(lower);
   Validate.notNull(upper);
   final Function1D<Double, Double> integral = getIntegralFunction(function, lower, upper);
   final GaussianQuadratureData quadrature = _generator.generate(_n);
   final double[] abscissas = quadrature.getAbscissas();
   final int n = abscissas.length;
   final double[] weights = quadrature.getWeights();
   double sum = 0;
   for (int i = 0; i < n; i++) {
     sum += integral.evaluate(abscissas[i]) * weights[i];
   }
   return sum;
 }
 @Test
 public void test() {
   double x;
   final double eps = 1e-5;
   for (int i = 0; i < 100; i++) {
     x = RANDOM.nextDouble();
     assertEquals(x, F.evaluate(T.getCDF(x)), 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 test() {
   DoubleMatrix2D m1 = ESTIMATE.getInitializedMatrix(J, X);
   DoubleMatrix2D m2 = J.evaluate(X);
   for (int i = 0; i < 2; i++) {
     for (int j = 0; j < 2; j++) {
       assertEquals(m1.getEntry(i, j), m2.getEntry(i, j), 1e-9);
     }
   }
 }
 @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);
       }
     }
   }
 }
 /**
  * @param x The array of data, not null, must contain at least three data points
  * @return The sample skewness
  */
 @Override
 public Double evaluate(final double[] x) {
   Validate.notNull(x, "x");
   Validate.isTrue(x.length >= 3, "Need at least three points to calculate sample skewness");
   double sum = 0;
   double variance = 0;
   final double mean = MEAN.evaluate(x);
   for (final Double d : x) {
     final double diff = d - mean;
     variance += diff * diff;
     sum += diff * diff * diff;
   }
   final int n = x.length;
   variance /= n - 1;
   return Math.sqrt(n - 1.) * sum / (Math.pow(variance, 1.5) * Math.sqrt(n) * (n - 2));
 }
 @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);
       }
     }
   }
 }
  @Override
  public LeastSquareResultsWithTransform getFitResult(
      final EuropeanVanillaOption[] options,
      final BlackFunctionData[] data,
      final double[] errors,
      final double[] initialFitParameters,
      final BitSet fixed) {
    testData(options, data, errors, initialFitParameters, fixed, N_PARAMETERS);
    final int n = options.length;
    final double forward = data[0].getForward();
    final double maturity = options[0].getTimeToExpiry();
    for (int i = 1; i < n; i++) {
      Validate.isTrue(
          CompareUtils.closeEquals(options[i].getTimeToExpiry(), maturity),
          "All options must have the same maturity "
              + maturity
              + "; have one with maturity "
              + options[i].getTimeToExpiry());
    }
    final UncoupledParameterTransforms transforms =
        new UncoupledParameterTransforms(
            new DoubleMatrix1D(initialFitParameters), TRANSFORMS, fixed);
    final Function1D<DoubleMatrix1D, Double> function =
        new Function1D<DoubleMatrix1D, Double>() {

          @SuppressWarnings("synthetic-access")
          @Override
          public Double evaluate(final DoubleMatrix1D fp) {
            final DoubleMatrix1D mp = transforms.inverseTransform(fp);
            final double alpha = mp.getEntry(0);
            final double beta = mp.getEntry(1);
            final double nu = mp.getEntry(2);
            final double rho = mp.getEntry(3);
            double chiSqr = 0;
            final SABRFormulaData sabrFormulaData = new SABRFormulaData(alpha, beta, rho, nu);
            for (int i = 0; i < n; i++) {
              chiSqr +=
                  FunctionUtils.square(
                      (data[i].getBlackVolatility()
                              - _formula
                                  .getVolatilityFunction(options[i], forward)
                                  .evaluate(sabrFormulaData))
                          / errors[i]);
            }
            return chiSqr;
          }
        };
    final ScalarMinimizer lineMinimizer = new BrentMinimizer1D();
    final ConjugateDirectionVectorMinimizer minimzer =
        new ConjugateDirectionVectorMinimizer(lineMinimizer, 1e-6, 10000);
    final DoubleMatrix1D fp = transforms.transform(new DoubleMatrix1D(initialFitParameters));
    final DoubleMatrix1D minPos = minimzer.minimize(function, fp);
    final double chiSquare = function.evaluate(minPos);
    final DoubleMatrix1D res = transforms.inverseTransform(minPos);
    return new LeastSquareResultsWithTransform(
        new LeastSquareResults(
            chiSquare, res, new DoubleMatrix2D(new double[N_PARAMETERS][N_PARAMETERS])),
        transforms);
    // return new LeastSquareResults(chiSquare, res, new DoubleMatrix2D(new
    // double[N_PARAMETERS][N_PARAMETERS]));
  }
 @Test(expectedExceptions = IllegalArgumentException.class)
 public void testNegative() {
   F.evaluate(-4.);
 }
 @Test(expectedExceptions = IllegalArgumentException.class)
 public void testNull() {
   F.evaluate((Double) null);
 }
 @Test(expectedExceptions = IllegalArgumentException.class)
 public void testWrongNumberOfElements() {
   CASH_ONLY.evaluate(XN);
 }
 @Test(expectedExceptions = IllegalArgumentException.class)
 public void testNullVector() {
   CASH_ONLY.evaluate((DoubleMatrix1D) null);
 }