关于「Baum–Welch:从没有标签的点击流里估计参数」,《隐马尔可夫与客户决策阶段》给出的定量结论是什么?

结论:第四章用 Baum–Welch 从无标签点击流估计参数,十个随机起点的解码准确率从 0.3877 到 0.5181。

数据口径:书中阶段、页面类型与全部概率均为示意;全部计算由书中程序生成;本书是非个人化经营建议,只使用站内匿名化与客户同意的数据,不跨站追踪,不推断敏感属性,阶段解码不用于施压、差别定价或主动联系客户,紧迫感只来自真实库存与真实截止。 第一章在示意参数下量出:前向滤波的四分类准确率比最后一次点击高 0.0410,就绪判别从 0.6990 提高到 0.7630;增益随阶段黏性与页面区分力在 0.0307 到 0.1187 之间变化,维特比最后一格在页面模糊时反而低 0.1007。

English version

对照数据表

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

150 visits, 1550 clicks, 10 random starts x 40 iterations
runfinal logLgap to bestrelabeldecode acc
1-2629.7069.82530120.4303
2-2621.7441.86302130.5039
3-2621.4461.56501230.4845
4-2623.2503.36923010.4619
5-2633.10913.22832100.3877
6-2623.6133.73203210.4129
7-2628.4788.59732010.4329
8-2619.8810.00030120.5181
9-2619.9780.09712300.5123
10-2622.6202.73932010.4787
runs within 1 log unit of the best: 2 of 10
decode accuracy across runs: 0.3877 to 0.5181
runs whose state 0..3 already meant BRW..DEC: 1 of 10
查看完整验算程序
# Chapter 4. p42: ten random starts, ten answers.
# Model: 150 visits simulated from the four-stage HMM of p11
# (illustrative). Baum-Welch (scaled, as in p41) is run 40 iterations
# from each of 10 random starts drawn with a fixed seed. For every run:
# final log-likelihood, gap to the best run, the relabelling of fitted
# states that best matches the true stages (searched over all 24
# permutations), and the per-click accuracy of Viterbi decoding with the
# fitted parameters under that relabelling. EM climbs; it does not
# promise the summit.
import itertools
import math
import random

TRUE_PI = [0.70, 0.20, 0.08, 0.02]
TRUE_A = [[0.70, 0.22, 0.06, 0.02],
          [0.08, 0.64, 0.23, 0.05],
          [0.02, 0.10, 0.66, 0.22],
          [0.01, 0.03, 0.16, 0.80]]
TRUE_B = [[0.45, 0.30, 0.08, 0.07, 0.06, 0.04],
          [0.15, 0.35, 0.25, 0.12, 0.08, 0.05],
          [0.08, 0.22, 0.15, 0.30, 0.17, 0.08],
          [0.05, 0.12, 0.08, 0.15, 0.30, 0.30]]
K, M = 4, 6


def draw(rng, probs):
    u, acc = rng.random(), 0.0
    for k, p in enumerate(probs):
        acc += p  # step
        if u < acc:
            return k
    return len(probs) - 1


def rand_row(rng, n):
    w = [rng.random() + 0.2 for _ in range(n)]
    z = math.fsum(w)
    return [x / z for x in w]


def fb(pi, a, b, obs):
    T = len(obs)
    al, cs = [], []
    x = [pi[k] * b[k][obs[0]] for k in range(K)]
    for t in range(T):
        if t:
            x = [math.fsum(al[-1][i] * a[i][j] for i in range(K))
                 * b[j][obs[t]] for j in range(K)]
        c = math.fsum(x)
        cs.append(c)
        al.append([v / c for v in x])
    be = [[1.0] * K]
    for t in range(T - 2, -1, -1):
        nb = be[0]
        be.insert(0, [math.fsum(a[i][j] * b[j][obs[t + 1]] * nb[j]
                                for j in range(K)) / cs[t + 1]
                      for i in range(K)])
    gam = [[al[t][k] * be[t][k] for k in range(K)] for t in range(T)]
    xi = [[[al[t][i] * a[i][j] * b[j][obs[t + 1]] * be[t + 1][j]
            / cs[t + 1] for j in range(K)] for i in range(K)]
          for t in range(T - 1)]
    return math.fsum(math.log(c) for c in cs), gam, xi


