Skip to content

EProportions · Guide test 298

Tajima D neutrality test

Compares two estimators of genetic variation to test a neutral population model; demography also affects the result.

Runs in CodeSplash (Experimental)Python example validatedR example validated

Do it in CodeSplash

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

In Neutrality test (Tajima's D), choose Reference distribution = Coalescent simulation or choose Reference distribution = Tajima's beta distribution.

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 · Tajima's D neutrality test

12
8.0

Scaled mutation rate 4Nμ for the region: about θ × (1 + ½ + …) segregating sites are expected.

4.0

Exponential growth looking back in time: coalescences crowd near the root, rare variants pile up and D turns negative. 0 is the standard neutral model.

0.050

Alternative

Your data

A simulated genealogy with mutations
past ↑ (coalescent time)θ_π = 3.64 · θ_W = 3.31

Left: the sample's genealogy; ticks are mutations, those on tips are singletons. Right: how many sites carry k derived copies, against the neutral expectation (dashed). D compares θ_π (pulled up by intermediate-frequency sites) with θ_W (every site counts once): too many singletons make D negative.

If H₀ were true

D = 0.40
  1. The reference: how D varies across samples when H₀ is true, 1000 neutral genealogies with 10 segregating sites (Hudson's fixed-S method) (neutral coalescent simulated with the observed number of segregating sites; two-sided p is twice the smaller tail).
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .591).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

D=θ^π−θ^We1S+e2S(S−1),θ^W=Sa1,a1=∑i=1n−11iD=\frac{\hat\theta_\pi-\hat\theta_W}{\sqrt{e_1S+e_2S(S-1)}},\qquad \hat\theta_W=\frac{S}{a_1},\quad a_1=\sum_{i=1}^{n-1}\frac1i

With your numbers

θ^π=3.64, θ^W=3.31 (S=10, n=12) ⇒ D=0.40\hat\theta_\pi = 3.64,\ \hat\theta_W = 3.31\ (S = 10,\ n = 12)\ \Rightarrow\ D = 0.40
θ^π\hat\theta_\pi
average number of differences between two sequences
θ^W\hat\theta_W
Watterson's estimate from the number of segregating sites
e1, e2e_1,\ e_2
Tajima's constants that scale the variance of the difference

The verdict

D = 0.404, p = .591

p > α = .050: compatible with H₀

Negative D means an excess of rare variants (growth, a sweep, purifying selection); positive D an excess of intermediate ones (balancing selection, a bottleneck, structure). Demography and selection give the same signal, so D alone cannot separate them. Research use only, not a diagnostic or clinical result.

Statistic
0.404
p-value
0.5914
Tajima's D neutrality test on 12 sequences with 10 segregating sites: D = 0.404, p = .591. The p-value is above α = .050, so the data are compatible with H0.

Tajima's D neutrality test on 12 sequences with 10 segregating sites: D = 0.404, p = .591. The p-value is above α = .050, so the data are compatible with H₀.

Question and hypotheses

H0: neutral evolution under the specified standard population model; H1: a departure affecting the allele-frequency spectrum.

Assumptions

A sample of homologous sequences; standard neutral coalescent assumptions including the chosen demographic/recombination model, typically constant-size random-mating population and infinite-sites mutation approximation.

Formula

D=π^−S/a1e1S+e2S(S−1)D=\frac{\widehat\pi-S/a_1}{\sqrt{e_1S+e_2S(S-1)}}

Parameters and notation

n sequences,S segregating sites,π̂ average pairwise differences; a1=Σi=1..n−1 1/i,a2=Σ1/i²; b1=(n+1)/[3(n−1)],b2=2(n²+n+3)/[9n(n−1)]; c1=b1−1/a1,c2=b2−(n+2)/(na1)+a2/a1²; e1=c1/a1,e2=c2/(a1²+a2).

Structure of the method

Compute average pairwise sequence differences and the segregating-sites estimator, subtract them and standardize using sample-size-dependent covariance constants. Tajima D summarizes allele-frequency-spectrum imbalance; a p value requires a neutral demographic/mutation/recombination reference rather than automatic standard-normal calibration.

Estimated and nuisance parameters

Data-derived quantities are segregating sites and mean pairwise differences. The constants a1,a2,e1,e2 are deterministic functions of sequence count. Demography, recombination and mutation assumptions determine the null model; they are not all identified by the single D statistic.

Settings and design choices

Prespecify loci/windows, eligible sites, missing-data treatment, sequence count and whether variation is measured per locus or per site consistently. Select neutral-model simulation parameters from justified information and account for scanning many overlapping windows. The example computes D only and does not claim a calibrated p value.

Fitting and adaptation

Adaptation means constructing a defensible neutral reference and simulating the same sampling/ascertainment process. Recompute the statistic in every replicate, including missing-site filters. A fitted demographic null introduces uncertainty that should be examined rather than presenting one convenient population-history assumption as known.

Sensitivity and robustness

When no sites segregate, the standardized denominator is zero and D is undefined. Growth, bottlenecks, structure, linked selection and sequencing error can produce similar values. Correlated windows are not independent tests. Report the statistic, site/sequence counts and simulation assumptions, avoiding a uniquely selective interpretation from sign alone.

How to use and calibrate

Use neutral-model simulations or appropriate reference tables, with the locus length and demographic/recombination assumptions specified.

Example application

Assess an excess of rare variants (negative D) or intermediate-frequency variants (positive D).

Interpretation and pitfalls

Watch out

D is not generally standard normal. Population growth, structure and selection can produce similar signals.

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

Derived/alternate allele counts at eight segregating biallelic sites among 12 sampled sequences. Computes Tajima D only; p-values require an explicit neutral demographic/recombination model or appropriate reference tables, not a standard normal shortcut.

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)
n=12;counts=np.array([1,2,3,4,2,1,5,6]);S=len(counts)
pi=np.sum(2*counts*(n-counts)/(n*(n-1)));j=np.arange(1,n);a1=np.sum(1/j);a2=np.sum(1/j**2);b1=(n+1)/(3*(n-1));b2=2*(n*n+n+3)/(9*n*(n-1));c1=b1-1/a1;c2=b2-(n+2)/(a1*n)+a2/a1**2;e1=c1/a1;e2=c2/(a1*a1+a2)
D=(pi-S/a1)/np.sqrt(e1*S+e2*S*(S-1));print(D,S,pi)
Output
0.3918923023128549 8 2.909090909090909
R

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

set.seed(2026)
n<-12;S<-8;counts<-c(1,2,3,4,2,1,5,6)
pi<-sum(2*counts*(n-counts)/(n*(n-1)));a1<-sum(1/(1:(n-1)));a2<-sum(1/(1:(n-1))^2);b1<-(n+1)/(3*(n-1));b2<-2*(n^2+n+3)/(9*n*(n-1));c1<-b1-1/a1;c2<-b2-(n+2)/(a1*n)+a2/a1^2;e1<-c1/a1;e2<-c2/(a1^2+a2)
D<-(pi-S/a1)/sqrt(e1*S+e2*S*(S-1));print(c(Tajima_D=D,segregating_sites=S,pairwise_differences=pi))
Output
            Tajima_D    segregating_sites pairwise_differences 
           0.3918923            8.0000000            2.9090909

Sources

  • Tajima (1989) — Tajima (1989) · Statistical method for testing the neutral mutation hypothesis by DNA polymorphismdoi.org
  • 123.3.585doi.org
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 298, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT298.