org.apache.commons.math3.exception.NotANumberException Java Examples

The following examples show how to use org.apache.commons.math3.exception.NotANumberException. 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
/**
 * Create an enumerated distribution using the given random number generator
 * and probability mass function enumeration.
 *
 * @param rng random number generator.
 * @param pmf probability mass function enumerated as a list of <T, probability>
 * pairs.
 * @throws NotPositiveException if any of the probabilities are negative.
 * @throws NotFiniteNumberException if any of the probabilities are infinite.
 * @throws NotANumberException if any of the probabilities are NaN.
 * @throws MathArithmeticException all of the probabilities are 0.
 */
public EnumeratedDistribution(final RandomGenerator rng, final List<Pair<T, Double>> pmf)
    throws NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException {
    random = rng;

    singletons = new ArrayList<T>(pmf.size());
    final double[] probs = new double[pmf.size()];

    for (int i = 0; i < pmf.size(); i++) {
        final Pair<T, Double> sample = pmf.get(i);
        singletons.add(sample.getKey());
        final double p = sample.getValue();
        if (p < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        if (Double.isInfinite(p)) {
            throw new NotFiniteNumberException(p);
        }
        if (Double.isNaN(p)) {
            throw new NotANumberException();
        }
        probs[i] = p;
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);
}
 
Example #2
Source File: EnumeratedIntegerDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function definition.
 *
 * @param rng random number generator.
 * @param singletons array of random variable values.
 * @param probabilities array of probabilities.
 * @throws DimensionMismatchException if
 * {@code singletons.length != probabilities.length}
 * @throws NotPositiveException if any of the probabilities are negative.
 * @throws NotFiniteNumberException if any of the probabilities are infinite.
 * @throws NotANumberException if any of the probabilities are NaN.
 * @throws MathArithmeticException all of the probabilities are 0.
 */
public EnumeratedIntegerDistribution(final RandomGenerator rng,
                                   final int[] singletons, final double[] probabilities)
    throws DimensionMismatchException, NotPositiveException, MathArithmeticException,
            NotFiniteNumberException, NotANumberException {
    super(rng);
    if (singletons.length != probabilities.length) {
        throw new DimensionMismatchException(probabilities.length, singletons.length);
    }

    final List<Pair<Integer, Double>> samples = new ArrayList<Pair<Integer, Double>>(singletons.length);

    for (int i = 0; i < singletons.length; i++) {
        samples.add(new Pair<Integer, Double>(singletons[i], probabilities[i]));
    }

    innerDistribution = new EnumeratedDistribution<Integer>(rng, samples);
}
 
Example #3
Source File: EnumeratedIntegerDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function definition.
 *
 * @param rng random number generator.
 * @param singletons array of random variable values.
 * @param probabilities array of probabilities.
 * @throws DimensionMismatchException if
 * {@code singletons.length != probabilities.length}
 * @throws NotPositiveException if any of the probabilities are negative.
 * @throws NotFiniteNumberException if any of the probabilities are infinite.
 * @throws NotANumberException if any of the probabilities are NaN.
 * @throws MathArithmeticException all of the probabilities are 0.
 */
public EnumeratedIntegerDistribution(final RandomGenerator rng,
                                   final int[] singletons, final double[] probabilities)
    throws DimensionMismatchException, NotPositiveException, MathArithmeticException,
            NotFiniteNumberException, NotANumberException {
    super(rng);
    if (singletons.length != probabilities.length) {
        throw new DimensionMismatchException(probabilities.length, singletons.length);
    }

    final List<Pair<Integer, Double>> samples = new ArrayList<Pair<Integer, Double>>(singletons.length);

    for (int i = 0; i < singletons.length; i++) {
        samples.add(new Pair<Integer, Double>(singletons[i], probabilities[i]));
    }

    innerDistribution = new EnumeratedDistribution<Integer>(rng, samples);
}
 
Example #4
Source File: EnumeratedIntegerDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function definition.
 *
 * @param rng random number generator.
 * @param singletons array of random variable values.
 * @param probabilities array of probabilities.
 * @throws DimensionMismatchException if
 * {@code singletons.length != probabilities.length}
 * @throws NotPositiveException if any of the probabilities are negative.
 * @throws NotFiniteNumberException if any of the probabilities are infinite.
 * @throws NotANumberException if any of the probabilities are NaN.
 * @throws MathArithmeticException all of the probabilities are 0.
 */
public EnumeratedIntegerDistribution(final RandomGenerator rng,
                                   final int[] singletons, final double[] probabilities)
    throws DimensionMismatchException, NotPositiveException, MathArithmeticException,
            NotFiniteNumberException, NotANumberException {
    super(rng);
    if (singletons.length != probabilities.length) {
        throw new DimensionMismatchException(probabilities.length, singletons.length);
    }

    final List<Pair<Integer, Double>> samples = new ArrayList<Pair<Integer, Double>>(singletons.length);

    for (int i = 0; i < singletons.length; i++) {
        samples.add(new Pair<Integer, Double>(singletons[i], probabilities[i]));
    }

    innerDistribution = new EnumeratedDistribution<Integer>(rng, samples);
}
 
Example #5
Source File: EnumeratedDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create an enumerated distribution using the given random number generator
 * and probability mass function enumeration.
 *
 * @param rng random number generator.
 * @param pmf probability mass function enumerated as a list of <T, probability>
 * pairs.
 * @throws NotPositiveException if any of the probabilities are negative.
 * @throws NotFiniteNumberException if any of the probabilities are infinite.
 * @throws NotANumberException if any of the probabilities are NaN.
 * @throws MathArithmeticException all of the probabilities are 0.
 */
public EnumeratedDistribution(final RandomGenerator rng, final List<Pair<T, Double>> pmf)
    throws NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException {
    random = rng;

    singletons = new ArrayList<T>(pmf.size());
    final double[] probs = new double[pmf.size()];

    for (int i = 0; i < pmf.size(); i++) {
        final Pair<T, Double> sample = pmf.get(i);
        singletons.add(sample.getKey());
        final double p = sample.getValue();
        if (p < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        if (Double.isInfinite(p)) {
            throw new NotFiniteNumberException(p);
        }
        if (Double.isNaN(p)) {
            throw new NotANumberException();
        }
        probs[i] = p;
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);
}
 
Example #6
Source File: EnumeratedRealDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function enumeration.
 *
 * @param rng random number generator.
 * @param singletons array of random variable values.
 * @param probabilities array of probabilities.
 * @throws DimensionMismatchException if
 * {@code singletons.length != probabilities.length}
 * @throws NotPositiveException if any of the probabilities are negative.
 * @throws NotFiniteNumberException if any of the probabilities are infinite.
 * @throws NotANumberException if any of the probabilities are NaN.
 * @throws MathArithmeticException all of the probabilities are 0.
 */
public EnumeratedRealDistribution(final RandomGenerator rng,
                                final double[] singletons, final double[] probabilities)
    throws DimensionMismatchException, NotPositiveException, MathArithmeticException,
           NotFiniteNumberException, NotANumberException {
    super(rng);
    if (singletons.length != probabilities.length) {
        throw new DimensionMismatchException(probabilities.length, singletons.length);
    }

    List<Pair<Double, Double>> samples = new ArrayList<Pair<Double, Double>>(singletons.length);

    for (int i = 0; i < singletons.length; i++) {
        samples.add(new Pair<Double, Double>(singletons[i], probabilities[i]));
    }

    innerDistribution = new EnumeratedDistribution<Double>(rng, samples);
}
 
Example #7
Source File: EnumeratedRealDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function enumeration.
 *
 * @param rng random number generator.
 * @param singletons array of random variable values.
 * @param probabilities array of probabilities.
 * @throws DimensionMismatchException if
 * {@code singletons.length != probabilities.length}
 * @throws NotPositiveException if any of the probabilities are negative.
 * @throws NotFiniteNumberException if any of the probabilities are infinite.
 * @throws NotANumberException if any of the probabilities are NaN.
 * @throws MathArithmeticException all of the probabilities are 0.
 */
public EnumeratedRealDistribution(final RandomGenerator rng,
                                final double[] singletons, final double[] probabilities)
    throws DimensionMismatchException, NotPositiveException, MathArithmeticException,
           NotFiniteNumberException, NotANumberException {
    super(rng);
    if (singletons.length != probabilities.length) {
        throw new DimensionMismatchException(probabilities.length, singletons.length);
    }

    List<Pair<Double, Double>> samples = new ArrayList<Pair<Double, Double>>(singletons.length);

    for (int i = 0; i < singletons.length; i++) {
        samples.add(new Pair<Double, Double>(singletons[i], probabilities[i]));
    }

    innerDistribution = new EnumeratedDistribution<Double>(rng, samples);
}
 
Example #8
Source File: EnumeratedRealDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function enumeration.
 *
 * @param rng random number generator.
 * @param singletons array of random variable values.
 * @param probabilities array of probabilities.
 * @throws DimensionMismatchException if
 * {@code singletons.length != probabilities.length}
 * @throws NotPositiveException if any of the probabilities are negative.
 * @throws NotFiniteNumberException if any of the probabilities are infinite.
 * @throws NotANumberException if any of the probabilities are NaN.
 * @throws MathArithmeticException all of the probabilities are 0.
 */
public EnumeratedRealDistribution(final RandomGenerator rng,
                                final double[] singletons, final double[] probabilities)
    throws DimensionMismatchException, NotPositiveException, MathArithmeticException,
           NotFiniteNumberException, NotANumberException {
    super(rng);
    if (singletons.length != probabilities.length) {
        throw new DimensionMismatchException(probabilities.length, singletons.length);
    }

    List<Pair<Double, Double>> samples = new ArrayList<Pair<Double, Double>>(singletons.length);

    for (int i = 0; i < singletons.length; i++) {
        samples.add(new Pair<Double, Double>(singletons[i], probabilities[i]));
    }

    innerDistribution = new EnumeratedDistribution<Double>(rng, samples);
}
 
Example #9
Source File: EnumeratedIntegerDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function definition.
 *
 * @param rng random number generator.
 * @param singletons array of random variable values.
 * @param probabilities array of probabilities.
 * @throws DimensionMismatchException if
 * {@code singletons.length != probabilities.length}
 * @throws NotPositiveException if any of the probabilities are negative.
 * @throws NotFiniteNumberException if any of the probabilities are infinite.
 * @throws NotANumberException if any of the probabilities are NaN.
 * @throws MathArithmeticException all of the probabilities are 0.
 */
public EnumeratedIntegerDistribution(final RandomGenerator rng,
                                   final int[] singletons, final double[] probabilities)
    throws DimensionMismatchException, NotPositiveException, MathArithmeticException,
            NotFiniteNumberException, NotANumberException {
    super(rng);
    if (singletons.length != probabilities.length) {
        throw new DimensionMismatchException(probabilities.length, singletons.length);
    }

    final List<Pair<Integer, Double>> samples = new ArrayList<Pair<Integer, Double>>(singletons.length);

    for (int i = 0; i < singletons.length; i++) {
        samples.add(new Pair<Integer, Double>(singletons[i], probabilities[i]));
    }

    innerDistribution = new EnumeratedDistribution<Integer>(rng, samples);
}
 
Example #10
Source File: EnumeratedRealDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function enumeration.
 *
 * @param rng random number generator.
 * @param singletons array of random variable values.
 * @param probabilities array of probabilities.
 * @throws DimensionMismatchException if
 * {@code singletons.length != probabilities.length}
 * @throws NotPositiveException if any of the probabilities are negative.
 * @throws NotFiniteNumberException if any of the probabilities are infinite.
 * @throws NotANumberException if any of the probabilities are NaN.
 * @throws MathArithmeticException all of the probabilities are 0.
 */
public EnumeratedRealDistribution(final RandomGenerator rng,
                                final double[] singletons, final double[] probabilities)
    throws DimensionMismatchException, NotPositiveException, MathArithmeticException,
           NotFiniteNumberException, NotANumberException {
    super(rng);
    if (singletons.length != probabilities.length) {
        throw new DimensionMismatchException(probabilities.length, singletons.length);
    }

    List<Pair<Double, Double>> samples = new ArrayList<Pair<Double, Double>>(singletons.length);

    for (int i = 0; i < singletons.length; i++) {
        samples.add(new Pair<Double, Double>(singletons[i], probabilities[i]));
    }

    innerDistribution = new EnumeratedDistribution<Double>(rng, samples);
}
 
Example #11
Source File: EnumeratedDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create an enumerated distribution using the given random number generator
 * and probability mass function enumeration.
 *
 * @param rng random number generator.
 * @param pmf probability mass function enumerated as a list of <T, probability>
 * pairs.
 * @throws NotPositiveException if any of the probabilities are negative.
 * @throws NotFiniteNumberException if any of the probabilities are infinite.
 * @throws NotANumberException if any of the probabilities are NaN.
 * @throws MathArithmeticException all of the probabilities are 0.
 */
public EnumeratedDistribution(final RandomGenerator rng, final List<Pair<T, Double>> pmf)
    throws NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException {
    random = rng;

    singletons = new ArrayList<T>(pmf.size());
    final double[] probs = new double[pmf.size()];

    for (int i = 0; i < pmf.size(); i++) {
        final Pair<T, Double> sample = pmf.get(i);
        singletons.add(sample.getKey());
        final double p = sample.getValue();
        if (p < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        if (Double.isInfinite(p)) {
            throw new NotFiniteNumberException(p);
        }
        if (Double.isNaN(p)) {
            throw new NotANumberException();
        }
        probs[i] = p;
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);
}
 
Example #12
Source File: EnumeratedIntegerDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function definition.
 *
 * @param rng random number generator.
 * @param singletons array of random variable values.
 * @param probabilities array of probabilities.
 * @throws DimensionMismatchException if
 * {@code singletons.length != probabilities.length}
 * @throws NotPositiveException if any of the probabilities are negative.
 * @throws NotFiniteNumberException if any of the probabilities are infinite.
 * @throws NotANumberException if any of the probabilities are NaN.
 * @throws MathArithmeticException all of the probabilities are 0.
 */
public EnumeratedIntegerDistribution(final RandomGenerator rng,
                                   final int[] singletons, final double[] probabilities)
    throws DimensionMismatchException, NotPositiveException, MathArithmeticException,
            NotFiniteNumberException, NotANumberException {
    super(rng);
    if (singletons.length != probabilities.length) {
        throw new DimensionMismatchException(probabilities.length, singletons.length);
    }

    final List<Pair<Integer, Double>> samples = new ArrayList<Pair<Integer, Double>>(singletons.length);

    for (int i = 0; i < singletons.length; i++) {
        samples.add(new Pair<Integer, Double>(singletons[i], probabilities[i]));
    }

    innerDistribution = new EnumeratedDistribution<Integer>(rng, samples);
}
 
Example #13
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>
 * <strong>Algorithm Description</strong>: if the lower bound is excluded,
 * scales the output of Random.nextDouble(), but rejects 0 values (i.e.,
 * will generate another random double if Random.nextDouble() returns 0).
 * This is necessary to provide a symmetric output interval (both
 * endpoints excluded).
 * </p>
 *
 * @throws NumberIsTooLargeException if {@code lower >= upper}
 * @throws NotFiniteNumberException if one of the bounds is infinite
 * @throws NotANumberException if one of the bounds is NaN
 */
public double nextUniform(double lower, double upper, boolean lowerInclusive)
    throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException {

    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }

    if (Double.isInfinite(lower)) {
        throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, lower);
    }
    if (Double.isInfinite(upper)) {
        throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, upper);
    }

    if (Double.isNaN(lower) || Double.isNaN(upper)) {
        throw new NotANumberException();
    }

    final RandomGenerator generator = getRandomGenerator();

    // ensure nextDouble() isn't 0.0
    double u = generator.nextDouble();
    while (!lowerInclusive && u <= 0.0) {
        u = generator.nextDouble();
    }

    return u * upper + (1.0 - u) * lower;
}
 
