关于「分段常数风险:最大似然、似然比与找切换点」,《直播间留存的生存分析》给出的定量结论是什么?

结论:第四章用分段常数风险把切换尖峰压成两个数,风险比 9.55。

数据口径:书中的直播间、观众数、风险参数、钩子效果与节目单均为示意;全部计算由书中程序生成;本书是非个人化经营建议,钩子只能是真实的内容或写明真实库存与真实截止时刻的福利,不用会重置的倒计时,不诱导未成年人消费,平台规则以官方公示为准。 第一章说明截尾让平均停留偏低:示意直播间里把下播当离开得 408.1 秒,只算已离开者得 278.0 秒,乘积极限估计的限定平均停留是 337.9 秒。

English version

对照数据表

下表数字由本章 Python 程序原样打印,可重新运行复现。 程序只用 Python 标准库与结论吻合的打印值: 9.55

settled exits 1092 over 1126713 viewer-seconds
M0 flat params1 logL -8669.44 AIC 17340.88
M1 two levels params2 logL -8289.01 AIC 16582.01
M2 per piece params 11 logL-8286.27 AIC 16594.55
M1 levels: switch 0.00690 rest 0.00072 per s
hazard ratio 9.5595 % CI 8.38 to 10.89
LR M0 vs M1760.9 on 1 df; LR M1 vs M2: 5.5 on 9 df
M2 switch windows:
1800.00638 900:0.00709 1260:0.00635 1980:0.00725 2700:0.00717

核心公式

\[\ell = \sum_k \left( d_k \log \lambda_k - \lambda_k E_k \right),\]
查看完整验算程序
# Chapter 4. p41: piecewise-constant hazard by maximum likelihood.
# Same illustrative room R, settled viewers, programme clock. With the
# hazard constant inside each piece, the MLE is exits / exposure and the
# log-likelihood is sum(d log lam - lam E) (the Poisson form). Three
# models: M0 one flat rate; M1 two levels (switch windows [s, s + 30)
# versus the rest); M2 one rate per piece of the run sheet (each switch
# window and each stretch between them). Compared by AIC and LR test.
import math
import random

T_END = 3600
SWITCH = [180, 900, 1260, 1980, 2700]   # 环节切换秒数(示意)
SPIKE_LEN, B_SW = 30, 0.012
LAM0 = 1.0 / 600.0
A_OPEN, TAU = 0.12, 6.0
SHAPE = 2.0
SETTLE = 60


def prog_hazard(horizon):
    h = [LAM0] * horizon
    for s in SWITCH:
        for t in range(s, s + SPIKE_LEN):
            h[t] = LAM0 + B_SW
    return h


def open_int(u):
    return A_OPEN * TAU * (math.exp(-u / TAU) - math.exp(-(u + 1) / TAU))


def simulate(seed, horizon=T_END):
    rng = random.Random()
    rng.seed(seed)
    ent = [rng.randrange(0, 60) for _ in range(400)]
    ent += [rng.randrange(0, 3300) for _ in range(2600)]
    ent.sort()
    ph = prog_hazard(horizon)
    rows = []
    for e in ent:
        z = rng.gammavariate(SHAPE, 1.0 / SHAPE)
        need = rng.expovariate(1.0)
        cum, t = 0.0, e
        while t < horizon:
            cum += z * (ph[t] + open_int(t - e))   # step
            t += 1
            if cum >= need:
                break
        if cum >= need:
            rows.append((e, t - e, 1))
        else:
            rows.append((e, horizon - e, 0))
    return rows


def clock_tables(rows, settle):
    pres = [0] * (T_END + 1)
    exits = [0] * T_END
    for e, d, ev in rows:
        start = e + settle
        if start >= e + d:
            continue
        pres[start] += 1
        pres[e + d] -= 1
        if ev:
            exits[e + d - 1] += 1
    run, at = 0, []
    for t in range(T_END):
        run += pres[t]   # step
        at.append(run)
    return at, exits


def loglik(groups):
    # groups: 每组 (退出数, 人秒);返回 (对数似然, 参数个数)
    parts = []
    for d, e in groups:
        lam = d / e
        parts.append(d * math.log(lam) - lam * e if d else 0.0)
    return math.fsum(parts), len(groups)


def pool(at, ex, ranges):
    d = math.fsum(math.fsum(ex[a:b]) for a, b in ranges)
    e = math.fsum(math.fsum(at[a:b]) for a, b in ranges)
    return d, e


at, ex = clock_tables(simulate(20261113), SETTLE)
spikes = [(s, s + SPIKE_LEN) for s in SWITCH]
cuts = [0]
for s in SWITCH:
    cuts += [s, s + SPIKE_LEN]
cuts.append(T_END)
pieces = [(cuts[i], cuts[i + 1]) for i in range(len(cuts) - 1)]
rest = [p for p in pieces if p not in spikes]
g0 = [pool(at, ex, [(0, T_END)])]
g1 = [pool(at, ex, spikes), pool(at, ex, rest)]
g2 = [pool(at, ex, [p]) for p in pieces]
print("settled exits %d over %.0f viewer-seconds"
      % (int(g0[0][0]), g0[0][1]))
res = []
for name, g in (("M0 flat", g0), ("M1 two levels", g1),
                ("M2 per piece", g2)):
    ll, k = loglik(g)
    res.append((ll, k))
    print("%-14s params %2d  logL %10.2f  AIC %10.2f"
          % (name, k, ll, 2 * k - 2 * ll))
(d1, e1), (d0, e0) = g1
ratio = (d1 / e1) / (d0 / e0)
se = math.sqrt(1.0 / d1 + 1.0 / d0)
print("M1 levels: switch %.5f  rest %.5f per s" % (d1 / e1, d0 / e0))
print("hazard ratio %.2f  95 %% CI %.2f to %.2f"
      % (ratio, ratio * math.exp(-1.96 * se), ratio * math.exp(1.96 * se)))
print("LR M0 vs M1: %.1f on 1 df;  LR M1 vs M2: %.1f on %d df"
      % (2 * (res[1][0] - res[0][0]), 2 * (res[2][0] - res[1][0]),
         res[2][1] - res[1][1]))
print("M2 switch windows:")
print("  " + "  ".join(
    "%d:%.5f" % (a, d / e) for (a, b), (d, e) in zip(pieces, g2)
    if (a, b) in spikes))

唯一数据源与引用

范雅福(Fan, Y.). (2026). 《直播间留存卡普兰迈耶生存曲线、划走风险尖峰定位与福利钩子插入秒数母典》第 4 章「分段常数风险:最大似然、似然比与找切换点」(版本 1.0.0). 5D Library. DOI 待发布

DOI 待发布 · 专著页正式发布后上线(本站暂无专著页) · 5DL-B-277 · v1.0.0 · CC-BY-NC-ND-4.0

BibTeX
@book{fan2026277,
  author    = {Fan, Yafu},
  title     = {Kaplan-Meier Retention Curves for Livestream Rooms: Locating Swipe-Away Hazard Spikes and Timing the Insertion of Hooks},
  year      = {2026},
  publisher = {5D Library},
  edition   = {1.0.0},
  note      = {DOI pending}
}

本书其他章节结论