Package com.opengamma.analytics.math.function

Examples of com.opengamma.analytics.math.function.DoubleFunction1D


    final int mid = (n + 1) / 2;
    final double[] x = new double[n];
    final double[] w = new double[n];
    final Pair<DoubleFunction1D, DoubleFunction1D>[] polynomials = LEGENDRE.getPolynomialsAndFirstDerivative(n);
    final Pair<DoubleFunction1D, DoubleFunction1D> pair = polynomials[n];
    final DoubleFunction1D function = pair.getFirst();
    final DoubleFunction1D derivative = pair.getSecond();
    for (int i = 0; i < mid; i++) {
      final double root = ROOT_FINDER.getRoot(function, derivative, getInitialRootGuess(i, n));
      x[i] = -root;
      x[n - i - 1] = root;
      final double dp = derivative.evaluate(root);
      w[i] = 2 / ((1 - root * root) * dp * dp);
      w[n - i - 1] = w[i];
    }
    return new GaussianQuadratureData(x, w);
  }
View Full Code Here


    final double[] x = new double[n];
    final double[] w = new double[n];
    final int m = (n + 1) / 2;
    final Pair<DoubleFunction1D, DoubleFunction1D>[] polynomials = HERMITE.getPolynomialsAndFirstDerivative(n);
    final Pair<DoubleFunction1D, DoubleFunction1D> pair = polynomials[n];
    final DoubleFunction1D function = pair.getFirst();
    final DoubleFunction1D derivative = pair.getSecond();
    double root = 0;
    for (int i = 0; i < m; i++) {
      root = getInitialRootGuess(root, i, n, x);
      root = ROOT_FINDER.getRoot(function, derivative, root);
      final double dp = derivative.evaluate(root);
      x[i] = -root;
      x[n - 1 - i] = root;
      w[i] = 2. / (dp * dp);
      w[n - 1 - i] = w[i];
    }
View Full Code Here

  @Override
  public GaussianQuadratureData generate(final int n) {
    Validate.isTrue(n > 0);
    final Pair<DoubleFunction1D, DoubleFunction1D>[] polynomials = LAGUERRE.getPolynomialsAndFirstDerivative(n, _alpha);
    final Pair<DoubleFunction1D, DoubleFunction1D> pair = polynomials[n];
    final DoubleFunction1D p1 = polynomials[n - 1].getFirst();
    final DoubleFunction1D function = pair.getFirst();
    final DoubleFunction1D derivative = pair.getSecond();
    final double[] x = new double[n];
    final double[] w = new double[n];
    double root = 0;
    for (int i = 0; i < n; i++) {
      root = ROOT_FINDER.getRoot(function, derivative, getInitialRootGuess(root, i, n, x));
      x[i] = root;
      w[i] = -GAMMA_FUNCTION.evaluate(_alpha + n) / MathUtils.factorialDouble(n) / (derivative.evaluate(root) * p1.evaluate(root));
    }
    return new GaussianQuadratureData(x, w);
  }
View Full Code Here

  @Override
  public GaussianQuadratureData generate(final int n) {
    Validate.isTrue(n > 0, "n > 0");
    final Pair<DoubleFunction1D, DoubleFunction1D>[] polynomials = JACOBI.getPolynomialsAndFirstDerivative(n, _alpha, _beta);
    final Pair<DoubleFunction1D, DoubleFunction1D> pair = polynomials[n];
    final DoubleFunction1D previous = polynomials[n - 1].getFirst();
    final DoubleFunction1D function = pair.getFirst();
    final DoubleFunction1D derivative = pair.getSecond();
    final double[] x = new double[n];
    final double[] w = new double[n];
    double root = 0;
    for (int i = 0; i < n; i++) {
      final double d = 2 * n + _c;
      root = getInitialRootGuess(root, i, n, x);
      root = ROOT_FINDER.getRoot(function, derivative, root);
      x[i] = root;
      w[i] = GAMMA_FUNCTION.evaluate(_alpha + n) * GAMMA_FUNCTION.evaluate(_beta + n) / MathUtils.factorialDouble(n) / GAMMA_FUNCTION.evaluate(n + _c + 1) * d * Math.pow(2, _c)
          / (derivative.evaluate(root) * previous.evaluate(root));
    }
    return new GaussianQuadratureData(x, w);
  }
View Full Code Here

  }

  @Test
  public void correlationGetter() {
    final double correlation = 0.50;
    final DoubleFunction1D correlationFunction = new RealPolynomialFunction1D(new double[] {correlation}); // Constant function
    final SABRInterestRateCorrelationParameters sabrCorrelation = new SABRInterestRateCorrelationParameters(ALPHA_SURFACE, BETA_SURFACE, RHO_SURFACE, NU_SURFACE, DAYCOUNT, correlationFunction);
    assertEquals("SABR with correlation: get correlation", correlationFunction, sabrCorrelation.getCorrelation());
  }