Example #14
Source File: PercentileTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=NotANumberException.class)
public void testNanStrategyFailed() {
    double[] specialValues =
            new double[] { 0d, 1d, 2d, 3d, 4d, Double.NaN };
    new Percentile(50d).
    withEstimationType(Percentile.EstimationType.R_9).
    withNaNStrategy(NaNStrategy.FAILED).
    evaluate(specialValues);
}
 
Example #15
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>
 * <strong>Algorithm Description</strong>: if the lower bound is excluded,
 * scales the output of Random.nextDouble(), but rejects 0 values (i.e.,
 * will generate another random double if Random.nextDouble() returns 0).
 * This is necessary to provide a symmetric output interval (both
 * endpoints excluded).
 * </p>
 *
 * @throws NumberIsTooLargeException if {@code lower >= upper}
 * @throws NotFiniteNumberException if one of the bounds is infinite
 * @throws NotANumberException if one of the bounds is NaN
 */
public double nextUniform(double lower, double upper, boolean lowerInclusive)
    throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException {

    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }

    if (Double.isInfinite(lower)) {
        throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, lower);
    }
    if (Double.isInfinite(upper)) {
        throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, upper);
    }

    if (Double.isNaN(lower) || Double.isNaN(upper)) {
        throw new NotANumberException();
    }

    final RandomGenerator generator = getRandomGenerator();

    // ensure nextDouble() isn't 0.0
    double u = generator.nextDouble();
    while (!lowerInclusive && u <= 0.0) {
        u = generator.nextDouble();
    }

    return u * upper + (1.0 - u) * lower;
}
 
