关于「最后一次点击的错觉:整条序列与单点判别的准确率」,《隐马尔可夫与客户决策阶段》给出的定量结论是什么?

结论:只看最后一次点击判断客户阶段,在示意参数下四分类错误率是 0.5180,就绪判别准确率 0.6990。用整条序列的前向滤波,四分类准确率提高 0.0410,就绪判别提高到 0.7630;提高的幅度取决于阶段黏性与页面区分力,在 p12 的扫描里从 0.0307 到 0.1187。维特比路径的最后一格并不总是更好,页面模糊时反而低 0.1007。拉宾纳的教程区分了「当前最可能的状态」与「最可能的整条路径」两个问题,本书接下来两章分别展开:第二章讲维特比怎样在格子上找出最可能的路径,以及为什么必须在对数域里算;第三章讲前向后向算法,以及为什么回复访客之前「再等两次点击」往往值得。

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

English version

对照数据表

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

4000 simulated visits, stage at the last click
rule4-stage accerrorready/not acc
last0.48200.51800.6990
viterbi0.47800.52200.7282
forward0.52300.47700.7630
forward minus last: 0.0410(95% CI 0.0267 to 0.0553)
error cut by the full sequence: 0.079 of last-click error
查看完整验算程序
# Chapter 1. p11: last click against the whole sequence, current stage.
# Model: a four-stage hidden Markov model of one anonymised site visit
# (browse, research, compare, decide) emitting six page types. 4,000
# visits of 6 to 14 clicks are simulated with a fixed seed; the stage at
# the last click is judged by (a) the last click alone with its Bayes
# prior, (b) the Viterbi path's final state, (c) the forward filter.
# All parameters are illustrative, not estimated from any real site.
import math
import random

STAGES = ["BRW", "RES", "CMP", "DEC"]
PAGES = ["LIST", "ITEM", "SPEC", "REVW", "SHIP", "CART"]
# 示意参数:初始阶段、阶段转移(允许少量回退)、各阶段的页面类型分布
PI = [0.70, 0.20, 0.08, 0.02]
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]]
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 = len(STAGES), len(PAGES)


def lse(xs):
    # 对数域求和:log(sum(exp(x))),用 fsum 避免下溢与累加误差
    top = max(xs)
    if top == -math.inf:
        return top
    return top + math.log(math.fsum(math.exp(x - top) for x in xs))


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 simulate(rng, t_len):
    s = [draw(rng, PI)]
    for _ in range(t_len - 1):
        s.append(draw(rng, A[s[-1]]))
    return s, [draw(rng, B[k]) for k in s]


def forward_last(obs):
    la = [math.log(PI[k]) + math.log(B[k][obs[0]]) for k in range(K)]
    for o in obs[1:]:
        la = [lse([la[i] + math.log(A[i][j]) for i in range(K)])
              + math.log(B[j][o]) for j in range(K)]
    return max(range(K), key=lambda k: la[k])


def viterbi(obs):
    d = [math.log(PI[k]) + math.log(B[k][obs[0]]) for k in range(K)]
    back = []
    for o in obs[1:]:
        ptr, nd = [], []
        for j in range(K):
            i = max(range(K), key=lambda q: d[q] + math.log(A[q][j]))
            ptr.append(i)
            nd.append(d[i] + math.log(A[i][j]) + math.log(B[j][o]))
        back.append(ptr)
        d = nd
    path = [max(range(K), key=lambda k: d[k])]
    for ptr in reversed(back):
        path.append(ptr[path[-1]])
    return path[::-1]


def marginal(t_len):
    p = PI[:]
    for _ in range(t_len - 1):
        p = [math.fsum(p[i] * A[i][j] for i in range(K)) for j in range(K)]
    return p


def last_click(obs):
    p = marginal(len(obs))
    return max(range(K), key=lambda k: p[k] * B[k][obs[-1]])


rng = random.Random(20261113)
N = 4000
hits = {"last": [], "viterbi": [], "forward": []}
ready = {"last": [], "viterbi": [], "forward": []}
for _ in range(N):
    s, o = simulate(rng, rng.randint(6, 14))
    truth = s[-1]
    guess = {"last": last_click(o), "viterbi": viterbi(o)[-1],
             "forward": forward_last(o)}
    for key, g in guess.items():
        hits[key].append(1.0 if g == truth else 0.0)
        ready[key].append(1.0 if (g >= 2) == (truth >= 2) else 0.0)

print("%d simulated visits, stage at the last click" % N)
print("rule        4-stage acc  error   ready/not acc")
for key in ("last", "viterbi", "forward"):
    a = math.fsum(hits[key]) / N
    r = math.fsum(ready[key]) / N
    print("%-10s  %10.4f  %6.4f  %13.4f" % (key, a, 1.0 - a, r))
diff = [f - l for f, l in zip(hits["forward"], hits["last"])]
m = math.fsum(diff) / N
sd = math.sqrt(math.fsum((x - m) ** 2 for x in diff) / (N - 1))
half = 1.96 * sd / math.sqrt(N)
print("forward minus last: %.4f  (95%% CI %.4f to %.4f)"
      % (m, m - half, m + half))
err_l = 1.0 - math.fsum(hits["last"]) / N
err_f = 1.0 - math.fsum(hits["forward"]) / N
print("error cut by the full sequence: %.3f of last-click error"
      % ((err_l - err_f) / err_l))

唯一数据源与引用

范雅福(Fan, Y.). (2026). 《隐马尔可夫维特比解码、浏览点击序列反推客户隐藏决策阶段与出手时机母典》第 1 章「最后一次点击的错觉:整条序列与单点判别的准确率」(版本 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}
}

本书其他章节结论