AnyLogic AnyLogic
Expand
Font size
  • java.lang.Object
    • com.anylogic.engine.CustomDistribution
All Implemented Interfaces:
java.io.Serializable

public class CustomDistribution
extends java.lang.Object
implements java.io.Serializable
This class is used to generate random numbers from a probability density function (PDF) defined as a number of pairs (value,rate) with possible interpolation. You can supply the value and rate arrays directly, or construct a distribution from a TableFunction. Each draw requires a random number generator, so there are two different modes the CustomDistribution can work:
1. You do not provide the RNG in the constructor, but supply it each time you call get( Random ). In this case the CustomDistribution can safely be made static and shared across multiple models and objects.
2. You provide the RNG in the constructor, the RNG is then remembered in the CustomDistribution and you can use get() method without any parameters. This is simpler syntax, but such CustomDistribution cannot be shared between models with different RNGs used. The get( Random ) method can still be used in that case.
Author:
AnyLogic North America, LLC https://anylogic.com
See Also:
Serialized Form

Nested Class Summary

Nested Classes 
static class  CustomDistribution.InterpolationType  
Modifier and Type Class Description

Field Summary

Fields 
static CustomDistribution.InterpolationType INTERPOLATION_LINEAR  
static CustomDistribution.InterpolationType INTERPOLATION_NONE  
static CustomDistribution.InterpolationType INTERPOLATION_STEP  
Modifier and Type Field Description

Constructor Summary

Constructors 
CustomDistribution​(double[] observations)
Constructs a custom empirical distribution from the given array of observations.
CustomDistribution​(double[] intervalStarts, double[] numberOfObservations)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals.
CustomDistribution​(double[] values, double[] rates, CustomDistribution.InterpolationType interpolationtype)
Constructs a custom distribution from the given arrays of values and rates, and a given interpolation type.
CustomDistribution​(double[] values, double[] rates, CustomDistribution.InterpolationType interpolationtype, Utilities owner)
Constructs a custom distribution from the given arrays of values and rates, and a given interpolation type.
CustomDistribution​(double[] values, double[] rates, CustomDistribution.InterpolationType interpolationtype, java.util.Random rng)
Constructs a custom distribution from the given arrays of values and rates, and a given interpolation type.
CustomDistribution​(double[] intervalStarts, double[] numberOfObservations, Utilities owner)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals.
CustomDistribution​(double[] intervalStarts, double[] numberOfObservations, java.util.Random rng)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals.
CustomDistribution​(double[] intervalStarts, int[] numberOfObservations)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals.
CustomDistribution​(double[] intervalStarts, int[] numberOfObservations, Utilities owner)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals.
CustomDistribution​(double[] intervalStarts, int[] numberOfObservations, java.util.Random rng)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals.
CustomDistribution​(double[] observations, Utilities owner)
Constructs a custom empirical distribution from the given array of observations.
CustomDistribution​(double[] observations, java.util.Random rng)
Constructs a custom empirical distribution from the given array of observations.
CustomDistribution​(int[] observations)
Constructs a custom discrete empirical distribution from the given array of discrete observations.
CustomDistribution​(int[] intervalStarts, double[] numberOfObservations)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals.
CustomDistribution​(int[] intervalStarts, double[] numberOfObservations, Utilities owner)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals.
CustomDistribution​(int[] intervalStarts, double[] numberOfObservations, java.util.Random rng)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals.
CustomDistribution​(int[] intervalStarts, int[] numberOfObservations)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals.
CustomDistribution​(int[] intervalStarts, int[] numberOfObservations, Utilities owner)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals.
CustomDistribution​(int[] intervalStarts, int[] numberOfObservations, java.util.Random rng)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals.
CustomDistribution​(int[] observations, Utilities owner)
Constructs a custom discrete empirical distribution from the given array of discrete observations.
CustomDistribution​(int[] observations, java.util.Random rng)
Constructs a custom discrete empirical distribution from the given array of discrete observations.
CustomDistribution​(TableFunction table)
Constructs a custom distribution from a given TableFunction.
CustomDistribution​(TableFunction table, java.util.Random rng)
Constructs a custom distribution from a given TableFunction.
Constructor Description

Method Summary