Example #16
Source File: EnumeratedDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create an enumerated distribution using the given random number generator
 * and probability mass function enumeration.
 *
 * @param rng random number generator.
 * @param pmf probability mass function enumerated as a list of <T, probability>
 * pairs.
 * @throws NotPositiveException if any of the probabilities are negative.
 * @throws NotFiniteNumberException if any of the probabilities are infinite.
 * @throws NotANumberException if any of the probabilities are NaN.
 * @throws MathArithmeticException all of the probabilities are 0.
 */
public EnumeratedDistribution(final RandomGenerator rng, final List<Pair<T, Double>> pmf)
    throws NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException {
    random = rng;

    singletons = new ArrayList<T>(pmf.size());
    final double[] probs = new double[pmf.size()];

    for (int i = 0; i < pmf.size(); i++) {
        final Pair<T, Double> sample = pmf.get(i);
        singletons.add(sample.getKey());
        final double p = sample.getValue();
        if (p < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        if (Double.isInfinite(p)) {
            throw new NotFiniteNumberException(p);
        }
        if (Double.isNaN(p)) {
            throw new NotANumberException();
        }
        probs[i] = p;
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);

    cumulativeProbabilities = new double[probabilities.length];
    double sum = 0;
    for (int i = 0; i < probabilities.length; i++) {
        sum += probabilities[i];
        cumulativeProbabilities[i] = sum;
    }
}
 
Example #17
Source File: MathArrays.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check that no entry of the input array is {@code NaN}.
 *
 * @param in Array to be tested.
 * @throws NotANumberException if an entry is {@code NaN}.
 * @since 3.4
 */
public static void checkNotNaN(final double[] in)
    throws NotANumberException {
    for(int i = 0; i < in.length; i++) {
        if (Double.isNaN(in[i])) {
            throw new NotANumberException();
        }
    }
}
 
Example #18
Source File: EnumeratedDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create an enumerated distribution using the given random number generator
 * and probability mass function enumeration.
 *
 * @param rng random number generator.
 * @param pmf probability mass function enumerated as a list of <T, probability>
 * pairs.
 * @throws NotPositiveException if any of the probabilities are negative.
 * @throws NotFiniteNumberException if any of the probabilities are infinite.
 * @throws NotANumberException if any of the probabilities are NaN.
 * @throws MathArithmeticException all of the probabilities are 0.
 */
public EnumeratedDistribution(final RandomGenerator rng, final List<Pair<T, Double>> pmf)
    throws NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException {
    random = rng;

    singletons = new ArrayList<T>(pmf.size());
    final double[] probs = new double[pmf.size()];

    for (int i = 0; i < pmf.size(); i++) {
        final Pair<T, Double> sample = pmf.get(i);
        singletons.add(sample.getKey());
        final double p = sample.getValue();
        if (p < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        if (Double.isInfinite(p)) {
            throw new NotFiniteNumberException(p);
        }
        if (Double.isNaN(p)) {
            throw new NotANumberException();
        }
        probs[i] = p;
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);

    cumulativeProbabilities = new double[probabilities.length];
    double sum = 0;
    for (int i = 0; i < probabilities.length; i++) {
        sum += probabilities[i];
        cumulativeProbabilities[i] = sum;
    }
}
 
Example #19
Source File: MathArrays.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check that no entry of the input array is {@code NaN}.
 *
 * @param in Array to be tested.
 * @throws NotANumberException if an entry is {@code NaN}.
 * @since 3.4
 */
public static void checkNotNaN(final double[] in)
    throws NotANumberException {
    for(int i = 0; i < in.length; i++) {
        if (Double.isNaN(in[i])) {
            throw new NotANumberException();
        }
    }
}
 
Example #20
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>
 * <strong>Algorithm Description</strong>: if the lower bound is excluded,
 * scales the output of Random.nextDouble(), but rejects 0 values (i.e.,
 * will generate another random double if Random.nextDouble() returns 0).
 * This is necessary to provide a symmetric output interval (both
 * endpoints excluded).
 * </p>
 *
 * @throws NumberIsTooLargeException if {@code lower >= upper}
 * @throws NotFiniteNumberException if one of the bounds is infinite
 * @throws NotANumberException if one of the bounds is NaN
 */
public double nextUniform(double lower, double upper, boolean lowerInclusive)
    throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException {

    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }

    if (Double.isInfinite(lower)) {
        throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, lower);
    }
    if (Double.isInfinite(upper)) {
        throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, upper);
    }

    if (Double.isNaN(lower) || Double.isNaN(upper)) {
        throw new NotANumberException();
    }

    final RandomGenerator generator = getRandomGenerator();

    // ensure nextDouble() isn't 0.0
    double u = generator.nextDouble();
    while (!lowerInclusive && u <= 0.0) {
        u = generator.nextDouble();
    }

    return u * upper + (1.0 - u) * lower;
}
 
