de.lmu.ifi.dbs.elki.math
Class MathUtil

java.lang.Object
  extended by de.lmu.ifi.dbs.elki.math.MathUtil

public final class MathUtil
extends Object

A collection of math related utility functions.


Field Summary
(package private) static double[] ERFAPP_A
          Coefficients for erf approximation.
(package private) static double[] ERFAPP_B
          Coefficients for erf approximation.
(package private) static double[] ERFAPP_C
          Coefficients for erf approximation.
(package private) static double[] ERFAPP_D
          Coefficients for erf approximation.
(package private) static double[] ERFAPP_P
          Coefficients for erf approximation.
(package private) static double[] ERFAPP_Q
          Coefficients for erf approximation.
(package private) static double[] ERFINV_A
          Coefficients for erfinv approximation, rational version
(package private) static double[] ERFINV_B
          Coefficients for erfinv approximation, rational version
(package private) static double[] ERFINV_C
          Coefficients for erfinv approximation, rational version
(package private) static double[] ERFINV_D
          Coefficients for erfinv approximation, rational version
(package private) static double[] LANCZOS
          LANCZOS-Coefficients for Gamma approximation.
(package private) static double NUM_PRECISION
          Numerical precision to use
static double ONE_BY_SQRTPI
          Precomputed value of 1 / sqrt(pi)
(package private) static double P_HIGH
          Treshold for switching nethods for erfinv approximation
(package private) static double P_LOW
          Treshold for switching nethods for erfinv approximation
static double SQRT2
          Square root of 2.
static double SQRTHALF
          Square root of 0.5 == 1 / Sqrt(2)
static double SQRTTWOPI
          Squre root of two times Pi.
static double TWOPI
          Two times Pi.
 
Constructor Summary
private MathUtil()
          Fake constructor for static class.
 
Method Summary
static double approximateBinomialCoefficient(int n, int k)
           Binomial coefficent, also known as "n choose k")
static double approximateFactorial(int n)
          Compute the Factorial of n, often written as c!
static long binomialCoefficient(long n, long k)
           Binomial coefficient, also known as "n choose k".
static double cosineSimilarity(Vector v1, Vector v2)
          Compute the cosine similarity for two vectors.
static double deg2rad(double deg)
          Convert Degree to Radians
static double erf(double x)
          Error function for Gaussian distributions = Normal distributions.
static double erfc(double x)
          Complementary error function for Gaussian distributions = Normal distributions.
static double erfinv(double x)
          Inverse error function.
static BigInteger factorial(BigInteger n)
          Compute the Factorial of n, often written as c!
static long factorial(int n)
          Compute the Factorial of n, often written as c!
static double hypotenuse(double a, double b)
          Computes the square root of the sum of the squared arguments without under or overflow.
static double latlngDistance(double lat1, double lon1, double lat2, double lon2)
          Compute the approximate on-earth-surface distance of two points.
static double logGamma(double x)
          Compute logGamma.
static double mahalanobisDistance(Matrix weightMatrix, Vector o1_minus_o2)
          Compute the Mahalanobis distance using the given weight matrix
static double normalCDF(double x, double mu, double sigma)
          Cumulative probability density function (CDF) of a normal distribution.
static double normalPDF(double x, double mu, double sigma)
          Probability density function of the normal distribution.
static double normalProbit(double x, double mu, double sigma)
          Inverse cumulative probability density function (probit) of a normal distribution.
static double pearsonCorrelationCoefficient(double[] x, double[] y)
           Provides the Pearson product-moment correlation coefficient for two FeatureVectors.
static double pearsonCorrelationCoefficient(NumberVector<?,?> x, NumberVector<?,?> y)
           Provides the Pearson product-moment correlation coefficient for two FeatureVectors.
static double rad2deg(double rad)
          Radians to Degree
static double[] randomDoubleArray(int len)
          Produce an array of random numbers in [0:1]
static double[] randomDoubleArray(int len, Random r)
          Produce an array of random numbers in [0:1]
static double regularizedGammaP(double a, double x)
          Returns the regularized gamma function P(a, x).
static double regularizedGammaQ(double a, double x)
          Returns the regularized gamma function Q(a, x) = 1 - P(a, x).
static double standardNormalProbit(double d)
          Approximate the inverse error function for normal distributions.
static long sumFirstIntegers(long i)
          Compute the sum of the i first integers.
static double weightedPearsonCorrelationCoefficient(double[] x, double[] y, double[] weights)
           Provides the Pearson product-moment correlation coefficient for two FeatureVectors.
static double weightedPearsonCorrelationCoefficient(NumberVector<?,?> x, NumberVector<?,?> y, double[] weights)
           Provides the Pearson product-moment correlation coefficient for two FeatureVectors.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TWOPI

public static final double TWOPI
Two times Pi.

See Also:
Constant Field Values

SQRTTWOPI

public static final double SQRTTWOPI
Squre root of two times Pi.


SQRT2

public static final double SQRT2
Square root of 2.


SQRTHALF

