What does Regression Discontinuity at Membership Tiers conclude, in numbers, about “The McCrary Density Test: Local Lines on Either Side of a Histogram, Power and Blind Spots”?
Answer: Chapter 4 builds a McCrary-style density test by hand, whose z reaches 24.14 on data with top-ups, and shows the swapping kind of sorting it cannot see, where the predetermined covariate test gives z of 13.14.
Scope of the numbers: Parameters are illustrative; all calculations are generated by the book's programs. This is general analysis, not individualised business advice, and the book neither recommends nor supplies misleading threshold copy designed to provoke top-ups.
Data table
The numbers below are printed verbatim by the chapter's Python program and can be reproduced by running it. standard-library Python onlyprinted values that match the answer: 24.14
| data | f-(c) | f+(c) | theta | se | z |
|---|---|---|---|---|---|
| clean | 0.02225 | 0.02131 | -0.0432 | 0.0525 | -0.82 |
| top-up 0.3 0.00994 | 0.04326 | 1.4708 | 0.0609 | 24.14 | |
| customers in [c, c + 1.5): clean 615, top-up 1389 |
Show the full program
# Chapter 4. p41: a McCrary-style density test at the VIP line.
# Steps of McCrary (2008): a fine histogram of the running variable
# (bin width 0.5 units, bins never straddle the line), heights
# f_j = count / (n * bin), then a local linear fit with a triangular
# kernel (h = 8) to the bin heights on each side. theta = ln f+ - ln f-
# and its approximate standard error
# se = sqrt( (1 / (n h)) * (24 / 5) * (1 / f+ + 1 / f-) ).
# Two illustrative data sets from seed 20261141, 20000 customers: clean,
# and with top-ups: a customer within 4 units below c = 60 tops up to
# land in [c, c + 1.5) with probability min(1, 2 P / (1 + exp(-2 u))),
# P = 0.3, so engaged customers top up more often.
import math
import random
C, N, BIN, H = 60.0, 20000, 0.5, 8.0
def draw(rng, n, topup=0.0, land=1.5, c=C):
rows = []
for _ in range(n):
u = rng.gauss(0.0, 1.0)
x = 50.0 + 12.0 * u + 8.0 * rng.gauss(0.0, 1.0)
q = min(1.0, 2.0 * topup / (1.0 + math.exp(-2.0 * u)))
r = rng.random()
if c - 4.0 <= x < c and r < q:
x = c + rng.uniform(0.0, land)
d = 1 if x >= c else 0
g = 0.15 * (x - c) + 0.0003 * (x - c) ** 3
y = 20.0 + 6.0 * u + g + (2.0 + u) * d + rng.gauss(0.0, 3.0)
rows.append((x, y, u))
return rows
def intercept(pts, c, h):
xs, ys, ws = [], [], []
for x, y in pts:
t = abs(x - c) / h
if t < 1.0:
xs.append(x - c)
ys.append(y)
ws.append(1.0 - t)
s0 = math.fsum(ws)
s1 = math.fsum(w * x for w, x in zip(ws, xs))
s2 = math.fsum(w * x * x for w, x in zip(ws, xs))
t0 = math.fsum(w * y for w, y in zip(ws, ys))
t1 = math.fsum(w * x * y for w, x, y in zip(ws, xs, ys))
return (s2 * t0 - s1 * t1) / (s0 * s2 - s1 * s1)
def density_test(xs, c, b, h):
n = len(xs)
k = int(math.ceil(3 * h / b))
cnt = {j: 0 for j in range(-k, k)}
for x in xs:
j = int(math.floor((x - c) / b))
if -k <= j < k:
cnt[j] = cnt[j] + 1
left = [(c + (j + 0.5) * b, cnt[j] / (n * b)) for j in range(-k, 0)]
right = [(c + (j + 0.5) * b, cnt[j] / (n * b)) for j in range(0, k)]
fm, fp = intercept(left, c, h), intercept(right, c, h)
theta = math.log(fp) - math.log(fm)
se = math.sqrt(24.0 / 5.0 / (n * h) * (1.0 / fp + 1.0 / fm))
return fm, fp, theta, se
if __name__ == "__main__":
print("data f-(c) f+(c) theta se z")
band = []
for name, p in (("clean", 0.0), ("top-up 0.3", 0.3)):
rows = draw(random.Random(20261141), N, topup=p)
fm, fp, th, se = density_test([r[0] for r in rows], C, BIN, H)
print("{:10s} {:7.5f} {:7.5f} {:7.4f} {:6.4f} {:6.2f}".format(
name, fm, fp, th, se, th / se))
band.append(len([r for r in rows if C <= r[0] < C + 1.5]))
print("customers in [c, c + 1.5): clean {}, top-up {}".format(
band[0], band[1]))Single source and citation
Fan, Y. (2026). Regression-Discontinuity Designs at Membership-Tier Thresholds: Local Causal Identification on Either Side of a Spend-Based Upgrade Line (Version 1.0.0), Chapter 4: The McCrary Density Test: Local Lines on Either Side of a Histogram, Power and Blind Spots. 5D Library. DOI pending
DOI pending · monograph page goes live after publication · 5DL-B-303 · v1.0.0 · CC-BY-NC-ND-4.0
BibTeX
@book{fan2026303,
author = {Fan, Yafu},
title = {Regression-Discontinuity Designs at Membership-Tier Thresholds: Local Causal Identification on Either Side of a Spend-Based Upgrade Line},
year = {2026},
publisher = {5D Library},
edition = {1.0.0},
note = {DOI pending}
}Other chapter answers in this book
- Chapter 1 · Either Side of the VIP Line: Selection Bias, the Naive Gap and a Local Comparison at the Threshold
- Chapter 2 · Local Linear Regression: Triangular-Kernel Weighted Least Squares, Boundary Bias and an Effect That Holds Only at the Threshold
- Chapter 3 · Choosing the Bandwidth: Trading Bias for Variance, a Monte Carlo Optimum and Boundary Cross-Validation
- Chapter 5 · Topping Up to Upgrade: How Precisely Manipulable Spending Breaks the Design
- Chapter 6 · Three Robustness Checks: High-Order Polynomials, Placebo Thresholds and the Fuzzy Design
- Chapter 7 · Sources East and West: Rank for Count in the Book of Lord Shang and the Scholarship Discontinuity of 1960
- Chapter 8 · Closing the Loop: Evaluating a Membership-Tier Threshold, the Distilled Tool and a Pre-Registered Plan