#209Fleiss' KappaMedium

Fleiss' Kappa

Background

Fleiss' kappa extends Cohen's kappa to settings where each item is rated by multiple raters (typically 3+). It measures inter-rater agreement after correcting for chance, returning a single scalar in [1,1][-1, 1] where 1 = perfect agreement, 0 = chance-level, negative = systematic disagreement. Standard metric for RLHF data-quality QA and human-evaluation pipelines.

Problem statement

Implement fleiss_kappa(ratings) where ratings is shape (N,K)(N, K) with ratings[i, k] = number of raters who assigned item ii to category kk. The total per row must be constant (equal raters per item). Compute:

Pi=1n(n1)(knik2n),Pˉ=1NiPi,Pe=kpk2,κ=PˉPe1PeP_i = \frac{1}{n(n-1)}\Bigl(\sum_k n_{ik}^2 - n\Bigr),\quad \bar P = \tfrac{1}{N}\sum_i P_i,\quad P_e = \sum_k p_k^2,\quad \kappa = \frac{\bar P - P_e}{1 - P_e}

where nn is the per-item rater count and pk=1Nninikp_k = \tfrac{1}{Nn}\sum_i n_{ik}.

Input

  • ratingsnp.ndarray shape (N,K)(N, K) of non-negative integer counts. Each row sums to the same total nn (the number of raters).

Output

Returns a Python float, the Fleiss kappa.

Examples

Example 1 — perfect agreement

Input:  ratings = [[3,0], [0,3], [3,0]]   # 3 raters, 2 categories, all agree per item
Output: 1.0

Example 2 — chance-level agreement

Input:  ratings drawn from uniform multinomial
Output: ≈ 0.0

Constraints

  • Rows must sum to the same value (equal raters per item). ValueError otherwise.
  • Need n2n \ge 2 raters per item.
  • If Pe=1P_e = 1 (only one category has any votes), return 1.01.0.

Notes

  • Interpretation. κ0.6\kappa \ge 0.6 is typically called "substantial agreement"; 0.8\ge 0.8 "almost perfect". Below 0.40.4 means the labelling task / guidelines need work.
  • vs Cohen. Cohen's kappa needs exactly 2 raters; Fleiss handles any number 2\ge 2.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Reference example: perfect agreement returns 1.0
  • Example: four items, two categories, mixed agreement
  • Sample: three categories, partial agreement