public class RandomSamples extends Object
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 and Description |
---|
RandomSamples()
Constructor for the RandomSamples class.
|
RandomSamples(Random r)
Constructor for the RandomSamples class.
|
Modifier and Type | Method and Description |
---|---|
int[] |
getIndices()
Returns the indices computed from a call to
getSamples() . |
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.
|
void |
setRandomObject(Random r)
Sets the seed for the random number generator.
|
public RandomSamples()
public RandomSamples(Random r)
This constructor sets the random number generator to be used by the
RandomSamples
class.
r
- a Random
that is the random number generatorpublic double[] getSamples(double[] population, int nSamp)
population
- a double
array containing the population
to be samplednSamp
- an int
indicating the sample size desireddouble
array containing the samplespublic double[][] getSamples(double[][] population, int nSamp)
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()
.
population
- a double
matrix containing the population
to be samplednSamp
- an int
indicating the sample size desireddouble
matrix containing the samplespublic int[] getIndices()
getSamples()
.int
array of length nSamp
containing
the indices of the samplepublic int[] getSampleIndices(int nSamp, int nPop)
nPop-1
, in increasing order.nSamp
- an int
indicating the sample size desirednPop
- an int
indicating the number of items in the
populationint
array of length nSamp
containing
the indices of the samplepublic int[] getPermutation(int k)
k
- an int
which represents the number of integers to
be permuted. The integers to permute are the numbers 1, ..., k.int
array containing the permuted integerspublic void setRandomObject(Random r)
r
- a Random
that is the random number generatorCopyright © 2020 Rogue Wave Software. All rights reserved.