Example #1
0
 @Override
 public double value(double x) {
   if (x < gamma) {
     return sf1.value(x);
   } else {
     return (1 - sf2.value(x));
   }
 }
 /* (non-Javadoc)
  * @see net.finmath.stochastic.RandomVariableInterface#apply(org.apache.commons.math3.analysis.UnivariateFunction)
  */
 @Override
 public RandomVariableInterface apply(
     org.apache.commons.math3.analysis.UnivariateFunction function) {
   if (isDeterministic()) {
     double newValueIfNonStochastic = function.value(valueIfNonStochastic);
     return new RandomVariable(time, newValueIfNonStochastic);
   } else {
     double[] newRealizations = new double[realizations.length];
     for (int i = 0; i < newRealizations.length; i++)
       newRealizations[i] = function.value(realizations[i]);
     return new RandomVariable(time, newRealizations);
   }
 }
  /** Test of parameters for the interpolator. */
  @Test
  public void testParameters() {
    UnivariateInterpolator interpolator = new DividedDifferenceInterpolator();

    try {
      // bad abscissas array
      double x[] = {1.0, 2.0, 2.0, 4.0};
      double y[] = {0.0, 4.0, 4.0, 2.5};
      UnivariateFunction p = interpolator.interpolate(x, y);
      p.value(0.0);
      Assert.fail("Expecting NonMonotonicSequenceException - bad abscissas array");
    } catch (NonMonotonicSequenceException ex) {
      // expected
    }
  }
 /**
  * @param f Function.
  * @param x Argument.
  * @return {@code f(x)}
  * @throws TooManyEvaluationsException if the maximal number of evaluations is exceeded.
  */
 private double eval(UnivariateFunction f, double x) {
   try {
     evaluations.incrementCount();
   } catch (MaxCountExceededException e) {
     throw new TooManyEvaluationsException(e.getMax());
   }
   return f.value(x);
 }
 /* 136:    */
 /* 137:    */ private double findUpperBound(UnivariateFunction f, double a, double h)
       /* 138:    */ {
   /* 139:249 */ double yA = f.value(a);
   /* 140:250 */ double yB = yA;
   /* 141:251 */ for (double step = h;
       step < 1.7976931348623157E+308D;
       step *= FastMath.max(2.0D, yA / yB))
   /* 142:    */ {
     /* 143:252 */ double b = a + step;
     /* 144:253 */ yB = f.value(b);
     /* 145:254 */ if (yA * yB <= 0.0D) {
       /* 146:255 */ return b;
       /* 147:    */ }
     /* 148:    */ }
   /* 149:258 */ throw new MathIllegalStateException(
       LocalizedFormats.UNABLE_TO_BRACKET_OPTIMUM_IN_LINE_SEARCH, new Object[0]);
   /* 150:    */ }
  /**
   * Test of interpolator for the sine function.
   *
   * <p>|sin^(n)(zeta)| <= 1.0, zeta in [0, 2*PI]
   */
  @Test
  public void testSinFunction() {
    UnivariateFunction f = new Sin();
    UnivariateInterpolator interpolator = new DividedDifferenceInterpolator();
    double x[], y[], z, expected, result, tolerance;

    // 6 interpolating points on interval [0, 2*PI]
    int n = 6;
    double min = 0.0, max = 2 * FastMath.PI;
    x = new double[n];
    y = new double[n];
    for (int i = 0; i < n; i++) {
      x[i] = min + i * (max - min) / n;
      y[i] = f.value(x[i]);
    }
    double derivativebound = 1.0;
    UnivariateFunction p = interpolator.interpolate(x, y);

    z = FastMath.PI / 4;
    expected = f.value(z);
    result = p.value(z);
    tolerance = FastMath.abs(derivativebound * partialerror(x, z));
    Assert.assertEquals(expected, result, tolerance);

    z = FastMath.PI * 1.5;
    expected = f.value(z);
    result = p.value(z);
    tolerance = FastMath.abs(derivativebound * partialerror(x, z));
    Assert.assertEquals(expected, result, tolerance);
  }