All Methods Instance Methods Concrete Methods 
double get()
Returns a random value distributed according to the given table of (value,rate) pairs.
double get​(double min, double max, double shift, double stretch)
Generates a sample of truncated custom distribution.
This method uses the random number generator set during construction and throws exception if no RNG was set.
This distribution is sketched by sketch coefficient, then shifted to the right by shift, after that it is truncated to fit in [min, max] interval.
double get​(double min, double max, double shift, double stretch, java.util.Random rng)
Generates a sample of truncated custom distribution.
This method requires a random number generator.
double get​(java.util.Random rng)
Returns a random value distributed according to the given table of (value,rate) pairs.
int getInt()
Returns a random value distributed according to the given table of (value,rate) pairs.
int getInt​(java.util.Random rng)
Returns a random value distributed according to the given table of (value,rate) pairs.
java.lang.String toString()
Returns the textual representation of the custom distribution.
Modifier and Type Method Description

Methods inherited from class java.lang.Object

equals, getClass, hashCode, notify, notifyAll, wait, wait, wait

Field Detail

INTERPOLATION_NONE

public static final CustomDistribution.InterpolationType INTERPOLATION_NONE

INTERPOLATION_STEP

public static final CustomDistribution.InterpolationType INTERPOLATION_STEP

INTERPOLATION_LINEAR

public static final CustomDistribution.InterpolationType INTERPOLATION_LINEAR

Constructor Detail

CustomDistribution

public CustomDistribution​(int[] intervalStarts,
                          int[] numberOfObservations)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals. Note that some arrays are used by reference and can be modified.
The random number generator is not set after this constructor and must then be provided in each call of get( Random ) method.
Parameters:
intervalStarts - the array of interval starts (2 or more, no duplicates, can be sorted or not), the last number is the end of the last interval
numberOfObservations - the array of observation counters (same size as values, no negative rates), the last number isn't actually used, it is assumed to be zero.

CustomDistribution

public CustomDistribution​(int[] intervalStarts,
                          int[] numberOfObservations,
                          java.util.Random rng)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals. Note that some arrays are used by reference and can be modified.
The random number generator is set after this constructor, so you can use both get() and get( Random ) methods. The default RNG can be accessed as getDefaultRandomGenerator() if called from Agent or Experiment.
Parameters:
intervalStarts - the array of interval starts (2 or more, no duplicates, can be sorted or not), the last number is the end of the last interval
numberOfObservations - the array of observation counters (same size as values, no negative rates), the last number isn't actually used, it is assumed to be zero.
rng - the random number generator that will be used

CustomDistribution

public CustomDistribution​(int[] intervalStarts,
                          int[] numberOfObservations,
                          Utilities owner)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals. Note that some arrays are used by reference and can be modified.
The random number generator will be obtained from the given owner (agent/experiment).
Parameters:
intervalStarts - the array of interval starts (2 or more, no duplicates, can be sorted or not), the last number is the end of the last interval
numberOfObservations - the array of observation counters (same size as values, no negative rates), the last number isn't actually used, it is assumed to be zero.
owner - owner of distribution, will be used as a source of random number generation

CustomDistribution

public CustomDistribution​(int[] intervalStarts,
                          double[] numberOfObservations)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals. Note that some arrays are used by reference and can be modified.
The random number generator is not set after this constructor and must then be provided in each call of get( Random ) method.
Parameters:
intervalStarts - the array of interval starts (2 or more, no duplicates, can be sorted or not), the last number is the end of the last interval
numberOfObservations - the array of observation counters (same size as values, no negative rates), the last number isn't actually used, it is assumed to be zero.

CustomDistribution

public CustomDistribution​(int[] intervalStarts,
                          double[] numberOfObservations,
                          java.util.Random rng)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals. Note that some arrays are used by reference and can be modified.
The random number generator is set after this constructor, so you can use both get() and get( Random ) methods. The default RNG can be accessed as getDefaultRandomGenerator() if called from Agent or Experiment.
Parameters:
intervalStarts - the array of interval starts (2 or more, no duplicates, can be sorted or not), the last number is the end of the last interval
numberOfObservations - the array of observation counters (same size as values, no negative rates), the last number isn't actually used, it is assumed to be zero.
rng - the random number generator that will be used

