Class BasicStats

java.lang.Object
org.bzdev.math.stats.BasicStats
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
BasicStats.Population, BasicStats.Sample

public abstract class BasicStats extends Object implements Cloneable
Class to compute means and variances of a series of values. See West D.H.D, Updating mean and variance estimaes: An improved method. Commm. ACM22, 9 (Sept 1979, 532--535) for the algorithm. The implementation uses Kahan's summation algorithm to further improve the accuracy.

This is an abstract class: subclasses provide population and sample variances and standard deviations.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    BasicStats specialized to compute a population mean and variance.
    static class 
    BasicStats specialized to compute a sample mean and variance.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Constructor.
    protected
    BasicStats(double[] array)
    Constructor given an array containing all the data used to compute a mean and variance.
    protected
    BasicStats(double mean, double variance, long n)
    Constructor given a mean, variance, and data-set size.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(double value)
    Add a new value to the computation of the mean and variance.
    Add the values from another BasicStats object.
     
    protected abstract double
    Get the correction factor.
    double
    Get the mean value.
    double
    Get the standard deviation.
    double
    Get the variance.
    static double
    mean(double[] data)
    Compute the mean of a data set.
    static double
    median(double[] data)
    Compute the median of a data set.
    long
    Get the number of data points that were entered.
    static double
    trimmedMean(int D, double[] data)
    Compute the P-percent trimmed mean of a data set when P = (100)(1/D) for a positive integer D.
    static double
    trimmedMean(int N, int D, double[] data)
    Compute the P-percent trimmed mean of a data set when P = 100(N/D) for positive integers N and D.

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BasicStats

      protected BasicStats()
      Constructor. The method add(double) must be called repeatedly to provide the data used to compute the covariance matrix and mean array.
    • BasicStats

      protected BasicStats(double mean, double variance, long n)
      Constructor given a mean, variance, and data-set size. The uncorrected variance is the variance for a population variance. Constructors for subclasses should divide the variance provided by the value that getCorrection() would return.
      Parameters:
      mean - the mean value
      variance - the uncorrected variance
      n - the data-set size
    • BasicStats

      protected BasicStats(double[] array)
      Constructor given an array containing all the data used to compute a mean and variance.
      Parameters:
      array - an array, each component of which is a value to add to the computation
      Throws:
      NullPointerException - the argument was null.
  • Method Details

    • clone

      public Object clone() throws CloneNotSupportedException
      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException
    • getCorrection

      protected abstract double getCorrection()
      Get the correction factor. The variance that is computed will be scaled by a correction factor. This factor should be 1.0 when the data represents a total population. For a sample of a population, the factor is m/(m-1) where m is the sample size (the number of data points).
      Returns:
      the correction factor
    • add

      public BasicStats add(double value)
      Add a new value to the computation of the mean and variance.
      Parameters:
      value - the value to add
      Returns:
      this object
    • addAll

      public BasicStats addAll(BasicStats stats)
      Add the values from another BasicStats object.
      Parameters:
      stats - the BasicStats object whose values should be added
      Returns:
      this object
    • size

      public long size()
      Get the number of data points that were entered.
      Returns:
      the number of data points that were entered
    • getVariance

      public double getVariance() throws IllegalStateException
      Get the variance.
      Returns:
      the variance
      Throws:
      IllegalStateException - not enough data to compute a variance
    • getSDev

      public double getSDev()
      Get the standard deviation.
      Returns:
      the standard deviation
    • getMean

      public double getMean()
      Get the mean value.
      Returns:
      the mean value
    • mean

      public static double mean(double[] data)
      Compute the mean of a data set.
      Parameters:
      data - the data whose trimmed is to be computed
      Returns:
      the mean of the data set
      Throws:
      IllegalArgumentException - if the length of the array is zero
      NullPointerException - the last argument was null.
    • trimmedMean

      public static double trimmedMean(int D, double[] data)
      Compute the P-percent trimmed mean of a data set when P = (100)(1/D) for a positive integer D. The data will be divided into a number of bins and the mean will be computed ignoring the first and last bin.

      There are several common cases:

      • for a 25% trimmed mean, set the number of bins to 4.
      • for a 10% trimmed mean, set the number of bins to 10.
      • for a 5% trimmed mean, set the number of bins to 20.
      If there data set length is not an integral multiple of the number of bins, The trimmed means are computed given offsets from the initial index (0) and ending index (data.length), repeated twice to bracket the desired value. An esimate is then made by interpolation.
      Parameters:
      D - the number of bins in which to divide the data
      data - the data whose trimmed mean is to be computed
      Returns:
      the trimmed mean of the data
      Throws:
      IllegalArgumentException - if D is not a positive integer or the length of the array is zero
      NullPointerException - the last argument was null
    • trimmedMean

      public static double trimmedMean(int N, int D, double[] data)
      Compute the P-percent trimmed mean of a data set when P = 100(N/D) for positive integers N and D. The data will be divided into a number of bins and the mean will be computed ignoring the first and last bin.

      The data will be divided into bins whose length is the length of the data set multiplied by N/D. Rational numbers are used to represent the fraction trimmed to minimize the dependency of the result on floating-point roundoff errors. A typical choice for D is 100 (in which case N is the percentage trimmed) or 1000 (so that 125, for example, will correspond to 12.5%).

      If the data set length is not an integral multiple of the number of bins, The trimmed means are computed given offsets from the initial index (0) and ending index (data.length), repeated twice to bracket the desired value. An esimate is then made by interpolation.

      Parameters:
      N - the numererator of the ratio N/D representing the fraction of the data set trimmed from both ends
      D - the denominator of the ratio N/D representing the fraction of the data set trimmed from both ends
      data - the data whose trimmed mean is to be computed
      Returns:
      the trimmed mean of the data
      Throws:
      IllegalArgumentException - if N or D is not a positive integer or the length of the array is zero
      NullPointerException - the last argument was null
    • median

      public static double median(double[] data)
      Compute the median of a data set.
      Parameters:
      data - the data set
      Returns:
      the median of the values stored in argument array
      Throws:
      IllegalArgumentException - if the length of the array is zero
      NullPointerException - the last argument was null.