Example #21
Source File: PercentileTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=NotANumberException.class)
public void testNanStrategyFailed() {
    double[] specialValues =
            new double[] { 0d, 1d, 2d, 3d, 4d, Double.NaN };
    new Percentile(50d).
    withEstimationType(Percentile.EstimationType.R_9).
    withNaNStrategy(NaNStrategy.FAILED).
    evaluate(specialValues);
}
 
Example #22
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>
 * <strong>Algorithm Description</strong>: if the lower bound is excluded,
 * scales the output of Random.nextDouble(), but rejects 0 values (i.e.,
 * will generate another random double if Random.nextDouble() returns 0).
 * This is necessary to provide a symmetric output interval (both
 * endpoints excluded).
 * </p>
 *
 * @throws NumberIsTooLargeException if {@code lower >= upper}
 * @throws NotFiniteNumberException if one of the bounds is infinite
 * @throws NotANumberException if one of the bounds is NaN
 */
public double nextUniform(double lower, double upper, boolean lowerInclusive)
    throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException {

    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }

    if (Double.isInfinite(lower)) {
        throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, lower);
    }
    if (Double.isInfinite(upper)) {
        throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, upper);
    }

    if (Double.isNaN(lower) || Double.isNaN(upper)) {
        throw new NotANumberException();
    }

    final RandomGenerator generator = getRandomGenerator();

    // ensure nextDouble() isn't 0.0
    double u = generator.nextDouble();
    while (!lowerInclusive && u <= 0.0) {
        u = generator.nextDouble();
    }

    return u * upper + (1.0 - u) * lower;
}
 
