Skip to content

BCompare groups · Guide test 032

Sign test

Tests a median or paired difference using the balance of positive and negative signs.

Runs in CodeSplashPython example validatedR example validated

Do it in CodeSplash

In the app, set it up in Analyze with this method:

Do it in CodeSplash Run this test on your own data, in Python or R.

Bench

Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.

Bench · Sign test

20
0.40
1.0
0.050

Alternative

Your data

Drag a point or use ↑/↓ to change it
Before · n = 20 · mean 10.13After · n = 20 · mean 9.59

If H₀ were true

S = 15
  1. The reference: how S varies across samples when H₀ is true.
  2. Your statistic, from the data above.
  3. The p-value: the total probability of outcomes no more likely than yours (p = .041).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

S=#{di>0},S∼Bin⁡(n,12) under H0S = \#\{d_i > 0\},\qquad S \sim \operatorname{Bin}(n, \tfrac12)\ \text{under } H_0

With your numbers

S=15 of n=20S = 15\ \text{of}\ n = 20
did_i
within-pair differences (zeros dropped)
SS
how many differences are positive
nn
non-zero differences

The verdict

S = 15 of 20, p = .041

p ≤ α = .050: reject H₀

Only the direction of each difference counts, which makes it robust and assumption-light — and less powerful than the signed-rank test when differences are symmetric.

Statistic
15.000
p-value
0.0414
Sign test on Before: n = 20; After: n = 20: S = 15 of 20, p = .041. The p-value is below α = .050, so the data are hard to reconcile with H0.

Sign test on Before: n = 20; After: n = 20: S = 15 of 20, p = .041. The p-value is below α = .050, so the data are hard to reconcile with H₀.

Question and hypotheses

H0: P(D>0)=½ among nonzero differences; for a continuous distribution this corresponds to median(D)=0.

Assumptions

Independent pairs or independent one sample deviations from a target; no symmetry assumption. Account explicitly for zero differences.

Formula

B=∑iI(Di>0),B∼Binomial⁡(n∗,1/2)B=\sum_iI(D_i>0),\qquad B\sim\operatorname{Binomial}(n_*,1/2)

Parameters and notation

n* counts nonzero differences. For a one sample problem Di=Xi−m0; for pairs Di=Xi−Yi−Δ0.

Structure of the method

Convert prespecified one-sample deviations or paired changes into positive, negative, and zero signs. Under the conditional nonzero-sign null, the positive count follows a binomial distribution with probability one-half. Exact one- or two-sided tails require no normal approximation and ignore all information about difference magnitudes.

Estimated and nuisance parameters

The positive count and number of nonzero differences are observed statistics. The conditional positive-sign probability can be estimated by their ratio; no scale parameter is estimated. The hypothesized median or paired shift is fixed. For discrete distributions, that median statement may differ from the conditional sign-probability null after discarding zeros.

Settings and design choices

Specify subtraction direction, null shift, zero handling, sidedness, alpha, and exact two-sided tail convention. A tolerance used to classify floating-point zeros should follow measurement resolution, not be chosen to improve significance. Repeated within-subject readings require a defined independent pair or subject summary before counting signs.

Fitting and adaptation

Recompute signs after correcting units or matching records, keeping the null shift unchanged unless the scientific question changes. A paired resampling design preserves pairs. For an equivalence question, formulate a dedicated interval/one-sided testing framework rather than interpreting failure to reject equal signs as evidence of negligible change.

Sensitivity and robustness

Many zeros reduce effective sample size and can make the conditional sign target unrepresentative of overall change. Discreteness limits attainable p-values, and exact tests can be conservative. Dependence between pairs invalidates the ordinary binomial count model even though the statistic itself uses only signs.

How to use and calibrate

Use the binomial tail corresponding to the alternative. For a discrete median null, zero handling can change the exact hypothesis.

Example application

Compare before/after ratings when only the direction of change is trustworthy.

Interpretation and pitfalls

Watch out

Discarding magnitude sacrifices power. Extensive zeros make “median equals zero” and conditional sign probability distinct.

What changed in edition 2

  • New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.

Python and R

Drops zero differences; tests equal probabilities of positive and negative differences.

Python · native

Validated — Executed successfully in the validation environment · needs numpy, scipy

import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
x = np.array([4.2,5.1,5.4,4.8,6.0,5.8,4.9,5.6,6.2,5.0])
y = np.array([4.0,4.4,4.8,5.0,5.1,4.3,4.7,4.6,5.2,4.9])
d=x-y; d=d[d!=0]
print(stats.binomtest(np.sum(d>0),len(d),p=.5))
Output
BinomTestResult(k=9, n=10, alternative='two-sided', statistic=0.9, pvalue=0.021484375)
R

Validated — Executed successfully in the validation environment · needs Base/standard library

x <- c(4.2,5.1,5.4,4.8,6.0,5.8,4.9,5.6,6.2,5.0)
y <- c(4.0,4.4,4.8,5.0,5.1,4.3,4.7,4.6,5.2,4.9)
d <- x-y; d <- d[d!=0]; print(binom.test(sum(d>0),length(d),p=.5))
Output
	Exact binomial test

data:  sum(d > 0) and length(d)
number of successes = 9, number of trials = 10, p-value = 0.02148
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
 0.5549839 0.9974714
sample estimates:
probability of success 
                   0.9

Sources

Statistics guide v4 (historical):
R: Executed successfully in the validation environment
Reference's record:
Edition 2 review: Python: executed · R: not executed (Rscript unavailable). Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.

From the CodeSplash statistics guide (v4), entry 032, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT032.