// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/IAnnotationUnit.java
// public interface IAnnotationUnit
//     extends Serializable
// {
//     /**
//      * Returns the index of the rater who coded this unit (in case of a coding study) or defined the
//      * boundaries of this unit (in case of a unitizing study). The first rater has index 0.
//      */
//     public int getRaterIdx();
// 
//     /**
//      * Returns the category assigned to this unit by one of the raters. The category might be null
//      * if, and only if, the unit represents a missing value (in case of a coding study) or a gap (in
//      * case of a unitizing study).
//      */
//     public Object getCategory();
// }
// 
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/ICategorySpecificAgreement.java
// public interface ICategorySpecificAgreement
// {
//     /**
//      * Calculates the inter-rater agreement for the given category.
//      * 
//      * @see ICategorySpecificAgreement
//      */
//     /*
//      * TODO @throws NullPointerException if the study is null or the given category is null.
//      * 
//      * @throws ArrayIndexOutOfBoundsException if the study does not contain the given category.
//      * 
//      * @throws ArithmeticException if the study does not contain annotations for the given category.
//      */
//     public double calculateCategoryAgreement(final Object category);
// }
// 
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/IChanceCorrectedDisagreement.java
// public interface IChanceCorrectedDisagreement
//     extends IAgreementMeasure
// {
//     /**
//      * Returns the observed disagreement of an annotation study. The observed disagreement is
//      * basically the proportion of annotation units that the raters disagree on divided by the
//      * number of units in the given study.
//      */
//     public double calculateObservedDisagreement();
// 
//     /**
//      * Returns the expected disagreement of an annotation study. The expected disagreement is the
//      * proportion of disagreement that would be expected by chance alone. The expected disagreement
//      * should be equal to the observed disagreement if each rater makes a random decision for each
//      * unit.
//      */
//     public double calculateExpectedDisagreement();
// }
// 
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/InsufficientDataException.java
// public class InsufficientDataException
//     extends RuntimeException
// {
//     private static final long serialVersionUID = -3081535421752493402L;
// 
//     public InsufficientDataException()
//     {
//         super();
//     }
// 
//     public InsufficientDataException(final String message, final Throwable cause)
//     {
//         super(message, cause);
//     }
// 
//     public InsufficientDataException(final String message)
//     {
//         super(message);
//     }
// 
//     public InsufficientDataException(final Throwable cause)
//     {
//         super(cause);
//     }
// }
// 
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/distance/IDistanceFunction.java
// public interface IDistanceFunction
// {
// 
//     /**
//      * Returns a distance value for the given pair of categories used within the given annotation
//      * study. Normally, the measure should return a distance of 0 if, and only if, the two
//      * categories are equal and a positive number otherwise.
//      */
//     public double measureDistance(final IAnnotationStudy study, final Object category1,
//             final Object category2);
// 
// }


