Java Code Examples for org.apache.commons.math.exception.util.LocalizedFormats#NUMBER_OF_SAMPLES

The following examples show how to use org.apache.commons.math.exception.util.LocalizedFormats#NUMBER_OF_SAMPLES . 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: RandomDataImpl.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Uses a 2-cycle permutation shuffle to generate a random permutation.
 * <strong>Algorithm Description</strong>: Uses a 2-cycle permutation
 * shuffle to generate a random permutation of <code>c.size()</code> and
 * then returns the elements whose indexes correspond to the elements of the
 * generated permutation. This technique is described, and proven to
 * generate random samples, <a
 * href="http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node83.html">
 * here</a>
 *
 * @param c
 *            Collection to sample from.
 * @param k
 *            sample size.
 * @return the random sample.
 * @throws NumberIsTooLargeException if {@code k > c.size()}.
 * @throws NotStrictlyPositiveException if {@code k <= 0}.
 */
public Object[] nextSample(Collection<?> c, int k) {
    int len = c.size();
    if (k > len) {
        throw new NumberIsTooLargeException(LocalizedFormats.SAMPLE_SIZE_EXCEEDS_COLLECTION_SIZE,
                                            k, len, true);
    }
    if (k <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES, k);
    }

    Object[] objects = c.toArray();
    int[] index = nextPermutation(len, k);
    Object[] result = new Object[k];
    for (int i = 0; i < k; i++) {
        result[i] = objects[index[i]];
    }
    return result;
}
 
Example 2
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Uses a 2-cycle permutation shuffle to generate a random permutation.
 * <strong>Algorithm Description</strong>: Uses a 2-cycle permutation
 * shuffle to generate a random permutation of <code>c.size()</code> and
 * then returns the elements whose indexes correspond to the elements of the
 * generated permutation. This technique is described, and proven to
 * generate random samples, <a
 * href="http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node83.html">
 * here</a>
 *
 * @param c
 *            Collection to sample from.
 * @param k
 *            sample size.
 * @return the random sample.
 * @throws NumberIsTooLargeException if {@code k > c.size()}.
 * @throws NotStrictlyPositiveException if {@code k <= 0}.
 */
public Object[] nextSample(Collection<?> c, int k) {
    int len = c.size();
    if (k > len) {
        throw new NumberIsTooLargeException(LocalizedFormats.SAMPLE_SIZE_EXCEEDS_COLLECTION_SIZE,
                                            k, len, true);
    }
    if (k <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES, k);
    }

    Object[] objects = c.toArray();
    int[] index = nextPermutation(len, k);
    Object[] result = new Object[k];
    for (int i = 0; i < k; i++) {
        result[i] = objects[index[i]];
    }
    return result;
}
 
Example 3
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Uses a 2-cycle permutation shuffle to generate a random permutation.
 * <strong>Algorithm Description</strong>: Uses a 2-cycle permutation
 * shuffle to generate a random permutation of <code>c.size()</code> and
 * then returns the elements whose indexes correspond to the elements of the
 * generated permutation. This technique is described, and proven to
 * generate random samples, <a
 * href="http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node83.html">
 * here</a>
 *
 * @param c
 *            Collection to sample from.
 * @param k
 *            sample size.
 * @return the random sample.
 * @throws NumberIsTooLargeException if {@code k > c.size()}.
 * @throws NotStrictlyPositiveException if {@code k <= 0}.
 */
public Object[] nextSample(Collection<?> c, int k) {
    int len = c.size();
    if (k > len) {
        throw new NumberIsTooLargeException(LocalizedFormats.SAMPLE_SIZE_EXCEEDS_COLLECTION_SIZE,
                                            k, len, true);
    }
    if (k <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES, k);
    }

    Object[] objects = c.toArray();
    int[] index = nextPermutation(len, k);
    Object[] result = new Object[k];
    for (int i = 0; i < k; i++) {
        result[i] = objects[index[i]];
    }
    return result;
}
 
Example 4
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Uses a 2-cycle permutation shuffle to generate a random permutation.
 * <strong>Algorithm Description</strong>: Uses a 2-cycle permutation
 * shuffle to generate a random permutation of <code>c.size()</code> and
 * then returns the elements whose indexes correspond to the elements of the
 * generated permutation. This technique is described, and proven to
 * generate random samples, <a
 * href="http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node83.html">
 * here</a>
 *
 * @param c
 *            Collection to sample from.
 * @param k
 *            sample size.
 * @return the random sample.
 * @throws NumberIsTooLargeException if {@code k > c.size()}.
 * @throws NotStrictlyPositiveException if {@code k <= 0}.
 */
