Class DirichletSampler
- java.lang.Object
-
- org.apache.commons.rng.sampling.distribution.DirichletSampler
-
- All Implemented Interfaces:
ObjectSampler<double[]>
,SharedStateObjectSampler<double[]>
,SharedStateSampler<SharedStateObjectSampler<double[]>>
- Direct Known Subclasses:
DirichletSampler.GeneralDirichletSampler
,DirichletSampler.SymmetricDirichletSampler
public abstract class DirichletSampler extends java.lang.Object implements SharedStateObjectSampler<double[]>
Sampling from a Dirichlet distribution.Sampling uses:
- Since:
- 1.4
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
DirichletSampler.GeneralDirichletSampler
Sample from a Dirichlet distribution with different concentration parameters for each category.private static class
DirichletSampler.SymmetricDirichletSampler
Sample from a symmetric Dirichlet distribution with the same concentration parameter for each category.
-
Field Summary
Fields Modifier and Type Field Description private static int
MIN_CATGEORIES
The minimum number of categories.private UniformRandomProvider
rng
RNG (used for the toString() method).
-
Constructor Summary
Constructors Constructor Description DirichletSampler(UniformRandomProvider rng)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description private static SharedStateContinuousSampler
createSampler(UniformRandomProvider rng, double alpha)
Creates a gamma sampler for a category with the given concentration parameter.protected abstract int
getK()
Gets the number of categories.private static boolean
isNonZeroPositiveFinite(double x)
Return true if the value is non-zero, positive and finite.protected abstract double
nextGamma(int category)
Create a gamma sample for the given category.static DirichletSampler
of(UniformRandomProvider rng, double... alpha)
Creates a new Dirichlet distribution sampler.double[]
sample()
Create an object sample.static DirichletSampler
symmetric(UniformRandomProvider rng, int k, double alpha)
Creates a new symmetric Dirichlet distribution sampler using the same concentration parameter for each category.java.lang.String
toString()
private static void
validateNumberOfCategories(int k)
Validate the number of categories.abstract DirichletSampler
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 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.commons.rng.sampling.ObjectSampler
samples, samples
-
-
-
-
Field Detail
-
MIN_CATGEORIES
private static final int MIN_CATGEORIES
The minimum number of categories.- See Also:
- Constant Field Values
-
rng
private final UniformRandomProvider rng
RNG (used for the toString() method).
-
-
Constructor Detail
-
DirichletSampler
DirichletSampler(UniformRandomProvider rng)
- Parameters:
rng
- Generator of uniformly distributed random numbers.
-
-
Method Detail
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
sample
public double[] sample()
Create an object sample.- Specified by:
sample
in interfaceObjectSampler<double[]>
- Returns:
- a sample.
-
getK
protected abstract int getK()
Gets the number of categories.- Returns:
- k
-
nextGamma
protected abstract double nextGamma(int category)
Create a gamma sample for the given category.- Parameters:
category
- Category.- Returns:
- the sample
-
withUniformRandomProvider
public abstract DirichletSampler 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<SharedStateObjectSampler<double[]>>
- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
-
of
public static DirichletSampler of(UniformRandomProvider rng, double... alpha)
Creates a new Dirichlet distribution sampler.- Parameters:
rng
- Generator of uniformly distributed random numbers.alpha
- Concentration parameters.- Returns:
- the sampler
- Throws:
java.lang.IllegalArgumentException
- if the number of concentration parameters is less than 2; or if any concentration parameter is not strictly positive.
-
symmetric
public static DirichletSampler symmetric(UniformRandomProvider rng, int k, double alpha)
Creates a new symmetric Dirichlet distribution sampler using the same concentration parameter for each category.- Parameters:
rng
- Generator of uniformly distributed random numbers.k
- Number of categories.alpha
- Concentration parameter.- Returns:
- the sampler
- Throws:
java.lang.IllegalArgumentException
- if the number of categories is less than 2; or if the concentration parameter is not strictly positive.
-
validateNumberOfCategories
private static void validateNumberOfCategories(int k)
Validate the number of categories.- Parameters:
k
- Number of categories.- Throws:
java.lang.IllegalArgumentException
- if the number of categories is less than 2.
-
createSampler
private static SharedStateContinuousSampler createSampler(UniformRandomProvider rng, double alpha)
Creates a gamma sampler for a category with the given concentration parameter.- Parameters:
rng
- Generator of uniformly distributed random numbers.alpha
- Concentration parameter.- Returns:
- the sampler
- Throws:
java.lang.IllegalArgumentException
- if the concentration parameter is not strictly positive.
-
isNonZeroPositiveFinite
private static boolean isNonZeroPositiveFinite(double x)
Return true if the value is non-zero, positive and finite.- Parameters:
x
- Value.- Returns:
- true if non-zero positive finite
-
-