import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.dkpro.statistics.agreement.IAnnotationUnit;
import org.dkpro.statistics.agreement.ICategorySpecificAgreement;
import org.dkpro.statistics.agreement.IChanceCorrectedDisagreement;
import org.dkpro.statistics.agreement.InsufficientDataException;
import org.dkpro.statistics.agreement.distance.IDistanceFunction;
        double result = 0.0;
        for (Entry<Object, Map<Object, Double>> cat1 : coincidenceMatrix.entrySet()) {
            for (Entry<Object, Double> cat2 : cat1.getValue().entrySet()) {
                result += cat2.getValue()
                        * distanceFunction.measureDistance(study, cat1.getKey(), cat2.getKey());
                n += cat2.getValue();
            }
        }
        result /= n;
        return result;
    }

    /**
     * Calculates the expected inter-rater agreement using the defined distance function to infer
     * the assumed probability distribution.
     * 
     * @throws NullPointerException
     *             if the annotation study is null.
     * @throws ArithmeticException
     *             if there are no items or raters in the annotation study.
     */
    @Override
    public double calculateExpectedDisagreement()
    {
        ensureDistanceFunction();
        if (coincidenceMatrix == null) {
            coincidenceMatrix = CodingAnnotationStudy.countCategoryCoincidence(study);
        }

        if (study.getCategoryCount() <= 1) {
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/IAnnotationUnit.java
// public interface IAnnotationUnit
//     extends Serializable
// {
//     /**
//      * Returns the index of the rater who coded this unit (in case of a coding study) or defined the
//      * boundaries of this unit (in case of a unitizing study). The first rater has index 0.
//      */
//     public int getRaterIdx();
// 
//     /**
//      * Returns the category assigned to this unit by one of the raters. The category might be null
//      * if, and only if, the unit represents a missing value (in case of a coding study) or a gap (in
//      * case of a unitizing study).
//      */
//     public Object getCategory();
// }
// 
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/ICategorySpecificAgreement.java
// public interface ICategorySpecificAgreement
// {
//     /**
//      * Calculates the inter-rater agreement for the given category.
//      * 
//      * @see ICategorySpecificAgreement
//      */
//     /*
//      * TODO @throws NullPointerException if the study is null or the given category is null.
//      * 
//      * @throws ArrayIndexOutOfBoundsException if the study does not contain the given category.
//      * 
//      * @throws ArithmeticException if the study does not contain annotations for the given category.
//      */
//     public double calculateCategoryAgreement(final Object category);
// }
// 
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/IChanceCorrectedDisagreement.java
// public interface IChanceCorrectedDisagreement
//     extends IAgreementMeasure
// {
//     /**
//      * Returns the observed disagreement of an annotation study. The observed disagreement is
//      * basically the proportion of annotation units that the raters disagree on divided by the
//      * number of units in the given study.
//      */
//     public double calculateObservedDisagreement();
// 
//     /**
//      * Returns the expected disagreement of an annotation study. The expected disagreement is the
//      * proportion of disagreement that would be expected by chance alone. The expected disagreement
//      * should be equal to the observed disagreement if each rater makes a random decision for each
//      * unit.
//      */
//     public double calculateExpectedDisagreement();
// }
// 
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/InsufficientDataException.java
// public class InsufficientDataException
//     extends RuntimeException
// {
//     private static final long serialVersionUID = -3081535421752493402L;
// 
//     public InsufficientDataException()
//     {
//         super();
//     }
// 
//     public InsufficientDataException(final String message, final Throwable cause)
//     {
//         super(message, cause);
//     }
// 
//     public InsufficientDataException(final String message)
//     {
//         super(message);
//     }
// 
//     public InsufficientDataException(final Throwable cause)
//     {
//         super(cause);
//     }
// }
// 
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/distance/IDistanceFunction.java
// public interface IDistanceFunction
// {
// 
//     /**
//      * Returns a distance value for the given pair of categories used within the given annotation
//      * study. Normally, the measure should return a distance of 0 if, and only if, the two
//      * categories are equal and a positive number otherwise.
//      */
//     public double measureDistance(final IAnnotationStudy study, final Object category1,
//             final Object category2);
// 
// }


// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/coding/KrippendorffAlphaAgreement.java
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.dkpro.statistics.agreement.IAnnotationUnit;
import org.dkpro.statistics.agreement.ICategorySpecificAgreement;
import org.dkpro.statistics.agreement.IChanceCorrectedDisagreement;
import org.dkpro.statistics.agreement.InsufficientDataException;
import org.dkpro.statistics.agreement.distance.IDistanceFunction;

        double result = 0.0;
        for (Entry<Object, Map<Object, Double>> cat1 : coincidenceMatrix.entrySet()) {
            for (Entry<Object, Double> cat2 : cat1.getValue().entrySet()) {
                result += cat2.getValue()
                        * distanceFunction.measureDistance(study, cat1.getKey(), cat2.getKey());
                n += cat2.getValue();
            }
        }
        result /= n;
        return result;
    }

    /**
     * Calculates the expected inter-rater agreement using the defined distance function to infer
     * the assumed probability distribution.
     * 
     * @throws NullPointerException
     *             if the annotation study is null.
     * @throws ArithmeticException
     *             if there are no items or raters in the annotation study.
     */
    @Override
    public double calculateExpectedDisagreement()
    {
        ensureDistanceFunction();
        if (coincidenceMatrix == null) {
            coincidenceMatrix = CodingAnnotationStudy.countCategoryCoincidence(study);
        }

        if (study.getCategoryCount() <= 1) {
            throw new InsufficientDataException(