Tutorial: Quickstart¶
New data can change scores without making anyone worse off. This quickstart shows two checks: whether scores tend to be higher in the group under review, and whether a measure tied to a concern has moved in the harmful direction.
Install samesame:
python -m pip install samesame
1. Set up the groups¶
Each number is one application's predicted risk. source is the reference group; target is the group under review. In this example, higher risk means more concern.
import numpy as np
import samesame as ss
source = np.array([0.10, 0.22, 0.28, 0.35])
target = np.array([0.30, 0.42, 0.55, 0.61])
test = ss.MannWhitneyU(source, target)
2. Set the test options¶
The test keeps the scores fixed and shuffles which group each score belongs to. It compares the observed ranking with rankings from those shuffled groups. Set a seed and n_permutations to make the run repeatable. Configuring the test returns a new test object; the original stays unchanged.
test = test.set_permutation(
seed=np.random.default_rng(12345),
n_permutations=9999,
)
This example uses 9,999 random permutations.
3. Run the test and read the result¶
shift = test()
print(shift.pvalue, shift.statistic)
Expected output: p-value 0.056 and statistic 0.94. The target scores tend to rank higher than the source scores. This is a score difference, not a result about whether outcomes got worse.
4. Check a direction linked to harm¶
Use Dsos when you have chosen a value that is tied to an outcome you care about. It expects higher values to mean more harm. If lower values mean worse outcomes, reverse their sign before testing.
test = ss.Dsos(source, target)
test = test.set_permutation(
seed=np.random.default_rng(12345),
n_permutations=9999,
)
harm = test()
print(harm.pvalue)
This check focuses on the high-risk end of the scores, not the average. A small p-value is a reason to investigate, not proof that outcomes got worse.
For real model scores, use predictions from cases the model did not train on. Record the seed and requested n_permutations if you need to repeat the run.
For other questions, see Choose a test. For an example using reported outcomes, see Compare outcomes.