Package com.imsl.stat

Class RandomSamples

java.lang.Object
com.imsl.stat.RandomSamples

public class RandomSamples extends Object
Generates a simple pseudorandom sample from a finite population, a sample of indices, or a permutation of an array of indices.

getPermutation(int k) generates a pseudorandom permutation of the integers from 1 to k. It begins by filling a vector of length k with the consecutive integers 1 to k. Then, with M initially equal to k, a random index j between 1 and M is generated. The element of the vector with the index M-1 and the element with index j swap places in the vector. M is then decremented by 1 and the process repeats until M=1.

getSampleIndices(int nSamp, int nPop) generates the indices of a pseudorandom sample, without replacement, of size nSamp numbers from a population of size nPop. If nSamp is greater than nPop/2, the integers from 1 to nPop are selected sequentially with a probability conditional on the number selected and the number still to be considered. If, when the i-th population index is considered and j items have been included in the sample, then the index i is included with probability (nSamp - j)/(nPop + 1 - i).

If nSamp is not greater than nPop/2, a O (nSamp) algorithm due to Ahrens and Dieter (1985) is used. Of the methods discussed by Ahrens and Dieter, SG* is used in getSampleIndices(int nSamp, int nPop). It involves a preliminary selection of q indices using a geometric distribution for the distances between each index and the next one. If the preliminary sample size q is less than nSamp, a new preliminary sample is chosen, and this is continued until a preliminary sample greater in size than nSamp is chosen. This preliminary sample is then thinned using the same sampling method as that in which the sample size is greater than half of the population size.

getSamples() generates a pseudorandom sample from a given population, without replacement, using an algorithm due to McLeod and Bellhouse (1983). The first nSamp items in the population are included in the sample. Then, for each successive item from the population, a random item in the sample is replaced by that item from the population with probability equal to the sample size divided by the number of population items that have been encountered at that time.

To retrieve the random indices, use getIndices() after calling getSamples().

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor for the RandomSamples class.
    Constructor for the RandomSamples class.
  • Method Summary

    Modifier and Type
    Method
    Description
    int[]
    Returns the indices computed from a call to getSamples().
    int[]
    Returns a permutation array of integers.
    int[]
    getSampleIndices(int nSamp, int nPop)
    Computes and returns an array of sampled indices.
    double[][]
    getSamples(double[][] population, int nSamp)
    Generates a pseudorandom sample from a given population matrix, without replacement.
    double[]
    getSamples(double[] population, int nSamp)
    Generates a pseudorandom sample from a given population array, without replacement.
    void
    Sets the seed for the random number generator.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RandomSamples

      public RandomSamples()
      Constructor for the RandomSamples class.
    • RandomSamples

      public RandomSamples(Random r)
      Constructor for the RandomSamples class.

      This constructor sets the random number generator to be used by the RandomSamples class.

      Parameters:
      r - a Random that is the random number generator
  • Method Details

    • getSamples

      public double[] getSamples(double[] population, int nSamp)
      Generates a pseudorandom sample from a given population array, without replacement.
      Parameters:
      population - a double array containing the population to be sampled
      nSamp - an int indicating the sample size desired
      Returns:
      a double array containing the samples
    • getSamples

      public double[][] getSamples(double[][] population, int nSamp)
      Generates a pseudorandom sample from a given population matrix, without replacement.

      getSamples() implements an algorithm due to McLeod and Bellhouse (1983).

      The first nSamp items in the population are included in the sample. Then, for each successive item from the population, a random item in the sample is replaced by that item from the population with probability equal to the sample size divided by the number of population items that have been encountered at that time.

      To retrieve the random indices, use getIndices() after calling getSamples().

      Parameters:
      population - a double matrix containing the population to be sampled
      nSamp - an int indicating the sample size desired
      Returns:
      a double matrix containing the samples
    • getIndices

      public int[] getIndices()
      Returns the indices computed from a call to getSamples().
      Returns:
      an int array of length nSamp containing the indices of the sample
    • getSampleIndices

      public int[] getSampleIndices(int nSamp, int nPop)
      Computes and returns an array of sampled indices. This is a random sample (without replacement) of the integers from 0 to nPop-1, in increasing order.
      Parameters:
      nSamp - an int indicating the sample size desired
      nPop - an int indicating the number of items in the population
      Returns:
      an int array of length nSamp containing the indices of the sample
    • getPermutation

      public int[] getPermutation(int k)
      Returns a permutation array of integers.
      Parameters:
      k - an int which represents the number of integers to be permuted. The integers to permute are the numbers 1, ..., k.
      Returns:
      an int array containing the permuted integers
    • setRandomObject

      public void setRandomObject(Random r)
      Sets the seed for the random number generator.
      Parameters:
      r - a Random that is the random number generator