Repository navigation
DOC: Follow up #10738 on krippendorff_alpha, with tighter tests - #10763
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow up to #10738 (
krippendorff_alpha). Its review left the points below. The code is unchanged; the docstring and the tests change.NumPy's guide.
Changes
DOC:Layout. The docstring says subjects in rows and raters in columns (theaggregate_raterslayout). R'sirr::kripp.alphaandDescTools::KrippAlphaexpect the raters in rows (checked in R 4.6.1 with irr 0.85 and DescTools 0.99.60:t(x)andxgive 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 Pythonkrippendorffpackage 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]]returnsnan(alpha is 0 / 0) while[[1, 1], [2, 2]]returns 1.0, and R'sirr::kripp.alphareturns 1 formatrix(c(1, 1, 1, 1), 2); aNotessentence 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 is1 - (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;tracemalloc2720 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_pairsdrew data in awhile n_checked < 5loop withcontinue, so the number of datasets was hidden. It is parametrized over kind, level and seed (ids such asconsecutive-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_undefinedis parametrized (no pairs, all missing, one value; nominal and interval) and runs withwarnings.simplefilter("error"). Deleting thelen(values) < 2return left all of the old tests green, because 0 / 0 givesnanwith aRuntimeWarningthat 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. Thedata.shape[0] == 0return is redundant (the later check also returnsnan); it is left in the code.TST:pytest.raises(ValueError, match="ndim")matched a message ofarray_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 andinvariance[interval]);np.roundof the unique values fails 10 (the continuous cases and the invariance test); replacing thelen(values) < 2return bypassfails 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 checkof the two changed files andgit diff --checkpass;numpydoc1.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.
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