Class StdStats


  • public final class StdStats
    extends Object
    The StdStats class provides static methods for computing statistics such as min, max, mean, sample standard deviation, and sample variance.

    For additional documentation, see Section 2.2 of Computer Science: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.

    Author:
    Robert Sedgewick, Kevin Wayne
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void main​(String[] args)
      Unit tests StdStats.
      static double max​(double[] a)
      Returns the maximum value in the specified array.
      static double max​(double[] a, int lo, int hi)
      Returns the maximum value in the specified subarray.
      static int max​(int[] a)
      Returns the maximum value in the specified array.
      static double mean​(double[] a)
      Returns the average value in the specified array.
      static double mean​(double[] a, int lo, int hi)
      Returns the average value in the specified subarray.
      static double mean​(int[] a)
      Returns the average value in the specified array.
      static double min​(double[] a)
      Returns the minimum value in the specified array.
      static double min​(double[] a, int lo, int hi)
      Returns the minimum value in the specified subarray.
      static int min​(int[] a)
      Returns the minimum value in the specified array.
      static void plotBars​(double[] a)
      Plots bars from (0, ai) to (ai) for each i to standard draw.
      static void plotLines​(double[] a)
      Plots the line segments connecting (i, ai) to (i+1, ai+1) for each i to standard draw.
      static void plotPoints​(double[] a)
      Plots the points (0, a0), (1, a1), ..., (n-1, an-1) to standard draw.
      static double stddev​(double[] a)
      Returns the sample standard deviation in the specified array.
      static double stddev​(double[] a, int lo, int hi)
      Returns the sample standard deviation in the specified subarray.
      static double stddev​(int[] a)
      Returns the sample standard deviation in the specified array.
      static double stddevp​(double[] a)
      Returns the population standard deviation in the specified array.
      static double stddevp​(double[] a, int lo, int hi)
      Returns the population standard deviation in the specified subarray.
      static double var​(double[] a)
      Returns the sample variance in the specified array.
      static double var​(double[] a, int lo, int hi)
      Returns the sample variance in the specified subarray.
      static double var​(int[] a)
      Returns the sample variance in the specified array.
      static double varp​(double[] a)
      Returns the population variance in the specified array.
      static double varp​(double[] a, int lo, int hi)
      Returns the population variance in the specified subarray.
    • Method Detail

      • max

        public static double max​(double[] a)
        Returns the maximum value in the specified array.
        Parameters:
        a - the array
        Returns:
        the maximum value in the array a[]; Double.NEGATIVE_INFINITY if no such value
      • max

        public static double max​(double[] a,
                                 int lo,
                                 int hi)
        Returns the maximum value in the specified subarray.
        Parameters:
        a - the array
        lo - the left endpoint of the subarray (inclusive)
        hi - the right endpoint of the subarray (exclusive)
        Returns:
        the maximum value in the subarray a[lo..hi); Double.NEGATIVE_INFINITY if no such value
        Throws:
        IllegalArgumentException - if a is null
        IllegalArgumentException - unless (0 <= lo) && (lo < hi) && (hi <= a.length)
      • max

        public static int max​(int[] a)
        Returns the maximum value in the specified array.
        Parameters:
        a - the array
        Returns:
        the maximum value in the array a[]; Integer.MIN_VALUE if no such value
      • min

        public static double min​(double[] a)
        Returns the minimum value in the specified array.
        Parameters:
        a - the array
        Returns:
        the minimum value in the array a[]; Double.POSITIVE_INFINITY if no such value
      • min

        public static double min​(double[] a,
                                 int lo,
                                 int hi)
        Returns the minimum value in the specified subarray.
        Parameters:
        a - the array
        lo - the left endpoint of the subarray (inclusive)
        hi - the right endpoint of the subarray (exclusive)
        Returns:
        the maximum value in the subarray a[lo..hi); Double.POSITIVE_INFINITY if no such value
        Throws:
        IllegalArgumentException - if a is null
        IllegalArgumentException - unless (0 <= lo) && (lo < hi) && (hi <= a.length)
      • min

        public static int min​(int[] a)
        Returns the minimum value in the specified array.
        Parameters:
        a - the array
        Returns:
        the minimum value in the array a[]; Integer.MAX_VALUE if no such value
      • mean

        public static double mean​(double[] a)
        Returns the average value in the specified array.
        Parameters:
        a - the array
        Returns:
        the average value in the array a[]; Double.NaN if no such value
      • mean

        public static double mean​(double[] a,
                                  int lo,
                                  int hi)
        Returns the average value in the specified subarray.
        Parameters:
        a - the array
        lo - the left endpoint of the subarray (inclusive)
        hi - the right endpoint of the subarray (exclusive)
        Returns:
        the average value in the subarray a[lo..hi); Double.NaN if no such value
        Throws:
        IllegalArgumentException - if a is null
        IllegalArgumentException - unless (0 <= lo) && (lo < hi) && (hi <= a.length)
      • mean

        public static double mean​(int[] a)
        Returns the average value in the specified array.
        Parameters:
        a - the array
        Returns:
        the average value in the array a[]; Double.NaN if no such value
      • var

        public static double var​(double[] a)
        Returns the sample variance in the specified array.
        Parameters:
        a - the array
        Returns:
        the sample variance in the array a[]; Double.NaN if no such value
      • var

        public static double var​(double[] a,
                                 int lo,
                                 int hi)
        Returns the sample variance in the specified subarray.
        Parameters:
        a - the array
        lo - the left endpoint of the subarray (inclusive)
        hi - the right endpoint of the subarray (exclusive)
        Returns:
        the sample variance in the subarray a[lo..hi); Double.NaN if no such value
        Throws:
        IllegalArgumentException - if a is null
        IllegalArgumentException - unless (0 <= lo) && (lo < hi) && (hi <= a.length)
      • var

        public static double var​(int[] a)
        Returns the sample variance in the specified array.
        Parameters:
        a - the array
        Returns:
        the sample variance in the array a[]; Double.NaN if no such value
      • varp

        public static double varp​(double[] a)
        Returns the population variance in the specified array.
        Parameters:
        a - the array
        Returns:
        the population variance in the array a[]; Double.NaN if no such value
      • varp

        public static double varp​(double[] a,
                                  int lo,
                                  int hi)
        Returns the population variance in the specified subarray.
        Parameters:
        a - the array
        lo - the left endpoint of the subarray (inclusive)
        hi - the right endpoint of the subarray (exclusive)
        Returns:
        the population variance in the subarray a[lo..hi); Double.NaN if no such value
        Throws:
        IllegalArgumentException - if a is null
        IllegalArgumentException - unless (0 <= lo) && (lo < hi) && (hi <= a.length)
      • stddev

        public static double stddev​(double[] a)
        Returns the sample standard deviation in the specified array.
        Parameters:
        a - the array
        Returns:
        the sample standard deviation in the array a[]; Double.NaN if no such value
      • stddev

        public static double stddev​(int[] a)
        Returns the sample standard deviation in the specified array.
        Parameters:
        a - the array
        Returns:
        the sample standard deviation in the array a[]; Double.NaN if no such value
      • stddev

        public static double stddev​(double[] a,
                                    int lo,
                                    int hi)
        Returns the sample standard deviation in the specified subarray.
        Parameters:
        a - the array
        lo - the left endpoint of the subarray (inclusive)
        hi - the right endpoint of the subarray (exclusive)
        Returns:
        the sample standard deviation in the subarray a[lo..hi); Double.NaN if no such value
        Throws:
        IllegalArgumentException - if a is null
        IllegalArgumentException - unless (0 <= lo) && (lo < hi) && (hi <= a.length)
      • stddevp

        public static double stddevp​(double[] a)
        Returns the population standard deviation in the specified array.
        Parameters:
        a - the array
        Returns:
        the population standard deviation in the array; Double.NaN if no such value
      • stddevp

        public static double stddevp​(double[] a,
                                     int lo,
                                     int hi)
        Returns the population standard deviation in the specified subarray.
        Parameters:
        a - the array
        lo - the left endpoint of the subarray (inclusive)
        hi - the right endpoint of the subarray (exclusive)
        Returns:
        the population standard deviation in the subarray a[lo..hi); Double.NaN if no such value
        Throws:
        IllegalArgumentException - if a is null
        IllegalArgumentException - unless (0 <= lo) && (lo < hi) && (hi <= a.length)
      • plotPoints

        public static void plotPoints​(double[] a)
        Plots the points (0, a0), (1, a1), ..., (n-1, an-1) to standard draw.
        Parameters:
        a - the array of values
      • plotLines

        public static void plotLines​(double[] a)
        Plots the line segments connecting (i, ai) to (i+1, ai+1) for each i to standard draw.
        Parameters:
        a - the array of values
      • plotBars

        public static void plotBars​(double[] a)
        Plots bars from (0, ai) to (ai) for each i to standard draw.
        Parameters:
        a - the array of values
      • main

        public static void main​(String[] args)
        Unit tests StdStats. Convert command-line arguments to array of doubles and call various methods.
        Parameters:
        args - the command-line arguments