@@ -0,0 +1,3199 @@
+/*******************************************************************************%0A * Copyright (c) 2010 Haifeng Li%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A *******************************************************************************/%0A%0Apackage smile.validation;%0A%0Aimport smile.sort.QuickSort;%0A%0A/**%0A * The area under the curve (AUC). When using normalized units, the area under%0A * the curve is equal to the probability that a classifier will rank a%0A * randomly chosen positive instance higher than a randomly chosen negative%0A * one (assuming 'positive' ranks higher than 'negative').%0A * %3Cp%3E%0A * In statistics, a receiver operating characteristic (ROC), or ROC curve,%0A * is a graphical plot that illustrates the performance of a binary classifier%0A * system as its discrimination threshold is varied. The curve is created by%0A * plotting the true positive rate (TPR) against the false positive rate (FPR)%0A * at various threshold settings.%0A * %3Cp%3E%0A * AUC is quite noisy as a classification measure and has some other%0A * significant problems in model comparison.%0A *%0A * @author Haifeng Li%0A */%0Apublic class AUC %7B%0A%0A    public AUC() %7B%0A    %7D%0A%0A    public double measure(int%5B%5D truth, double%5B%5D prediction) %7B%0A        if (truth.length != prediction.length) %7B%0A            throw new IllegalArgumentException(String.format(%22The vector sizes don't match: %25d != %25d.%22, truth.length, prediction.length));%0A        %7D%0A%0A        int totalPositive = 0;%0A        int totalNegative = 0;%0A%0A        int%5B%5D label = truth.clone();%0A        double%5B%5D pred = prediction.clone();%0A        for (int i = 0; i %3C truth.length; i++) %7B%0A            if (label%5Bi%5D == 0) %7B%0A                totalNegative++;%0A            %7D else if (label%5Bi%5D == 1) %7B%0A                totalPositive++;%0A            %7D else %7B%0A                throw new IllegalArgumentException(%22AUC is only for binary classification. Invalid label: %22 + label%5Bi%5D);%0A            %7D%0A        %7D%0A%0A        QuickSort.sort(pred, label);%0A%0A        double fp = 0;%0A        double tp = 0;%0A        double fpPrev = 0;%0A        double tpPrev = 0;%0A        double area = 0;%0A        double fPrev = Double.MIN_VALUE;%0A%0A        for (int i = 0; i %3C truth.length; i++) %7B%0A            double curF = pred%5Bi%5D;%0A            if (curF != fPrev) %7B%0A                area += Math.abs(fp - fpPrev) * ((tp + tpPrev) / 2.0);%0A                fPrev = curF;%0A                fpPrev = fp;%0A                tpPrev = tp;%0A            %7D%0A%0A            if (label%5Bi%5D == +1) %7B%0A                tp++;%0A            %7D else %7B%0A                fp++;%0A            %7D%0A        %7D%0A%0A        area += Math.abs(totalNegative - fpPrev) * ((totalPositive + tpPrev) / 2.0);%0A        area /= ((double) totalPositive * totalNegative);%0A        return area;%0A    %7D%0A%7D

add AUC