public static double auc(double[] x, double[] y) {
        if (x.length != y.length) {
            throw new IllegalArgumentException("x and y must be the same length, x.length = " + x.length + ", y.length = " + y.length);
        }
        double output = 0.0;

        for (int i = 1; i < x.length; i++) {
            double ySum = y[i] + y[i-1];
            double xDiff = x[i] - x[i-1];
            if (xDiff < -1e-12) {
                throw new IllegalStateException(String.format("X is not increasing, x[%d]=%f, x[%d]=%f",i,x[i],i-1,x[i-1]));
            }
            output += (ySum * xDiff) / 2.0;
        }

        return output;
    }
@Test
    public void testAUC() {
        double output;

        try {
            output = Util.auc(new double[]{0.0,1.0},new double[]{0.0,1.0,2.0});
            fail("Exception not thrown for mismatched lengths.");
        } catch (IllegalArgumentException e) { }

        try {
            output = Util.auc(new double[]{0.0,1.0,2.0,1.5,3.0}, new double[]{1.0,1.0,1.0,1.0,1.0});
            fail("Exception not thrown for non-increasing x.");
        } catch (IllegalStateException e) { }

        output = Util.auc(new double[]{4,6,8},new double[]{1,2,3});
        assertEquals(8.0,output,DELTA);

        output = Util.auc(new double[]{0,1},new double[]{0,1});
        assertEquals(0.5,output,DELTA);

        output = Util.auc(new double[]{0,0,1},new double[]{1,1,0});
        assertEquals(0.5,output,DELTA);

        output = Util.auc(new double[]{0,1},new double[]{1,1});
        assertEquals(1,output,DELTA);

        output = Util.auc(new double[]{0,0.5,1},new double[]{0,0.5,1});
        assertEquals(0.5,output,DELTA);
    }