CustomDistribution

public CustomDistribution​(int[] intervalStarts,
                          double[] numberOfObservations,
                          Utilities owner)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals. Note that some arrays are used by reference and can be modified.
The random number generator will be obtained from the given owner (agent/experiment).
Parameters:
intervalStarts - the array of interval starts (2 or more, no duplicates, can be sorted or not), the last number is the end of the last interval
numberOfObservations - the array of observation counters (same size as values, no negative rates), the last number isn't actually used, it is assumed to be zero.
owner - owner of distribution, will be used as a source of random number generation

CustomDistribution

public CustomDistribution​(double[] intervalStarts,
                          int[] numberOfObservations)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals. Note that some arrays are used by reference and can be modified.
The random number generator is not set after this constructor and must then be provided in each call of get( Random ) method.
Parameters:
intervalStarts - the array of interval starts (2 or more, no duplicates, can be sorted or not), the last number is the end of the last interval
numberOfObservations - the array of observation counters (same size as values, no negative rates), the last number isn't actually used, it is assumed to be zero.

CustomDistribution

public CustomDistribution​(double[] intervalStarts,
                          int[] numberOfObservations,
                          java.util.Random rng)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals. Note that some arrays are used by reference and can be modified.
The random number generator is set after this constructor, so you can use both get() and get( Random ) methods. The default RNG can be accessed as getDefaultRandomGenerator() if called from Agent or Experiment.
Parameters:
intervalStarts - the array of interval starts (2 or more, no duplicates, can be sorted or not), the last number is the end of the last interval
numberOfObservations - the array of observation counters (same size as values, no negative rates), the last number isn't actually used, it is assumed to be zero.
rng - the random number generator that will be used

CustomDistribution

public CustomDistribution​(double[] intervalStarts,
                          int[] numberOfObservations,
                          Utilities owner)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals. Note that some arrays are used by reference and can be modified.
The random number generator will be obtained from the given owner (agent/experiment).
Parameters:
intervalStarts - the array of interval starts (2 or more, no duplicates, can be sorted or not), the last number is the end of the last interval
numberOfObservations - the array of observation counters (same size as values, no negative rates), the last number isn't actually used, it is assumed to be zero.
owner - owner of distribution, will be used as a source of random number generation

CustomDistribution

public CustomDistribution​(double[] intervalStarts,
                          double[] numberOfObservations)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals. Note that some arrays are used by reference and can be modified.
The random number generator is not set after this constructor and must then be provided in each call of get( Random ) method.
Parameters:
intervalStarts - the array of interval starts (2 or more, no duplicates, can be sorted or not), the last number is the end of the last interval
numberOfObservations - the array of observation counters (same size as values, no negative rates), the last number isn't actually used, it is assumed to be zero.

CustomDistribution

public CustomDistribution​(double[] intervalStarts,
                          double[] numberOfObservations,
                          java.util.Random rng)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals. Note that some arrays are used by reference and can be modified.
The random number generator is set after this constructor, so you can use both get() and get( Random ) methods. The default RNG can be accessed as getDefaultRandomGenerator() if called from Agent or Experiment.
Parameters:
intervalStarts - the array of interval starts (2 or more, no duplicates, can be sorted or not), the last number is the end of the last interval
numberOfObservations - the array of observation counters (same size as values, no negative rates), the last number isn't actually used, it is assumed to be zero.
rng - the random number generator that will be used

CustomDistribution

public CustomDistribution​(double[] intervalStarts,
                          double[] numberOfObservations,
                          Utilities owner)
Constructs a custom empirical distribution from the given arrays of interval starts and number of observations found in the corresponding intervals. Note that some arrays are used by reference and can be modified.
The random number generator will be obtained from the given owner (agent/experiment).
Parameters:
intervalStarts - the array of interval starts (2 or more, no duplicates, can be sorted or not), the last number is the end of the last interval
numberOfObservations - the array of observation counters (same size as values, no negative rates), the last number isn't actually used, it is assumed to be zero.
owner - owner of distribution, will be used as a source of random number generation

CustomDistribution

public CustomDistribution​(double[] values,
                          double[] rates,
                          CustomDistribution.InterpolationType interpolationtype)
