Skip to content

DPredict · Guide test 230

Pettitt change point test

Uses ranks to test for a single change in distributional location at an unknown point.

Runs in CodeSplashPython (calls R) example not runR example not run

Do it in CodeSplash

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

In Change-point tests (level shifts), choose Test = Pettitt (ranks).

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 · Pettitt change point test

50
0.000
0.00

Each error carries φ × the previous one; 1 is a random walk.

1.0
0.050

Your data

Simulated with a known ρ

If H₀ were true

K = 316.00
  1. The reference: how K varies across samples when H₀ is true, Pettitt's K for n = 50 (asymptotic) (2 exp(−6K²/(n³ + n²))).
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .018).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

Ut=2∑s≤trs−t(n+1),K=max⁡t∣Ut∣,p≈2e−6K2/(n3+n2)U_t = 2\sum_{s \le t} r_s - t(n+1),\qquad K = \max_t |U_t|,\qquad p \approx 2e^{-6K^2/(n^3 + n^2)}

With your numbers

K=316 at t=24,n=50K = 316\ \text{at}\ t = 24,\qquad n = 50
rsr_s
rank of observation s
UtU_t
Mann–Whitney statistic comparing before and after t

The verdict

K = 316, p = .018

p ≤ α = .050: reject H₀

Rank-based, so outliers matter little; trend or autocorrelation also produce large K.

Statistic
316.000
p-value
0.0182
Pettitt change point test on 50 (x, y) pairs: K = 316, p = .018. The p-value is below α = .050, so the data are hard to reconcile with H0.

Pettitt change point test on 50 (x, y) pairs: K = 316, p = .018. The p-value is below α = .050, so the data are hard to reconcile with H₀.

Question and hypotheses

H0: one continuous distribution throughout the series; H1: one unknown location change.

Assumptions

Independent observations under the null; a single change-point interpretation; appropriate handling of ties.

Formula

Uk=2∑i=1kRi−k(n+1)K=max⁡1≤k<n∣Uk∣\begin{gathered}U_k=2\sum_{i=1}^{k}R_i-k(n+1)\\[5pt] K=\max_{1\le k<n}|U_k|\end{gathered}

Parameters and notation

Ri are ranks in the whole series; argmax|Uk| estimates the change point.

Structure of the method

Rank the whole series, form a cumulative rank score at every eligible split, and take the largest absolute score. The argmax is a candidate location estimate, while the test statistic is the maximum over the whole search. These are different outputs with different uncertainty questions.

Estimated and nuisance parameters

Estimate ranks, the maximized rank contrast, and candidate breakpoint; no parametric error distribution or neural model is fitted. The null calibration accounts for the split search. A breakpoint confidence interval is not supplied automatically by the test p value and requires a separately justified inferential procedure.

Settings and design choices

Choose admissible split range, one- versus two-direction change interpretation, tie treatment, and permutation versus approximate p-value calibration. If endpoint trimming or other constraints are added, calibrate that exact search. The common exponential approximation should not be treated as an exact finite-sample result.

Fitting and adaptation

For a new domain, identify whether iid exchangeability is defensible before choosing permutation calibration. If data are serially dependent, use a valid dependence-aware change-point method or resampling design. Re-run the full rank/search algorithm in each replicate; testing only the selected split ignores selection.

Sensitivity and robustness

Inspect serial correlation, multiple changes, gradual trends, heavy ties, and short segments. Several breaks can mask or distort the estimated single break. The supplied gently trending series demonstrates a calculation but is not a clean one-step-change example; rejection need not identify a unique physical discontinuity.

How to use and calibrate

Use permutation calibration, or the common approximation p≈min[1,2exp{−6K²/(n³+n²)}].

Example application

Locate a possible shift in annual measurements.

Interpretation and pitfalls

Watch out

The p value concerns the maximum across all candidate splits. Serial dependence or repeated scanning needs additional treatment.

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

Keep observations in temporal order. Serial correlation and seasonality can invalidate the default independent-error calibration; do not treat the selected change point as known in advance.

Python · invokes R

Not run — Python launcher syntax checked; R backend runtime not verified · needs Python standard library + Rscript; R packages: trend

# Python launcher for the complete R implementation below.
# Requires Rscript on PATH and the listed R packages.
import shutil
import subprocess
rscript = shutil.which("Rscript")
if rscript is None:
    raise RuntimeError("Install R and make Rscript available on PATH.")
r_code = r"""
set.seed(2026)
x<-seq_len(60)*.02+rnorm(60)
print(trend::pettitt.test(x))
"""
# No shell interpolation. A failed R execution raises an error in Python.
result = subprocess.run([rscript, "--vanilla", "-"], input=r_code,
                        text=True, capture_output=True)
print(result.stdout)
if result.stderr:
    print(result.stderr)
result.check_returncode()
R

Not run — Syntax checked; runtime not verified in the final environment · needs trend

set.seed(2026)
x<-seq_len(60)*.02+rnorm(60)
print(trend::pettitt.test(x))

Sources

Statistics guide v4 (historical):
Python: Python launcher syntax checked; R backend runtime not verified · R: Syntax checked; runtime not verified in the final environment
Reference's record:
Edition 2 review: Python: skip · R: not executed (Rscript unavailable); why not run: Python: Python-to-R bridge; Rscript is unavailable. R code was not executed. Python: Not run in the reference's review: Python-to-R bridge; Rscript is unavailable. R code was not executed. R: Reviewed but not run: R wasn't available to the reference's authors.

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