Java Code Examples for org.apache.commons.math3.exception.util.LocalizedFormats#INPUT_ARRAY

The following examples show how to use org.apache.commons.math3.exception.util.LocalizedFormats#INPUT_ARRAY . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EnumeratedDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generate a random sample from the distribution.
 * <p>
 * If the requested samples fit in the specified array, it is returned
 * therein. Otherwise, a new array is allocated with the runtime type of
 * the specified array and the size of this collection.
 *
 * @param sampleSize the number of random values to generate.
 * @param array the array to populate.
 * @return an array representing the random sample.
 * @throws NotStrictlyPositiveException if {@code sampleSize} is not positive.
 * @throws NullArgumentException if {@code array} is null
 */
public T[] sample(int sampleSize, final T[] array) throws NotStrictlyPositiveException {
    if (sampleSize <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES, sampleSize);
    }

    if (array == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    T[] out;
    if (array.length < sampleSize) {
        @SuppressWarnings("unchecked") // safe as both are of type T
        final T[] unchecked = (T[]) Array.newInstance(array.getClass().getComponentType(), sampleSize);
        out = unchecked;
    } else {
        out = array;
    }

    for (int i = 0; i < sampleSize; i++) {
        out[i] = sample();
    }

    return out;

}
 
Example 2
Source File: GaussianFitter.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs instance with the specified observed points.
 *
 * @param observations Observed points from which to guess the
 * parameters of the Gaussian.
 * @throws NullArgumentException if {@code observations} is
 * {@code null}.
 * @throws NumberIsTooSmallException if there are less than 3
 * observations.
 */
public ParameterGuesser(WeightedObservedPoint[] observations) {
    if (observations == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }
    if (observations.length < 3) {
        throw new NumberIsTooSmallException(observations.length, 3, true);
    }

    final WeightedObservedPoint[] sorted = sortObservations(observations);
    final double[] params = basicGuess(sorted);

    norm = params[0];
    mean = params[1];
    sigma = params[2];
}
 
Example 3
Source File: EnumeratedDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generate a random sample from the distribution.
 * <p>
 * If the requested samples fit in the specified array, it is returned
 * therein. Otherwise, a new array is allocated with the runtime type of
 * the specified array and the size of this collection.
 *
 * @param sampleSize the number of random values to generate.
 * @param array the array to populate.
 * @return an array representing the random sample.
 * @throws NotStrictlyPositiveException if {@code sampleSize} is not positive.
 * @throws NullArgumentException if {@code array} is null
 */
public T[] sample(int sampleSize, final T[] array) throws NotStrictlyPositiveException {
    if (sampleSize <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES, sampleSize);
    }

    if (array == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    T[] out;
    if (array.length < sampleSize) {
        @SuppressWarnings("unchecked") // safe as both are of type T
        final T[] unchecked = (T[]) Array.newInstance(array.getClass().getComponentType(), sampleSize);
        out = unchecked;
    } else {
        out = array;
    }

    for (int i = 0; i < sampleSize; i++) {
        out[i] = sample();
    }

    return out;

}
 
Example 4
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Set the data array.  The input array is copied, not referenced.
 *
 * @param values data array to store
 * @param begin the index of the first element to include
 * @param length the number of elements to include
 * @throws MathIllegalArgumentException if values is null or the indices
 * are not valid
 * @see #evaluate()
 */