public Object[] nextSample(Collection<?> c, int k) {
    int len = c.size();
    if (k > len) {
        throw new NumberIsTooLargeException(LocalizedFormats.SAMPLE_SIZE_EXCEEDS_COLLECTION_SIZE,
                                            k, len, true);
    }
    if (k <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES, k);
    }

    Object[] objects = c.toArray();
    int[] index = nextPermutation(len, k);
    Object[] result = new Object[k];
    for (int i = 0; i < k; i++) {
        result[i] = objects[index[i]];
    }
    return result;
}
 
Example 5
Source File: AbstractContinuousDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generate a random sample from the distribution.  The default implementation
 * generates the sample by calling {@link #sample()} in a loop.
 *
 * @param sampleSize Number of random values to generate.
 * @return an array representing the random sample.
 * @throws MathException if an error occurs generating the sample.
 * @throws NotStrictlyPositiveException if {@code sampleSize} is not positive.
 * @since 2.2
 */
public double[] sample(int sampleSize) throws MathException {
    if (sampleSize <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,
                                               sampleSize);
    }
    double[] out = new double[sampleSize];
    for (int i = 0; i < sampleSize; i++) {
        out[i] = sample();
    }
    return out;
}
 
Example 6
Source File: AbstractIntegerDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a random sample from the distribution.  The default
 * implementation generates the sample by calling {@link #sample()}
 * in a loop.
 *
 * @param sampleSize number of random values to generate.
 * @since 2.2
 * @return an array representing the random sample.
 * @throws MathException if an error occurs generating the sample.
 * @throws NotStrictlyPositiveException if {@code sampleSize <= 0}.
 */
public int[] sample(int sampleSize) throws MathException {
    if (sampleSize <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,
                                               sampleSize);
    }
    int[] out = new int[sampleSize];
    for (int i = 0; i < sampleSize; i++) {
        out[i] = sample();
    }
    return out;
}
 
Example 7
Source File: HypergeometricDistributionImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a new hypergeometric distribution with the given the
 * population size, the number of successes in the population, and
 * the sample size.
 *
 * @param populationSize Population size.
 * @param numberOfSuccesses Number of successes in the population.
 * @param sampleSize Sample size.
 * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
 * @throws NotStrictlyPositiveException if {@code populationSize <= 0}.
 * @throws NotPositiveException if {@code populationSize < 0}.
 * @throws NumberIsTooLargeException if {@code numberOfSuccesses > populationSize}.
 * @throws NumberIsTooLargeException if {@code sampleSize > populationSize}.
 */
public HypergeometricDistributionImpl(int populationSize,
                                      int numberOfSuccesses,
                                      int sampleSize) {
    if (populationSize <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.POPULATION_SIZE,
                                               populationSize);
    }
    if (numberOfSuccesses < 0) {
        throw new NotPositiveException(LocalizedFormats.NUMBER_OF_SUCCESSES,
                                       numberOfSuccesses);
    }
    if (sampleSize < 0) {
        throw new NotPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,
                                       sampleSize);
    }

    if (numberOfSuccesses > populationSize) {
        throw new NumberIsTooLargeException(LocalizedFormats.NUMBER_OF_SUCCESS_LARGER_THAN_POPULATION_SIZE,
                                            numberOfSuccesses, populationSize, true);
    }
    if (sampleSize > populationSize) {
        throw new NumberIsTooLargeException(LocalizedFormats.SAMPLE_SIZE_LARGER_THAN_POPULATION_SIZE,
                                            sampleSize, populationSize, true);
    }

    this.numberOfSuccesses = numberOfSuccesses;
    this.populationSize = populationSize;
    this.sampleSize = sampleSize;
}
 
Example 8
Source File: AbstractContinuousDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generate a random sample from the distribution.  The default implementation
 * generates the sample by calling {@link #sample()} in a loop.
 *
 * @param sampleSize Number of random values to generate.
 * @return an array representing the random sample.
 * @throws MathException if an error occurs generating the sample.
 * @throws NotStrictlyPositiveException if {@code sampleSize} is not positive.
 * @since 2.2
 */
public double[] sample(int sampleSize) throws MathException {
    if (sampleSize <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,
                                               sampleSize);
    }
    double[] out = new double[sampleSize];
    for (int i = 0; i < sampleSize; i++) {
        out[i] = sample();
    }
    return out;
}
 
Example 9
Source File: AbstractIntegerDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a random sample from the distribution.  The default
 * implementation generates the sample by calling {@link #sample()}
 * in a loop.
 *
 * @param sampleSize number of random values to generate.
 * @since 2.2
 * @return an array representing the random sample.
 * @throws MathException if an error occurs generating the sample.
 * @throws NotStrictlyPositiveException if {@code sampleSize <= 0}.
 */
public int[] sample(int sampleSize) throws MathException {
    if (sampleSize <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,
                                               sampleSize);
    }
    int[] out = new int[sampleSize];
    for (int i = 0; i < sampleSize; i++) {
        out[i] = sample();
    }
    return out;
}
 
