/****************************************************************************** * Name: Kevin Wayne * NetID: wayne * Precept: P00 * * Description: Computes the G-statistic of the observed frequencies * and the expected frequencies. ******************************************************************************/ public class GStatistic { public static void main(String[] args) { double gStatistic = 0.0; int numberOfNonzeros = 0; // read data from standard input and update statistics int n = StdIn.readInt(); for (int i = 0; i < n; i++) { int x = StdIn.readInt(); int y = StdIn.readInt(); if (y != 0) numberOfNonzeros++; if (x != 0) gStatistic += 2.0 * x * Math.log(1.0 * x / y); } // print results to standard output int degreesOfFreedom = numberOfNonzeros - 1; System.out.printf("degrees of freedom = %d\n", degreesOfFreedom); System.out.printf("G-statistic = %.4f\n", gStatistic); } }