Class Combinatorics

Object
Combinatorics

public class Combinatorics extends Object
Collection of convenience methods for mathematical operations dealing with combinatorics.
Author:
Christoph Hauert
  • Constructor Summary

    Constructors
    Constructor
    Description
    Ensure non-instantiability with private default constructor
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    combinationFrac(int n, int m, int k)
    Efficient calculation of \[\dfrac{\binom{n}{k}}{\binom{m}{k}}=\frac{n!(m-k)!}{m!(n-k)!},\] or Binomial[n,k]/Binomial[m,k] for Mathematica:, while reducing the risk of numerical overflow.
    static int
    combinations(int n, int k)
    Combinations: number of ways to draw k elements from pool of size n.
    static long
    combinations(long n, long k)
    Combinations: number of ways to draw k elements from pool of size n.
    static double
    H2(int X, int x, int Y, int y)
    Efficient calculation of the hypergeometric probability distribution H<sub>2</sub>(X,x,Y,y) for sampling x individuals from pool of size X and y individuals from pool of size Y.
    static double
    pow(double x, int n)
    Calculate x^n for double x and integer n.
    static double
    pow(int x, int n)
    Calculate x^n for integer x and integer n.
    private static double
    powabs(double x, int n)
    Helper method to calculate \(x^n\) with \(n>0\).
    private static int
    powabs(int x, int n)
    Helper method to calculate \(x^n\) with \(n>0\).

    Methods inherited from class Object

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

    • Combinatorics

      public Combinatorics()
      Ensure non-instantiability with private default constructor
  • Method Details

    • pow

      public static double pow(int x, int n)
      Calculate x^n for integer x and integer n. For n&geq;0 the result is an integer but returned as a double, which is necessary to deal with n<0. All intermediate calculations are perfomed as integers and will throw the corresponding exceptions, e.g. if numbers exceed Integer#MAX_VALUE. This is an optimization for the CPU intense Math.pow(double, double).
      Parameters:
      x - the basis of the power
      n - the integer exponent of the power
      Returns:
      x^n
    • powabs

      private static int powabs(int x, int n)
      Helper method to calculate \(x^n\) with \(n>0\).
      Parameters:
      x - the basis of the power
      n - the (positive) integer exponent of the power
      Returns:
      x^n
    • pow

      public static double pow(double x, int n)
      Calculate x^n for double x and integer n. This is an optimization for the CPU intense Math.pow(double, double).
      Parameters:
      x - the basis of the power
      n - the exponent of the power
      Returns:
      x^n
    • powabs

      private static double powabs(double x, int n)
      Helper method to calculate \(x^n\) with \(n>0\).
      Parameters:
      x - the basis of the power
      n - the (positive) integer exponent of the power
      Returns:
      x^n
    • combinations

      public static int combinations(int n, int k)
      Combinations: number of ways to draw k elements from pool of size n.

      Mathematica: Binomial[n,k] = n!/(k!(n-k)!)

      Parameters:
      n - the pool size
      k - the number of samples
      Returns:
      Binomial[n,k]
      Throws:
      ArithmeticException - if result &gt;Integer.MAX_VALUE
    • combinations

      public static long combinations(long n, long k)
      Combinations: number of ways to draw k elements from pool of size n.

      Mathematica: Binomial[n,k] = n!/(k!(n-k)!)

      Parameters:
      n - the pool size
      k - the number of samples
      Returns:
      Binomial[n,k]
      Throws:
      ArithmeticException - if result &gt;Long.MAX_VALUE
    • combinationFrac

      public static double combinationFrac(int n, int m, int k)
      Efficient calculation of \[\dfrac{\binom{n}{k}}{\binom{m}{k}}=\frac{n!(m-k)!}{m!(n-k)!},\] or Binomial[n,k]/Binomial[m,k] for Mathematica:, while reducing the risk of numerical overflow.

      Note, this is the same as H2(n, 0, m-n, k) with m&gt;n.

      Parameters:
      n - the size of first pool
      m - the size of the second pool
      k - the number of samples to draw from each pool (without replacement)
      Returns:
      Binomial[n,k]/Binomial[m,k]
      See Also:
    • H2

      public static double H2(int X, int x, int Y, int y)
      Efficient calculation of the hypergeometric probability distribution H<sub>2</sub>(X,x,Y,y) for sampling x individuals from pool of size X and y individuals from pool of size Y.

      Mathematica: H_2[X, x, Y, y] = Binomial[X,x] Binomial[Y,y] / Binomial[X+Y,x+y]

      Parameters:
      X - the size of first pool
      x - the number samples from first pool
      Y - the size of second pool
      y - the number samples from second pool
      Returns:
      H_2[X, x, Y, y]