关于「McCrary 密度检验:直方图两侧的局部线性、检验力与盲区」,《会员门槛的断点回归》给出的定量结论是什么?
结论:第四章手写 McCrary 式密度检验,凑单数据上 z 值达到 24.14,并演示它看不见的对调式排序,此时前定协变量检验的 z 值为 13.14。
数据口径:落笔前本书核对原文并作蓝图修正:1960 年的论文研究的是奖学金荣誉证书而非会员体系,2008 年的指南是方法论文,二者都没有给出任何会员权益的效应量,书中的高估倍数全部来自固定种子的示意数据;命题也必须带边界陈述:只识别门槛处的局部效应,客人能精确凑单时设计失效,估计对带宽敏感,不宜用全局高阶多项式。 书中参数为示意;全部计算由书中程序生成。
对照数据表
下表数字由本章 Python 程序原样打印,可重新运行复现。 程序只用 Python 标准库与结论吻合的打印值: 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 |
查看完整验算程序
# 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]))唯一数据源与引用
范雅福(Fan, Y.). (2026). 《断点回归设计与会员等级门槛、消费满额升级线两侧的局部因果识别母典》第 4 章「McCrary 密度检验:直方图两侧的局部线性、检验力与盲区」(版本 1.0.0). 5D Library. DOI 待发布
DOI 待发布 · 专著页正式发布后上线(本站暂无专著页) · 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}
}