Constructs a custom distribution from the given arrays of values and rates, and a given interpolation type. Note that arrays are not copied; they are used by reference and can be modified.
The random number generator is not set after this constructor and must then be provided in each call of get( Random ) method.
Parameters:
values - the array of values (2 or more, no duplicates, can be sorted or not)
rates - the array of probability rates (same size as values, no negative rates)
interpolationtype - the interpolation type: INTERPOLATION_NONE, INTERPOLATION_STEP, or INTERPOLATION_LINEAR

CustomDistribution

public CustomDistribution​(TableFunction table)
Constructs a custom distribution from a given TableFunction. Takes table function's interpolation type and check if it can be supported. Note that arrays are not copied: the table function arrays are used by reference.
The random number generator is not set after this constructor and must then be provided in each call of get( Random ) method.
Parameters:
table - the table function

CustomDistribution

public CustomDistribution​(double[] values,
                          double[] rates,
                          CustomDistribution.InterpolationType interpolationtype,
                          java.util.Random rng)
Constructs a custom distribution from the given arrays of values and rates, and a given interpolation type. Note that arrays are not copied; they are used by reference and can be modified.
The random number generator is set after this constructor, so you can use both get() and get( Random ) methods. The default RNG can be accessed as getDefaultRandomGenerator() if called from Agent or Experiment.
Parameters:
values - the array of values (2 or more, no duplicates, can be sorted or not)
rates - the array of probability rates (same size as values, no negative rates)
interpolationtype - the interpolation type: INTERPOLATION_NONE, INTERPOLATION_STEP, or INTERPOLATION_LINEAR
rng - the random number generator that will be used

CustomDistribution

public CustomDistribution​(double[] values,
                          double[] rates,
                          CustomDistribution.InterpolationType interpolationtype,
                          Utilities owner)
Constructs a custom distribution from the given arrays of values and rates, and a given interpolation type. Note that arrays are not copied; they are used by reference and can be modified.
The random number generator will be obtained from the given owner (agent/experiment).
Parameters:
values - the array of values (2 or more, no duplicates, can be sorted or not)
rates - the array of probability rates (same size as values, no negative rates)
interpolationtype - the interpolation type: INTERPOLATION_NONE, INTERPOLATION_STEP, or INTERPOLATION_LINEAR
owner - owner of distribution, will be used as a source of random number generation

CustomDistribution

public CustomDistribution​(TableFunction table,
                          java.util.Random rng)
Constructs a custom distribution from a given TableFunction. Takes table function's interpolation type and check if it can be supported. Note that arrays are not copied: the table function arrays are used by reference.
The random number generator is set after this constructor, so you can use both get() and get( Random ) methods. The default RNG can be accessed as getDefaultRandomGenerator() if called from Agent or Experiment.
Parameters:
table - the table function
rng - the random number generator that will be used

CustomDistribution

public CustomDistribution​(double[] observations)
Constructs a custom empirical distribution from the given array of observations. Note that array is used internally by reference and can be modified.
The random number generator is not set after this constructor and must then be provided in each call of get( Random ) method.
Parameters:
observations - the array of observed values (at least one value, can be sorted or not)

CustomDistribution

public CustomDistribution​(double[] observations,
                          Utilities owner)
Constructs a custom empirical distribution from the given array of observations. Note that array is used internally by reference and can be modified.
The random number generator will be obtained from the given owner (agent/experiment).
Parameters:
observations - the array of observed values (at least one value, can be sorted or not)
owner - owner of distribution, will be used as a source of random number generation

CustomDistribution

public CustomDistribution​(double[] observations,
                          java.util.Random rng)
Constructs a custom empirical distribution from the given array of observations. Note that array is used internally by reference and can be modified.
The random number generator is set after this constructor, so you can use both get() and get( Random ) methods. The default RNG can be accessed as getDefaultRandomGenerator() if called from Agent or Experiment.
Parameters:
observations - the array of observed values (at least one value, can be sorted or not)
rng - the random number generator that will be used

CustomDistribution

public CustomDistribution​(int[] observations)
Constructs a custom discrete empirical distribution from the given array of discrete observations. Note that array is used internally by reference and can be modified.
The random number generator is not set after this constructor and must then be provided in each call of get( Random ) method.
Parameters:
observations - the array of observed values (at least one value, can be sorted or not)

