Class Maths


  • public final class Maths
    extends java.lang.Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static double EPSILON
      Numbers that are closer than this are considered equal by almostEquals.
      static double log2  
    • Constructor Summary

      Constructors 
      Constructor Description
      Maths()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean almostEquals​(double[] d1, double[] d2, double eps)  
      static boolean almostEquals​(double d1, double d2)  
      static boolean almostEquals​(double d1, double d2, double epsilon)  
      static double beta​(double a, double b)  
      static boolean checkWithinRange​(double value, double min, double max)
      Checks if min <= value <= max.
      static double cosh​(double a)  
      static double factorial​(int n)  
      static double gamma​(double x)  
      static double getEntropy​(double[] dist)  
      static double jensenShannonDivergence​(double[] p1, double[] p2)
      Returns the Jensen-Shannon divergence.
      static double klDivergence​(double[] p1, double[] p2)
      Returns the KL divergence, K(p1 || p2).
      static double logBeta​(double a, double b)  
      static double logBinom​(int x, int n, double p)
      Computes p(x;n,p) where x~B(n,p)
      static double logFactorial​(int n)  
      static double logGamma​(double x)  
      static double logit​(double p)  
      static double numCombinations​(int n, int r)  
      static double numPermutations​(int n, int r)  
      static double oldLogGamma​(double x)  
      static double pbinom​(int x, int n, double p)
      Vastly inefficient O(x) method to compute cdf of B(n,p)
      static double regularizedGammaP​(double shape, double x)
      Regularized Gamma function, used for Gamma CDF.
      static double sigmod​(double beta)  
      static double sigmod_rev​(double sig)  
      static double subtractLogProb​(double a, double b)
      Returns the difference of two doubles expressed in log space, that is,
      static double sumLogProb​(double[] vals)
      Sums an array of numbers log(x1)...log(xn).
      static double sumLogProb​(double a, double b)
      Returns the sum of two doubles expressed in log space, that is,
      static double tanh​(double a)  
      • Methods inherited from class java.lang.Object

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

      • EPSILON

        public static double EPSILON
        Numbers that are closer than this are considered equal by almostEquals.
      • log2

        public static final double log2
    • Constructor Detail

      • Maths

        public Maths()
    • Method Detail

      • logGamma

        public static final double logGamma​(double x)
      • oldLogGamma

        public static double oldLogGamma​(double x)
      • logBeta

        public static double logBeta​(double a,
                                     double b)
      • beta

        public static double beta​(double a,
                                  double b)
      • gamma

        public static double gamma​(double x)
      • factorial

        public static double factorial​(int n)
      • logFactorial

        public static double logFactorial​(int n)
      • regularizedGammaP

        public static double regularizedGammaP​(double shape,
                                               double x)
        Regularized Gamma function, used for Gamma CDF. Operates by evaluating an infinite sum that converges quickly in practice.
      • logBinom

        public static double logBinom​(int x,
                                      int n,
                                      double p)
        Computes p(x;n,p) where x~B(n,p)
      • pbinom

        public static double pbinom​(int x,
                                    int n,
                                    double p)
        Vastly inefficient O(x) method to compute cdf of B(n,p)
      • sigmod

        public static double sigmod​(double beta)
      • sigmod_rev

        public static double sigmod_rev​(double sig)
      • logit

        public static double logit​(double p)
      • numCombinations

        public static double numCombinations​(int n,
                                             int r)
      • numPermutations

        public static double numPermutations​(int n,
                                             int r)
      • cosh

        public static double cosh​(double a)
      • tanh

        public static double tanh​(double a)
      • almostEquals

        public static boolean almostEquals​(double d1,
                                           double d2)
      • almostEquals

        public static boolean almostEquals​(double d1,
                                           double d2,
                                           double epsilon)
      • almostEquals

        public static boolean almostEquals​(double[] d1,
                                           double[] d2,
                                           double eps)
      • checkWithinRange

        public static boolean checkWithinRange​(double value,
                                               double min,
                                               double max)
        Checks if min <= value <= max.
      • klDivergence

        public static double klDivergence​(double[] p1,
                                          double[] p2)
        Returns the KL divergence, K(p1 || p2). The log is w.r.t. base 2.

        *Note*: If any value in p2 is 0.0 then the KL-divergence is infinite.

      • jensenShannonDivergence

        public static double jensenShannonDivergence​(double[] p1,
                                                     double[] p2)
        Returns the Jensen-Shannon divergence.
      • sumLogProb

        public static double sumLogProb​(double a,
                                        double b)
        Returns the sum of two doubles expressed in log space, that is,
            sumLogProb = log (e^a + e^b)
                       = log e^a(1 + e^(b-a))
                       = a + log (1 + e^(b-a))
         
        By exponentiating b-a, we obtain better numerical precision than we would if we calculated e^a or e^b directly.

        Note: This function is just like sumNegLogProb in Transducer, except that the logs aren't negated.

      • sumLogProb

        public static double sumLogProb​(double[] vals)
        Sums an array of numbers log(x1)...log(xn). This saves some of the unnecessary calls to Math.log in the two-argument version.

        Note that this implementation IGNORES elements of the input array that are more than LOGTOLERANCE (currently 30.0) less than the maximum element.

        Cursory testing makes me wonder if this is actually much faster than repeated use of the 2-argument version, however -cas.

        Parameters:
        vals - An array log(x1), log(x2), ..., log(xn)
        Returns:
        log(x1+x2+...+xn)
      • subtractLogProb

        public static double subtractLogProb​(double a,
                                             double b)
        Returns the difference of two doubles expressed in log space, that is,
            sumLogProb = log (e^a - e^b)
                       = log e^a(1 - e^(b-a))
                       = a + log (1 - e^(b-a))
         
        By exponentiating b-a, we obtain better numerical precision than we would if we calculated e^a or e^b directly.

        Returns NaN if b > a (so that log(e^a - e^b) is undefined).

      • getEntropy

        public static double getEntropy​(double[] dist)