Skip to content

DPredict · Guide test 209

Bartels rank test

Tests randomness using a rank based von Neumann successive difference statistic.

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:

  • Randomness and serial-dependence tests — Is the series (or a model's residuals) just random noise, or does it carry runs, trends, autocorrelation, volatility clustering or nonlinear structure?

In Randomness and serial-dependence tests, choose Test = Bartels rank (von Neumann on 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 · Bartels rank test

40
0.040
0.40

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

0.0
0.050

Alternative

Your data

Simulated with a known ρ

If H₀ were true

z = −2.59
  1. The reference: how z varies across samples when H₀ is true.
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .010).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

RVN=∑t(Rt−Rt+1)2∑t(Rt−Rˉ)2,z=RVN−2σRVN = \dfrac{\sum_t(R_t-R_{t+1})^2}{\sum_t(R_t-\bar R)^2},\qquad z = \dfrac{RVN-2}{\sigma}

With your numbers

RVN=1.20,z=(−2.59)RVN = 1.20,\quad z = (-2.59)
RtR_t
rank of the t-th value
RVNRVN
rank von Neumann ratio, 2 for randomness

The verdict

RVN = 1.20, z = −2.59, p = .010

p ≤ α = .050: reject H₀

Small RVN: trend or positive serial dependence. The rank version of the von Neumann ratio, more powerful than the runs test.

Statistic
−2.587
p-value
0.0097
Bartels rank test on 40 (x, y) pairs: RVN = 1.20, z = −2.59, p = .010. The p-value is below α = .050, so the data are hard to reconcile with H0.

Bartels rank test on 40 (x, y) pairs: RVN = 1.20, z = −2.59, p = .010. The p-value is below α = .050, so the data are hard to reconcile with H₀.

Question and hypotheses

H0: observations occur in random order.

Assumptions

Exchangeable continuous observations; appropriate tie correction or permutation for ties.

Formula

RVN=∑t=1n−1(Rt+1−Rt)2∑t=1n(Rt−Rˉ)2RVN=\frac{\sum_{t=1}^{n-1}(R_{t+1}-R_t)^2}{\sum_{t=1}^{n}(R_t-\bar R)^2}

Parameters and notation

Rt ranks the observations. Under no ties, E(RVN)=2; asymptotic variance is about 4/n.

Structure of the method

Rank observations in their original sequence, sum squared differences of adjacent ranks, and normalize by total rank dispersion. Small ratios indicate smooth ordering and large ratios indicate alternating ordering. The null reference uses rank randomness, with finite-sample approximations distinct from the ordinary von Neumann statistic on raw values.

Estimated and nuisance parameters

Sample ranks and their mean/dispersion are derived summaries; no distributional mean, variance model, or neural parameter is fitted. Ties alter the rank set and may change the null moments assumed by a no-ties approximation.

Settings and design choices

Specify rank/tie conventions, alternative, and exact, beta-approximate, normal, or permutation calibration. The documented trend implementation uses a scaled beta approximation for 10≤n<100 and a normal approximation for larger n; its complete-observation requirement should be respected.

Fitting and adaptation

No predictive fitting is intrinsic. For a permutation reference, preserve the observed rank multiset and permute only units exchangeable under the null. Applying the test to fitted residuals can introduce dependence from estimation, so any simulation intended to validate that use should refit the originating model. Do not reorder observations to improve results.

Sensitivity and robustness

Short samples, repeated measurements, many ties, missing timestamps, or artificial interpolation affect interpretation. Ranking reduces the effect of extreme magnitudes but does not make arbitrary dependence harmless. The test assesses particular successive-rank structure rather than all forms of randomness or stationarity. Report sequence length, tie prevalence, and calibration method alongside the statistic.

How to use and calibrate

Use Bartels’s exact/approximate rank distribution or permutations; small values indicate smooth positive dependence, large values alternation.

Example application

Detect serial structure in an ordered measurement sequence.

Interpretation and pitfalls

Watch out

A rank-based randomness statistic is not a general stationarity test; ties and short series need care.

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

The input vector is in time/sequence order, not sorted by magnitude.

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<-rnorm(60);print(trend::bartels.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<-rnorm(60);print(trend::bartels.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 209, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT209.