Top Description Fields Constructors Methods
org.apache.commons.math3.distribution

public Class EnumeratedDistribution<T>

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Type Parameters
<T>
type of the elements in the sample space.
Imports
java.io.Serializable, java.lang.reflect.Array, java.util.ArrayList, .Arrays, .List, org.apache.commons.math3.exception.MathArithmeticException, .NotANumberException, .NotFiniteNumberException, .NotPositiveException, .NotStrictlyPositiveException, .NullArgumentException, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.random.RandomGenerator, .Well19937c, org.apache.commons.math3.util.MathArrays, .Pair

A generic implementation of a discrete probability distribution (Wikipedia) over a finite sample space, based on an enumerated list of <value, probability> pairs. Input probabilities must all be non-negative, but zero values are allowed and their sum does not have to equal one. Constructors will normalize input probabilities to make them sum to one.

The list of pairs does not, strictly speaking, have to be a function and it can contain null values. The pmf created by the constructor will combine probabilities of equal values and will treat null values as equal. For example, if the list of pairs <"dog", 0.2>, <null, 0.1>, <"pig", 0.2>, <"dog", 0.1>, <null, 0.4> is provided to the constructor, the resulting pmf will assign mass of 0.5 to null, 0.3 to "dog" and 0.2 to null.

Since
3.2

Field Summary

Modifier and TypeField and Description
private final double[]
cumulativeProbabilities

Cumulative probabilities, cached to speed up sampling.

private final double[]
probabilities

Probabilities of respective random variable values.

protected final RandomGenerator
random

RNG instance used to generate samples from the distribution.

private static final long
serialVersionUID

Serializable UID.

private final List<T>
singletons

List of random variable values.

Constructor Summary

AccessConstructor and Description
public
EnumeratedDistribution(final List<Pair<T, Double>>
probability mass function enumerated as a list of pairs.
pmf
)

Create an enumerated distribution using the given probability mass function enumeration.

public
EnumeratedDistribution(final RandomGenerator
random number generator.
rng
,
final List<Pair<T, Double>>
probability mass function enumerated as a list of pairs.
pmf
)

Create an enumerated distribution using the given random number generator and probability mass function enumeration.

Method Summary

Modifier and TypeMethod and Description
public List<Pair<T, Double>>

Returns:

the probability mass function.
getPmf
()

Return the probability mass function as a list of <value, probability> pairs.

pack-priv double

Returns:

the value of the probability mass function at x
probability
(final T
the point at which the PMF is evaluated
x
)

For a random variable X whose values are distributed according to this distribution, this method returns P(X = x).

public void
reseedRandomGenerator(long
the new seed
seed
)

Reseed the random generator used to generate samples.

public T

Returns:

a random value.
sample
()

Generate a random value sampled from this distribution.

public Object[]

Returns:

an array representing the random sample.
sample
(int
the number of random values to generate.
sampleSize
)

Generate a random sample from the distribution.

public T[]

Returns:

an array representing the random sample.
sample
(int
the number of random values to generate.
sampleSize
,
final T[]
the array to populate.
array
)

Generate a random sample from the distribution.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cumulativeProbabilitiesback to summary
private final double[] cumulativeProbabilities

Cumulative probabilities, cached to speed up sampling.

probabilitiesback to summary
private final double[] probabilities

Probabilities of respective random variable values. For i = 0, ..., singletons.size() - 1, probability[i] is the probability that a random variable following this distribution takes the value singletons[i].

randomback to summary
protected final RandomGenerator random

RNG instance used to generate samples from the distribution.

serialVersionUIDback to summary
private static final long serialVersionUID

Serializable UID.

singletonsback to summary
private final List<T> singletons

List of random variable values.

Constructor Detail

EnumeratedDistributionback to summary
public EnumeratedDistribution(final List<Pair<T, Double>> pmf) throws NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException

Create an enumerated distribution using the given probability mass function enumeration.

Note

this constructor will implicitly create an instance of Well19937c as random generator to be used for sampling only (see sample() and sample(int)). In case no sampling is needed for the created distribution, it is advised to pass null as random generator via the appropriate constructors to avoid the additional initialisation overhead.

Parameters
pmf:List<Pair<T, Double>>

probability mass function enumerated as a list of pairs.

Exceptions
NotPositiveException:
if any of the probabilities are negative.
MathArithmeticException:
all of the probabilities are 0.
NotFiniteNumberException:
if any of the probabilities are infinite.
NotANumberException:
if any of the probabilities are NaN.
EnumeratedDistributionback to summary
public EnumeratedDistribution(final RandomGenerator rng, final List<Pair<T, Double>> pmf) throws NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException

Create an enumerated distribution using the given random number generator and probability mass function enumeration.

Parameters
rng:RandomGenerator

random number generator.

pmf:List<Pair<T, Double>>

probability mass function enumerated as a list of pairs.

Exceptions
NotPositiveException:
if any of the probabilities are negative.
MathArithmeticException:
all of the probabilities are 0.
NotFiniteNumberException:
if any of the probabilities are infinite.
NotANumberException:
if any of the probabilities are NaN.

Method Detail

getPmfback to summary
public List<Pair<T, Double>> getPmf()

Return the probability mass function as a list of pairs.

Note that if duplicate and / or null values were provided to the constructor when creating this EnumeratedDistribution, the returned list will contain these values. If duplicates values exist, what is returned will not represent a pmf (i.e., it is up to the caller to consolidate duplicate mass points).

Returns:List<Pair<T, Double>>

the probability mass function.

probabilityback to summary
pack-priv double probability(final T x)

For a random variable X whose values are distributed according to this distribution, this method returns P(X = x). In other words, this method represents the probability mass function (PMF) for the distribution.

Note that if x1 and x2 satisfy x1.equals(x2), or both are null, then probability(x1) = probability(x2).

Parameters
x:T

the point at which the PMF is evaluated

Returns:double

the value of the probability mass function at x

reseedRandomGeneratorback to summary
public void reseedRandomGenerator(long seed)

Reseed the random generator used to generate samples.

Parameters
seed:long

the new seed

sampleback to summary
public T sample()

Generate a random value sampled from this distribution.

Returns:T

a random value.

sampleback to summary
public Object[] sample(int sampleSize) throws NotStrictlyPositiveException

Generate a random sample from the distribution.

Parameters
sampleSize:int

the number of random values to generate.

Returns:Object[]

an array representing the random sample.

Exceptions
NotStrictlyPositiveException:
if sampleSize is not positive.
sampleback to summary
public T[] sample(int sampleSize, final T[] array) throws NotStrictlyPositiveException

Generate a random sample from the distribution.

If the requested samples fit in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.

Parameters
sampleSize:int

the number of random values to generate.

array:T[]

the array to populate.

Returns:T[]

an array representing the random sample.

Exceptions
NotStrictlyPositiveException:
if sampleSize is not positive.
NullArgumentException:
if array is null