Class RandomSamples
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().
-
Constructor Summary
ConstructorsConstructorDescriptionConstructor for the RandomSamples class.Constructor for the RandomSamples class. -
Method Summary
Modifier and TypeMethodDescriptionint[]Returns the indices computed from a call togetSamples().int[]getPermutation(int k) 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.voidSets the seed for the random number generator.
-
Constructor Details
-
RandomSamples
public RandomSamples()Constructor for the RandomSamples class. -
RandomSamples
Constructor for the RandomSamples class.This constructor sets the random number generator to be used by the
RandomSamplesclass.- Parameters:
r- aRandomthat 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- adoublearray containing the population to be samplednSamp- anintindicating the sample size desired- Returns:
- a
doublearray 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
nSampitems 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 callinggetSamples().- Parameters:
population- adoublematrix containing the population to be samplednSamp- anintindicating the sample size desired- Returns:
- a
doublematrix containing the samples
-
getIndices
public int[] getIndices()Returns the indices computed from a call togetSamples().- Returns:
- an
intarray of lengthnSampcontaining 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 tonPop-1, in increasing order.- Parameters:
nSamp- anintindicating the sample size desirednPop- anintindicating the number of items in the population- Returns:
- an
intarray of lengthnSampcontaining the indices of the sample
-
getPermutation
public int[] getPermutation(int k) Returns a permutation array of integers.- Parameters:
k- anintwhich represents the number of integers to be permuted. The integers to permute are the numbers 1, ..., k.- Returns:
- an
intarray containing the permuted integers
-
setRandomObject
Sets the seed for the random number generator.- Parameters:
r- aRandomthat is the random number generator
-