def em_step(pi, a, b, data):
    ll, g1 = [], [[] for _ in range(K)]
    na = [[[] for _ in range(K)] for _ in range(K)]
    da = [[] for _ in range(K)]
    nb = [[[] for _ in range(M)] for _ in range(K)]
    db = [[] for _ in range(K)]
    for obs in data:
        lv, gam, xi = fb(pi, a, b, obs)
        ll.append(lv)
        for k in range(K):
            g1[k].append(gam[0][k])
        for t, o in enumerate(obs):
            for k in range(K):
                nb[k][o].append(gam[t][k])
                db[k].append(gam[t][k])
                if t < len(obs) - 1:
                    da[k].append(gam[t][k])
                    for j in range(K):
                        na[k][j].append(xi[t][k][j])
    n = len(data)
    pi = [math.fsum(g1[k]) / n for k in range(K)]
    a = [[math.fsum(na[i][j]) / math.fsum(da[i]) for j in range(K)]
         for i in range(K)]
    b = [[math.fsum(nb[k][m]) / math.fsum(db[k]) for m in range(M)]
         for k in range(K)]
    return pi, a, b, math.fsum(ll)


def viterbi(pi, a, b, obs):
    d = [math.log(pi[k] + 1e-300) + math.log(b[k][obs[0]] + 1e-300)
         for k in range(K)]
    back = []
    for o in obs[1:]:
        ptr = [max(range(K), key=lambda i: d[i] + math.log(a[i][j]
                                                            + 1e-300))
               for j in range(K)]
        d = [d[ptr[j]] + math.log(a[ptr[j]][j] + 1e-300)
             + math.log(b[j][o] + 1e-300) for j in range(K)]
        back.append(ptr)
    path = [max(range(K), key=lambda k: d[k])]
    for ptr in reversed(back):
        path.append(ptr[path[-1]])
    return path[::-1]


rng = random.Random(20261113)
data, truth = [], []
for _ in range(150):
    s = [draw(rng, TRUE_PI)]
    for _ in range(rng.randint(6, 14) - 1):
        s.append(draw(rng, TRUE_A[s[-1]]))
    truth.append(s)
    data.append([draw(rng, TRUE_B[k]) for k in s])
n_clicks = len([1 for d in data for _ in d])
runs = []
for r in range(10):
    pi = rand_row(rng, K)
    a = [rand_row(rng, K) for _ in range(K)]
    b = [rand_row(rng, M) for _ in range(K)]
    for _ in range(40):
        pi, a, b, ll = em_step(pi, a, b, data)
    ll = em_step(pi, a, b, data)[3]
    paths = [viterbi(pi, a, b, o) for o in data]
    best_perm, best_hit = None, -1
    for perm in itertools.permutations(range(K)):
        hit = len([1 for p, s in zip(paths, truth)
                   for x, y in zip(p, s) if perm[x] == y])
        if hit > best_hit:
            best_perm, best_hit = perm, hit
    runs.append((ll, best_perm, best_hit / n_clicks))
top = max(x[0] for x in runs)
print("150 visits, %d clicks, 10 random starts x 40 iterations"
      % n_clicks)
print("run   final logL   gap to best   relabel   decode acc")
for r, (ll, perm, acc) in enumerate(runs, 1):
    print("%3d  %11.3f  %12.3f   %s      %.4f"
          % (r, ll, top - ll, "".join(str(p) for p in perm), acc))
near = len([1 for x in runs if top - x[0] < 1.0])
accs = sorted(x[2] for x in runs)
print("runs within 1 log unit of the best: %d of 10" % near)
print("decode accuracy across runs: %.4f to %.4f" % (accs[0], accs[-1]))
ident = len([1 for x in runs if x[1] == (0, 1, 2, 3)])
print("runs whose state 0..3 already meant BRW..DEC: %d of 10" % ident)

唯一数据源与引用

范雅福(Fan, Y.). (2026). 《隐马尔可夫维特比解码、浏览点击序列反推客户隐藏决策阶段与出手时机母典》第 4 章「Baum–Welch:从没有标签的点击流里估计参数」(版本 1.0.0). 5D Library. DOI 待发布

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

BibTeX
@book{fan2026300,
  author    = {Fan, Yafu},
  title     = {Hidden Markov Models and Viterbi Decoding: Inferring a Customer's Hidden Decision Stage from Click Sequences and Timing the Offer},
  year      = {2026},
  publisher = {5D Library},
  edition   = {1.0.0},
  note      = {DOI pending}
}

本书其他章节结论