Metadata-Version: 2.4
Name: how-many-questions
Version: 1.0.1
Summary: How many questions an eval needs to see a loss between two versions of a model, with the exact sign test.
Author: Zuhal Karadavut
License-Expression: MIT
Project-URL: Homepage, https://zuhalsicimkaradavut.com/projects/how-many-questions/
Project-URL: Repository, https://github.com/zuhalsicim/how-many-questions
Project-URL: Issues, https://github.com/zuhalsicim/how-many-questions/issues
Keywords: llm-evaluation,statistics,power-analysis,sign-test,sample-size,benchmarking
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# how-many-questions

[![PyPI](https://img.shields.io/pypi/v/how-many-questions.svg)](https://pypi.org/project/how-many-questions/)
[![Python 3.12+](https://img.shields.io/pypi/pyversions/how-many-questions.svg)](https://pypi.org/project/how-many-questions/)
[![ci](https://github.com/zuhalsicim/how-many-questions/actions/workflows/ci.yml/badge.svg)](https://github.com/zuhalsicim/how-many-questions/actions/workflows/ci.yml)
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.24711636.svg)](https://doi.org/10.5281/zenodo.24711636)

**PyPI:** [pypi.org/project/how-many-questions](https://pypi.org/project/how-many-questions/) · **Project page & proof:** [zuhalsicimkaradavut.com/projects/how-many-questions](https://zuhalsicimkaradavut.com/projects/how-many-questions/) · **Paired benchmark:** [sql-under-compression](https://github.com/zuhalsicim/sql-under-compression) · **Author:** [Zuhal Karadavut (Zuhal Sicim · @zuhalsicim)](https://zuhalsicimkaradavut.com)

```bash
pip install how-many-questions
how-many-questions --from 59 26 1034 --questions 200
```

How many questions does an eval need before it can see that a new version of a model, a 4-bit copy
for example, answers worse than the old one?

It depends on two numbers: the loss to be found, and the share of questions the two versions answer
differently. With 85 of 1,034 questions answered differently (8.2 %) and a loss of 3.2 points, a
run needs 668 questions to see the loss with a chance of 80 %. A run of 200 questions sees it with
a chance of 26.9 %.

These are the numbers of a real comparison. In
[sql-under-compression](https://github.com/zuhalsicim/sql-under-compression), Qwen2.5-Coder-1.5B-Instruct
at q4_k_m answered 85 of the 1,034 Spider questions differently from f16 and lost 3.2 points: 59
questions lost, 26 gained, p = 0.00045 in the exact sign test. My first run there had 200 questions
and could not see the loss (9 lost, 5 gained, p = 0.424). Taking the full run's numbers as the truth,
that was the likely outcome, not bad luck.

## Use

Python 3.12 or newer, nothing to install: clone the repository and run it from its folder.

How many questions a loss needs, and the chance of a run of a given size. `--from` takes the counts
of a finished run, questions lost, questions gained and questions asked, so nothing is rounded:

```
$ python -m howmany --from 59 26 1034 --questions 200
Questions needed: 668
  for a chance of 80.0 % to see a loss of 3.2 points (33 of 1,034 questions)
  (sign test below 0.05, 85 of 1,034 questions answered differently).
At 200 questions the chance is 26.9 %.
```

Before a run there are no counts yet, so `--differ` and `--loss` take a share in percent and a loss
in points. Rounded to one decimal, the same run gives a slightly different plan:

```
$ python -m howmany --differ 8.2 --loss 3.2 --questions 200
Questions needed: 663
  for a chance of 80.0 % to see a loss of 3.2 points
  (sign test below 0.05, 8.2 % of questions answered differently).
At 200 questions the chance is 27.1 %.
```

The smallest loss a run of a given size sees with a chance of 80 %:

```
$ python -m howmany --differ 8.2 --questions 200
Smallest loss seen with a chance of 80.0 % at 200 questions: 5.8 points
  (sign test below 0.05, 8.2 % of questions answered differently).
```

```
$ python -m howmany --differ 8.2 --questions 1034
Smallest loss seen with a chance of 80.0 % at 1,034 questions: 2.6 points
  (sign test below 0.05, 8.2 % of questions answered differently).
```

The share of differing questions comes from a pilot run: count the questions the two versions
answer differently and divide by the questions asked. In the first run above that was 14 of 200,
7 %:

```
$ python -m howmany --differ 7 --loss 3.2
Questions needed: 571
  for a chance of 80.0 % to see a loss of 3.2 points
  (sign test below 0.05, 7 % of questions answered differently).
```

`--level` sets the level the sign test must land below (default 0.05), `--power` the wanted chance
(default 0.8). The same functions are in `howmany/power.py`: `power`, `questions_needed`,
`detectable_loss`, `sign_test` and `lost_needed`.

## How it works

Both versions answer the same questions, so only the questions they answer differently tell them
apart. "Lost" went from right to wrong, "gained" from wrong to right. The exact sign test asks how
likely a split at least this uneven would be if each differing question were a fair coin toss. It
is the test that [release-gate](https://github.com/zuhalsicim/release-gate), my tool for deciding whether a model version may replace the current
release, and sql-under-compression use; for right-or-wrong answers it is the exact McNemar test.

For a plan, the number of differing questions in a run of n is binomial with n and the differ rate,
and each differing question is lost with chance (differ rate + loss) / (2 x differ rate). The power
is the chance that the test lands below the level with more lost than gained questions. The code
sums it over every number of differing questions, leaving out only terms that add up to less
than 1e-18; for each number, the fewest lost questions that reach the level are counted with
exact integers. The search for a sample size
doubles the run until the power reaches the target, then bisects.

The tests check the power against a sum over every possible outcome for runs of up to 47 questions
(within 1e-12), the counts against a direct scan with the sign test, the thresholds and p-values
against the ones sql-under-compression publishes, and the power against the table in
[my article on that run](https://huggingface.co/blog/ZuhalSicim/4-bit-loss-200-questions-could-not-see).
Every example in this README is a test:
it is run and its output compared with the lines shown here.

## Limits

- **The two inputs are assumptions.** Using the loss that a run has just measured to explain that
  same run's p-value is circular. Plan with the smallest loss that would matter, for example the
  largest one a release may lose.
- **The differ rate of a pilot is itself uncertain.** A higher differ rate needs more questions for
  the same loss, so the safe choice is the upper end of the pilot's interval.
- **Questions are taken as independent.** Spider's questions share 20 databases, and questions on
  the same database tend to fail together. Then the real noise is larger than the sign test assumes
  and a plan from this tool is too small. Evan Miller's
  [Adding Error Bars to Evals](https://arxiv.org/abs/2411.00640) shows how to account for such
  clusters.
- **Only a loss counts.** The test is two-sided, as in release-gate, but the power counts only the
  runs that show more lost than gained questions.
- **From 80 % of questions differing, the power can drop a little with one more question.** Then
  the number of differing questions is almost fixed. The size found reaches the target and one
  question fewer does not, but a smaller run may reach it too: with 95 % of questions differing and
  a loss of 73 points it reports 15, while 13 already reaches 80 %. Below 80 % no such case was
  found.
- **Runs go up to 100,000 questions.** A plan that needs more is reported as "more than 100,000".
  The slowest plan measured took 14 s.
- The smallest loss is printed rounded up to one decimal, so the printed loss is seen with at
  least the wanted chance.

## Run the checks

```
pip install -r requirements-dev.txt
ruff check . && ruff format --check .
mypy
vulture howmany tests --min-confidence 80
pytest
```

The reasons behind each choice are in [DECISIONS.md](DECISIONS.md).

## Files

- `howmany/power.py`: the sign test, its power, the sample size and the smallest loss a run sees
- `howmany/__main__.py`: the command line (`--differ` and `--loss`, or `--from` with a run's counts)
- `tests/`: the checks above
- `CITATION.cff`: how to cite the tool ("Cite this repository" on GitHub; Zenodo reads it for a DOI)
