What does Survival Analysis for Livestream Retention conclude, in numbers, about “Piecewise-Constant Hazards: Maximum Likelihood, Likelihood Ratios and Finding the Switches”?

Answer: Chapter 4 compresses the switch spikes into two numbers with a piecewise-constant hazard, a hazard ratio of 9.55.

Scope of the numbers: Its rooms, audiences, hazard parameters, hook effects and run sheets are illustrative; all calculations are generated by its programs; the book is not individual business advice. Marked boxes separate published evidence, runnable Python calculations (18 programs), classical sources and the author's hypotheses.

中文版

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: 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

Key formula

\[\ell = \sum_k \left( d_k \log \lambda_k - \lambda_k E_k \right),\]
Show the full program
# 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))

Single source and citation

Fan, Y. (2026). Kaplan-Meier Retention Curves for Livestream Rooms: Locating Swipe-Away Hazard Spikes and Timing the Insertion of Hooks (Version 1.0.0), Chapter 4: Piecewise-Constant Hazards: Maximum Likelihood, Likelihood Ratios and Finding the Switches. 5D Library. DOI pending

DOI pending · monograph page goes live after publication · 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}
}

Other chapter answers in this book