Example #23
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>
 * <strong>Algorithm Description</strong>: if the lower bound is excluded,
 * scales the output of Random.nextDouble(), but rejects 0 values (i.e.,
 * will generate another random double if Random.nextDouble() returns 0).
 * This is necessary to provide a symmetric output interval (both
 * endpoints excluded).
 * </p>
 *
 * @throws N if one of the bounds is infinite or
 * {@code NaN}
 * @throws NumberIsTooLargeException if {@code lower >= upper}
 * @throws NotFiniteNumberException if one of the bounds is infinite
 * @throws NotANumberException if one of the bounds is not a number
 */
public double nextUniform(double lower, double upper, boolean lowerInclusive)
    throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException {

    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }

    if (Double.isInfinite(lower)) {
        throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, lower);
    }
    if (Double.isInfinite(upper)) {
        throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, upper);
    }

    if (Double.isNaN(lower) || Double.isNaN(upper)) {
        throw new NotANumberException();
    }

    final RandomGenerator generator = getRan();

    // ensure nextDouble() isn't 0.0
    double u = generator.nextDouble();
    while (!lowerInclusive && u <= 0.0) {
        u = generator.nextDouble();
    }

    return u * upper + (1.0 - u) * lower;
}
 
Example #24
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>
 * <strong>Algorithm Description</strong>: if the lower bound is excluded,
 * scales the output of Random.nextDouble(), but rejects 0 values (i.e.,
 * will generate another random double if Random.nextDouble() returns 0).
 * This is necessary to provide a symmetric output interval (both
 * endpoints excluded).
 * </p>
 *
 * @throws NumberIsTooLargeException if {@code lower >= upper}
 * @throws NotFiniteNumberException if one of the bounds is infinite
 * @throws NotANumberException if one of the bounds is NaN
 */
