Class BasicStats.Sample

java.lang.Object
org.bzdev.math.stats.BasicStats
org.bzdev.math.stats.BasicStats.Sample
All Implemented Interfaces:
Cloneable
Enclosing class:
BasicStats

public static class BasicStats.Sample extends BasicStats
BasicStats specialized to compute a sample mean and variance.
  • Constructor Details

    • Sample

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

      public Sample(double mean, double variance, long n)
      Constructor given a mean, variance, and data-set size.
      Parameters:
      mean - the mean value
      variance - the sample variance
      n - the data-set size
    • Sample

      public Sample(double[] array)
      Constructor given an array containing all the data used to compute a covariance matrix.
      Parameters:
      array - an array, each component of which is an array containing the values for n variables
  • Method Details

    • identity

      public static final org.bzdev.math.stats.BasicStats.Sample.Identity identity()
      Get an instance of Sample that serves as an identity for Stream.reduce(Object,BiFunction,BinaryOperator). An identity implementation will not store any data, but will return an appropriate instance of BasicStats that does store the data added, but with no history of previously added values.

      If rv is a random variable that returns a stream of double values, the following example shows a typical use of this method:

      
       BasicStats stats = rv.parallelStream(1000000)
         .mapToObj(Double::valueOf)
         .reduce(BasicStats.Sample.identity(),
                  BasicStats::add,
                  BasicStats::addAll);
       
      The method mapToObj is needed because the reduce method is a method of Stream but not DoubleStream. When used with a parallel stream, the mapToObj method;'s argument will typically be a lambda expression that peforms significantly more compuation: otherwise the cost of threading can result in the parallel version performing worse than the sequential version.
      Returns:
      an instance of BasicStat that can be used as an idenity in a "reduce" method.
    • getCorrection

      protected double getCorrection()
      Description copied from class: BasicStats
      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).
      Specified by:
      getCorrection in class BasicStats
      Returns:
      the correction factor