public static final double SQRTHALF
Square root of 0.5 == 1 / Sqrt(2)


ONE_BY_SQRTPI

public static final double ONE_BY_SQRTPI
Precomputed value of 1 / sqrt(pi)


ERFAPP_A

static final double[] ERFAPP_A
Coefficients for erf approximation. Loosely based on http://www.netlib.org/specfun/erf


ERFAPP_B

static final double[] ERFAPP_B
Coefficients for erf approximation. Loosely based on http://www.netlib.org/specfun/erf


ERFAPP_C

static final double[] ERFAPP_C
Coefficients for erf approximation. Loosely based on http://www.netlib.org/specfun/erf


ERFAPP_D

static final double[] ERFAPP_D
Coefficients for erf approximation. Loosely based on http://www.netlib.org/specfun/erf


ERFAPP_P

static final double[] ERFAPP_P
Coefficients for erf approximation. Loosely based on http://www.netlib.org/specfun/erf


ERFAPP_Q

static final double[] ERFAPP_Q
Coefficients for erf approximation. Loosely based on http://www.netlib.org/specfun/erf


P_LOW

static final double P_LOW
Treshold for switching nethods for erfinv approximation

See Also:
Constant Field Values

P_HIGH

static final double P_HIGH
Treshold for switching nethods for erfinv approximation

See Also:
Constant Field Values

ERFINV_A

static final double[] ERFINV_A
Coefficients for erfinv approximation, rational version


ERFINV_B

static final double[] ERFINV_B
Coefficients for erfinv approximation, rational version


ERFINV_C

static final double[] ERFINV_C
Coefficients for erfinv approximation, rational version


ERFINV_D

static final double[] ERFINV_D
Coefficients for erfinv approximation, rational version


LANCZOS

static final double[] LANCZOS
LANCZOS-Coefficients for Gamma approximation. These are said to have higher precision than those in "Numerical Recipes". They probably come from Paul Godfrey: http://my.fit.edu/~gabdo/gamma.txt


NUM_PRECISION

static final double NUM_PRECISION
Numerical precision to use

See Also:
Constant Field Values
Constructor Detail

MathUtil

private MathUtil()
Fake constructor for static class.

Method Detail

hypotenuse

public static double hypotenuse(double a,
                                double b)
Computes the square root of the sum of the squared arguments without under or overflow.

Parameters:
a - first cathetus
b - second cathetus
Returns:
sqrt(a<sup>2</sup> + b<sup>2</sup>)

mahalanobisDistance

public static double mahalanobisDistance(Matrix weightMatrix,
                                         Vector o1_minus_o2)
Compute the Mahalanobis distance using the given weight matrix

Parameters:
weightMatrix - Weight Matrix
o1_minus_o2 - Delta vector
Returns:
Mahalanobis distance

pearsonCorrelationCoefficient

public static double pearsonCorrelationCoefficient(NumberVector<?,?> x,
                                                   NumberVector<?,?> y)

Provides the Pearson product-moment correlation coefficient for two FeatureVectors.

Parameters:
x - first FeatureVector
y - second FeatureVector
Returns:
the Pearson product-moment correlation coefficient for x and y

weightedPearsonCorrelationCoefficient

public static double weightedPearsonCorrelationCoefficient(NumberVector<?,?> x,
                                                           NumberVector<?,?> y,
                                                           double[] weights)

Provides the Pearson product-moment correlation coefficient for two FeatureVectors.

Parameters:
x - first FeatureVector
y - second FeatureVector
Returns:
the Pearson product-moment correlation coefficient for x and y

pearsonCorrelationCoefficient

public static double pearsonCorrelationCoefficient(double[] x,
                                                   double[] y)

Provides the Pearson product-moment correlation coefficient for two FeatureVectors.

Parameters:
x - first FeatureVector
y - second FeatureVector
Returns:
the Pearson product-moment correlation coefficient for x and y

weightedPearsonCorrelationCoefficient

public static double weightedPearsonCorrelationCoefficient(double[] x,
                                                           double[] y,
                                                           double[] weights)

Provides the Pearson product-moment correlation coefficient for two FeatureVectors.

Parameters:
x - first FeatureVector
y - second FeatureVector
Returns:
the Pearson product-moment correlation coefficient for x and y

factorial

public static BigInteger factorial(BigInteger n)
Compute the Factorial of n, often written as c! in mathematics.

Use this method if for large values of n.

Parameters:
n - Note: n >= 0. This BigInteger n will be 0 after this method finishes.
Returns:
n * (n-1) * (n-2) * ... * 1

factorial

public static long factorial(int n)
Compute the Factorial of n, often written as c! in mathematics.

Parameters:
n - Note: n >= 0
Returns:
n * (n-1) * (n-2) * ... * 1

binomialCoefficient

public static long binomialCoefficient(long n,
                                       long k)

Binomial coefficient, also known as "n choose k".

Parameters:
n - Total number of samples. n > 0
k - Number of elements to choose. n >= k, k >= 0
Returns:
n! / (k! * (n-k)!)

approximateFactorial

