Class FastLoadedDiceRollerDiscreteSampler.FLDRSampler
- java.lang.Object
-
- org.apache.commons.rng.sampling.distribution.FastLoadedDiceRollerDiscreteSampler
-
- org.apache.commons.rng.sampling.distribution.FastLoadedDiceRollerDiscreteSampler.FLDRSampler
-
- All Implemented Interfaces:
DiscreteSampler
,SharedStateDiscreteSampler
,SharedStateSampler<SharedStateDiscreteSampler>
- Enclosing class:
- FastLoadedDiceRollerDiscreteSampler
private static class FastLoadedDiceRollerDiscreteSampler.FLDRSampler extends FastLoadedDiceRollerDiscreteSampler
Class to implement the FLDR sample algorithm.
-
-
Field Summary
Fields Modifier and Type Field Description private int
booleanSource
Provides a bit source for booleans.private static int
EMPTY_BOOL_SOURCE
Empty boolean source.private int[]
h
Number of leaf nodes at each level.private int
k
Number of levels in the discrete distribution generating (DDG) tree.private int[]
lH
Stores the leaf node labels in increasing order.private int
n
Number of categories.private UniformRandomProvider
rng
Underlying source of randomness.
-
Constructor Summary
Constructors Modifier Constructor Description (package private)
FLDRSampler(UniformRandomProvider rng, int n, int k, int[] h, int[] lH)
Creates a sampler.private
FLDRSampler(UniformRandomProvider rng, FastLoadedDiceRollerDiscreteSampler.FLDRSampler source)
Creates a copy with a new source of randomness.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private int
flip()
Provides a source of boolean bits.int
sample()
Creates anint
sample.java.lang.String
toString()
FastLoadedDiceRollerDiscreteSampler
withUniformRandomProvider(UniformRandomProvider rng)
Create a new instance of the sampler with the same underlying state using the given uniform random provider as the source of randomness.-
Methods inherited from class org.apache.commons.rng.sampling.distribution.FastLoadedDiceRollerDiscreteSampler
checkArraySize, indexOfNonZero, of, of, of
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.commons.rng.sampling.distribution.DiscreteSampler
samples, samples
-
-
-
-
Field Detail
-
EMPTY_BOOL_SOURCE
private static final int EMPTY_BOOL_SOURCE
Empty boolean source. This is the location of the sign-bit after 31 right shifts on the boolean source.- See Also:
- Constant Field Values
-
rng
private final UniformRandomProvider rng
Underlying source of randomness.
-
n
private final int n
Number of categories.
-
k
private final int k
Number of levels in the discrete distribution generating (DDG) tree. Equal toceil(log2(m))
wherem
is the sum of observations.
-
h
private final int[] h
Number of leaf nodes at each level.
-
lH
private final int[] lH
Stores the leaf node labels in increasing order. NamedH
in the FLDR paper.
-
booleanSource
private int booleanSource
Provides a bit source for booleans.A cached value from a call to
UniformRandomProvider.nextInt()
.Only stores 31-bits when full as 1 bit has already been consumed. The sign bit is a flag that shifts down so the source eventually equals 1 when all bits are consumed and will trigger a refill.
-
-
Constructor Detail
-
FLDRSampler
FLDRSampler(UniformRandomProvider rng, int n, int k, int[] h, int[] lH)
Creates a sampler.The input parameters are not validated and must be correctly computed tables.
- Parameters:
rng
- Generator of uniformly distributed random numbers.n
- Number of categoriesk
- Number of levels in the discrete distribution generating (DDG) tree. Equal toceil(log2(m))
wherem
is the sum of observations.h
- Number of leaf nodes at each level.lH
- Stores the leaf node labels in increasing order.
-
FLDRSampler
private FLDRSampler(UniformRandomProvider rng, FastLoadedDiceRollerDiscreteSampler.FLDRSampler source)
Creates a copy with a new source of randomness.- Parameters:
rng
- Generator of uniformly distributed random numbers.source
- Source to copy.
-
-
Method Detail
-
sample
public int sample()
Creates anint
sample.- Returns:
- a sample.
-
flip
private int flip()
Provides a source of boolean bits.Note: This replicates the boolean cache functionality of
o.a.c.rng.core.source32.IntProvider
. The method has been simplified to return anint
value rather than aboolean
.- Returns:
- the bit (0 or 1)
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
withUniformRandomProvider
public FastLoadedDiceRollerDiscreteSampler withUniformRandomProvider(UniformRandomProvider rng)
Create a new instance of the sampler with the same underlying state using the given uniform random provider as the source of randomness.- Specified by:
withUniformRandomProvider
in interfaceSharedStateSampler<SharedStateDiscreteSampler>
- Specified by:
withUniformRandomProvider
in classFastLoadedDiceRollerDiscreteSampler
- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
-
-