# Algorithm Implementation/Statistics/Cohen's kappa

Implemented and tested as described at Wikipedia . This is just a first working draft without any optimization but it works just fine. Comments are for debug purposes only.

```import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CohensKappa {

public static double computeKappa(List<List<Integer>> scores) {
if (scores.size() != 2) {
throw new IllegalArgumentException("Only scores from two annotators are valid for Cohen's Kappa");
}

if (scores.get(0).size() != scores.get(1).size()) {
throw new IllegalArgumentException("The scores from both annotators must have the same length");
}

Map<Integer, Map<Integer, Integer>> table = new HashMap<Integer, Map<Integer, Integer>>();

int tableSize = 0;

for (int i = 0; i < scores.get(0).size(); i++) {
int first = scores.get(0).get(i);
int second = scores.get(1).get(i);

if (!table.containsKey(first)) {
table.put(first, new HashMap<Integer, Integer>());
}

if (!table.get(first).containsKey(second)) {
table.get(first).put(second, 0);
}

table.get(first).put(second, table.get(first).get(second) + 1);

if (first > tableSize) {
tableSize = first + 1;
}

if (second > tableSize) {
tableSize = second + 1;
}
}

boolean DEBUG = true;
if (DEBUG) {
System.out.println(table);
}

// sum the rows and cols
Map<Integer, Integer> sumRows = new HashMap<Integer, Integer>();
Map<Integer, Integer> sumCols = new HashMap<Integer, Integer>();

for (Map.Entry<Integer, Map<Integer, Integer>> entry : table.entrySet()) {
int rowNumber = entry.getKey();
int sumRow = 0;

for (Map.Entry<Integer, Integer> rowEntry : entry.getValue().entrySet()) {
int colNumber = rowEntry.getKey();
int value = rowEntry.getValue();

sumRow += value;

if (!sumCols.containsKey(colNumber)) {
sumCols.put(colNumber, 0);
}

sumCols.put(colNumber, sumCols.get(colNumber) + value);
}

sumRows.put(rowNumber, sumRow);
}

// sum the total
int sumTotal = 0;
for (Integer rowSum : sumRows.values()) {
sumTotal += rowSum;
}

if (DEBUG) {
System.out.println("Row sums: " + sumRows);
System.out.println("Col sums: " + sumCols);
System.out.println("Total: " + sumTotal);
System.out.println("table size: " + tableSize);
}

// sum diagonal
int sumDiagonal = 0;
for (int i = 1; i <= tableSize; i++) {
int value = 0;
if (table.containsKey(i) && table.get(i).containsKey(i)) {
value = table.get(i).get(i);
}

sumDiagonal += value;
}

if (DEBUG) {
System.out.println("Sum diagonal: " + sumDiagonal);
}

double p = (double) sumDiagonal / (double) sumTotal;

if (DEBUG) {
System.out.println("p: " + p);
}

double peSum = 0;
for (int i = 1; i <= tableSize; i++) {
if (sumRows.containsKey(i) && sumCols.containsKey(i)) {
peSum += (double) sumRows.get(i) * (double) sumCols.get(i);
}
}

if (DEBUG) {
System.out.println("pesum: " + peSum);
}

double pe = peSum / (sumTotal * sumTotal);

if (DEBUG) {
System.out.println("pe: " + pe);
}

return (p - pe) / (1.0d - pe);
}

public static void main(String[] args) {
exampleWikipediaOne();
exampleWikipediaTwo();
examplePaper1();
examplePaper2();
}

public static void exampleWikipediaOne() {
List<Integer> one = new ArrayList<Integer>();
List<Integer> two = new ArrayList<Integer>();
for (int i = 0; i < 45; i++) {
}

for (int i = 0; i < 15; i++) {
}

for (int i = 0; i < 25; i++) {
}

for (int i = 0; i < 15; i++) {
}

List<List<Integer>> list = new ArrayList<List<Integer>>();

System.out.println(computeKappa(list));
}

public static void exampleWikipediaTwo() {
List<Integer> one = new ArrayList<Integer>();
List<Integer> two = new ArrayList<Integer>();
for (int i = 0; i < 25; i++) {
}

for (int i = 0; i < 35; i++) {
}

for (int i = 0; i < 5; i++) {
}

for (int i = 0; i < 35; i++) {
}

List<List<Integer>> list = new ArrayList<List<Integer>>();

System.out.println(computeKappa(list));
}

/*
* Example from http://www-users.york.ac.uk/~mb55/msc/clinimet/week4/kappash2.pdf Table 4, pg. 3
*/
public static void examplePaper1() {
List<Integer> one = new ArrayList<Integer>();
List<Integer> two = new ArrayList<Integer>();

for (int i = 0; i < 12; i++) {
}

for (int i = 0; i < 4; i++) {
}

for (int i = 0; i < 2; i++) {
}

for (int i = 0; i < 12; i++) {
}

for (int i = 0; i < 56; i++) {
}

for (int i = 0; i < 3; i++) {
}

for (int i = 0; i < 4; i++) {
}

for (int i = 0; i < 1; i++) {
}

List<List<Integer>> list = new ArrayList<List<Integer>>();

System.out.println(computeKappa(list));
}

/*
* Example from http://www-users.york.ac.uk/~mb55/msc/clinimet/week4/kappash2.pdf Table 6, pg. 4
*/
public static void examplePaper2() {
List<Integer> one = new ArrayList<Integer>();
List<Integer> two = new ArrayList<Integer>();

for (int i = 0; i < 2; i++) {
}

for (int i = 0; i < 12; i++) {
}

for (int i = 0; i < 8; i++) {
}

for (int i = 0; i < 9; i++) {
}

for (int i = 0; i < 35; i++) {
}

for (int i = 0; i < 43; i++) {
}

for (int i = 0; i < 7; i++) {
}

for (int i = 0; i < 4; i++) {
}

for (int i = 0; i < 36; i++) {
}

for (int i = 0; i < 103; i++) {
}

for (int i = 0; i < 40; i++) {
}

for (int i = 0; i < 1; i++) {
}

for (int i = 0; i < 8; i++) {
}

for (int i = 0; i < 36; i++) {
}

for (int i = 0; i < 22; i++) {