/** * returns a matrix of LLR values from a given contingency table. * @param contingencyTable * @return */ public static double[,] GetLLRs(int[,] contingencyTable) { int rowNum = contingencyTable.GetLength(0); int colNum = contingencyTable.GetLength(1); // first get the total count of instances in the matrix int totalCount = 0; for (int row = 0; row < rowNum; row++) { for (int col = 0; col < colNum; col++) { totalCount += contingencyTable[row, col]; } } double[,] llrs = new double[rowNum, colNum]; for (int row = 0; row < rowNum; row++) { for (int col = 0; col < colNum; col++) { // get the observed value for the cell double o = contingencyTable[row, col]; //calculate likelihood ratio //############################################# WARNING!! CHECK THE FOLLOWING 3 LINES OF CODE // SEE WIKIPEDIA EXAMPLE IN LIKELIHOOD RATIO double lr = o * totalCount / DataTools.GetRowSum(contingencyTable, row) / DataTools.SumColumn(contingencyTable, col); if (lr == 0) { llrs[row, col] = -999.9; // take log to get LLR } else { llrs[row, col] = Math.Log10(lr); } } } // for all rows and columns return(llrs); }
public static double RowRelativeFrequency(int[,] table, int row, int totalCount) { return(DataTools.GetRowSum(table, row) / (double)totalCount); }