Statistical functions on arrays of numbers, namely, the mean, variance, standard deviation, covariance, min and max : Math « Development Class « Java






Statistical functions on arrays of numbers, namely, the mean, variance, standard deviation, covariance, min and max

       

/*
 * Copyright (c) 2009-2010, Sergey Karakovskiy and Julian Togelius
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Mario AI nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

//package ch.idsia.utils.statistics;

import java.io.IOException;
import java.io.PrintStream;
import java.util.Enumeration;
import java.util.Vector;

/**
 * This class implements some simple statistical functions on arrays of numbers,
 * namely, the mean, variance, standard deviation, covariance, min and max.
 */

public class Stats {

  /**
   * Converts a vector of Numbers into an array of double. This function does
   * not necessarily belong here, but is commonly required in order to apply
   * the statistical functions conveniently, since they only deal with arrays
   * of double. (Note that a Number of the common superclass of all the Object
   * versions of the primitives, such as Integer, Double etc.).
   */
  // package that at present just provides average and sd of a
  // vector of doubles

  // also enables writing the
  // Gnuplot comments begin with #

  // next need to find out how to select a particular line style
  // found it :
  // This plots sin(x) and cos(x) with linespoints, using the same line type
  // but different point types:
  // plot sin(x) with linesp lt 1 pt 3, cos(x) with linesp lt 1 pt 4
  public static double[] v2a(Vector v) {
    double[] d = new double[v.size()];
    int i = 0;
    for (Enumeration e = v.elements(); e.hasMoreElements();)
      d[i++] = ((Number) e.nextElement()).doubleValue();
    return d;
  }

  /**
   * Calculates the square of a double.
   * 
   * @return Returns x*x
   */

  public static double sqr(double x) {
    return x * x;
  }

  /**
   * Returns the average of an array of double.
   */

  public static double mean(double[] v) {
    double tot = 0.0;
    for (int i = 0; i < v.length; i++)
      tot += v[i];
    return tot / v.length;
  }

  /**
   * @param v
   *            - sample
   * @return the average of an array of int.
   */

  public static double mean(int[] v) {
    double tot = 0.0;
    for (int i = 0; i < v.length; i++)
      tot += v[i];
    return tot / v.length;
  }

  /**
   * Returns the sample standard deviation of an array of double.
   */

  public static double sdev(double[] v) {
    return Math.sqrt(variance(v));
  }

  /**
   * Returns the standard error of an array of double, where this is defined
   * as the standard deviation of the sample divided by the square root of the
   * sample size.
   */

  public static double stderr(double[] v) {
    return sdev(v) / Math.sqrt(v.length);
  }

  /**
   * Returns the variance of the array of double.
   */

  public static double variance(double[] v) {
    double mu = mean(v);
    double sumsq = 0.0;
    for (int i = 0; i < v.length; i++)
      sumsq += sqr(mu - v[i]);
    return sumsq / (v.length);
    // return 1.12; this was done to test a discrepancy with Business
    // Statistics
  }

  /**
   * this alternative version was used to check correctness
   */

  private static double variance2(double[] v) {
    double mu = mean(v);
    double sumsq = 0.0;
    for (int i = 0; i < v.length; i++)
      sumsq += sqr(v[i]);
    System.out.println(sumsq + " : " + mu);
    double diff = (sumsq - v.length * sqr(mu));
    System.out.println("Diff = " + diff);
    return diff / (v.length);
  }

  /**
   * Returns the covariance of the paired arrays of double.
   */

  public static double covar(double[] v1, double[] v2) {
    double m1 = mean(v1);
    double m2 = mean(v2);
    double sumsq = 0.0;
    for (int i = 0; i < v1.length; i++)
      sumsq += (m1 - v1[i]) * (m2 - v2[i]);
    return sumsq / (v1.length);
  }

  public static double correlation(double[] v1, double[] v2) {
    // an inefficient implementation!!!
    return covar(v1, v2) / (sdev(v1) * sdev(v2));
  }

  public static double correlation2(double[] v1, double[] v2) {
    // an inefficient implementation!!!
    return sqr(covar(v1, v2)) / (covar(v1, v1) * covar(v2, v2));
  }

  /**
   * Returns the maximum value in the array.
   */

  public static double max(double[] v) {
    double m = v[0];
    for (int i = 1; i < v.length; i++)
      m = Math.max(m, v[i]);
    return m;
  }

  /**
   * Returns the minimum value in the array.
   */

  public static double min(double[] v) {
    double m = v[0];
    for (int i = 1; i < v.length; i++)
      m = Math.min(m, v[i]);
    return m;
  }

  /**
   * Prints the means and standard deviation of the data to the standard
   * output.
   */

  public static void analyse(double[] v) {
    analyse(v, System.out);
    // System.out.println("Average = " + mean(v) + "  sd = " + sdev(v));
  }

  /**
   * Prints the means and standard deviation of the data to the specified
   * PrintStream
   * 
   * @param v
   *            contains the data
   * @param s
   *            is the corresponding PrintStream
   */

  public static void analyse(double[] v, PrintStream s) {
    s.println("Average = " + mean(v) + "  sd = " + sdev(v));
  }

  /**
   * @param v
   *            contains the data
   * @return A String summary of the with the mean and standard deviation of
   *         the data.
   */

