Example usage for org.apache.commons.math3.util ArithmeticUtils factorialLog

List of usage examples for org.apache.commons.math3.util ArithmeticUtils factorialLog

Introduction

In this page you can find the example usage for org.apache.commons.math3.util ArithmeticUtils factorialLog.

Prototype

public static double factorialLog(final int n) throws NotPositiveException 

Source Link

Document

Compute the natural logarithm of the factorial of n .

Usage

From source file:edu.oregonstate.eecs.mcplan.ml.ClusterContingencyTable.java

public double expectedMutualInformation() {
    double emi = 0.0;
    for (int i = 0; i < R; ++i) {
        for (int j = 0; j < C; ++j) {
            // We take max( _, 1 ) instead of max( _, 0 ) as in the paper
            // because when nij is 0, the product is 0, but log( 0 )
            // causes NaN.
            final int start = Math.max(a[i] + b[j] - N, 1);
            final int end = Math.min(a[i], b[j]);
            for (int nij = start; nij <= end; ++nij) {
                final double p = nij / ((double) N);
                final double L = FastMath.log(2, N * nij / ((double) a[i] * b[j]));
                final double logNum = ArithmeticUtils.factorialLog(a[i]) + ArithmeticUtils.factorialLog(b[j])
                        + ArithmeticUtils.factorialLog(N - a[i]) + ArithmeticUtils.factorialLog(N - b[j]);
                final double logDenom = ArithmeticUtils.factorialLog(N) + ArithmeticUtils.factorialLog(nij)
                        + ArithmeticUtils.factorialLog(a[i] - nij) + ArithmeticUtils.factorialLog(b[j] - nij)
                        + ArithmeticUtils.factorialLog(N - a[i] - b[j] + nij);
                final double all = p * L * FastMath.exp(logNum - logDenom);
                emi += all;/*from w w  w. java2s.  c om*/
            }
        }
    }
    return emi;
}