Class TimeStatistics

java.lang.Object
com.amalgamasimulation.utils.TimeStatistics
All Implemented Interfaces:
ITimeStatistics

public class TimeStatistics extends Object implements ITimeStatistics
Represents a time series data with fixed granularity. Granularity is the size of time buckets at which the values are accumulated and aggregated.

Typical uses of this class include:

  • Collecting daily inventory statistics in supply chain or manufacturing models
  • Collecting truck runs count statistics by months
  • Collecting amount of material transported by hours
Author:
Andrey Malykhanov
  • Constructor Details

    • TimeStatistics

      public TimeStatistics(double granularity)
  • Method Details

    • replace

      public void replace(double time, double value)
      Description copied from interface: ITimeStatistics
      Replaces the value in the time bucket corresponding to the specified time with the specified value. Makes the bucket containing the specified time non-empty.
      Specified by:
      replace in interface ITimeStatistics
      Parameters:
      time - specified time, used to determine the time bucket
      value - specified value, can be both positive and negative. This value is set to the corresponding time bucket

      The figure below illustrates the result of calling this method:

    • add

      public void add(double time, double value)
      Description copied from interface: ITimeStatistics
      Adds the specified value to the time bucket corresponding to the specified time. Makes the bucket containing the specified time non-empty.
      Specified by:
      add in interface ITimeStatistics
      Parameters:
      time - specified time, used to determine the time bucket
      value - specified value, can be both positive and negative

      The figure below illustrates the result of calling this method:

    • lastTime

      public double lastTime()
      Description copied from interface: ITimeStatistics
      Returns the last time that was set by calling ITimeStatistics.add(double, double) or ITimeStatistics.replace(double, double) method, or Double.NEGATIVE_INFINITY if none of these methods have yet been called.
      Specified by:
      lastTime in interface ITimeStatistics
      Returns:
      last time that was set by calling ITimeStatistics.add(double, double) or ITimeStatistics.replace(double, double) method, or Double.NEGATIVE_INFINITY
    • lastValue

      public double lastValue()
      Description copied from interface: ITimeStatistics
      Returns the value in the time bucket that corresponds to ITimeStatistics.lastTime(), or 0 if ITimeStatistics.lastTime() is equal to Double.NEGATIVE_INFINITY, i.e. if none of these methods have yet been called.
      Specified by:
      lastValue in interface ITimeStatistics
      Returns:
      value in the time bucket that corresponds to ITimeStatistics.lastTime(), or 0
    • totalStatistics

      public DoubleSummaryStatistics totalStatistics()
      Description copied from interface: ITimeStatistics
      Returns a DoubleSummaryStatistics instance that contains the statistics across all existing time buckets.
      Specified by:
      totalStatistics in interface ITimeStatistics
      Returns:
      DoubleSummaryStatistics instance that contains the statistics across all values in the existing time buckets

      The figure below illustrates the result of calling this method:

    • interpolatedStatistics

      public DoubleSummaryStatistics interpolatedStatistics(double beginTime, double endTime, boolean includeZeros)
      Description copied from interface: ITimeStatistics
      Returns a DoubleSummaryStatistics instance that contains the statistics across all time buckets between the buckets containing the specified begin and end times. Interpolates the data for empty time buckets: assumes the value in an empty time bucket is equal to the value in the previous non-empty time bucket, or 0 if there is no such bucket.

      Specifies whether to include zero values into the statistics or not.

      Specified by:
      interpolatedStatistics in interface ITimeStatistics
      Parameters:
      beginTime - begin time, points to the first time bucket of collecting the statistics
      endTime - end time, points to the last time bucket of collecting the statistics
      includeZeros - whether or not to include zeroes while calculating statistics
      Returns:
      DoubleSummaryStatistics instance that contains the statistics across all time buckets between the specified begin and end times

      The figure below illustrates the result of calling this method:

    • interpolatedStatistics

      public DoubleSummaryStatistics interpolatedStatistics(double beginTime, double endTime)
      Description copied from interface: ITimeStatistics
      Returns a DoubleSummaryStatistics instance that contains the statistics across all time buckets between the buckets containing the specified begin and end times. Interpolates the data for empty time buckets: assumes the value in an empty time bucket is equal to the value in the previous non-empty time bucket, or 0 if there is no such bucket.

      Includes zero values into the statistics.

      Specified by:
      interpolatedStatistics in interface ITimeStatistics
      Parameters:
      beginTime - begin time, points to the first time bucket of collecting the statistics
      endTime - end time, points to the last time bucket of collecting the statistics
      Returns:
      DoubleSummaryStatistics instance that contains the statistics across all time buckets between the specified begin and end times

      The figure below illustrates the result of calling this method:

    • valueAt

      public double valueAt(double time)
      Description copied from interface: ITimeStatistics
      Returns the value in the bucket containing the specified time. If the specified time is associated with an empty bucket, zero is returned.
      Specified by:
      valueAt in interface ITimeStatistics
      Parameters:
      time - specified time
      Returns:
      value in the bucket containing the specified time, or zero
    • interpolatedValueAt

      public double interpolatedValueAt(double time)
      Description copied from interface: ITimeStatistics
      Returns the value in the bucket containing the specified time. If no values were added to the bucket containing the specified time, then returns the value in the last non-empty preceding the current bucket.

      In other words, returns a linearly interpolated value at the specified time.

      If there are no non-empty buckets at of before the specified time, returns zero.

      Specified by:
      interpolatedValueAt in interface ITimeStatistics
      Parameters:
      time - specified time
      Returns:
      linearly interpolated value at the specified time

      The figure below illustrates the result of interpolation:

    • sumBetween

      public double sumBetween(double beginTime, double endTime)
      Description copied from interface: ITimeStatistics
      Returns the sum of values in all non-empty buckets the buckets containing the specified begin and end times. Does not do interpolation, i.e. does not consider empty buckets.

      If the sum is needed with interpolated values, use ITimeStatistics.interpolatedStatistics(double, double) method.

      Specified by:
      sumBetween in interface ITimeStatistics
      Parameters:
      beginTime - begin time, points to the first time bucket of summation
      endTime - end time, points to the last time bucket of summation
      Returns:
      sum of values in all non-empty buckets between the specified begin and end times
    • getSum

      public TimeStatistics getSum(ITimeStatistics otherTimeStatistics)
      Description copied from interface: ITimeStatistics
      Returns a new instance of ITimeStatistics which is the sum of this instance and the other specified instance.

      Sum of two ITimeStatistics instances is an bucket-wise sum of them without considering any interpolation.

      Specified by:
      getSum in interface ITimeStatistics
      Parameters:
      otherTimeStatistics - ITimeStatistics which is summed with this one
      Returns:
      new instance of ITimeStatistics
    • minTime

      public double minTime()
      Description copied from interface: ITimeStatistics
      Returns zero, which is the minimum argument for a typical time series statistics.
      Specified by:
      minTime in interface ITimeStatistics
      Returns:
      always returns zero
    • maxTime

      public double maxTime()
      Description copied from interface: ITimeStatistics
      Returns the time corresponding to the right border of a non-empty bucket with the biggest argument.
      Specified by:
      maxTime in interface ITimeStatistics
      Returns:
      time of a non-empty bucket with the biggest argument
    • granularity

      public double granularity()
      Description copied from interface: ITimeStatistics
      Returns the granularity of this time statistics. Granularity is the size of time buckets at which the values are accumulated and aggregated.
      Specified by:
      granularity in interface ITimeStatistics
      Returns:
      granularity of this time statistics
    • plotData

      public List<Pair<Double,Double>> plotData()
      Description copied from interface: ITimeStatistics
      Returns the data for plotting the content of this ITimeStatistics instance on a line chart. The data is returned in the form of List of argument-value Pairs. The returned list is sorted by arguments in ascending order.
      Specified by:
      plotData in interface ITimeStatistics
      Returns:
      List of argument-value Pairs
    • cumulativePlotData

      public List<Pair<Double,Double>> cumulativePlotData()
      Description copied from interface: ITimeStatistics
      Returns the data for plotting the cumulative sum of the content of this TimeStatistics instance on a line chart. The data is returned in the form of List of argument-value Pairs. The returned list is sorted by arguments in ascending order.
      Specified by:
      cumulativePlotData in interface ITimeStatistics
      Returns:
      List of argument-value Pairs
    • timeOfCumulative

      public double timeOfCumulative(double cumulativeValue)
    • copy

      public TimeStatistics copy()
      Description copied from interface: ITimeStatistics
      Returns a copy of this ITimeStatistics instance.
      Specified by:
      copy in interface ITimeStatistics
      Returns:
      copy of this ITimeStatistics instance
    • dataSize

      public int dataSize()
      Description copied from interface: ITimeStatistics
      Returns the number of data points stored inside this ITimeStatistics instance.
      Specified by:
      dataSize in interface ITimeStatistics
      Returns:
      number of data points stored inside this ITimeStatistics instance
    • nonEmptyBucketsStream

      public Stream<Pair<Double,Double>> nonEmptyBucketsStream()
      Description copied from interface: ITimeStatistics
      Returns a Stream of argument-value Pairs. Each pair represents a non-empty (but potentially zero) bucket. Stream is ordered by time (the first element of a pair) in ascending order.
      Specified by:
      nonEmptyBucketsStream in interface ITimeStatistics
      Returns:
      Stream of argument-value Pairs