Class SummaryStat

  • All Implemented Interfaces:
    Serializable, Cloneable
    Direct Known Subclasses:
    QuantileEstimator, SamplingSummaryStat, TimeWeightedSummaryStat

    public class SummaryStat
    extends Object
    implements Serializable, Cloneable
    Class to collect the most important statistics without having to store all values encountered. It can return mean, standard deviation, variance, min, max etc. in O(1) time. Values are passed by calling the value(double) method. Values can be weighted, just call value(double, double) instead.

    In other simulation packages this is sometimes called "tally".

    This implementation is based on: D. H. D. West (1979). Communications of the ACM, 22, 9, 532-535: Updating Mean and Variance Estimates: An Improved Method

    Author:
    Torsten Hildebrandt
    See Also:
    Serialized Form
    • Field Detail

      • lastValue

        protected double lastValue
      • lastWeight

        protected double lastWeight
    • Constructor Detail

      • SummaryStat

        public SummaryStat()
      • SummaryStat

        public SummaryStat​(SummaryStat vs)
        Create a new SummaryStat-object initialized with the values of "vs". Copy constructor.
    • Method Detail

      • clear

        public void clear()
        Resets this object.
      • values

        public SummaryStat values​(double... vs)
        Convenience method to add all values given as arguments with a weight of 1.
        Parameters:
        vs - The values to add.
        Returns:
        this, to allow easy chaining of calls.
      • value

        public SummaryStat value​(double v)
        Adds the given value with a weight of 1.
        Parameters:
        v - The value to add.
        Returns:
        this, to allow easy chaining of calls.
      • value

        public SummaryStat value​(double v,
                                 double weight)
                          throws IllegalArgumentException
        Adds a value with a given weight.
        Parameters:
        v - The value to add.
        weight - The weight to give to this value. Has to be positive.
        Returns:
        this, to allow easy chaining of calls.
        Throws:
        IllegalArgumentException - If weight was negative.
      • mean

        public double mean()
        Returns the mean of all values given to value(double).
        Returns:
        The arithmetic mean of all values seen so far.
      • stdDev

        public double stdDev()
        The standard deviation of all values.
        Returns:
        The standard deviation of all values given to value(double).
      • variance

        public double variance()
        Returns the sample variance of the values.
        Returns:
        The (sample) variance of all values given to value(double). Returns NaN, if no values were added yet.
      • variancePopulation

        public double variancePopulation()
        Returns the population variance of the values.
        Returns:
        The (sample) variance of all values given to value(double). Returns NaN, if no values were added yet.
      • varCoeff

        public double varCoeff()
        Returns the coefficient of variation (stdDev() divided by mean()).
        Returns:
        The coefficient of variation.
      • sum

        public double sum()
        Returns the sum of all value(double)s (taking into account potential weights if value(double, double) is used).
        Returns:
        The sum of all values.
      • weightSum

        public double weightSum()
        Returns the sum of all weights. If only value(double) is used, then the value returned is identical to the value returned by numObs.
        Returns:
        The weight sum.
      • min

        public double min()
        Returns the minimum value seen so far.
        Returns:
        The minimum value seen so far, or NaN, if no values were given so far.
      • max

        public double max()
        Returns the maximum value seen so far.
        Returns:
        The maximum value seen so far, or NaN, if no values were given so far.
      • combine

        public SummaryStat combine​(SummaryStat other)
        Combines the data in other with this SummaryStat-Object. The combined object behaves as if it had also seen the data of "other".
        Parameters:
        other - The SummaryStat to combine with this object.
        Returns:
        Returns this to allow easy chaining of calls.
      • clone

        public SummaryStat clone()
        Clones this object. We can use the standard functionality here, as there are only primitive fields.
        Overrides:
        clone in class Object
        Returns:
        A clone of this SummaryStat.
      • confidenceIntervalLower

        public double confidenceIntervalLower()
        Returns:
        lower value of a confidence interval with a 0.95-confidence level
      • confidenceIntervalUpper

        public double confidenceIntervalUpper()
      • confidenceIntervalLower

        public double confidenceIntervalLower​(double errorProb)
      • confidenceIntervalUpper

        public double confidenceIntervalUpper​(double errorProb)
      • confIntRangeSingle

        public double confIntRangeSingle​(double errorProb)
      • lastWeight

        public double lastWeight()
        Returns the weight of the last value passed to value(double) or value(double, double).
        Returns:
        The last value's weight, or NaN if no numObs==0.
      • summarize

        public static SummaryStat summarize​(double... values)
        This method creates a new SummaryStat object and passes all values to it.
        Parameters:
        values - The values to use.
        Returns:
        A SummaryStat summarizing the values.
      • summarize

        public static SummaryStat summarize​(int... values)
        This method creates a new SummaryStat object and passes all values to it.
        Parameters:
        values - The values to use.
        Returns:
        A SummaryStat summarizing the values.
      • combine

        public static SummaryStat combine​(SummaryStat stats1,
                                          SummaryStat stats2)
        Creates a new SummaryStat object that behaves if all values seen by stats1 and stats2 would have been passed to it.
        Parameters:
        stats1 - SummaryStat summarizing first set of values.
        stats2 - SummaryStat summarizing second set of values.
        Returns:
        New SummaryStat object summarizing the union of first and second value set.