// 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;
/*
 * Copyright 2014
 * Ubiquitous Knowledge Processing (UKP) Lab
 * Technische Universität Darmstadt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dkpro.statistics.agreement.coding;



/**
 * Implementation of Krippendorff's (1980) alpha-measure for calculating a chance-corrected
 * inter-rater agreement for multiple raters with arbitrary distance/weighting functions. The basic
 * idea is to divide the estimated variance of within the items by the estimated total variance.
 * Before an inter-rater agreement can be calculated, an {@link IDistanceFunction} instance needs to
 * be assigned.<br>
 * <br>
 * References:
 * <ul>
 * <li>Krippendorff, K.: Estimating the reliability, systematic error and random error of interval
 * data. Educational and Psychological Measurement 30(1):61-70, 1970.</li>
 * <li>Krippendorff, K.: Content Analysis: An Introduction to Its Methodology. 2nd edition, Thousand
 * Oaks, CA: Sage Publications, 2004.</li>
 * <li>Artstein, R. &amp; Poesio, M.: Inter-Coder Agreement for Computational Linguistics.
 * Computational Linguistics 34(4):555-596, 2008.</li>
 * </ul>
 * 
 * @author Christian M. Meyer
 */
public class KrippendorffAlphaAgreement
    extends WeightedAgreement
// 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;

/*
 * Copyright 2014
 * Ubiquitous Knowledge Processing (UKP) Lab
 * Technische Universität Darmstadt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dkpro.statistics.agreement.coding;



/**
 * Implementation of Krippendorff's (1980) alpha-measure for calculating a chance-corrected
 * inter-rater agreement for multiple raters with arbitrary distance/weighting functions. The basic
 * idea is to divide the estimated variance of within the items by the estimated total variance.
 * Before an inter-rater agreement can be calculated, an {@link IDistanceFunction} instance needs to
 * be assigned.<br>
 * <br>
 * References:
 * <ul>
 * <li>Krippendorff, K.: Estimating the reliability, systematic error and random error of interval
 * data. Educational and Psychological Measurement 30(1):61-70, 1970.</li>
 * <li>Krippendorff, K.: Content Analysis: An Introduction to Its Methodology. 2nd edition, Thousand
 * Oaks, CA: Sage Publications, 2004.</li>
 * <li>Artstein, R. &amp; Poesio, M.: Inter-Coder Agreement for Computational Linguistics.
 * Computational Linguistics 34(4):555-596, 2008.</li>
 * </ul>
 * 
 * @author Christian M. Meyer
 */
public class KrippendorffAlphaAgreement
    extends WeightedAgreement
    implements IChanceCorrectedDisagreement, ICategorySpecificAgreement,