public double nextUniform(double lower, double upper, boolean lowerInclusive)
    throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException {

    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }

    if (Double.isInfinite(lower)) {
        throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, lower);
    }
    if (Double.isInfinite(upper)) {
        throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, upper);
    }

    if (Double.isNaN(lower) || Double.isNaN(upper)) {
        throw new NotANumberException();
    }

    final RandomGenerator generator = getRandomGenerator();

    // ensure nextDouble() isn't 0.0
    double u = generator.nextDouble();
    while (!lowerInclusive && u <= 0.0) {
        u = generator.nextDouble();
    }

    return u * upper + (1.0 - u) * lower;
}
 
Example #25
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected=NotANumberException.class)
public void testNaNsFailed() {
    double[] data = { 0, Double.POSITIVE_INFINITY, Double.NaN, Double.NEGATIVE_INFINITY };
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FAILED);
    ranking.rank(data);
}
 
Example #26
Source File: Solver.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
public void solve(final IScope scope, final SystemOfEquationsStatement seq, final double initialTime,
		final double finalTime, final IMap<String, IList<Double>> integrationValues) {

	seq.executeInScope(scope, () -> {
		final Map<Integer, IAgent> equationAgents = seq.getEquationAgents(scope);

		// GamaMap<Integer, GamaPair<IAgent, SingleEquationStatement>> myEQ = seq.getEquations(scope.getAgent());
		final IMap<Integer, GamaPair<IAgent, IExpression>> myVar = seq.getVariableDiff(scope.getAgent());
		/*
		 * prepare initial value of variables 1. loop through variables expression 2. if its equaAgents != null, it
		 * mean variable of external equation, set current scope to this agent scope 3. get value 4. return to
		 * previous scope
		 */

		final double[] y = new double[myVar.size()];
		// final ArrayList<IExpression> equationValues = new
		// ArrayList<IExpression>(eq.variables_diff.values());
		int i = 0;
		final int n = myVar.size();
		for (i = 0; i < n; i++) {
			final IAgent a = equationAgents.get(i);
			final String eqkeyname = a + myVar.get(i).getValue().toString();
			if (integrationValues.get(eqkeyname) == null) {
				integrationValues.put(eqkeyname, GamaListFactory.create(Double.class));
			}
			if (!a.dead()) {
				final boolean pushed = scope.push(a);
				try {
					y[i] = Cast.asFloat(scope, myVar.get(i).getValue().value(scope));

					if (Double.isInfinite(y[i])) {
						GAMA.reportAndThrowIfNeeded(scope,
								GamaRuntimeException.create(new NotANumberException(), scope), true);
					}
				} catch (final Exception ex1) {
					DEBUG.OUT(ex1.getMessage());
				} finally {
					if (pushed) {
						scope.pop(a);
					}
				}
			}

		}
		if (integrationValues.get(scope.getAgent() + seq.variable_time.getName()) == null) {
			integrationValues.put(scope.getAgent() + seq.variable_time.getName(),
					GamaListFactory.create(Double.class));
		}

		if (scope.getClock().getCycle() == 0) {
			storeValues(initialTime, y, integrationValues);
		}
		if (y.length > 0) {
			try {
				integrator.integrate(seq, initialTime, y, finalTime, y);
			} catch (final Exception ex) {
				DEBUG.OUT(ex.toString());
			}
		}

		seq.assignValue(scope, finalTime, y);
		storeValues(finalTime, y, integrationValues);
	});

}
 
