AnyLogic
Expand
Font size

Statistics

The Statistics object calculates statistical information (mean value, minimum, maximum, etc.) on a series of data samples of type double.

The object works differently depending on whether data are considered discrete or continuous.

Demo model: Statistics as Function of the Number of Observations Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).

Continuous data

Data samples are time-persistent; that is, value persists in continuous time but changes only at discrete time moments (like e.g. queue length). The samples must be added to this statistics with ascending time stamps. The mean, variance, etc. functions assume the last value added holds until the time of call. The mean of such dataset is a time-weighted value.

Discrete data

Samples have no duration in time (like e.g. products cost or patients LOS). They occur as isolated, discrete points in time, so the mean is simply the sum of individual samples divided by number of samples.

To create a statistics object

  1. Drag the  Statistics element from the  Analysis palette into the graphical editor.
  2. Go to the Properties view.
  3. Specify, whether data samples should be handled as Discrete or Continuous.
  4. Enter the expression evaluating the data item value in the Value edit box.
  5. Finally, choose, how you want this data element to be updated.

Properties

General

Name — The name of the statistics object. The name is used to identify and access the statistics.

Ignore — If selected, the statistics object is excluded from the model.

Visible — If selected, the statistics object is visible on a presentation at runtime.

Show name — If selected, the name of the statistics object is displayed on a presentation diagram.

Discrete (samples have no duration in time) — If selected, data are considered discrete.

Continuous (samples have duration in time) — If selected, data are considered continuous.

Value — Expression that will be dynamically evaluated to obtain the current data sample.

Update data automatically — If selected, new data samples are added automatically starting at the exact time (select the option Use model time) or date (select Use calendar dates) specified below and repeating with the specified Recurrence time.

Do not update data automatically — If selected, statistics is not updated automatically. In this case you should add new samples by yourself as described in Updating analysis data objects.

Log to database — If selected, data collected by this Statistics element will be added into the model execution log — statistics_log (if logging is turned on in the model's Database properties).

You can set the Statistics object to write data into the model execution log — statistics_log. To do this, select the statistics’ option Log to database, and enable the model to write to the log as described in Model execution logs.

While running the model, you can view the collected statistics and optionally copy it to the Clipboard so that later on you can paste it to some other application (e.g. to Excel) to perform statistical analysis.

API for working with collected data

You can work with collected data using the API of the statistics element. A set of functions differs depending on the type of the Statistics element:

Continuous statistics
Function Description
void add(double value, double time) Adds a new data sample to the statistics, i.e. notifies the statistics about the value change at the given time.

value — the new value.
time — the time.
int count() Returns the number of samples added to the statistics.
void reset() Discards all statistics accumulated.
double max() Returns the maximum sample value, or -infinity if no samples have been added.
double min() Returns the minimum sample value, or infinity if no samples have been added.
String toString() Returns the tab-separated multiline textual representation of the statistics.
double deviation() Returns the deviation of the statistics at the time of last update.
double deviation(double time) Returns the standard deviation of the statistics at the given time assuming the last value added holds until the given time.
double integral() Returns the mean integral of the statistics at the time of last update, or 0 if no samples have been added.
double integral(double time) Returns the integral of the statistics at the given time assuming the last value added holds until the given time, or 0 if no samples have been added.
double mean() Returns the mean of the statistics at the time of last update, or 0 if no samples have been added.
double mean(double time) Returns the mean of the statistics at the given time assuming the last value added holds until the given time, or 0 if no samples have been added.
double meanConfidence() Returns the half-width mean confidence of the statistics at the time of last update, or 0 if no samples have been added. Mean confidence interval is calculated in assumption that confidence level is equal to 95%.
double meanConfidence(double time) Returns the half-width mean confidence interval of the statistics at the given time assuming the last value added holds until the given time, or infinity if less than 2 samples have been added or if no time has elapsed. Mean confidence interval is calculated in assumption that confidence level is equal to 95%.
double variance() Returns the variance of the statistics at the time of last update, or 0 if no samples have been added.
double variance(double time) Returns the variance of the statistics at the given time assuming the last value added holds until the given time, or 0 if no samples have been added.
Discrete statistics
Function Description
void add(double value) Adds a sample value to the statistics.
int count() Returns the number of samples added to the statistics.
void reset() Discards all statistics accumulated.
double max() Returns the maximum sample value, or -infinity if no samples have been added.
double min() Returns the minimum sample value, or infinity if no samples have been added.
String toString() Returns the tab-separated multiline textual representation of the statistics.
double deviation() Returns the standard deviation of the statistics.
double mean() Returns the mean of the statistics, or 0 if no samples have been added.
double meanConfidence() Returns the half-width mean confidence interval of the statistics, or 0 if no samples have been added. Mean confidence interval is calculated in assumption that confidence level is equal to 95%.
double sum() The function returns sum of the samples added to the statistics, or 0 if no samples have been added.
double variance() Returns the variance of the statistics, or 0 if less than 2 samples have been added.

Visualizing collected statistics

You can visualize the collected statistics with charts.

The Statistics element differs from the Data set element in that it does not store the series of data samples themselves, but only the statistical information (mean value, minimum, maximum, and so on) collected on these samples. Each metric can be obtained using the corresponding function of the Statistics element: mean(), min(), max(), and so on.

Demo model: Statistics as Function of the Number of Observations Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).

You can learn how to visualize the collected statistics using the demo model mentioned above. Here the statistics are visualized with the time plot. In the Data section of its properties, you can see the data items, most of them display the metrics obtained from the Statistics element statistics.

Since the functions return scalar values, to visualize them, you need to select the Value option. The statistics element in this model is named statistics, so you call its functions in the Value field as statistics.mean(), statistics.mean() - statistics.deviation(), etc.

How can we improve this article?