Workspace/Coding labs
Loading progress

Compute a paired comparison with an exact sign-flip test

Advanced70 min

Implement paired_report(baseline, candidate) for dictionaries mapping identical case IDs to finite scores where higher is better, returning paired effect counts and a small exact two-sided sign-flip p-value.

Your task

  1. Require matching nonempty dictionaries with 1 to 16 identical case IDs and finite int/float scores. Reject booleans and invalid inputs with ValueError.
  2. Sort case IDs for deterministic pairing. Compute candidate minus baseline for each case.
  3. Return n, mean_delta, wins, ties, losses, and p_value. Ties are exact zero differences.
  4. Enumerate every assignment of -1/+1 signs to the paired differences. Count an assignment as extreme when its absolute mean is at least the observed absolute mean minus 1e-12.
  5. Divide extreme assignments by 2**n. Document that validity requires exchangeable pair signs under the null and independent units appropriate to the study.

Examples

EXAMPLE 1

InputFour cases change from score 0 to score 1

Outputmean_delta=1.0, wins=4, ties=0, losses=0, p_value=0.125

The very small sample yields a broad null distribution despite all observed changes favoring the candidate.
solution.pyPython 3.12