CustomDistribution

public CustomDistribution​(int[] observations,
                          java.util.Random rng)
Constructs a custom discrete empirical distribution from the given array of discrete observations. Note that array is used internally by reference and can be modified.
The random number generator is set after this constructor, so you can use both get() and get( Random ) methods. The default RNG can be accessed as getDefaultRandomGenerator() if called from Agent or Experiment.
Parameters:
observations - the array of observed values (at least one value, can be sorted or not)

CustomDistribution

public CustomDistribution​(int[] observations,
                          Utilities owner)
Constructs a custom discrete empirical distribution from the given array of discrete observations. Note that array is used internally by reference and can be modified.
The random number generator will be obtained from the given owner (agent/experiment).
Parameters:
observations - the array of observed values (at least one value, can be sorted or not)
owner - owner of distribution, will be used as a source of random number generation

Method Detail

getInt

public int getInt​(java.util.Random rng)
Returns a random value distributed according to the given table of (value,rate) pairs. This method requires a random number generator. If you are calling it from an Agent or an Experiment and wish to use the default RNG of the simulation engine you may write get( getDefaultRandomGenerator() ).
Parameters:
rng - the random number generator to be used.
Returns:
the random value

get

public double get​(java.util.Random rng)
Returns a random value distributed according to the given table of (value,rate) pairs. This method requires a random number generator. If you are calling it from an Agent or an Experiment and wish to use the default RNG of the simulation engine you may write get( getDefaultRandomGenerator() ).
Parameters:
rng - the random number generator to be used.
Returns:
the random value

get

public double get()
Returns a random value distributed according to the given table of (value,rate) pairs. This method uses the random number generator set during construction and throws exception if no RNG was set.
Returns:
the random value

getInt

public int getInt()
Returns a random value distributed according to the given table of (value,rate) pairs. This method uses the random number generator set during construction and throws exception if no RNG was set.
Returns:
the random value

get

public double get​(double min,
                  double max,
                  double shift,
                  double stretch,
                  java.util.Random rng)
Generates a sample of truncated custom distribution.
This method requires a random number generator. If you are calling it from an Agent or an Experiment and wish to use the default RNG of the simulation engine you may write get( getDefaultRandomGenerator() ).
This distribution is sketched by sketch coefficient, then shifted to the right by shift, after that it is truncated to fit in [min, max] interval. Truncation is performed by discarding every sample outside this interval and taking subsequent try.
For more details see get(Random)
Parameters:
min - the minimum value that this function will return. The distribution is truncated to return values above this. If the sample (stretched and shifted) is below this value it will be discarded and another sample will be drawn. Use -infinity for "No limit".
max - the maximum value that this function will return. The distribution is truncated to return values below this. If the sample (stretched and shifted) is bigger than this value it will be discarded and another sample will be drawn. Use +infinity for "No limit".
shift - the shift parameter that indicates how much the (stretched) distribution will shifted to the right
stretch - the stretch parameter that indicates how much the distribution will be stretched
rng - the random number generator.
Returns:
the generated sample

get

public double get​(double min,
                  double max,
                  double shift,
                  double stretch)
Generates a sample of truncated custom distribution.
This method uses the random number generator set during construction and throws exception if no RNG was set.
This distribution is sketched by sketch coefficient, then shifted to the right by shift, after that it is truncated to fit in [min, max] interval. Truncation is performed by discarding every sample outside this interval and taking subsequent try.
For more details see get()
Parameters:
min - the minimum value that this function will return. The distribution is truncated to return values above this. If the sample (stretched and shifted) is below this value it will be discarded and another sample will be drawn. Use -infinity for "No limit".
max - the maximum value that this function will return. The distribution is truncated to return values below this. If the sample (stretched and shifted) is bigger than this value it will be discarded and another sample will be drawn. Use +infinity for "No limit".
shift - the shift parameter that indicates how much the (stretched) distribution will shifted to the right
stretch - the stretch parameter that indicates how much the distribution will be stretched
Returns:
the generated sample

toString

public java.lang.String toString()
Returns the textual representation of the custom distribution.
Overrides:
toString in class java.lang.Object
Returns:
the textual representation of the custom distribution
How can we improve this article?