Example 10
Source File: HypergeometricDistributionImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a new hypergeometric distribution with the given the
 * population size, the number of successes in the population, and
 * the sample size.
 *
 * @param populationSize Population size.
 * @param numberOfSuccesses Number of successes in the population.
 * @param sampleSize Sample size.
 * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
 * @throws NotStrictlyPositiveException if {@code populationSize <= 0}.
 * @throws NotPositiveException if {@code populationSize < 0}.
 * @throws NumberIsTooLargeException if {@code numberOfSuccesses > populationSize}.
 * @throws NumberIsTooLargeException if {@code sampleSize > populationSize}.
 */
public HypergeometricDistributionImpl(int populationSize,
                                      int numberOfSuccesses,
                                      int sampleSize) {
    if (populationSize <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.POPULATION_SIZE,
                                               populationSize);
    }
    if (numberOfSuccesses < 0) {
        throw new NotPositiveException(LocalizedFormats.NUMBER_OF_SUCCESSES,
                                       numberOfSuccesses);
    }
    if (sampleSize < 0) {
        throw new NotPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,
                                       sampleSize);
    }

    if (numberOfSuccesses > populationSize) {
        throw new NumberIsTooLargeException(LocalizedFormats.NUMBER_OF_SUCCESS_LARGER_THAN_POPULATION_SIZE,
                                            numberOfSuccesses, populationSize, true);
    }
    if (sampleSize > populationSize) {
        throw new NumberIsTooLargeException(LocalizedFormats.SAMPLE_SIZE_LARGER_THAN_POPULATION_SIZE,
                                            sampleSize, populationSize, true);
    }

    this.numberOfSuccesses = numberOfSuccesses;
    this.populationSize = populationSize;
    this.sampleSize = sampleSize;
}
 
Example 11
Source File: AbstractContinuousDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generate a random sample from the distribution.  The default implementation
 * generates the sample by calling {@link #sample()} in a loop.
 *
 * @param sampleSize Number of random values to generate.
 * @return an array representing the random sample.
 * @throws MathException if an error occurs generating the sample.
 * @throws NotStrictlyPositiveException if {@code sampleSize} is not positive.
 * @since 2.2
 */
public double[] sample(int sampleSize) throws MathException {
    if (sampleSize <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,
                                               sampleSize);
    }
    double[] out = new double[sampleSize];
    for (int i = 0; i < sampleSize; i++) {
        out[i] = sample();
    }
    return out;
}
 
Example 12
Source File: AbstractIntegerDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a random sample from the distribution.  The default
 * implementation generates the sample by calling {@link #sample()}
 * in a loop.
 *
 * @param sampleSize number of random values to generate.
 * @since 2.2
 * @return an array representing the random sample.
 * @throws MathException if an error occurs generating the sample.
 * @throws NotStrictlyPositiveException if {@code sampleSize <= 0}.
 */
public int[] sample(int sampleSize) throws MathException {
    if (sampleSize <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,
                                               sampleSize);
    }
    int[] out = new int[sampleSize];
    for (int i = 0; i < sampleSize; i++) {
        out[i] = sample();
    }
    return out;
}
 
Example 13
Source File: HypergeometricDistributionImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a new hypergeometric distribution with the given the
 * population size, the number of successes in the population, and
 * the sample size.
 *
 * @param populationSize Population size.
 * @param numberOfSuccesses Number of successes in the population.
 * @param sampleSize Sample size.
 * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
 * @throws NotStrictlyPositiveException if {@code populationSize <= 0}.
 * @throws NotPositiveException if {@code populationSize < 0}.
 * @throws NumberIsTooLargeException if {@code numberOfSuccesses > populationSize}.
 * @throws NumberIsTooLargeException if {@code sampleSize > populationSize}.
 */
public HypergeometricDistributionImpl(int populationSize,
                                      int numberOfSuccesses,
                                      int sampleSize) {
    if (populationSize <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.POPULATION_SIZE,
                                               populationSize);
    }
    if (numberOfSuccesses < 0) {
        throw new NotPositiveException(LocalizedFormats.NUMBER_OF_SUCCESSES,
                                       numberOfSuccesses);
    }
    if (sampleSize < 0) {
        throw new NotPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,
                                       sampleSize);
    }

    if (numberOfSuccesses > populationSize) {
        throw new NumberIsTooLargeException(LocalizedFormats.NUMBER_OF_SUCCESS_LARGER_THAN_POPULATION_SIZE,
                                            numberOfSuccesses, populationSize, true);
    }
    if (sampleSize > populationSize) {
        throw new NumberIsTooLargeException(LocalizedFormats.SAMPLE_SIZE_LARGER_THAN_POPULATION_SIZE,
                                            sampleSize, populationSize, true);
    }

    this.numberOfSuccesses = numberOfSuccesses;
    this.populationSize = populationSize;
    this.sampleSize = sampleSize;
}