Exemple #1
0
  /**
   * Returns the weighted arithmetic mean of the entries in the specified portion of the input
   * array, or <code>Double.NaN</code> if the designated subarray is empty.
   *
   * <p>Throws <code>IllegalArgumentException</code> if either array is null.
   *
   * <p>See {@link Mean} for details on the computing algorithm. The two-pass algorithm described
   * above is used here, with weights applied in computing both the original estimate and the
   * correction factor.
   *
   * <p>Throws <code>IllegalArgumentException</code> if any of the following are true:
   *
   * <ul>
   *   <li>the values array is null
   *   <li>the weights array is null
   *   <li>the weights array does not have the same length as the values array
   *   <li>the weights array contains one or more infinite values
   *   <li>the weights array contains one or more NaN values
   *   <li>the weights array contains negative values
   *   <li>the start and length arguments do not determine a valid array
   * </ul>
   *
   * @param values the input array
   * @param weights the weights array
   * @param begin index of the first array element to include
   * @param length the number of elements to include
   * @return the mean of the values or Double.NaN if length = 0
   * @throws MathIllegalArgumentException if the parameters are not valid
   * @since 2.1
   */
  public double evaluate(
      final double[] values, final double[] weights, final int begin, final int length)
      throws MathIllegalArgumentException {
    if (test(values, weights, begin, length)) {
      Sum sum = new Sum();

      // Compute initial estimate using definitional formula
      double sumw = sum.evaluate(weights, begin, length);
      double xbarw = sum.evaluate(values, weights, begin, length) / sumw;

      // Compute correction factor in second pass
      double correction = 0;
      for (int i = begin; i < begin + length; i++) {
        correction += weights[i] * (values[i] - xbarw);
      }
      return xbarw + (correction / sumw);
    }
    return Double.NaN;
  }
Exemple #2
0
  /**
   * Returns the arithmetic mean of the entries in the specified portion of the input array, or
   * <code>Double.NaN</code> if the designated subarray is empty.
   *
   * <p>Throws <code>IllegalArgumentException</code> if the array is null.
   *
   * <p>See {@link Mean} for details on the computing algorithm.
   *
   * @param values the input array
   * @param begin index of the first array element to include
   * @param length the number of elements to include
   * @return the mean of the values or Double.NaN if length = 0
   * @throws MathIllegalArgumentException if the array is null or the array index parameters are not
   *     valid
   */
  @Override
  public double evaluate(final double[] values, final int begin, final int length)
      throws MathIllegalArgumentException {
    if (test(values, begin, length)) {
      Sum sum = new Sum();
      double sampleSize = length;

      // Compute initial estimate using definitional formula
      double xbar = sum.evaluate(values, begin, length) / sampleSize;

      // Compute correction factor in second pass
      double correction = 0;
      for (int i = begin; i < begin + length; i++) {
        correction += values[i] - xbar;
      }
      return xbar + (correction / sampleSize);
    }
    return Double.NaN;
  }