/**
  * Calculates the Nth generalized harmonic number. See <a
  * href="http://mathworld.wolfram.com/HarmonicSeries.html">Harmonic Series</a>.
  *
  * @param n the term in the series to calculate (must be &ge; 1)
  * @param m the exponent; special case m == 1.0 is the harmonic series
  * @return the nth generalized harmonic number
  */
 private double generalizedHarmonic(final int n, final double m) {
   double value = 0;
   for (int k = n; k > 0; --k) {
     value += 1.0 / FastMath.pow(k, m);
   }
   return value;
 }
  /**
   * The probability mass function P(X = x) for a Zipf distribution.
   *
   * @param x the value at which the probability density function is evaluated.
   * @return the value of the probability mass function at x
   */
  public double probability(final int x) {
    if (x <= 0 || x > numberOfElements) {
      return 0.0;
    }

    return (1.0 / FastMath.pow(x, exponent)) / generalizedHarmonic(numberOfElements, exponent);
  }
Exemple #3
0
  private void calculateBlbSum(
      double[] sample,
      double samplingRate,
      double bagExp,
      final int numberOfBags,
      final int numberOfBootstraps,
      Mean perbootstrapTime,
      Mean perbagTime) {
    bagSums = new double[numberOfBags];
    int bag_size = (int) FastMath.ceil(FastMath.pow(sample.length, bagExp));
    int[] index = new int[sample.length];
    for (int ii = 0; ii < sample.length; ii++) {
      index[ii] = ii;
    }
    int[] origIndex = index.clone();
    long bootstrapTime = 0;
    Mean actualSum = new Mean();
    for (int ii = 0; ii < numberOfBags; ii++) {
      SamplingUtilities.KnuthShuffle(index);
      double[] sampleBag = new double[bag_size];
      for (int jj = 0; jj < bag_size; jj++) {
        sampleBag[jj] = sample[index[jj]];
      }
      BootstrapSum sum =
          new BootstrapSum(sampleBag, samplingRate, numberOfBootstraps, sample.length);
      bagSums[ii] = sum.Sum();
      actualSum.increment(bagSums[ii]);
      perbootstrapTime.increment(sum.getMeanTime());
      bootstrapTime += sum.getTimes()[sum.getTimes().length - 1];
      perbagTime.increment(sum.getTimes()[sum.getTimes().length - 1]);
      index = origIndex.clone();
    }

    meanSum = actualSum.getResult();

    totalTime = bootstrapTime;
  }
  /** {@inheritDoc} */
  @Override
  public void integrate(final ExpandableStatefulODE equations, final double t)
      throws MathIllegalStateException, MathIllegalArgumentException {

    sanityChecks(equations, t);
    setEquations(equations);
    final boolean forward = t > equations.getTime();

    // create some internal working arrays
    final double[] y0 = equations.getCompleteState();
    final double[] y = y0.clone();
    final int stages = c.length + 1;
    final double[][] yDotK = new double[stages][y.length];
    final double[] yTmp = y0.clone();
    final double[] yDotTmp = new double[y.length];

    // set up an interpolator sharing the integrator arrays
    final RungeKuttaStepInterpolator interpolator = (RungeKuttaStepInterpolator) prototype.copy();
    interpolator.reinitialize(
        this, yTmp, yDotK, forward, equations.getPrimaryMapper(), equations.getSecondaryMappers());
    interpolator.storeTime(equations.getTime());

    // set up integration control objects
    stepStart = equations.getTime();
    double hNew = 0;
    boolean firstTime = true;
    initIntegration(equations.getTime(), y0, t);

    // main integration loop
    isLastStep = false;
    do {

      interpolator.shift();

      // iterate over step size, ensuring local normalized error is smaller than 1
      double error = 10;
      while (error >= 1.0) {

        if (firstTime || !fsal) {
          // first stage
          computeDerivatives(stepStart, y, yDotK[0]);
        }

        if (firstTime) {
          final double[] scale = new double[mainSetDimension];
          if (vecAbsoluteTolerance == null) {
            for (int i = 0; i < scale.length; ++i) {
              scale[i] = scalAbsoluteTolerance + scalRelativeTolerance * FastMath.abs(y[i]);
            }
          } else {
            for (int i = 0; i < scale.length; ++i) {
              scale[i] = vecAbsoluteTolerance[i] + vecRelativeTolerance[i] * FastMath.abs(y[i]);
            }
          }
          hNew = initializeStep(forward, getOrder(), scale, stepStart, y, yDotK[0], yTmp, yDotK[1]);
          firstTime = false;
        }

        stepSize = hNew;
        if (forward) {
          if (stepStart + stepSize >= t) {
            stepSize = t - stepStart;
          }
        } else {
          if (stepStart + stepSize <= t) {
            stepSize = t - stepStart;
          }
        }

        // next stages
        for (int k = 1; k < stages; ++k) {

          for (int j = 0; j < y0.length; ++j) {
            double sum = a[k - 1][0] * yDotK[0][j];
            for (int l = 1; l < k; ++l) {
              sum += a[k - 1][l] * yDotK[l][j];
            }
            yTmp[j] = y[j] + stepSize * sum;
          }

          computeDerivatives(stepStart + c[k - 1] * stepSize, yTmp, yDotK[k]);
        }

        // estimate the state at the end of the step
        for (int j = 0; j < y0.length; ++j) {
          double sum = b[0] * yDotK[0][j];
          for (int l = 1; l < stages; ++l) {
            sum += b[l] * yDotK[l][j];
          }
          yTmp[j] = y[j] + stepSize * sum;
        }

        // estimate the error at the end of the step
        error = estimateError(yDotK, y, yTmp, stepSize);
        if (error >= 1.0) {
          // reject the step and attempt to reduce error by stepsize control
          final double factor =
              FastMath.min(
                  maxGrowth, FastMath.max(minReduction, safety * FastMath.pow(error, exp)));
          hNew = filterStep(stepSize * factor, forward, false);
        }
      }

      // local error is small enough: accept the step, trigger events and step handlers
      interpolator.storeTime(stepStart + stepSize);
      System.arraycopy(yTmp, 0, y, 0, y0.length);
      System.arraycopy(yDotK[stages - 1], 0, yDotTmp, 0, y0.length);
      stepStart = acceptStep(interpolator, y, yDotTmp, t);
      System.arraycopy(y, 0, yTmp, 0, y.length);

      if (!isLastStep) {

        // prepare next step
        interpolator.storeTime(stepStart);

        if (fsal) {
          // save the last evaluation for the next step
          System.arraycopy(yDotTmp, 0, yDotK[0], 0, y0.length);
        }

        // stepsize control for next step
        final double factor =
            FastMath.min(maxGrowth, FastMath.max(minReduction, safety * FastMath.pow(error, exp)));
        final double scaledH = stepSize * factor;
        final double nextT = stepStart + scaledH;
        final boolean nextIsLast = forward ? (nextT >= t) : (nextT <= t);
        hNew = filterStep(scaledH, forward, nextIsLast);

        final double filteredNextT = stepStart + hNew;
        final boolean filteredNextIsLast = forward ? (filteredNextT >= t) : (filteredNextT <= t);
        if (filteredNextIsLast) {
          hNew = t - stepStart;
        }
      }

    } while (!isLastStep);

    // dispatch results
    equations.setTime(stepStart);
    equations.setCompleteState(y);

    resetInternalState();
  }
Exemple #5
0
 /**
  * Returns a <code>double</code> whose value is <tt>(this<sup>exponent</sup>)</tt>, returning the
  * result in reduced form.
  *
  * @param exponent exponent to which this <code>BigFraction</code> is to be raised.
  * @return <tt>this<sup>exponent</sup></tt>.
  */
 public double pow(final double exponent) {
   return FastMath.pow(numerator.doubleValue(), exponent)
       / FastMath.pow(denominator.doubleValue(), exponent);
 }