package edu.neu.ccs.pyramid.eval;

import edu.neu.ccs.pyramid.dataset.MultiLabel;
import edu.neu.ccs.pyramid.dataset.MultiLabelClfDataSet;
import edu.neu.ccs.pyramid.multilabel_classification.MultiLabelClassifier;

import java.util.stream.IntStream;

/**
 * Created by Rainicy on 8/11/15.
 */
public class HammingLoss {

    public static double hammingLoss(double tp, double tn, double fp, double fn){
        return (fp+fn)/(tp+tn+fp+fn);
    }

    public static double hammingLoss(MultiLabel label, MultiLabel prediction, int numLabels){
        return MultiLabel.symmetricDifference(label, prediction).size()/(double) numLabels;
    }


    /**
     * Hamming loss:
     * From "Mining Multi-label Data by Grigorios Tsoumakas".
     * @param multiLabels ground truth
     * @param predictions prediction
     * @return
     */
    public static double hammingLoss(MultiLabel[] multiLabels, MultiLabel[] predictions, int numLabels){
        return IntStream.range(0,multiLabels.length).parallel()
                .mapToDouble(i -> hammingLoss(multiLabels[i], predictions[i], numLabels))
                .average().getAsDouble();
    }

    public static double hammingLoss(MultiLabelClassifier classifier, MultiLabelClfDataSet dataSet){
        return hammingLoss(dataSet.getMultiLabels(),classifier.predict(dataSet),classifier.getNumClasses());
    }

}

@@ -634,16 +634,284 @@
 %0A    %7D%0A%0A
+    /**%0A     * not divided by numLabels%0A     * @param label%0A     * @param prediction%0A     * @return%0A     */%0A    public static double unnormalized(MultiLabel label, MultiLabel prediction)%7B%0A        return MultiLabel.symmetricDifference(label, prediction).size();%0A    %7D%0A%0A
 %0A    /**

add non-normalized hamming loss