perm_test¶
- perm_test(y_obs, z_obs, alloc, *, contrast=(1.0, -1.0), tau=0.0, method='mean', alternative='two-sided', exact=False)[source]¶
Fisher randomization test of a sharp null hypothesis.
For two groups the null is
c_1 Y_i(1) + c_2 Y_i(2) = taufor every unit. Both coefficients must be nonzero. Under this null the missing potential outcomes are imputed from the observed ones, and the statisticsum_g c_g * mean_g(of outcomes, or of their ranks) is recomputed for each assignment inalloc. Two-sided p-values measure distance fromtau. With more than two groups, only the sharp null of identical potential outcomes in every group is supported:tau=0and coefficients summing to zero. A single contrast constraint does not identify the missing potential outcomes in that case.Rank statistics are supported only under the no-effect null (
tau=0and coefficients summing to zero). For a nonzero effect, outcome units and rank units cannot be used interchangeably.- Parameters:
y_obs (np.ndarray) – Observed outcome per unit.
z_obs (np.ndarray) – Observed group label per unit.
alloc (np.ndarray) – Reference assignments, one row per assignment. By default, supply independent draws from the design that produced
z_obs; do not prepend the observed assignment. Random draws that happen to match it are retained. Withexact=True, supply the full equally weighted assignment distribution, includingz_obs.contrast (Sequence[float]) – Coefficient for each group, in sorted label order.
tau (float) – Value of the contrast under the sharp null.
method (Literal['mean', 'rank']) – Use outcomes (
"mean") or their ranks ("rank").alternative (Literal['two-sided', 'greater', 'less']) – Direction of the alternative hypothesis.
exact (bool) – If True, return the fraction of reference statistics at least as extreme. Otherwise use
(b + 1) / (B + 1), wherebofBsampled statistics are at least as extreme, to include the observed statistic and avoid zero Monte Carlo p-values.
- Returns:
The observed statistic, the reference statistics and the p-value.
- Raises:
ValueError – If inputs are invalid or the null is unsupported.
- Return type: