/** * Help method for computing entropy. */ public final double lnFunc(double num) { // Constant hard coded for efficiency reasons if (num < 1e-6) return 0; else return ContingencyTables.lnFunc(num); }
/** * Help method for computing entropy. */ public final double lnFunc(double num) { // Constant hard coded for efficiency reasons if (num < 1e-6) return 0; else return ContingencyTables.lnFunc(num); }
/** * Computes the columns' entropy for the given contingency table. * * @param matrix the contingency table * @return the columns' entropy */ public static double entropyOverColumns(double[][] matrix){ double returnValue = 0, sumForColumn, total = 0; for (int j = 0; j < matrix[0].length; j++){ sumForColumn = 0; for (int i = 0; i < matrix.length; i++) { sumForColumn += matrix[i][j]; } returnValue = returnValue - lnFunc(sumForColumn); total += sumForColumn; } if (Utils.eq(total, 0)) { return 0; } return (returnValue + lnFunc(total)) / (total * log2); }
/** * Computes the entropy of the given array. * * @param array the array * @return the entropy */ public static double entropy(double[] array) { double returnValue = 0, sum = 0; for (int i = 0; i < array.length; i++) { returnValue -= lnFunc(array[i]); sum += array[i]; } if (Utils.eq(sum, 0)) { return 0; } else { return (returnValue + lnFunc(sum)) / (sum * log2); } }
for (int j = 0; j < matrix.length; j++) sumForColumn += matrix[j][i]; preSplit += lnFunc(sumForColumn); total += sumForColumn; preSplit -= lnFunc(total); sumForRow = 0; for (int j = 0; j < matrix[0].length; j++) { postSplit += lnFunc(matrix[i][j]); sumForRow += matrix[i][j]; splitEnt += lnFunc(sumForRow); splitEnt -= lnFunc(total);
for (int j = 0; j < matrix.length; j++) sumForColumn += matrix[j][i]; preSplit += lnFunc(sumForColumn); total += sumForColumn; preSplit -= lnFunc(total); sumForRow = 0; for (int j = 0; j < matrix[0].length; j++) { postSplit += lnFunc(matrix[i][j]); sumForRow += matrix[i][j]; splitEnt += lnFunc(sumForRow); splitEnt -= lnFunc(total);
/** * Computes the entropy of the given array. * * @param array the array * @return the entropy */ public static double entropy(double[] array) { double returnValue = 0, sum = 0; for (int i = 0; i < array.length; i++) { returnValue -= lnFunc(array[i]); sum += array[i]; } if (Utils.eq(sum, 0)) { return 0; } else { return (returnValue + lnFunc(sum)) / (sum * log2); } }
/** * Computes the columns' entropy for the given contingency table. * * @param matrix the contingency table * @return the columns' entropy */ public static double entropyOverColumns(double[][] matrix){ double returnValue = 0, sumForColumn, total = 0; for (int j = 0; j < matrix[0].length; j++){ sumForColumn = 0; for (int i = 0; i < matrix.length; i++) { sumForColumn += matrix[i][j]; } returnValue = returnValue - lnFunc(sumForColumn); total += sumForColumn; } if (Utils.eq(total, 0)) { return 0; } return (returnValue + lnFunc(total)) / (total * log2); }
/** * Computes the rows' entropy for the given contingency table. * * @param matrix the contingency table * @return the rows' entropy */ public static double entropyOverRows(double[][] matrix) { double returnValue = 0, sumForRow, total = 0; for (int i = 0; i < matrix.length; i++) { sumForRow = 0; for (int j = 0; j < matrix[0].length; j++) { sumForRow += matrix[i][j]; } returnValue = returnValue - lnFunc(sumForRow); total += sumForRow; } if (Utils.eq(total, 0)) { return 0; } return (returnValue + lnFunc(total)) / (total * log2); }
/** * Computes conditional entropy of the rows given * the columns. * * @param matrix the contingency table * @return the conditional entropy of the rows given the columns */ public static double entropyConditionedOnColumns(double[][] matrix) { double returnValue = 0, sumForColumn, total = 0; for (int j = 0; j < matrix[0].length; j++) { sumForColumn = 0; for (int i = 0; i < matrix.length; i++) { returnValue = returnValue + lnFunc(matrix[i][j]); sumForColumn += matrix[i][j]; } returnValue = returnValue - lnFunc(sumForColumn); total += sumForColumn; } if (Utils.eq(total, 0)) { return 0; } return -returnValue / (total * log2); }
/** * Computes conditional entropy of the columns given * the rows. * * @param matrix the contingency table * @return the conditional entropy of the columns given the rows */ public static double entropyConditionedOnRows(double[][] matrix) { double returnValue = 0, sumForRow, total = 0; for (int i = 0; i < matrix.length; i++) { sumForRow = 0; for (int j = 0; j < matrix[0].length; j++) { returnValue = returnValue + lnFunc(matrix[i][j]); sumForRow += matrix[i][j]; } returnValue = returnValue - lnFunc(sumForRow); total += sumForRow; } if (Utils.eq(total, 0)) { return 0; } return -returnValue / (total * log2); }
/** * Computes the rows' entropy for the given contingency table. * * @param matrix the contingency table * @return the rows' entropy */ public static double entropyOverRows(double[][] matrix) { double returnValue = 0, sumForRow, total = 0; for (int i = 0; i < matrix.length; i++) { sumForRow = 0; for (int j = 0; j < matrix[0].length; j++) { sumForRow += matrix[i][j]; } returnValue = returnValue - lnFunc(sumForRow); total += sumForRow; } if (Utils.eq(total, 0)) { return 0; } return (returnValue + lnFunc(total)) / (total * log2); }
/** * Computes conditional entropy of the rows given * the columns. * * @param matrix the contingency table * @return the conditional entropy of the rows given the columns */ public static double entropyConditionedOnColumns(double[][] matrix) { double returnValue = 0, sumForColumn, total = 0; for (int j = 0; j < matrix[0].length; j++) { sumForColumn = 0; for (int i = 0; i < matrix.length; i++) { returnValue = returnValue + lnFunc(matrix[i][j]); sumForColumn += matrix[i][j]; } returnValue = returnValue - lnFunc(sumForColumn); total += sumForColumn; } if (Utils.eq(total, 0)) { return 0; } return -returnValue / (total * log2); }
/** * Computes conditional entropy of the columns given * the rows. * * @param matrix the contingency table * @return the conditional entropy of the columns given the rows */ public static double entropyConditionedOnRows(double[][] matrix) { double returnValue = 0, sumForRow, total = 0; for (int i = 0; i < matrix.length; i++) { sumForRow = 0; for (int j = 0; j < matrix[0].length; j++) { returnValue = returnValue + lnFunc(matrix[i][j]); sumForRow += matrix[i][j]; } returnValue = returnValue - lnFunc(sumForRow); total += sumForRow; } if (Utils.eq(total, 0)) { return 0; } return -returnValue / (total * log2); }
sumForColumn += matrix[j][i]; columnEntropy += lnFunc(sumForColumn); total += sumForColumn; columnEntropy -= lnFunc(total); for (int j = 0; j < matrix[0].length; j++) { sumForRow += matrix[i][j]; entropyConditionedOnRows += lnFunc(matrix[i][j]); rowEntropy += lnFunc(sumForRow); rowEntropy -= lnFunc(total); infoGain = columnEntropy - entropyConditionedOnRows; if (Utils.eq(columnEntropy, 0) || Utils.eq(rowEntropy, 0))
sumForColumn += matrix[j][i]; columnEntropy += lnFunc(sumForColumn); total += sumForColumn; columnEntropy -= lnFunc(total); for (int j = 0; j < matrix[0].length; j++) { sumForRow += matrix[i][j]; entropyConditionedOnRows += lnFunc(matrix[i][j]); rowEntropy += lnFunc(sumForRow); rowEntropy -= lnFunc(total); infoGain = columnEntropy - entropyConditionedOnRows; if (Utils.eq(columnEntropy, 0) || Utils.eq(rowEntropy, 0))