If none of the probability distributions supported by AnyLogic fits your
data, you can define your own
custom (empirical) distribution.
Having defined the custom distribution, you can draw random values from your distribution by calling the function of the same name as your distribution, e.g.: myDistribution().
To define a custom distribution
-
Drag the
Custom Distribution element from the
Agent palette onto the agent diagram.
- Go to the element properties to define the distribution by entering data.
-
First, choose the Type of the distribution. Type means the
type of values the distribution returns, and can be one of the
following:
- Continuous
- Discrete
- Options
-
Define the data for a custom distribution in the Data section of
the element's properties. There are three alternative options:
- To load data from the AnyLogic database, select the Loaded from database check box and choose the database table from the Table drop-down list. If needed, define the conditions specifying the particular values you want to select from the database table in the Choice conditions option. Using the properties below, specify the DB table columns containing the required data (see below).
-
If you have table data already defined in any other application or a
file, you can copy it to the clipboard and paste it into the
Data section by using the
Paste from Clipboard button. Make sure to previously clear the Loaded from database check box.
-
You can also enter the table data into the table manually. The table
is shown in the Data properties section (if the
Loaded from database check box is cleared). To remove entry,
select the corresponding row of the table and click the
Delete button. Use
,
to rearrange the table rows (available if Frequency table or Observed samples is set in the Define using option).
- General
-
Name — The name of the custom distribution. The name is used to access the custom distribution. You call the distribution like a function, e.g.: myDistribution().
Show name — If selected, the element is displayed on the graphical diagram.
Ignore — If selected, the custom distribution is excluded from the model.
Visible — Here you specify whether the element is visible on the presentation at the model runtime, or not. Choose yes or no using the control.
Type — The type of values the distribution returns:
- Continuous — Distribution returns continuous type of data.
- Discrete — Distribution returns discrete type of data.
- Options — Allows to specify probabilities for every alternative option of the chosen option list.
Define using — [Visible only if Continuous or Discrete is selected in the Type option] Specify what the distribution will be defined by:
- Ranges — Defines distribution with ranges.
- Frequency table — [Active only if Continuous is selected in the Type option]. Defines distribution with frequency table.
- Observed samples — Loads the observed values as they are.
Option list — [Visible only if Options is selected in the Type option] Here you select the option list for this custom distribution. The distribution will define the occurrence probabilities for every option of this list.
- Data
-
Here you define data for the custom distribution. Data can be specified in the table (see the instructions below), or loaded from the AnyLogic database.
Loaded from database — If set, allows to load data from the existing AnyLogic database defined by the user.
Table — [Visible if Loaded from database is selected] Here you can select the table of the AnyLogic database that contains the required data.
Choice conditions — Here you define one or several conditions that will specify the particular values to be selected from the specified table column. You can add
, duplicate
, delete
and arrange the conditions (
,
).
Value column — [Visible if Frequency table or Observed samples is selected in the Define using option] The column of the database table that contains values.
Weight column — [Visible only if Frequency table is selected in the Define using option] The probabilities for the corresponding values in the Value column.
Start column — [Visible if Ranges is selected in the Define using option] The column of the database table that contains values the data ranges start from.
End column — [Visible if Ranges is selected in the Define using option] The column of the database table that contains values the data ranges end with.
Option column — [Visible only if Options is selected in the Type option] The column of the database table that contains options of the option list. This column's Type should be exactly the same option list that is specified in the Option list property of this custom distribution.
Number of observations column — [Visible if Ranges is selected in the Define using option, or if Options is selected in the Type option] The column of the database table that contains the data (numbers of observations for corresponding ranges, or for options of the option list). This column must contain only numeric values.
- Preview
-
Here you can see the custom distribution preview.
- Advanced
-
Static — [Only visible if Library developer mode is enabled] If selected, the custom distribution is static. Static distribution is initialized once and has the same data for all instances of this agent type. This is very useful in agent based models, in case your agents have some distributions defined. Making a custom distribution static, AnyLogic engine does not waste time on initializing it several times for numerous agents.
Do not use static custom distribution, if you plan to run iterations of complex experiments (optimization, parameter variation, etc.) in parallel on different processor cores (this is set by the experiment's advanced option Allow parallel evaluations).Static custom distribution are not saved into a snapshot file when you save/restore a state of a running model. If you do not alter the data of a static custom distribution in your model, the state of the restored model will be exactly the same as you saved it into the snapshot. However, if you somehow change the data of a static custom distribution (e.g. in agent's On startup code), the modified data will not be saved into a snapshot, and the restored state of the model will differ from the state you have previously saved.
There are two alternative ways to define the distribution data for a discrete distribution. You choose the definition mode in the Define as property of the custom distribution.
-
Ranges. You define ranges and probabilities (weights) for these
ranges. The ranges should follow one another.
Discrete distribution defined using Ranges
-
Observed samples. In this mode you just load the observed values
as they are. The frequency of the particular value depends on the number
of times this value is met in the table (i.e. number of times this value
was observed). Use this mode if you have a table of observed data. The
easiest way to load data into the model is to copy it to the clipboard
from the data source file or a program and paste the data here by
clicking the Paste from Clipboard button below the table in the
Data property section.
Open the Preview properties section to see the distribution preview built for the data you have entered.Discrete distribution defined using Observed samples. Data and Preview sections.
There are three alternative ways to define the distribution data for a continuous distribution:
- Ranges. You define ranges and probabilities (weights) for these ranges. The ranges should follow one another.
-
Frequency table. Here you define the values in the first column
of the table and the corresponding weights in the second column. There
are three Interpolation types:
- None
- Step
- Linear
If Step is selected, you define the same things as in the Range definition mode.
If Linear is selected, you define sloped ranges.Continuous distribution defined using the Frequency table with Linear Interpolation. You can see sloped ranges in the Preview.
- Observed samples. In this mode you just load the observed values as they are. The frequency of the particular value depends on the number of times this value is met in the table (i.e. number of times this value was observed). Use this mode if you have the table of observed data. The easiest way to load data in the model is to copy it to the clipboard from the data source file or a program and paste the data here by clicking the Paste from Clipboard button below the table in the Data property section.
Use Options type to define probabilities for alternative options of the chosen Option list.
Here you can see the usage example of the distribution defining the probabilities for Male and Female options of the Sex option list.
AnyLogic provides three constructors for creating an empirical distribution (CustomDistribution class):
- CustomDistribution( double[] observations ) — creates an empirical probability distribution based on the actual continuous data points (observations).
- CustomDistribution( int[] observations ) — same for integer observations
- CustomDistribution( double[] intervalstarts, int[] nobservations ) — creates an empirical probability distribution based on a histogram. The histogram should be provided as two arrays: the start points of the intervals followed by the end point of the last interval (intervalstarts), and the number of observations in each interval (nobservations). The second array must either be one element shorter than the first one or contain 0 as the last element.
-
How can we improve this article?
-