public static double approximateFactorial(int n)
Compute the Factorial of n, often written as c! in mathematics.

Parameters:
n - Note: n >= 0
Returns:
n * (n-1) * (n-2) * ... * 1

approximateBinomialCoefficient

public static double approximateBinomialCoefficient(int n,
                                                    int k)

Binomial coefficent, also known as "n choose k")

Parameters:
n - Total number of samples. n > 0
k - Number of elements to choose. n >= k, k >= 0
Returns:
n! / (k! * (n-k)!)

erfc

public static double erfc(double x)
Complementary error function for Gaussian distributions = Normal distributions. Numerical approximation using taylor series. Implementation loosely based on http://www.netlib.org/specfun/erf

Parameters:
x - parameter value
Returns:
erfc(x)

erf

public static double erf(double x)
Error function for Gaussian distributions = Normal distributions. Numerical approximation using taylor series. Implementation loosely based on http://www.netlib.org/specfun/erf

Parameters:
x - parameter value
Returns:
erf(x)

erfinv

public static double erfinv(double x)
Inverse error function.

Parameters:
x - parameter value
Returns:
erfinv(x)

standardNormalProbit

public static double standardNormalProbit(double d)
Approximate the inverse error function for normal distributions. Largely based on:

http://www.math.uio.no/~jacklam/notes/invnorm/index.html
by Peter John Acklam

Parameters:
d - Quantile. Must be in [0:1], obviously.
Returns:
Inverse erf.

normalPDF

public static double normalPDF(double x,
                               double mu,
                               double sigma)
Probability density function of the normal distribution.
 1/(SQRT(2*pi*sigma^2)) * e^(-(x-mu)^2/2sigma^2)
 

Parameters:
x - The value.
mu - The mean.
sigma - The standard deviation.
Returns:
PDF of the given normal distribution at x.

normalCDF

public static double normalCDF(double x,
                               double mu,
                               double sigma)
Cumulative probability density function (CDF) of a normal distribution.

Parameters:
x - value to evaluate CDF at
mu - Mean value
sigma - Standard deviation.
Returns:
The CDF of the normal given distribution at x.

normalProbit

public static double normalProbit(double x,
                                  double mu,
                                  double sigma)
Inverse cumulative probability density function (probit) of a normal distribution.

Parameters:
x - value to evaluate probit function at
mu - Mean value
sigma - Standard deviation.
Returns:
The probit of the normal given distribution at x.

logGamma

public static double logGamma(double x)
Compute logGamma. Based loosely on "Numerical Recpies" and the work of Paul Godfrey at http://my.fit.edu/~gabdo/gamma.txt TODO: find out which approximation really is the best...

Parameters:
x - Parameter x
Returns:
@return log(Γ(x))

regularizedGammaP

public static double regularizedGammaP(double a,
                                       double x)
Returns the regularized gamma function P(a, x). Includes the quadrature way of computing. TODO: find "the" most accurate version of this. We seem to agree with others for the first 10+ digits, but diverge a bit later than that.

Parameters:
a - Parameter a
x - Parameter x
Returns:
Gamma value

regularizedGammaQ

public static double regularizedGammaQ(double a,
                                       double x)
Returns the regularized gamma function Q(a, x) = 1 - P(a, x). Includes the continued fraction way of computing, based loosely on the book "Numerical Recipes"; but probably not with the exactly same precision, since we reimplemented this in our coding style, not literally. TODO: find "the" most accurate version of this. We seem to agree with others for the first 10+ digits, but diverge a bit later than that.

Parameters:
a - parameter a
x - parameter x
Returns:
Result

sumFirstIntegers

public static long sumFirstIntegers(long i)
Compute the sum of the i first integers.

Parameters:
i - maximum summand
Returns:
Sum

randomDoubleArray

public static double[] randomDoubleArray(int len)
Produce an array of random numbers in [0:1]

Parameters:
len - Length
Returns:
Array

randomDoubleArray

public static double[] randomDoubleArray(int len,
                                         Random r)
Produce an array of random numbers in [0:1]

Parameters:
len - Length
r - Random generator
Returns:
Array

deg2rad

public static double deg2rad(double deg)
Convert Degree to Radians

Parameters:
deg - Degree value
Returns:
Radian value

rad2deg

public static double rad2deg(double rad)
Radians to Degree

Parameters:
rad - Radians value
Returns:
Degree value

latlngDistance

public static double latlngDistance(double lat1,
                                    double lon1,
                                    double lat2,
                                    double lon2)
Compute the approximate on-earth-surface distance of two points.

Parameters:
lat1 - Latitude of first point in degree
lon1 - Longitude of first point in degree
lat2 - Latitude of second point in degree
lon2 - Longitude of second point in degree
Returns:
Distance in km (approximately)

cosineSimilarity

public static double cosineSimilarity(Vector v1,
                                      Vector v2)
Compute the cosine similarity for two vectors.

Parameters:
v1 - First vector
v2 - Second vector
Returns:
Cosine similarity

Release 0.4.0 (2011-09-20_1324)