Class ProbMath

java.lang.Object
nl.tudelft.simulation.jstats.math.ProbMath

public final class ProbMath extends Object
The ProbMath class defines some very basic probabilistic mathematical functions.

Copyright (c) 2004-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://simulation.tudelft.nl. The DSOL project is distributed under a three-clause BSD-style license, which can be found at https://https://simulation.tudelft.nl/dsol/docs/latest/license.html.

Author:
Alexander Verbraeck
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double[]
    stored values of n! as a double value.
    static final long[]
    stored values of n! as a long value.
    static final double[]
    stored values of a^n as a double value.
    static final long[]
    stored values of 2^n as a long value.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    beta(double z, double w)
    Calculates Beta(z, w) where Beta(z, w) = Γ(z) Γ(w) / Γ(z + w).
    static long
    comb(int n, int k)
    computes the combinations of n over k as a long.
    static long
    comb(long n, long k)
    computes the combinations of n over k as a long.
    static double
    combinations(int n, int k)
    computes the combinations of n over k.
    static double
    combinations(long n, long k)
    computes the combinations of n over k.
    static double
    erf(double z)
    Approximates erf(z) using a Taylor series.
    The Taylor series for erf(z) for abs(z) < 0.5 that is used is:
        erf(z) = (exp(-z2) / √π) Σ [ 2z2n + 1 / (2n + 1)!!]
    The Taylor series for erf(z) for abs(z) > 3.7 that is used is:
        erf(z) = 1 - (exp(-z2) / √π) Σ [ (-1)n (2n - 1)!! z-(2n + 1) / 2n]
    See https://mathworld.wolfram.com/Erf.html.
    static double
    erfInv(double y)
    Approximates erf-1(p) based on http://www.naic.edu/~jeffh/inverse_cerf.c code.
    static long
    fac(int n)
    Compute n factorial as a long.
    static long
    fac(long n)
    Compute n factorial as a long.
    static double
    factorial(int n)
    Compute n factorial as a double.
    static double
    factorial(long n)
    Compute n factorial as a double.
    static double
    gamma(double x)
    Calculates gamma(x).
    static double
    gammaln(double xx)
    Calculates ln(gamma(x)).
    static long
    perm(int n, int k)
    Computes the k-permutations of n as a long.
    static long
    perm(long n, long k)
    Computes the k-permutations of n as a long.
    static double
    permutations(int n, int k)
    Compute the k-permutations of n.
    static double
    permutations(long n, long k)
    Compute the k-permutations of n.

    Methods inherited from class java.lang.Object

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

    • FACTORIAL_DOUBLE

      public static final double[] FACTORIAL_DOUBLE
      stored values of n! as a double value.
    • FACTORIAL_LONG

      public static final long[] FACTORIAL_LONG
      stored values of n! as a long value.
    • POW2_DOUBLE

      public static final double[] POW2_DOUBLE
      stored values of a^n as a double value.
    • POW2_LONG

      public static final long[] POW2_LONG
      stored values of 2^n as a long value.
  • Method Details

    • factorial

      public static double factorial(int n)
      Compute n factorial as a double. fac(n) = n * (n-1) * (n-2) * ... 2 * 1.
      Parameters:
      n - int; the value to calculate n! for
      Returns:
      double; n factorial
    • factorial

      public static double factorial(long n)
      Compute n factorial as a double. fac(n) = n * (n-1) * (n-2) * ... 2 * 1.
      Parameters:
      n - long; the value to calculate n! for
      Returns:
      double; n factorial
    • fac

      public static long fac(int n)
      Compute n factorial as a long. fac(n) = n * (n-1) * (n-2) * ... 2 * 1.
      Parameters:
      n - int; the value to calculate n! for
      Returns:
      double; n factorial
    • fac

      public static long fac(long n)
      Compute n factorial as a long. fac(n) = n * (n-1) * (n-2) * ... 2 * 1.
      Parameters:
      n - long; the value to calculate n! for
      Returns:
      double; n factorial
    • permutations

      public static double permutations(int n, int k)
      Compute the k-permutations of n.
      Parameters:
      n - int; the first parameter
      k - int; the second parameter
      Returns:
      the k-permutations of n
    • permutations

      public static double permutations(long n, long k)
      Compute the k-permutations of n.
      Parameters:
      n - long; the first parameter
      k - long; the second parameter
      Returns:
      the k-permutations of n
    • perm

      public static long perm(int n, int k)
      Computes the k-permutations of n as a long.
      Parameters:
      n - int; the first parameter
      k - int; the second parameter
      Returns:
      the k-permutations of n
    • perm

      public static long perm(long n, long k)
      Computes the k-permutations of n as a long.
      Parameters:
      n - long; the first parameter
      k - long; the second parameter
      Returns:
      the k-permutations of n
    • combinations

      public static double combinations(int n, int k)
      computes the combinations of n over k.
      Parameters:
      n - int; the first parameter
      k - int; the second parameter
      Returns:
      the combinations of n over k
    • combinations

      public static double combinations(long n, long k)
      computes the combinations of n over k.
      Parameters:
      n - long; the first parameter
      k - long; the second parameter
      Returns:
      the combinations of n over k
    • comb

      public static long comb(int n, int k)
      computes the combinations of n over k as a long.
      Parameters:
      n - int; the first parameter
      k - int; the second parameter
      Returns:
      the combinations of n over k
    • comb

      public static long comb(long n, long k)
      computes the combinations of n over k as a long.
      Parameters:
      n - long; the first parameter
      k - long; the second parameter
      Returns:
      the combinations of n over k
    • erf

      public static double erf(double z)
      Approximates erf(z) using a Taylor series.
      The Taylor series for erf(z) for abs(z) < 0.5 that is used is:
          erf(z) = (exp(-z2) / √π) Σ [ 2z2n + 1 / (2n + 1)!!]
      The Taylor series for erf(z) for abs(z) > 3.7 that is used is:
          erf(z) = 1 - (exp(-z2) / √π) Σ [ (-1)n (2n - 1)!! z-(2n + 1) / 2n]
      See https://mathworld.wolfram.com/Erf.html.
      For 0.5 < z < 3.7 it approximates erf(z) using the following Taylor series:
          erf(z) = (2/√π) (z - z3/3 + z5/10 - z7/42 + z9/216 - ...)
      The factors are given by https://oeis.org/A007680, which evaluates to a(n) = (2n+1)n!. See https://en.wikipedia.org/wiki/Error_function.
      Parameters:
      z - double; the value to calculate the error function for
      Returns:
      erf(z)
    • erfInv

      public static double erfInv(double y)
      Approximates erf-1(p) based on http://www.naic.edu/~jeffh/inverse_cerf.c code.
      Parameters:
      y - double; the cumulative probability to calculate the inverse error function for
      Returns:
      erf-1(p)
    • gammaln

      public static double gammaln(double xx)
      Calculates ln(gamma(x)). Java version of gammln function in Numerical Recipes in C, p.214.
      Parameters:
      xx - double; the value to calculate the gamma function for
      Returns:
      double; gamma(x)
      Throws:
      IllegalArgumentException - when x is < 0
    • gamma

      public static double gamma(double x)
      Calculates gamma(x). Based on the gammln function in Numerical Recipes in C, p.214.
      Parameters:
      x - double; the value to calculate the gamma function for
      Returns:
      double; gamma(x)
      Throws:
      IllegalArgumentException - when x is < 0
    • beta

      public static double beta(double z, double w)
      Calculates Beta(z, w) where Beta(z, w) = Γ(z) Γ(w) / Γ(z + w).
      Parameters:
      z - double; beta function parameter 1
      w - ; beta function parameter 2
      Returns:
      double; beta(z, w)
      Throws:
      IllegalArgumentException - when one of the parameters is < 0