Class BasicStatsMV.Sample

java.lang.Object
org.bzdev.math.stats.BasicStatsMV
org.bzdev.math.stats.BasicStatsMV.Sample
Enclosing class:
BasicStatsMV

public static class BasicStatsMV.Sample extends BasicStatsMV
Class to compute a sample variance.
  • Constructor Details

    • Sample

      public Sample(int n)
      Constructor. The method BasicStatsMV.add(double[]) must be called repeatedly to provide the dataset used to compute the variance and mean array.
      Parameters:
      n - the number of variables
    • Sample

      public Sample(double[] means, double[] variances, long m)
      Constructor given means, variances, and the data-set size (the number for each variable)
      Parameters:
      means - an array of length n containing the mean values for n variables
      variances - an array of length n containing the variances for n variables
      m - the data-set size
      Throws:
      IllegalArgumentException - the array lengths differ
    • Sample

      public Sample(double[][] arrays, int n) throws IllegalArgumentException
      Constructor given an array containing the initial dataset used to compute a variance. This dataset can be extended using the BasicStatsMV.add(double[]) method.
      Parameters:
      arrays - an array, each component of which is an array containing the values for n variables
      n - the number of variables
      Throws:
      IllegalArgumentException - the argument arrays was too short
  • Method Details

    • identity

      public static final org.bzdev.math.stats.BasicStatsMV.Sample.Identity identity(int n)
      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:

      
       BasicStatsMV stats = rvs.parallelStream(1000000)
         .mapToObj(Double::valueOf)
         .reduce(BasicStats.Sample.identity(10),
                  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.
      Parameters:
      n - the length of each array that can be entered
      Returns:
      an instance of BasicStat that can be used as an idenity in a "reduce" method.
    • getCorrection

      protected double getCorrection()
      Description copied from class: BasicStatsMV
      The elements of the variances array that is computed will be scaled by a correction factor. This factor should be 1.0 when the dataset 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 factors.
      Specified by:
      getCorrection in class BasicStatsMV
      Returns:
      the correction factor; Double.NaN if the factor cannot be computed due to too few entries