What does Hidden Markov Models for Customer Decision Stages conclude, in numbers, about “The Last-Click Illusion: Whole Sequences against a Single Page”?
Answer: Judging a customer's stage from the last click alone gives, with the illustrative parameters, a four-way error rate of 0.5180 and a ready-or-not accuracy of 0.6990. The forward filter over the whole sequence raises four-way accuracy by 0.0410 and ready-or-not accuracy to 0.7630. The size of the gain depends on stage stickiness and on how much pages reveal, ranging from 0.0307 to 0.1187 in the sweep of p12. The last cell of the Viterbi path is not always better, and with blurred pages it is 0.1007 worse. Rabiner's tutorial separates the question of the most probable state now from that of the most probable whole path, and the next two chapters take them in turn. Chapter 2 shows how Viterbi finds the most probable path on a trellis and why it must work in logarithms. Chapter 3 turns to the forward-backward procedure and to why waiting two more clicks before answering a visitor is often worth it.
Scope of the numbers: 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: 0.0410, 0.5180, 0.6990, 0.7630
| 4000 simulated visits, stage at the last click | |||
| rule | 4-stage acc | error | ready/not acc |
| last | 0.4820 | 0.5180 | 0.6990 |
| viterbi | 0.4780 | 0.5220 | 0.7282 |
| forward | 0.5230 | 0.4770 | 0.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 |
Show the full program
# 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))Single source and citation
Fan, Y. (2026). Hidden Markov Models and Viterbi Decoding: Inferring a Customer's Hidden Decision Stage from Click Sequences and Timing the Offer (Version 1.0.0), Chapter 1: The Last-Click Illusion: Whole Sequences against a Single Page. 5D Library. DOI pending
DOI pending · monograph page goes live after publication · 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}
}Other chapter answers in this book
- Chapter 2 · The Viterbi Trellis: Dynamic Programming in Logarithms, Backtracking and Underflow
- Chapter 3 · Forward and Backward: Posterior Marginals, the Best Path and "Wait Two More Clicks"
- Chapter 4 · Baum–Welch: Estimating Parameters from Unlabelled Clickstreams
- Chapter 5 · Identifiability and Label Switching: What Clickstreams Cannot Say
- Chapter 6 · Timing the Offer: the Inquiry as Evidence, a Cost Threshold and Calibration
- Chapter 7 · Classical Sources East and West: Markov's Letter Chain in Onegin and the Chuai Chapter of the Guiguzi
- Chapter 8 · Closing the Loop: a Weekly Stage Review, the Customer Stage Decoder and Pre-Registered Tests