Skip to content

ENH: Add a bootstrap confidence interval for Krippendorff's alpha - #11197

Open
Allan-dq wants to merge 1 commit into
statsmodels:mainfrom
Allan-dq:enh-krippendorff-bootstrap
Open

Allan-dq wants to merge 1 commit into
statsmodels:mainfrom
Allan-dq:enh-krippendorff-bootstrap

Conversation

@Allan-dq

Copy link
Copy Markdown

This adds krippendorff_alpha_bootstrap, which returns Krippendorff's alpha and its bootstrap confidence interval. The result unpacks directly as statistic, ci_low, ci_upp, and it also has nboot_valid, the number of valid bootstrap samples.

The method is a percentile bootstrap that resamples subjects (rows): each time, N subjects are drawn with replacement from the N subjects, all ratings of a subject are drawn together, and the interval is given by quantiles of the bootstrap values. This is the approach recommended by Zapf et al. (2016). I did not implement the bootstrap of Hayes and Krippendorff (2007), because the simulations of Zapf et al. showed that its coverage is far below the nominal level.

If all ratings in a bootstrap sample are equal, alpha is undefined. Such a sample is recorded as nan and left out of the interval. This is consistent with the current rule in krippendorff_alpha.

To avoid recounting the ratings in every sample, I split the body of krippendorff_alpha into two private functions. The results of krippendorff_alpha are unchanged, and the existing tests all pass without changes.

Verification: the tests reproduce the subjects drawn in each sample with the same random seed and call krippendorff_alpha directly on the drawn data; the results agree for all four levels of measurement. I also compared it case by case with the percentile method of scipy.stats.bootstrap, and the results are identical. In a coverage simulation with a nominal 95% level, the coverage was between 0.925 and 0.950 with 200 subjects and between 0.900 and 0.930 with 20 subjects, so the interval is too narrow with few subjects; the docstring says so. All 172 tests in inter_rater pass.

One question I'd like the maintainers' opinion on: statsmodels usually uses the parameter name alpha for the significance level, but the statistic this function computes is also called alpha. I added a note in the docstring; if you think it should be renamed, I can change it.

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

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 Opus 5.5).
    Used for: drafting the implementation, the tests and the docstrings, running the coverage simulation and the comparison with scipy, and translating this description from Chinese.
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.
Details

Notes:

  • It is essential that you add a test when making code changes. Tests are not
    needed for doc changes.
  • When adding a new function, test values should usually be verified in another package (e.g., R/SAS/Stata).
  • When fixing a bug, you must add a test that would produce the bug in main and
    then show that it is fixed with the new code.
  • New code additions must be well formatted. Changes should pass ruff. You can
    verify your changes are well formatted by running
    ruff check . --fix
    
    assuming ruff is installed. While passing this test is not required, it is good practice and it help
    improve code quality in statsmodels.
  • Docstring additions must render correctly, including escapes and LaTeX.
  • If AI tools were used to help write this PR, see the
    AI Policy for what disclosure
    and review is expected of you before submitting.

@Allan-dq
Allan-dq force-pushed the enh-krippendorff-bootstrap branch from 901e96b to b8cf8d1 Compare November 25, 2026 01:11
Add krippendorff_alpha_bootstrap to stats.inter_rater, which returns
Krippendorff's alpha with a percentile bootstrap confidence interval. The
subjects with at least two ratings are resampled with replacement and
alpha is computed for each sample; samples in which alpha is undefined
are counted and left out of the interval. The result is a frozen
dataclass that unpacks as (statistic, ci_low, ci_upp).

The computation of krippendorff_alpha is split into a helper that counts
the ratings and one that computes alpha from the counts with optional
subject frequencies, so a bootstrap sample needs no new tabulation.
krippendorff_alpha returns the same values as before.
@Allan-dq
Allan-dq force-pushed the enh-krippendorff-bootstrap branch from b8cf8d1 to 8d7f7a3 Compare November 26, 2026 23:02
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