Skip to content

DOC: Follow up #10738 on krippendorff_alpha, with tighter tests - #10763

Merged
bashtage merged 3 commits into
statsmodels:mainfrom
bashtage:wip-krippendorff-followup
Nov 16, 2026
Merged

bashtage merged 3 commits into
statsmodels:mainfrom
bashtage:wip-krippendorff-followup

Conversation

@bashtage

Copy link
Copy Markdown
Member

Follow up to #10738 (krippendorff_alpha). Its review left the points below. The code is unchanged; the docstring and the tests change.

  • tests added / passed.
  • code/documentation is well formatted.
  • properly formatted commit message. See
    NumPy's guide.

Changes

  • DOC: Layout. The docstring says subjects in rows and raters in columns (the aggregate_raters layout). R's irr::kripp.alpha and DescTools::KrippAlpha expect the raters in rows (checked in R 4.6.1 with irr 0.85 and DescTools 0.99.60: t(x) and x give 0.3458 and -0.1645), so a matrix in that layout has to be transposed; a transposed matrix is not detected and gives a valid-looking alpha. A doctest shows it: 6 subjects with 3 raters give 0.364 and the transposed matrix, read as 3 subjects with 6 ratings, gives -0.112. (The Python krippendorff package and the paper's layout are not mentioned: the package is not installed here, so I could not check it.)
  • DOC: One used category. [[1, 1], [1, 1]] returns nan (alpha is 0 / 0) while [[1, 1], [2, 2]] returns 1.0, and R's irr::kripp.alpha returns 1 for matrix(c(1, 1, 1, 1), 2); a Notes sentence says so.
  • DOC: The sentence 'For two raters this is Scott's pi.' followed the formula for alpha and read as alpha = pi. It now says that for two raters Fleiss' kappa is Scott's pi and that nominal alpha is 1 - (1 - pi) (n - 1) / n, Scott's pi with a finite-sample correction (50 subjects with two raters: pi = kappa = 0.0682296 and alpha = 0.0775473 = 1 - (1 - pi) (n - 1) / n; 5000 subjects: 0.00178 and 0.00188).
  • DOC: Cost. The ratings are held in a subjects by distinct-values matrix and in distinct-values by distinct-values matrices, so memory and time grow with subjects times distinct ratings plus the square of the distinct ratings. Measured (interval level, 5 raters, continuous ratings): 2000 subjects with 10000 distinct values took 9.5 s and a peak of 2.8 GB (/usr/bin/time -v; tracemalloc 2720 MB), 500 subjects with 2500 distinct values 170 MB, and integer ratings 1 to 5 for 2000 subjects 0.004 s and 0.6 MB. The docstring has the dependence but no numbers.
  • TST: test_krippendorff_alpha_random_vs_pairs drew data in a while n_checked < 5 loop with continue, so the number of datasets was hidden. It is parametrized over kind, level and seed (ids such as consecutive-nominal-0) and still compared with the pairwise definition. The kinds now include non-consecutive codes (3, 4, 9, 12), negative codes (nominal, ordinal and interval; the ratio level rejects them) and continuous float ratings for all four levels, not only the codes 0 to 3.
  • TST: test_krippendorff_alpha_undefined is parametrized (no pairs, all missing, one value; nominal and interval) and runs with warnings.simplefilter("error"). Deleting the len(values) < 2 return left all of the old tests green, because 0 / 0 gives nan with a RuntimeWarning that nothing asserted (the repository's warnings filter lists only specific messages, so NumPy's 0 / 0 is not an error by itself); now both one-value cases fail without it. The data.shape[0] == 0 return is redundant (the later check also returns nan); it is left in the code.
  • TST: pytest.raises(ValueError, match="ndim") matched a message of array_like; the match is dropped.

Mutation checks (the tree mutated, restored afterwards): computing the interval delta from the ranks of the codes instead of the values fails 6 cases (non-consecutive, negative, continuous interval); np.abs(data) after validation fails 5 (the negative cases and invariance[interval]); np.round of the unique values fails 10 (the continuous cases and the invariance test); replacing the len(values) < 2 return by pass fails both one-value cases.

Not changed (noted in the review)

Tests

  • statsmodels/stats/tests/test_inter_rater.py: 85 passed on Python 3.13 and on Python 3.10 (NumPy 1.23.5, SciPy 1.8.1); the new doctest passes.
  • ruff check of the two changed files and git diff --check pass; numpydoc 1.10 validation of the docstring reports no error.

AI Disclosure

Contributions must comply with the statsmodels AI Policy.

Tick exactly one of the following. If AI tools were used, replace the <...>
placeholders in the second option with the tool name(s) and how the tool's output was
used.

  • No AI tools were used to develop this pull request.
  • AI tools were used.
    Tool(s): Claude Code (Claude Sonnet 5.5).
    Used for: reviewing ENH: Add Krippendorff's alpha to stats.inter_rater #10738, and writing and checking the follow-up changes.
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

🤖 Generated with Claude Code

bashtage and others added 3 commits November 16, 2026 20:38
Follow-up to the merged statsmodels#10738. The docstring now says that raters are in
columns, the transpose of R irr::kripp.alpha and DescTools::KrippAlpha, and
that a transposed matrix is accepted and gives a different alpha without an
error (with an example). It says that a single used category gives nan while
irr returns 1, that for two raters Fleiss' kappa is Scott's pi so that alpha
is 1 - (1 - pi) * (n - 1) / n instead of being equal to pi, and that memory
and time grow with the number of distinct ratings.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Follow-up to the merged statsmodels#10738. Replace the while loop with continue in
test_krippendorff_alpha_random_vs_pairs by a parametrization over kind,
level and seed, and compare with the pairwise definition also for
non-consecutive codes, negative codes (not for ratio) and float ratings.
The undefined cases now run with warnings as errors: numpy's 0 / 0
RuntimeWarning is not an error in this repo, so deleting the
len(values) < 2 return kept every test green. Do not match the wording of
the message of array_like.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Removed unnecessary details about matrix transposition and detection.
@bashtage
bashtage marked this pull request as ready for review November 16, 2026 21:00
@bashtage
bashtage merged commit d5d8adc into statsmodels:main Nov 16, 2026
11 of 12 checks passed
@bashtage
bashtage deleted the wip-krippendorff-followup branch November 16, 2026 22:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant