我在计算使用Java的两个特性之间的互信息。Java中互信息的高效实现
我读过Calculating Mutual Information For Selecting a Training Set in Java了,但是那是,如果互信息是适当的海报,只有一些轻伪代码来实施的讨论。
我目前的代码在下面,但我希望有一种方法来优化它,因为我有大量的信息要处理。我知道调用另一种语言/框架可能会提高速度,但是现在想专注于用Java解决此问题。
任何帮助非常感谢。
public static double calculateNewMutualInformation(double frequencyOfBoth, double frequencyOfLeft,
double frequencyOfRight, int noOfTransactions) {
if (frequencyOfBoth == 0 || frequencyOfLeft == 0 || frequencyOfRight == 0)
return 0;
// supp = f11
double supp = frequencyOfBoth/noOfTransactions; // P(x,y)
double suppLeft = frequencyOfLeft/noOfTransactions; // P(x)
double suppRight = frequencyOfRight/noOfTransactions; // P(y)
double f10 = (suppLeft - supp); // P(x) - P(x,y)
double f00 = (1 - suppRight) - f10; // (1-P(y)) - P(x,y)
double f01 = (suppRight - supp); // P(y) - P(x,y)
// -1 * ((P(x) * log(Px)) + ((1 - P(x)) * log(1-p(x)))
double HX = -1 * ((suppLeft * MathUtils.logWithoutNaN(suppLeft)) + ((1 - suppLeft) * MathUtils.logWithoutNaN(1 - suppLeft)));
// -1 * ((P(y) * log(Py)) + ((1 - P(y)) * log(1-p(y)))
double HY = -1 * ((suppRight * MathUtils.logWithoutNaN(suppRight)) + ((1 - suppRight) * MathUtils.logWithoutNaN(1 - suppRight)));
double one = (supp * MathUtils.logWithoutNaN(supp)); // P(x,y) * log(P(x,y))
double two = (f10 * MathUtils.logWithoutNaN(f10));
double three = (f01 * MathUtils.logWithoutNaN(f01));
double four = (f00 * MathUtils.logWithoutNaN(f00));
double HXY = -1 * (one + two + three + four);
return (HX + HY - HXY)/(HX == 0 ? MathUtils.EPSILON : HX);
}
public class MathUtils {
public static final double EPSILON = 0.000001;
public static double logWithoutNaN(double value) {
if (value == 0) {
return Math.log(EPSILON);
} else if (value < 0) {
return 0;
}
return Math.log(value);
}
你有没有衡量性能,决定了它是慢? –
不错的问题,但是你可以将每个符号映射到互信息环境中的变量吗?因为我有点困惑。 – lonesome