edu.stanford.nlp.math
Class SloppyMath

java.lang.Object
  extended byedu.stanford.nlp.math.SloppyMath

public final class SloppyMath
extends Object

The class SloppyMath contains methods for performing basic numeric operations. In some cases, such as max and min, they cut a few corners in the implementation for the sake of efficiency. In particular, they may not handle special notions like NaN and -0.0 correctly. This was the origin of the class name, but some other operations are just useful math additions, such as logSum.

Author:
Christopher Manning

Constructor Summary
SloppyMath()
           
 
Method Summary
static double chiSquare2by2(int k, int n, int r, int m)
          Find a 2x2 chi-square value.
static double exactBinomial(int k, int n, double p)
          Find a one tailed exact binomial test probability.
static double hypergeometric(int k, int n, int r, int m)
          Find a hypergeometric distribution.
static boolean isDangerous(double d)
          Returns true if the argument is a "dangerous" double to have around, namely one that is infinite, NaN or zero.
static boolean isVeryDangerous(double d)
          Returns true if the argument is a "very dangerous" double to have around, namely one that is infinite or NaN.
static double logAdd(double lx, double ly)
          Returns the log of the sum of two numbers, which are themselves input in log form.
static double logSum(double[] logInputs)
          Returns the log of the sum of an array of numbers, which are themselves input in log form.
static void main(String[] args)
          Tests the hypergeometric distribution code, or other functions provided in this module.
static double max(double a, double b)
          Returns the greater of two double values.
static float max(float a, float b)
          Returns the greater of two float values.
static double min(double a, double b)
          Returns the smaller of two double values.
static float min(float a, float b)
          Returns the smaller of two float values.
static double oneTailedFishersExact(int k, int n, int r, int m)
          Find a one-tailed Fisher's exact probability.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SloppyMath

public SloppyMath()
Method Detail

max

public static float max(float a,
                        float b)
Returns the greater of two float values. That is, the result is the argument closer to positive infinity. If the arguments have the same value, the result is that same value. Does none of the special checks for NaN or -0.0f that Math.max does.

Parameters:
a - an argument.
b - another argument.
Returns:
the larger of a and b.

max

public static double max(double a,
                         double b)
Returns the greater of two double values. That is, the result is the argument closer to positive infinity. If the arguments have the same value, the result is that same value. Does none of the special checks for NaN or -0.0f that Math.max does.

Parameters:
a - an argument.
b - another argument.
Returns:
the larger of a and b.

min

public static float min(float a,
                        float b)
Returns the smaller of two float values. That is, the result is the value closer to negative infinity. If the arguments have the same value, the result is that same value. Does none of the special checks for NaN or -0.0f that Math.max does.

Parameters:
a - an argument.
b - another argument.
Returns:
the smaller of a and b.

min

public static double min(double a,
                         double b)
Returns the smaller of two double values. That is, the result is the value closer to negative infinity. If the arguments have the same value, the result is that same value. Does none of the special checks for NaN or -0.0f that Math.max does.

Parameters:
a - an argument.
b - another argument.
Returns:
the smaller of a and b.

isDangerous

public static boolean isDangerous(double d)
Returns true if the argument is a "dangerous" double to have around, namely one that is infinite, NaN or zero.


isVeryDangerous

public static boolean isVeryDangerous(double d)
Returns true if the argument is a "very dangerous" double to have around, namely one that is infinite or NaN.


logSum

public static double logSum(double[] logInputs)
Returns the log of the sum of an array of numbers, which are themselves input in log form. This is all natural logarithms. Reasonable care is taken to do this as efficiently as possible (under the assumption that the numbers might differ greatly in magnitude), with high accuracy, and without numerical overflow.

Returns:
log(x1 + ... + xn)

logAdd

public static double logAdd(double lx,
                            double ly)
Returns the log of the sum of two numbers, which are themselves input in log form. This uses natural logarithms. Reasonable care is taken to do this as efficiently as possible (under the assumption that the numbers might differ greatly in magnitude), with high accuracy, and without numerical overflow. Also, handle correctly the case of arguments being -Inf (e.g., probability 0).

Parameters:
lx - First number, in log form
ly - Second number, in log form
Returns:
log(exp(lx) + exp(ly))

hypergeometric

public static double hypergeometric(int k,
                                    int n,
                                    int r,
                                    int m)
Find a hypergeometric distribution. This uses exact math, trying fairly hard to avoid numeric overflow by interleaving multiplications and divisions. (To do: make it even better at avoiding overflow, by using loops that will do either a multiple or divide based on the size of the intermediate result.)

Parameters:
k - The number of black balls drawn
n - The total number of balls
r - The number of black balls
m - The number of balls drawn
Returns:
The hypergeometric value

exactBinomial

public static double exactBinomial(int k,
                                   int n,
                                   double p)
Find a one tailed exact binomial test probability. Finds the chance of this or a higher result

Parameters:
k - number of successes
n - Number of trials
p - Probability of a success

oneTailedFishersExact

public static double oneTailedFishersExact(int k,
                                           int n,
                                           int r,
                                           int m)
Find a one-tailed Fisher's exact probability. Chance of having seen this or a more extreme departure from what you would have expected given independence. I.e., k >= the value passed in. Warning: this was done just for collocations, where you are concerned with the case of k being larger than predicted. It doesn't correctly handle other cases, such as k being smaller than expected.

Parameters:
k - The number of black balls drawn
n - The total number of balls
r - The number of black balls
m - The number of balls drawn
Returns:
The Fisher's exact p-value

chiSquare2by2

public static double chiSquare2by2(int k,
                                   int n,
                                   int r,
                                   int m)
Find a 2x2 chi-square value. Note: could do this more neatly using simplified formula for 2x2 case.

Parameters:
k - The number of black balls drawn
n - The total number of balls
r - The number of black balls
m - The number of balls drawn
Returns:
The Fisher's exact p-value

main

public static void main(String[] args)
Tests the hypergeometric distribution code, or other functions provided in this module.

Parameters:
args - Either none, and the log add rountines are tested, or the following 4 arguments: k (cell), n (total), r (row), m (col)


Stanford NLP Group