Class RNGDistribution.Gillespie
- Enclosing class:
RNGDistribution
{0,1,2,3,..., n}
but with different weights.
Note: if multiple random number are drawn using the same distribution of weights, optimizations are posssible that require expensive initialization but then drawing the random number is significantly cheaper.
For an excellent overview, see Keith Schwarz' website Darts, Dice, and Coins: Sampling from a Discrete Distribution. Of particular interest is Vose's Alias Method, which is also what's implemented in NetLogo
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class RNGDistribution
RNGDistribution.Binomial, RNGDistribution.Exponential, RNGDistribution.Geometric, RNGDistribution.Gillespie, RNGDistribution.Normal, RNGDistribution.TestCommand, RNGDistribution.Uniform
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
The threshold for switching between the standard and the optimized version of the Gillespie algorithm.Fields inherited from class RNGDistribution
rng, seed, seedSet, testSamples
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a weighted distribution over intergers using the Gillespie algorithm and a new instance ofMersenneTwister
.Gillespie
(MersenneTwister rng) Creates a weighted distribution over intergers using the Gillespie algorithm using the random number generatorrng
. -
Method Summary
Modifier and TypeMethodDescriptionclone()
Clone this RNGDistribution to ensure both objects return identical sequences of random numbers.int
next
(double[] weights) Return a random integer with support[0, weights.length)
from the discrete distribution of weights defined by thedouble[]
arrayweights
.int
next
(int[] weights) Return a random integer with support[0, weights.length)
from the discrete distribution of weights defined by thedouble[]
arrayweights
.int
nextMax
(double[] weights, double max) Return a random integer with support[0, weights.length)
from the discrete distribution of weights defined by thedouble[]
arrayweights
.int
nextMax
(int[] weights, int max) Return a random integer with support[0, weights.length)
from the discrete distribution of weights defined by thedouble[]
arrayweights
.int
nextSum
(double[] weights, double sum) Return a random integer with support[0, weights.length)
from the discrete distribution of weights defined by thedouble[]
arrayweights
.int
nextSum
(int[] weights, int sum) Return a random integer with support[0, weights.length)
from the discrete distribution of weights defined by thedouble[]
arrayweights
.static void
test
(MersenneTwister rng, Logger logger, MersenneTwister.Chronometer clock) Test Gillespie algorithm for random weight distribution.Methods inherited from class RNGDistribution
clearRNGSeed, clone, getRNG, getRNGSeed, isRNGSeedSet, nextBoolean, nextByte, nextBytes, nextDouble, nextGaussian, nextInt, nextInt, random01, random01d, random0n, random0N, setRNG, setRNGSeed, setRNGSeed
-
Field Details
-
THRESHOLD_SIZE
public static final int THRESHOLD_SIZEThe threshold for switching between the standard and the optimized version of the Gillespie algorithm.- See Also:
-
-
Constructor Details
-
Gillespie
public Gillespie()Creates a weighted distribution over intergers using the Gillespie algorithm and a new instance ofMersenneTwister
. -
Gillespie
Creates a weighted distribution over intergers using the Gillespie algorithm using the random number generatorrng
.- Parameters:
rng
- random number generator- Throws:
IllegalArgumentException
-
-
Method Details
-
next
public int next(int[] weights) Return a random integer with support[0, weights.length)
from the discrete distribution of weights defined by thedouble[]
arrayweights
.Note: if the sum of weights or the maximum weight is known, consider using the methods
#nextSum(double[], double)
or#nextMax(double[], double)
, instead.- Parameters:
weights
- the array of weights of the discrete distribution- Returns:
- the random integer
-
nextSum
public int nextSum(int[] weights, int sum) Return a random integer with support[0, weights.length)
from the discrete distribution of weights defined by thedouble[]
arrayweights
. Given the sum of weightssum
the Gillespie algorithm picks a uniformly distributed random number in[0, sum)
and then walks through the array of weights until the cumulative sum exceeds the random number. The index of the weight that exceeds the random number is returned.Note: this is the standard, non-optimized version of the Gillespie algorithm and is most likely best for small supports.
- Parameters:
weights
- the array of weights of the discrete distributionsum
- the sum of all weights- Returns:
- the random integer
-
nextMax
public int nextMax(int[] weights, int max) Return a random integer with support[0, weights.length)
from the discrete distribution of weights defined by thedouble[]
arrayweights
. Given the maximum weightmax
, the drawing of weighted random numbers can be optmized by simplifying bookkeeping at the expense of drawing more random numbers.Notes:
- The optimization is most effective for large supports but can be hampered by heavily skewed weight distributions.
- For small supports the standard version of the Gillespie algorithm is likely faster.
- For skewed weights consider sorting the weights in descending order.
max
can be bigger than the actual maximum but the optimization becomes less efficient.
- Parameters:
weights
- the array of weights of the discrete distributionmax
- the maximum weight in the array- Returns:
- the random integer
- See Also:
-
next
public int next(double[] weights) Return a random integer with support[0, weights.length)
from the discrete distribution of weights defined by thedouble[]
arrayweights
. Optimized sampling is used above the threshold ofweights.length > 350
.Note: if the sum of weights or the maximum weight is known, consider using the methods
#nextSum(double[], double)
or#nextMax(double[], double)
, instead.- Parameters:
weights
- the array of weights of the discrete distribution- Returns:
- the random integer
-
nextSum
public int nextSum(double[] weights, double sum) Return a random integer with support[0, weights.length)
from the discrete distribution of weights defined by thedouble[]
arrayweights
. Given the sum of weightssum
the Gillespie algorithm picks a uniformly distributed random number in[0, sum)
and then walks through the array of weights until the cumulative sum exceeds the random number. The index of the weight that exceeds the random number is returned.Note: this is the standard, non-optimized version of the Gillespie algorithm and is most likely best for small supports.
- Parameters:
weights
- the array of weights of the discrete distributionsum
- the sum of all weights- Returns:
- the random integer
-
nextMax
public int nextMax(double[] weights, double max) Return a random integer with support[0, weights.length)
from the discrete distribution of weights defined by thedouble[]
arrayweights
. Given the maximum weightmax
, the drawing of weighted random numbers can be optmized by simplifying bookkeeping at the expense of drawing more random numbers.Notes:
- The optimization is most effective for large supports but can be hampered by heavily skewed weight distributions.
- For small supports the standard version of the Gillespie algorithm is likely faster. The threshold appears to be at around 350 elements.
- For skewed weights consider sorting the weights in descending order.
max
can be bigger than the actual maximum but the optimization becomes less efficient.
- Parameters:
weights
- the array of weights of the discrete distributionmax
- the maximum weight in the array- Returns:
- the random integer
- See Also:
-
clone
Description copied from class:RNGDistribution
Clone this RNGDistribution to ensure both objects return identical sequences of random numbers.IMPORTANT:
- Specified by:
clone
in classRNGDistribution
- Returns:
- clone of RNGDistribution
-
test
Test Gillespie algorithm for random weight distribution.The test samples the distribution and bins the random numbers. This sample distribution is compared to the theoretical expectation. The mean deviation is the mean difference between the actual number of events in each bin and their expected number. For a perfect match the mean deviation is
0
. The test passes if the mean deviation lies within one standard error from0
. This is more stringent than the traditional 95% confidence interval.- Parameters:
rng
- the random number generatorlogger
- the logger for reporting resultsclock
- the stop watch
-