ADescribe · distribution.modality
Modality (Hartigan's dip test)
Does this variable have one peak, or more than one?
Hartigan's dip — the largest distance between the data's ECDF and the closest unimodal distribution — with a Monte-Carlo p-value from uniform samples of the same size (shared seeded stream, same in Python and R), plus Sarle's bimodality coefficient from skewness and kurtosis as a screening heuristic.
A real result
Example “Faithful eruptions”, recorded in both engines. In the app, “Try this example” loads it into Analyze.
Hartigan's dip test, D = 0.092, p = .002
- Dip D
- 0.0924
- p
- 0.0020
- Bimodality coefficient
- 0.7692
Plots
View data
| eruptions |
|---|
| 3.600 |
| 1.800 |
| 3.333 |
| 2.283 |
| 4.533 |
| 2.883 |
| 4.700 |
| 3.600 |
| 1.950 |
| 4.350 |
| 1.833 |
| 3.917 |
| 4.200 |
| 1.750 |
| 4.700 |
| 2.167 |
| 1.750 |
| 4.800 |
| 1.600 |
| 4.250 |
| 1.800 |
| 1.750 |
| 3.450 |
| 3.067 |
| 4.533 |
| 3.600 |
| 1.967 |
| 4.083 |
| 3.850 |
| 4.433 |
| 4.300 |
| 4.467 |
| 3.367 |
| 4.033 |
| 3.833 |
| 2.017 |
| 1.867 |
| 4.833 |
| 1.833 |
| 4.783 |
| 4.350 |
| 1.883 |
| 4.567 |
| 1.750 |
| 4.533 |
| 3.317 |
| 3.833 |
| 2.100 |
| 4.633 |
| 2 |
| 4.800 |
| 4.716 |
| 1.833 |
| 4.833 |
| 1.733 |
| 4.883 |
| 3.717 |
| 1.667 |
| 4.567 |
| 4.317 |
First 60 of 272 rows.
Assumptions
- Continuous values — The dip assumes a continuous variable; many ties (rounding, counts) make modes hard to judge.
Modality
| Measure | Value | Reading |
|---|---|---|
| Hartigan's dip D | 0.0924 | more than one mode (p < .05) |
| p (uniform null) | 0.0020 | 499 simulations |
| Sarle's bimodality coefficient | 0.7692 | above 5/9: possibly bimodal |
| Skewness (G1) | −0.4182 | |
| Excess kurtosis (G2) | −1.506 |
- The dip is the largest distance between the data's ECDF and the closest unimodal distribution; its p-value compares it with uniform samples of the same size (the least favorable unimodal case, so the test is conservative).
- Sarle's coefficient is a screening heuristic from skewness and kurtosis, not a test; heavy skew alone can push it above 5/9.
- 146 tied value(s): ties are treated as infinitesimally separated (as diptest does).
Hartigan's dip test, D = 0.092, p = .002
- Dip D
- 0.0924
- p
- 0.0020
- Bimodality coefficient
- 0.7692
To keep this page light, the plots are shown once, under the R tab.
Assumptions
- Continuous values — The dip assumes a continuous variable; many ties (rounding, counts) make modes hard to judge.
Modality
| Measure | Value | Reading |
|---|---|---|
| Hartigan's dip D | 0.0924 | more than one mode (p < .05) |
| p (uniform null) | 0.0020 | 499 simulations |
| Sarle's bimodality coefficient | 0.7692 | above 5/9: possibly bimodal |
| Skewness (G1) | −0.4182 | |
| Excess kurtosis (G2) | −1.506 |
- The dip is the largest distance between the data's ECDF and the closest unimodal distribution; its p-value compares it with uniform samples of the same size (the least favorable unimodal case, so the test is conservative).
- Sarle's coefficient is a screening heuristic from skewness and kurtosis, not a test; heavy skew alone can push it above 5/9.
- 146 tied value(s): ties are treated as infinitesimally separated (as diptest does).
Do it in CodeSplash Run it on your own data, in Python or R, with the code to prove it.
Worked examples in the app
- Faithful eruptionsh_faithful · outcome: eruptions
- Normalish unimodalh_fit · outcome: normalish
- Rounded tiesh_shift · outcome: value
- Degenerate constanth_faithful · outcome: eruptions
- Degenerate n3h_faithful · outcome: eruptions
In the guides
Tests from the statistics guide this method runs
Where it sits in the decision guides
- Assess distribution: histogram, Q–Q · Statistics Guide · A — Describe data
- EDA (correlation, distributions) · DS & ML Decision Guide · Describe — summarize and visualize
What you need
- Variablenumeric · 1 column
- The numeric variable to check for more than one peak.
Choices you make
Advanced
- Uniform samplesdefault 1999
Simulated uniform samples for the dip's p-value (shared seeded stream, same in Python and R).
Checks and remedies
CodeSplash checks each testable assumption on every run and shows the result on the card; premises the data can’t test are marked. If a check fails, these are the ways forward.
Continuous values
The dip assumes a continuous variable; with many ties (rounding, counts) modes are hard to judge.
What it reports
- Tests: One mode (unimodal)
- n
- How many independent units the analysis used; it sets precision and power.
- dip
- Tests whether a distribution has a single peak; a small p-value suggests two or more groups mixed together.
- p
- How surprising your data would be if the null hypothesis were true, as a probability.
- bc
- A skew and kurtosis summary that rises when values form two humps. Above about 0.555 hints at more than one mode.
- bc_flag
- A skew and kurtosis summary that rises when values form two humps. Above about 0.555 hints at more than one mode.
- skewness
- How lopsided a distribution is; positive skew has a long right tail.
- kurtosis
- How heavy a distribution's tails are compared with a normal; positive excess means more extreme values.
- reps
- How many samples were simulated for a p-value. More gives a steadier p-value; it does not change what is tested.
- ties
- Equal values; rank tests average their ranks and need tie corrections, and exact p-values become approximate.
Python and R
Python and R agree to 6 decimals (relative tolerance 1e-10). Python uses pandas, numpy, codesplash; R uses stats, nanoparquet, codesplashr.
Default traps between the two languages
- The dip is written from Hartigan & Hartigan (1985) in both engines (codesplash.stats.distribution ↔ codesplashr) and checked against diptest (AS 217) on 1,000+ samples (equal without ties; within 1e-8 with ties, which both treat as infinitesimally separated); the guide's validated value D = 0.032661 is reproduced exactly.
- The p-value simulates uniform samples from the shared Wichmann–Hill stream (diptest interpolates its own table or simulates with R's RNG, so its p differs by Monte-Carlo error).
The exact script CodeSplash ran for example “Faithful eruptions” · Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Modality (Hartigan's dip test)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method distribution.modality 1.0.1 · spec a3a1987b3f540f5d
# Dataset: h_faithful (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import math
import numpy as np
import pandas as pd
import codesplash as cs
from codesplash.stats import distribution as csd
# %% Data
df = pd.read_parquet("tests/parity/fixtures/h_faithful.parquet", columns=["eruptions"])
n_input = len(df)
df = df.dropna(subset=["eruptions"]) # listwise deletion over the analysis columns
n_dropped = n_input - len(df)
warnings = []
if n_dropped:
warnings.append(cs.cs_warning("CS_W_ROWS_DROPPED", dropped=n_dropped, total=n_input))
if n_input and not len(df):
cs.cs_error("CS_E_NO_ROWS")
cs.check_finite_columns(df, ["eruptions"]) # infinite values stop the run (registry/errors.yaml)
# %% Test
y = "eruptions"
x = df[y].to_numpy(dtype=float)
x = x[np.isfinite(x)]
n = x.size
seed, reps = 42, int(499) # uniform null samples from the shared Wichmann–Hill stream
alpha = 0.05
if n < 4:
cs.cs_error("CS_E_TOO_FEW_ROWS", what="The dip test", min=4, n=n)
if len(np.unique(x)) < 2: # one value: no distribution shape to judge, and skewness and kurtosis are 0 / 0
cs.cs_error("CS_E_CONSTANT", column=y)
d, p = csd.dip_test(x, reps=reps, seed=seed)
bc, g1, g2 = csd.bimodality_coefficient(x)
bc_flag = bool(np.isfinite(bc) and bc > 5 / 9)
ties = int(n - np.unique(x).size)
# %% Result
values = {"n": n, "dip": d, "p": p, "bc": bc, "bc_flag": bc_flag, "skewness": g1, "kurtosis": g2, "reps": reps, "ties": ties}
multi = p < alpha
summary = (f"Hartigan's dip for {y}: D = {d:.4f}, {cs.fmt_p(p)} ({reps} uniform samples, seed {seed}) — "
+ ("evidence of more than one mode." if multi else "no evidence against a single mode.")
+ (f" Sarle's bimodality coefficient is {bc:.3f}, {'above' if bc_flag else 'below'} the 5/9 ≈ 0.556 benchmark." if np.isfinite(bc) else ""))
notes = ["The dip is the largest distance between the data's ECDF and the closest unimodal distribution; its p-value compares it with uniform samples of the same size (the least favorable unimodal case, so the test is conservative).",
"Sarle's coefficient is a screening heuristic from skewness and kurtosis, not a test; heavy skew alone can push it above 5/9."]
if ties:
notes.append(f"{ties} tied value(s): ties are treated as infinitesimally separated (as diptest does).")
cs.publish({
"method": "distribution.modality",
"methodVersion": "1.0.1",
"title": "Modality — Hartigan's dip test",
"engine": cs.engine_info("pandas", "numpy"),
"n": n,
"summary": summary,
"apa": f"Hartigan's dip test, D = {d:.3f}, {cs.fmt_p(p)}",
"statistics": [{"key": "dip", "label": "Dip D", "value": d}, {"key": "p", "label": "p", "value": p}, {"key": "bc", "label": "Bimodality coefficient", "value": bc}],
"tables": [{"key": "modality", "title": "Modality", "columns": [
{"key": "measure", "label": "Measure", "type": "string"}, {"key": "value", "label": "Value", "type": "number"},
{"key": "reading", "label": "Reading", "type": "string"}], "rows": [
{"measure": "Hartigan's dip D", "value": d, "reading": "more than one mode (p < .05)" if multi else "consistent with one mode"},
{"measure": "p (uniform null)", "value": p, "reading": f"{reps} simulations"},
{"measure": "Sarle's bimodality coefficient", "value": bc, "reading": "above 5/9: possibly bimodal" if bc_flag else "below 5/9"},
{"measure": "Skewness (G1)", "value": g1, "reading": ""}, {"measure": "Excess kurtosis (G2)", "value": g2, "reading": ""}]}],
"effectSizes": [],
"assumptions": [{"key": "continuous", "label": "Continuous values", "status": "warn" if ties > n // 4 else "info",
"detail": "The dip assumes a continuous variable; many ties (rounding, counts) make modes hard to judge."}],
"warnings": warnings,
"notes": notes,
"charts": [cs.chart("hist", "histogram", f"Distribution of {y}", {"x": cs.col(y, x)}, controls={"kde": True})],
"values": values,
})
The exact script CodeSplash ran for example “Faithful eruptions” · R version 4.5.1 (2025-06-13)
# Modality (Hartigan's dip test)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method distribution.modality 1.0.1 · spec a3a1987b3f540f5d
# Dataset: h_faithful (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr)
# %% Data
df <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/h_faithful.parquet", col_select = c("eruptions")))
n_input <- nrow(df)
df <- df[stats::complete.cases(df[c("eruptions")]), , drop = FALSE] # listwise deletion
df[] <- lapply(df, function(x) if (is.factor(x)) droplevels(x) else x)
n_dropped <- n_input - nrow(df)
warnings <- list()
if (n_dropped > 0) {
warnings <- c(warnings, cs_warning("CS_W_ROWS_DROPPED", dropped = n_dropped, total = n_input))
}
if (n_input > 0 && nrow(df) == 0) cs_error("CS_E_NO_ROWS")
cs_check_finite_columns(df, c("eruptions")) # infinite values stop the run (registry/errors.yaml)
# %% Test
y <- "eruptions"
x <- as.numeric(df[[y]])
x <- x[is.finite(x)]
n <- length(x)
seed <- 42 # uniform null samples from the shared Wichmann-Hill stream
reps <- as.integer(499)
alpha <- 0.05
if (n < 4) cs_error("CS_E_TOO_FEW_ROWS", what = "The dip test", min = 4, n = n)
if (length(unique(x)) < 2) cs_error("CS_E_CONSTANT", column = y) # one value: no distribution shape to judge, and skewness and kurtosis are 0 / 0
dt <- cs_dip_test(x, reps = reps, seed = seed)
d <- dt[1]
p <- dt[2]
bcv <- cs_bimodality_coefficient(x)
bc <- bcv[1]
bc_flag <- is.finite(bc) && bc > 5 / 9
ties <- n - length(unique(x))
# %% Result
values <- list(n = n, dip = d, p = p, bc = bc, bc_flag = bc_flag, skewness = bcv[2], kurtosis = bcv[3], reps = reps, ties = ties)
multi <- p < alpha
summary <- paste0(sprintf("Hartigan's dip for %s: D = %.4f, %s (%d uniform samples, seed %d) — ", y, d, cs_fmt_p(p), reps, as.integer(seed)),
if (multi) "evidence of more than one mode." else "no evidence against a single mode.",
if (is.finite(bc)) sprintf(" Sarle's bimodality coefficient is %.3f, %s the 5/9 ≈ 0.556 benchmark.", bc, if (bc_flag) "above" else "below") else "")
notes <- list("The dip is the largest distance between the data's ECDF and the closest unimodal distribution; its p-value compares it with uniform samples of the same size (the least favorable unimodal case, so the test is conservative).",
"Sarle's coefficient is a screening heuristic from skewness and kurtosis, not a test; heavy skew alone can push it above 5/9.")
if (ties > 0) notes[[length(notes) + 1]] <- sprintf("%d tied value(s): ties are treated as infinitesimally separated (as diptest does).", as.integer(ties))
cs_publish(list(
method = "distribution.modality",
methodVersion = "1.0.1",
title = "Modality — Hartigan's dip test",
engine = cs_engine_info("stats", "nanoparquet"),
n = n,
summary = summary,
apa = sprintf("Hartigan's dip test, D = %.3f, %s", d, cs_fmt_p(p)),
statistics = list(list(key = "dip", label = "Dip D", value = d), list(key = "p", label = "p", value = p), list(key = "bc", label = "Bimodality coefficient", value = bc)),
tables = list(list(key = "modality", title = "Modality", columns = list(
list(key = "measure", label = "Measure", type = "string"), list(key = "value", label = "Value", type = "number"),
list(key = "reading", label = "Reading", type = "string")), rows = list(
list(measure = "Hartigan's dip D", value = d, reading = if (multi) "more than one mode (p < .05)" else "consistent with one mode"),
list(measure = "p (uniform null)", value = p, reading = sprintf("%d simulations", reps)),
list(measure = "Sarle's bimodality coefficient", value = bc, reading = if (bc_flag) "above 5/9: possibly bimodal" else "below 5/9"),
list(measure = "Skewness (G1)", value = bcv[2], reading = ""), list(measure = "Excess kurtosis (G2)", value = bcv[3], reading = "")))),
effectSizes = list(),
assumptions = list(list(key = "continuous", label = "Continuous values", status = if (ties > n %/% 4) "warn" else "info",
detail = "The dip assumes a continuous variable; many ties (rounding, counts) make modes hard to judge.")),
warnings = warnings,
notes = notes,
charts = list(cs_chart("hist", "histogram", sprintf("Distribution of %s", y), list(x = cs_col(y, x)), controls = list(kde = TRUE))),
values = values
))
Sources
- Hartigan, J. A., & Hartigan, P. M. (1985). The dip test of unimodality. Annals of Statistics, 13, 70–84.
- Hartigan, P. M. (1985). Algorithm AS 217: Computation of the dip statistic to test for unimodality. Applied Statistics, 34, 320–325.
- Pfister, R., Schwarz, K. A., Janczyk, M., Dale, R., & Freeman, J. B. (2013). Good things peak in pairs: a note on the bimodality coefficient. Frontiers in Psychology, 4, 700.
From the CodeSplash method registry · distribution.modality v1.0.1.