Shift testing¶
Use this page when you already have a numeric signal for a source group and a target group.
Choose the function¶
| Function | What it answers | Use it when |
|---|---|---|
shift.detect_shift(...) |
Did anything change? | you want to detect any difference between source and target |
shift.detect_harm(...) |
Did the target group move in a worse direction? | you know what "worse" means for your signal |
Examples of useful signals include predicted risk, prediction error, model confidence, and domain classifier probabilities.
Common controls¶
Both functions accept:
n_resamplesto control the number of permutation resamplesbatchto limit memory use during the permutation testrandom_statefor reproducibilityweightsfor weighted testing withImportanceWeights
shift.detect_harm(...) also requires direction, which must be one of:
"higher-is-worse""higher-is-better"
What you get back¶
shift.detect_shift(...)returnsShiftResultshift.detect_harm(...)returnsHarmResult
In both cases, the fields most users look at first are:
.statistic.pvalue
ShiftResult also includes .statistic_name.
HarmResult also includes .direction.
Both results include .null_distribution when you need the full permutation output.
Posterior evidence for harmful shift¶
If you want posterior draws and a Bayes factor alongside the p-value, set
include_posterior=True.
import samesame as ss
result = ss.shift.detect_harm(
source_scores,
target_scores,
direction="higher-is-worse",
include_posterior=True,
)
print(f"p-value: {result.pvalue:.4f}")
print(f"Bayes factor: {result.bayes_factor:.2f}")
threshold is only valid when include_posterior=True. Otherwise detect_harm(...) raises a
ValueError.
API¶
Detect whether Source and Target Outlier score distributions differ.
Source code in src/samesame/shift.py
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | |
Detect whether Target is harmfully shifted relative to Source.
Source code in src/samesame/shift.py
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 | |
Result types¶
Bases: TestResult
Result of generic shift detection.
Source code in src/samesame/shift.py
38 39 40 41 42 43 | |
Bases: TestResult
Result of harmful-shift detection.
Source code in src/samesame/shift.py
46 47 48 49 50 51 52 53 | |