View Full Code Here

        assertEquals(l1[j].evaluate(x), l2[j].evaluate(x), EPS);
      }
    }
    final double alpha = 3.45;
    l1 = LAGUERRE.getPolynomials(6, alpha);
    final DoubleFunction1D f0 = new DoubleFunction1D() {

      @Override
      public Double evaluate(final Double d) {
        return 1.;
      }
    };
    final DoubleFunction1D f1 = new DoubleFunction1D() {

      @Override
      public Double evaluate(final Double d) {
        return 1 + alpha - d;
      }
    };
    final DoubleFunction1D f2 = new DoubleFunction1D() {

      @Override
      public Double evaluate(final Double d) {
        return d * d / 2 - (alpha + 2) * d + (alpha + 2) * (alpha + 1) / 2.;
      }
    };
    final DoubleFunction1D f3 = new DoubleFunction1D() {

      @Override
      public Double evaluate(final Double d) {
        return -d * d * d / 6 + (alpha + 3) * d * d / 2 - (alpha + 2) * (alpha + 3) * d / 2 + (alpha + 1) * (alpha + 2) * (alpha + 3) / 6;
      }
    };
    assertEquals(l1[0].evaluate(x), f0.evaluate(x), EPS);
    assertEquals(l1[1].evaluate(x), f1.evaluate(x), EPS);
    assertEquals(l1[2].evaluate(x), f2.evaluate(x), EPS);
    assertEquals(l1[3].evaluate(x), f3.evaluate(x), EPS);
  }