public void setData(final double[] values, final int begin, final int length)
throws MathIllegalArgumentException {
    if (values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (begin < 0) {
        throw new NotPositiveException(LocalizedFormats.START_POSITION, begin);
    }

    if (length < 0) {
        throw new NotPositiveException(LocalizedFormats.LENGTH, length);
    }

    if (begin + length > values.length) {
        throw new NumberIsTooLargeException(LocalizedFormats.SUBARRAY_ENDS_AFTER_ARRAY_END,
                                            begin + length, values.length, true);
    }
    storedData = new double[length];
    System.arraycopy(values, begin, storedData, 0, length);
}
 
Example 5
Source File: GaussianFitter.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs instance with the specified observed points.
 *
 * @param observations Observed points from which to guess the
 * parameters of the Gaussian.
 * @throws NullArgumentException if {@code observations} is
 * {@code null}.
 * @throws NumberIsTooSmallException if there are less than 3
 * observations.
 */
public ParameterGuesser(WeightedObservedPoint[] observations) {
    if (observations == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }
    if (observations.length < 3) {
        throw new NumberIsTooSmallException(observations.length, 3, true);
    }

    final WeightedObservedPoint[] sorted = sortObservations(observations);
    final double[] params = basicGuess(sorted);

    norm = params[0];
    mean = params[1];
    sigma = params[2];
}
 
Example 6
Source File: GaussianFitter.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs instance with the specified observed points.
 *
 * @param observations Observed points from which to guess the
 * parameters of the Gaussian.
 * @throws NullArgumentException if {@code observations} is
 * {@code null}.
 * @throws NumberIsTooSmallException if there are less than 3
 * observations.
 */
public ParameterGuesser(WeightedObservedPoint[] observations) {
    if (observations == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }
    if (observations.length < 3) {
        throw new NumberIsTooSmallException(observations.length, 3, true);
    }

    final WeightedObservedPoint[] sorted = sortObservations(observations);
    final double[] params = basicGuess(sorted);

    norm = params[0];
    mean = params[1];
    sigma = params[2];
}
 
Example 7
Source File: EnumeratedDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generate a random sample from the distribution.
 * <p>
 * If the requested samples fit in the specified array, it is returned
 * therein. Otherwise, a new array is allocated with the runtime type of
 * the specified array and the size of this collection.
 *
 * @param sampleSize the number of random values to generate.
 * @param array the array to populate.
 * @return an array representing the random sample.
 * @throws NotStrictlyPositiveException if {@code sampleSize} is not positive.
 * @throws NullArgumentException if {@code array} is null
 */
public T[] sample(int sampleSize, final T[] array) throws NotStrictlyPositiveException {
    if (sampleSize <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES, sampleSize);
    }

    if (array == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    T[] out;
    if (array.length < sampleSize) {
        @SuppressWarnings("unchecked") // safe as both are of type T
        final T[] unchecked = (T[]) Array.newInstance(array.getClass().getComponentType(), sampleSize);
        out = unchecked;
    } else {
        out = array;
    }

    for (int i = 0; i < sampleSize; i++) {
        out[i] = sample();
    }

    return out;

}
 
Example 8
Source File: GaussianFitter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs instance with the specified observed points.
 *
 * @param observations observed points upon which should base guess
 */
public ParameterGuesser(WeightedObservedPoint[] observations) {
    if (observations == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }
    if (observations.length < 3) {
        throw new NumberIsTooSmallException(observations.length, 3, true);
    }
    this.observations = observations.clone();
}
 
Example 9
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method is used by <code>evaluate(double[], int, int)</code> methods
 * to verify that the input parameters designate a subarray of positive length.
 * <p>
 * <ul>
 * <li>returns <code>true</code> iff the parameters designate a subarray of
 * non-negative length</li>
 * <li>throws <code>IllegalArgumentException</code> if the array is null or
 * or the indices are invalid</li>
 * <li>returns <code>false</li> if the array is non-null, but
 * <code>length</code> is 0 unless <code>allowEmpty</code> is <code>true</code>
 * </ul></p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @param allowEmpty if <code>true</code> then zero length arrays are allowed
 * @return true if the parameters are valid
 * @throws IllegalArgumentException if the indices are invalid or the array is null
 * @since 3.0
 */
protected boolean test(final double[] values, final int begin, final int length, final boolean allowEmpty){

    if (values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (begin < 0) {
        throw new NotPositiveException(LocalizedFormats.START_POSITION, begin);
    }

    if (length < 0) {
        throw new NotPositiveException(LocalizedFormats.LENGTH, length);
    }

    if (begin + length > values.length) {
        throw new NumberIsTooLargeException(LocalizedFormats.SUBARRAY_ENDS_AFTER_ARRAY_END,
                                            begin + length, values.length, true);
    }

    if (length == 0 && !allowEmpty) {
        return false;
    }

    return true;

}
 
Example 10
Source File: MathArrays.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method is used
 * to verify that the begin and length parameters designate a subarray of positive length
 * and the weights are all non-negative, non-NaN, finite, and not all zero.
 * <p>
 * <ul>
 * <li>returns <code>true</code> iff the parameters designate a subarray of
 * non-negative length and the weights array contains legitimate values.</li>
 * <li>throws <code>MathIllegalArgumentException</code> if any of the following are true:
 * <ul><li>the values array is null</li>
 *     <li>the weights array is null</li>
 *     <li>the weights array does not have the same length as the values array</li>
 *     <li>the weights array contains one or more infinite values</li>
 *     <li>the weights array contains one or more NaN values</li>
 *     <li>the weights array contains negative values</li>
 *     <li>the start and length arguments do not determine a valid array</li></ul>
 * </li>
 * <li>returns <code>false</li> if the array is non-null, but
 * <code>length</code> is 0 unless <code>allowEmpty</code> is <code>true</code>.
 * </ul></p>
 *
 * @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.
 * @param allowEmpty if {@code true} than allow zero length arrays to pass.
 * @return {@code true} if the parameters are valid.
 * @throws NullArgumentException if either of the arrays are null
 * @throws MathIllegalArgumentException if the array indices are not valid,
 * the weights array contains NaN, infinite or negative elements, or there
 * are no positive weights.
 * @since 3.3
 */
public static boolean verifyValues(final double[] values, final double[] weights,
        final int begin, final int length, final boolean allowEmpty) throws MathIllegalArgumentException {

    if (weights == null || values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (weights.length != values.length) {
        throw new DimensionMismatchException(weights.length, values.length);
    }

    boolean containsPositiveWeight = false;
    for (int i = begin; i < begin + length; i++) {
        final double weight = weights[i];
        if (Double.isNaN(weight)) {
            throw new MathIllegalArgumentException(LocalizedFormats.NAN_ELEMENT_AT_INDEX, Integer.valueOf(i));
        }
        if (Double.isInfinite(weight)) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, Double.valueOf(weight), Integer.valueOf(i));
        }
        if (weight < 0) {
            throw new MathIllegalArgumentException(LocalizedFormats.NEGATIVE_ELEMENT_AT_INDEX, Integer.valueOf(i), Double.valueOf(weight));
        }
        if (!containsPositiveWeight && weight > 0.0) {
            containsPositiveWeight = true;
        }
    }

    if (!containsPositiveWeight) {
        throw new MathIllegalArgumentException(LocalizedFormats.WEIGHT_AT_LEAST_ONE_NON_ZERO);
    }

    return verifyValues(values, begin, length, allowEmpty);
}
 
Example 11
Source File: MathArrays.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method is used
 * to verify that the input parameters designate a subarray of positive length.
 * <p>
 * <ul>
 * <li>returns <code>true</code> iff the parameters designate a subarray of
 * non-negative length</li>
 * <li>throws <code>IllegalArgumentException</code> if the array is null or
 * or the indices are invalid</li>
 * <li>returns <code>false</li> if the array is non-null, but
 * <code>length</code> is 0 unless <code>allowEmpty</code> is <code>true</code>
 * </ul></p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @param allowEmpty if <code>true</code> then zero length arrays are allowed
 * @return true if the parameters are valid
 * @throws MathIllegalArgumentException if the indices are invalid or the array is null
 * @since 3.3
 */
public static boolean verifyValues(final double[] values, final int begin,
        final int length, final boolean allowEmpty) throws MathIllegalArgumentException {

    if (values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (begin < 0) {
        throw new NotPositiveException(LocalizedFormats.START_POSITION, Integer.valueOf(begin));
    }

    if (length < 0) {
        throw new NotPositiveException(LocalizedFormats.LENGTH, Integer.valueOf(length));
    }

    if (begin + length > values.length) {
        throw new NumberIsTooLargeException(LocalizedFormats.SUBARRAY_ENDS_AFTER_ARRAY_END,
                Integer.valueOf(begin + length), Integer.valueOf(values.length), true);
    }

    if (length == 0 && !allowEmpty) {
        return false;
    }

    return true;

}
 
Example 12
Source File: StatUtils.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns the sample mode(s).  The mode is the most frequently occurring
 * value in the sample. If there is a unique value with maximum frequency,
 * this value is returned as the only element of the output array. Otherwise,
 * the returned array contains the maximum frequency elements in increasing
 * order.  For example, if {@code sample} is {0, 12, 5, 6, 0, 13, 5, 17},
 * the returned array will have length two, with 0 in the first element and
 * 5 in the second.
 *
 * <p>NaN values are ignored when computing the mode - i.e., NaNs will never
 * appear in the output array.  If the sample includes only NaNs or has
 * length 0, an empty array is returned.</p>
 *
 * @param sample input data
 * @param begin index (0-based) of the first array element to include
 * @param length the number of elements to include
 *
 * @return array of array of the most frequently occurring element(s) sorted in ascending order.
 * @throws MathIllegalArgumentException if the indices are invalid or the array is null
 * @since 3.3
 */
public static double[] mode(double[] sample, final int begin, final int length) {
    if (sample == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (begin < 0) {
        throw new NotPositiveException(LocalizedFormats.START_POSITION, Integer.valueOf(begin));
    }

    if (length < 0) {
        throw new NotPositiveException(LocalizedFormats.LENGTH, Integer.valueOf(length));
    }

    return getMode(sample, begin, length);
}
 
Example 13
Source File: Variance.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the variance of the entries in the input array, or
 * <code>Double.NaN</code> if the array is empty.
 * <p>
 * See {@link Variance} for details on the computing algorithm.</p>
 * <p>
 * Returns 0 for a single-value (i.e. length = 1) sample.</p>
 * <p>
 * Throws <code>MathIllegalArgumentException</code> if the array is null.</p>
 * <p>
 * Does not change the internal state of the statistic.</p>
 *
 * @param values the input array
 * @return the variance of the values or Double.NaN if length = 0
 * @throws MathIllegalArgumentException if the array is null
 */
@Override
public double evaluate(final double[] values) throws MathIllegalArgumentException {
    if (values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }
    return evaluate(values, 0, values.length);
}
 
Example 14
Source File: AbstractStorelessUnivariateStatistic.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * This default implementation calls {@link #clear}, then invokes
 * {@link #increment} in a loop over the the input array, and then uses
 * {@link #getResult} to compute the return value.
 * <p>
 * Note that this implementation changes the internal state of the
 * statistic.  Its side effects are the same as invoking {@link #clear} and
 * then {@link #incrementAll(double[])}.</p>
 * <p>
 * Implementations may override this method with a more efficient and
 * possibly more accurate implementation that works directly with the
 * input array.</p>
 * <p>
 * If the array is null, a MathIllegalArgumentException is thrown.</p>
 * @param values input array
 * @return the value of the statistic applied to the input array
 * @throws MathIllegalArgumentException if values is null
 * @see org.apache.commons.math3.stat.descriptive.UnivariateStatistic#evaluate(double[])
 */
@Override
public double evaluate(final double[] values) throws MathIllegalArgumentException {
    if (values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }
    return evaluate(values, 0, values.length);
}
 
Example 15
Source File: AbstractStorelessUnivariateStatistic.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * This default implementation calls {@link #clear}, then invokes
 * {@link #increment} in a loop over the the input array, and then uses
 * {@link #getResult} to compute the return value.
 * <p>
 * Note that this implementation changes the internal state of the
 * statistic.  Its side effects are the same as invoking {@link #clear} and
 * then {@link #incrementAll(double[])}.</p>
 * <p>
 * Implementations may override this method with a more efficient and
 * possibly more accurate implementation that works directly with the
 * input array.</p>
 * <p>
 * If the array is null, a MathIllegalArgumentException is thrown.</p>
 * @param values input array
 * @return the value of the statistic applied to the input array
 * @throws MathIllegalArgumentException if values is null
 * @see org.apache.commons.math3.stat.descriptive.UnivariateStatistic#evaluate(double[])
 */
@Override
public double evaluate(final double[] values) throws MathIllegalArgumentException {
    if (values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }
    return evaluate(values, 0, values.length);
}
 
Example 16
Source File: AbstractStorelessUnivariateStatistic.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * This default implementation just calls {@link #increment} in a loop over
 * the input array.
 * <p>
 * Throws IllegalArgumentException if the input values array is null.</p>
 *
 * @param values values to add
 * @throws IllegalArgumentException if values is null
 * @see org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic#incrementAll(double[])
 */
public void incrementAll(double[] values) {
    if (values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }
    incrementAll(values, 0, values.length);
}
 
Example 17
Source File: AbstractStorelessUnivariateStatistic.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * This default implementation just calls {@link #increment} in a loop over
 * the input array.
 * <p>
 * Throws IllegalArgumentException if the input values array is null.</p>
 *
 * @param values values to add
 * @throws MathIllegalArgumentException if values is null
 * @see org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic#incrementAll(double[])
 */
public void incrementAll(double[] values) throws MathIllegalArgumentException {
    if (values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }
    incrementAll(values, 0, values.length);
}
 
Example 18
Source File: StatUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the sample mode(s).  The mode is the most frequently occurring
 * value in the sample. If there is a unique value with maximum frequency,
 * this value is returned as the only element of the output array. Otherwise,
 * the returned array contains the maximum frequency elements in increasing
 * order.  For example, if {@code sample} is {0, 12, 5, 6, 0, 13, 5, 17},
 * the returned array will have length two, with 0 in the first element and
 * 5 in the second.
 *
 * <p>NaN values are ignored when computing the mode - i.e., NaNs will never
 * appear in the output array.  If the sample includes only NaNs or has
 * length 0, an empty array is returned.</p>
 *
 * @param sample input data
 * @return array of array of the most frequently occurring element(s) sorted in ascending order.
 * @throws MathIllegalArgumentException if the indices are invalid or the array is null
 * @since 3.3
 */
public static double[] mode(double[] sample) throws MathIllegalArgumentException {
    if (sample == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }
    return getMode(sample, 0, sample.length);
}
 
Example 19
Source File: Variance.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the variance of the entries in the input array, or
 * <code>Double.NaN</code> if the array is empty.
 * <p>
 * See {@link Variance} for details on the computing algorithm.</p>
 * <p>
 * Returns 0 for a single-value (i.e. length = 1) sample.</p>
 * <p>
 * Throws <code>MathIllegalArgumentException</code> if the array is null.</p>
 * <p>
 * Does not change the internal state of the statistic.</p>
 *
 * @param values the input array
 * @return the variance of the values or Double.NaN if length = 0
 * @throws MathIllegalArgumentException if the array is null
 */
@Override
public double evaluate(final double[] values) throws MathIllegalArgumentException {
    if (values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }
    return evaluate(values, 0, values.length);
}
 
Example 20
Source File: Variance.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the variance of the entries in the input array, or
 * <code>Double.NaN</code> if the array is empty.
 * <p>
 * See {@link Variance} for details on the computing algorithm.</p>
 * <p>
 * Returns 0 for a single-value (i.e. length = 1) sample.</p>
 * <p>
 * Throws <code>MathIllegalArgumentException</code> if the array is null.</p>
 * <p>
 * Does not change the internal state of the statistic.</p>
 *
 * @param values the input array
 * @return the variance of the values or Double.NaN if length = 0
 * @throws MathIllegalArgumentException if the array is null
 */
@Override
public double evaluate(final double[] values) throws MathIllegalArgumentException {
    if (values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }
    return evaluate(values, 0, values.length);
}