Example #27
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected=NotANumberException.class)
public void testNaNsFailed() {
    double[] data = { 0, Double.POSITIVE_INFINITY, Double.NaN, Double.NEGATIVE_INFINITY };
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FAILED);
    ranking.rank(data);
}
 
Example #28
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected=NotANumberException.class)
public void testNaNsFailed() {
    double[] data = { 0, Double.POSITIVE_INFINITY, Double.NaN, Double.NEGATIVE_INFINITY };
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FAILED);
    ranking.rank(data);
}
 
Example #29
Source File: NaturalRanking.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Rank <code>data</code> using the natural ordering on Doubles, with
 * NaN values handled according to <code>nanStrategy</code> and ties
 * resolved using <code>tiesStrategy.</code>
 *
 * @param data array to be ranked
 * @return array of ranks
 * @throws NotANumberException if the selected {@link NaNStrategy} is {@code FAILED}
 * and a {@link Double#NaN} is encountered in the input data
 */
public double[] rank(double[] data) {

    // Array recording initial positions of data to be ranked
    IntDoublePair[] ranks = new IntDoublePair[data.length];
    for (int i = 0; i < data.length; i++) {
        ranks[i] = new IntDoublePair(data[i], i);
    }

    // Recode, remove or record positions of NaNs
    List<Integer> nanPositions = null;
    switch (nanStrategy) {
        case MAXIMAL: // Replace NaNs with +INFs
            recodeNaNs(ranks, Double.POSITIVE_INFINITY);
            break;
        case MINIMAL: // Replace NaNs with -INFs
            recodeNaNs(ranks, Double.NEGATIVE_INFINITY);
            break;
        case REMOVED: // Drop NaNs from data
            ranks = removeNaNs(ranks);
            break;
        case FIXED:   // Record positions of NaNs
            nanPositions = getNanPositions(ranks);
            break;
        case FAILED:
            nanPositions = getNanPositions(ranks);
            if (nanPositions.size() > 0) {
                throw new NotANumberException();
            }
            break;
        default: // this should not happen unless NaNStrategy enum is changed
            throw new MathInternalError();
    }

    // Sort the IntDoublePairs
    Arrays.sort(ranks);

    // Walk the sorted array, filling output array using sorted positions,
    // resolving ties as we go
    double[] out = new double[ranks.length];
    int pos = 1;  // position in sorted array
    out[ranks[0].getPosition()] = pos;
    List<Integer> tiesTrace = new ArrayList<Integer>();
    tiesTrace.add(ranks[0].getPosition());
    for (int i = 1; i < ranks.length; i++) {
        if (Double.compare(ranks[i].getValue(), ranks[i - 1].getValue()) > 0) {
            // tie sequence has ended (or had length 1)
            pos = i + 1;
            if (tiesTrace.size() > 1) {  // if seq is nontrivial, resolve
                resolveTie(out, tiesTrace);
            }
            tiesTrace = new ArrayList<Integer>();
            tiesTrace.add(ranks[i].getPosition());
        } else {
            // tie sequence continues
            tiesTrace.add(ranks[i].getPosition());
        }
        out[ranks[i].getPosition()] = pos;
    }
    if (tiesTrace.size() > 1) {  // handle tie sequence at end
        resolveTie(out, tiesTrace);
    }
    if (nanStrategy == NaNStrategy.FIXED) {
        restoreNaNs(out, nanPositions);
    }
    return out;
}
 
Example #30
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected=NotANumberException.class)
public void testNaNsFailed() {
    double[] data = { 0, Double.POSITIVE_INFINITY, Double.NaN, Double.NEGATIVE_INFINITY };
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FAILED);
    ranking.rank(data);
}