View Full Code Here

  public void PolynomialFunctionRecoverTest() {

    final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter();
    final double[] coeff = new double[] {3.4, 5.6, 1., -4. };

    DoubleFunction1D func = new RealPolynomialFunction1D(coeff);

    final int degree = coeff.length - 1;

    final int nPts = 7;
    double[] xValues = new double[nPts];
    double[] yValues = new double[nPts];

    for (int i = 0; i < nPts; ++i) {
      xValues[i] = -5. + 10 * i / (nPts - 1);
      yValues[i] = func.evaluate(xValues[i]);
    }

    double[] yValuesNorm = new double[nPts];

    final double mean = _meanCal.evaluate(xValues);
    final double std = _stdCal.evaluate(xValues);
    final double ratio = mean / std;

    for (int i = 0; i < nPts; ++i) {
      final double tmp = xValues[i] / std - ratio;
      yValuesNorm[i] = func.evaluate(tmp);
    }

    /**
     * Tests for regress(..)
     */

    LeastSquaresRegressionResult result = regObj.regress(xValues, yValues, degree);

    double[] coeffResult = result.getBetas();

    for (int i = 0; i < degree + 1; ++i) {
      assertEquals(coeff[i], coeffResult[i], EPS * Math.abs(coeff[i]));
    }

    final double[] residuals = result.getResiduals();
    func = new RealPolynomialFunction1D(coeffResult);
    double[] yValuesFit = new double[nPts];
    for (int i = 0; i < nPts; ++i) {
      yValuesFit[i] = func.evaluate(xValues[i]);
    }

    for (int i = 0; i < nPts; ++i) {
      assertEquals(Math.abs(yValuesFit[i] - yValues[i]), 0., Math.abs(yValues[i]) * EPS);
    }

    for (int i = 0; i < nPts; ++i) {
      assertEquals(Math.abs(yValuesFit[i] - yValues[i]), Math.abs(residuals[i]), Math.abs(yValues[i]) * EPS);
    }

    double sum = 0.;
    for (int i = 0; i < nPts; ++i) {
      sum += residuals[i] * residuals[i];
    }
    sum = Math.sqrt(sum);

    /**
     * Tests for regressVerbose(.., false)
     */

    PolynomialsLeastSquaresFitterResult resultVer = regObj.regressVerbose(xValues, yValues, degree, false);
    coeffResult = resultVer.getCoeff();
    func = new RealPolynomialFunction1D(coeffResult);
    for (int i = 0; i < nPts; ++i) {
      yValuesFit[i] = func.evaluate(xValues[i]);
    }

    assertEquals(nPts - (degree + 1), resultVer.getDof(), 0);
    for (int i = 0; i < degree + 1; ++i) {
      assertEquals(coeff[i], coeffResult[i], EPS * Math.abs(coeff[i]));
    }

    for (int i = 0; i < nPts; ++i) {
      assertEquals(Math.abs(yValuesFit[i] - yValues[i]), 0., Math.abs(yValues[i]) * EPS);
    }

    assertEquals(sum, resultVer.getDiffNorm(), EPS);

    /**
     * Tests for regressVerbose(.., true)
     */

    PolynomialsLeastSquaresFitterResult resultNorm = regObj.regressVerbose(xValues, yValuesNorm, degree, true);

    coeffResult = resultNorm.getCoeff();
    final double[] meanAndStd = resultNorm.getMeanAndStd();

    assertEquals(nPts - (degree + 1), resultNorm.getDof(), 0);
    assertEquals(mean, meanAndStd[0], EPS);
    assertEquals(std, meanAndStd[1], EPS);
    for (int i = 0; i < degree + 1; ++i) {
      assertEquals(coeff[i], coeffResult[i], EPS * Math.abs(coeff[i]));
    }

    func = new RealPolynomialFunction1D(coeffResult);
    for (int i = 0; i < nPts; ++i) {
      final double tmp = xValues[i] / std - ratio;
      yValuesFit[i] = func.evaluate(tmp);
    }

    for (int i = 0; i < nPts; ++i) {
      assertEquals(Math.abs(yValuesFit[i] - yValuesNorm[i]), 0., Math.abs(yValuesNorm[i]) * EPS);
    }
View Full Code Here

    final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter();

    // final double[] coeff = new double[] {5. * (randObj.nextDouble() + .5), 5. * (randObj.nextDouble() - .5), 5. * (randObj.nextDouble() - 5.), 5. * (randObj.nextDouble() - .5) };
    final double[] coeff = new double[] {-(randObj.nextDouble() + 1.), (randObj.nextDouble() + 1.), -(randObj.nextDouble() + 1.), (randObj.nextDouble() + 1.) };
    final DoubleFunction1D func = new RealPolynomialFunction1D(coeff);

    final int degree = coeff.length - 1;

    final int nPts = 7;
    double[] xValues = new double[nPts];
    double[] yValues = new double[nPts];

    for (int i = 0; i < nPts; ++i) {
      xValues[i] = -5. + 10 * i / (nPts - 1);
      //xValues[i] = 3. * (randObj.nextDouble() + .5);
      yValues[i] = func.evaluate(xValues[i]);
    }

    PolynomialsLeastSquaresFitterResult result = regObj.regressVerbose(xValues, yValues, degree, false);

    final double[] coeffResult = result.getCoeff();
View Full Code Here

    final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter();

    // final double[] coeff = new double[] {5. * (randObj.nextDouble() + .5), 5. * (randObj.nextDouble() - .5), 5. * (randObj.nextDouble() - 5.), 5. * (randObj.nextDouble() - .5) };
    final double[] coeff = new double[] {-1.9564385860928322, 1.968428061753627, -1.8042487762604558, 1.1347030699838965, 2.1347030699838965 };
    final DoubleFunction1D func = new RealPolynomialFunction1D(coeff);

    final int degree = coeff.length - 1;

    final int nPts = 7;
    double[] xValues = new double[] {-2.0, -1.0, -.5, 0.0, 1.0, 2.0, 3.0 };
    final double mean = 0.357142857142857;
    final double std = 1.749149453169685;

    double[] xValuesNom = new double[nPts];
    double[] yValues = new double[nPts];

    for (int i = 0; i < nPts; ++i) {
      //  xValues[i] = -5. + 10 * i / (nPts - 1);
      //xValues[i] = 3. * (randObj.nextDouble() + .5);
      xValuesNom[i] = xValues[i] / std - mean / std;
      yValues[i] = func.evaluate(xValuesNom[i]);
    }

    PolynomialsLeastSquaresFitterResult resultNom = regObj.regressVerbose(xValues, yValues, degree, true);
    PolynomialsLeastSquaresFitterResult result = regObj.regressVerbose(xValuesNom, yValues, degree, false);

View Full Code Here

    System.out.println("\n");

    LeastSquaresRegressionResult result = regObj.regress(xValues, yValues, degree);
    final double[] coeffResult = result.getBetas();

    final DoubleFunction1D func = new RealPolynomialFunction1D(coeffResult);

    for (int i = 0; i < 100; ++i) {
      final double k = -5. + 10. * i / 100.;
      System.out.println(k + "\t" + func.evaluate(k));
    }

    System.out.println("\n");
    final double[] resResult = result.getResiduals();
    double resSumSqHalf = 0.;
View Full Code Here

TOP

Related Classes of com.opengamma.analytics.math.function.DoubleFunction1D

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.