  public static String analysisString(double[] v) {
    return "Average = " + mean(v) + "  sd = " + sdev(v) + "  min = "
        + min(v) + "  max = " + max(v);
  }

  /**
   * Returns a string that compares the root mean square of the data with the
   * standard deviation of the data. This is probably too specialised to be of
   * much general use.
   * 
   * @param v
   *            contains the data
   * @return root mean square = <...> standard deviation = <...>
   */
  public static String rmsString(double[] v) {
    double[] tv = new double[v.length];
    for (int i = 0; i < v.length; i++)
      tv[i] = v[i] * v[i];
    return "rms = " + mean(tv) + " sd = " + sdev(v) + "\n";
  }

  /**
   * Runs through some utils using the functions defined in this class.
   * 
   * @throws java.io.IOException
   */

  public static void main(String[] args) throws IOException {

    double[] d = new double[0];

    double dd = mean(d);

    System.out.println(dd + "\t" + Double.isNaN(dd));

    for (int i = 0; i < 3; i++) {
      double[] x = new double[i];
      System.out.println(mean(x) + "\t " + stderr(x) + "\t " + sdev(x));
    }
  }

}

   
    
    
    
    
    
    
  








Related examples in the same category

1.Absolute value
2.Find absolute value of float, int, double and long using Math.abs
3.Find ceiling value of a number using Math.ceil
4.Find exponential value of a number using Math.exp
5.Find floor value of a number using Math.floor
6.Find minimum of two numbers using Math.min
7.Find power using Math.pow
8.Find square root of a number using Math.sqrt
9.Find natural logarithm value of a number using Math.log
10.Find maximum of two numbers using Math.max
11.Get the power valueGet the power value
12.Using the Math Trig MethodsUsing the Math Trig Methods
13.Using BigDecimal for PrecisionUsing BigDecimal for Precision
14.Demonstrate our own version round()Demonstrate our own version round()
15.Demonstrate a few of the Math functions for TrigonometryDemonstrate a few of the Math functions for Trigonometry
16.Exponential DemoExponential Demo
17.Min Demo
18.Basic Math DemoBasic Math Demo
19.Using strict math in applicationsUsing strict math in applications
20.Conversion between polar and rectangular coordinates
21.Using the pow() function
22.Using strict math at the method level
23.Calculating hyperbolic functions
24.Calculating trigonometric functions
25.Weighted floating-point comparisons
26.Solving right triangles
27.Applying the quadratic formula
28.Calculate the floor of the log, base 2
29.Greatest Common Divisor (GCD) of positive integer numbers
30.Least Common Multiple (LCM) of two strictly positive integer numbers
31.Moving Average
32.Make Exponention
33.Caclulate the factorial of N
34.Trigonometric DemoTrigonometric Demo
35.Complex Number Demo
36.sqrt(a^2 + b^2) without under/overflow
37.Returns an integer hash code representing the given double array value.
38.Returns an integer hash code representing the given double value.
39.Returns n!. Shorthand for n Factorial, the product of the numbers 1,...,n as a double.
40.Returns n!. Shorthand for n Factorial, the product of the numbers 1,...,n.
41.Returns the hyperbolic sine of x.
42.Contains static definition for matrix math methods.
43.For a double precision value x, this method returns +1.0 if x >= 0 and -1.0 if x < 0. Returns NaN if x is NaN.
44.For a float value x, this method returns +1.0F if x >= 0 and -1.0F if x < 0. Returns NaN if x is NaN.
45.Normalize an angle in a 2&pi wide interval around a center value.
46.Normalizes an angle to a relative angle.
47.Normalizes an angle to an absolute angle
48.Normalizes an angle to be near an absolute angle
49.Returns the natural logarithm of n!.
50.Returns the least common multiple between two integer values.
51.Gets the greatest common divisor of the absolute value of two numbers
52.Matrix manipulation
53.Returns exact (http://mathworld.wolfram.com/BinomialCoefficient.html) Binomial Coefficient
54.Returns a double representation of the (http://mathworld.wolfram.com/BinomialCoefficient.html) Binomial Coefficient
55.Returns the natural log of the (http://mathworld.wolfram.com/BinomialCoefficient.html) Binomial Coefficient
56.Returns the hyperbolic cosine of x.
57.Math Utils
58.Implements the methods which are in the standard J2SE's Math class, but are not in in J2ME's.
59.Utility methods for mathematical problems.
60.A math utility class with static methods.
61.Computes the binomial coefficient "n over k"
62.Log Gamma
63.Log Beta
64.Beta
65.Gamma
66.Factorial
67.Computes p(x;n,p) where x~B(n,p)
68.Returns the sum of two doubles expressed in log space
69.sigmod
70.sigmod rev
71.Numbers that are closer than this are considered equal
72.Returns the KL divergence, K(p1 || p2).
73.Returns the sum of two doubles expressed in log space
74.Returns the difference of two doubles expressed in log space
75.Is Prime
76.This class calculates the Factorial of a numbers passed into the program through command line arguments.This class calculates the Factorial of a numbers passed into the program through command line arguments.
77.Calculates the Greatest Common Divisor of two numbers passed into the program through command line arguments.
78.Variance: the square of the standard deviation.
79.Population Standard Deviation
80.Returns from a static prime table the least prime that is greater than or equal to a specified value.