Skip to content

API: Weighting

Use overlap weighting when the groups have some feature profiles in common and you want to check whether a result remains among those comparable profiles. .overlap() gives less influence to profiles that are rare in the other group. It cannot create overlap where none exists.

source is the reference group; target is the group under review.

At a glance

Entry Role Output
MannWhitneyU(...).overlap() / Dsos(...).overlap() Convert calibrated P(target\|x) values into overlap weights Weighted copy of the same test class with .ess

.overlap() takes calibrated group-membership probabilities (p_source, p_target), not the scores being tested. It returns a weighted copy of the same test class. Its .ess property reports Kish's effective sample size: (sum w)^2 / sum(w^2). This summarizes weight concentration as an equivalent equally weighted sample size; it is not a literal number of retained observations.

.overlap() and .set_permutation() each return a new test object and leave the original unchanged. See API: Testing for batch.

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])
p_source = np.array([0.2, 0.3, 0.4, 0.5])
p_target = np.array([0.5, 0.6, 0.7, 0.8])

test = ss.Dsos(source, target)
test = test.overlap(p_source, p_target, shrinkage=0.5)
test = test.set_permutation(seed=np.random.default_rng(12345))
result = test()
print(test.ess)
print(result.pvalue, result.statistic)

For real data, estimate group probabilities out of fold with cross_val_predict and CalibratedClassifierCV. See Weight for common support for an example.

Formula

Let p_source and p_target contain the calibrated probability that each profile belongs to the group under review, given its features (P(target|x)). Let n_s and n_t be the group sizes. After adjusting for the group-size prior, the odds give the estimated feature-density ratio between the groups. The implementation clips probabilities to [1e-6, 1 - 1e-6].

The method first estimates how much more common each profile is in one group than the other:

  • r_s = p_source / (1 - p_source) * n_s / n_t for source.
  • r_t = (1 - p_target) / p_target * n_t / n_s for target.

For shrinkage s, the unnormalized weight equals 1 / (s + (1 - s) / r). The method then scales the weights to have a mean of one within each group.

  • 0.0 uses the full density ratio. It makes the strongest adjustment but can increase variability. The API default is 0.0; use it only when overlap is strong.
  • 0.5 gives symmetric overlap weights, which emphasize the region of covariate overlap. This is the recommended starting point because it balances adjustment and precision.
  • 1.0 gives every observation equal weight, returning the unweighted test.

Check .ess before interpreting the p-value. Treat ESS/n below 0.1, or a collapse in either group, as a warning that the result has low precision; this is a practical warning threshold, not a universal validity cutoff. A larger p-value after ESS collapses is not reassurance.

Calibrate probabilities using the target-group share implied by the array sizes, n_t / (n_s + n_t). If calibration used a different group share, adjust the odds before calling .overlap().

Use out-of-fold group probabilities and keep them separate from the scores being tested. Start unweighted; add weights for a defined overlap question. Check .ess before interpreting a weighted p-value. Weights cannot create overlap where none exists.