What does Hidden Markov Models for Customer Decision Stages conclude, in numbers, about “Baum–Welch: Estimating Parameters from Unlabelled Clickstreams”?
Answer: Chapter 4 estimates parameters from unlabelled clickstreams with Baum–Welch, and ten random starts decode with accuracy from 0.3877 to 0.5181.
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.3877, 0.5181
| 150 visits, 1550 clicks, 10 random starts x 40 iterations | ||||
| run | final logL | gap to best | relabel | decode acc |
| 1 | -2629.706 | 9.825 | 3012 | 0.4303 |
| 2 | -2621.744 | 1.863 | 0213 | 0.5039 |
| 3 | -2621.446 | 1.565 | 0123 | 0.4845 |
| 4 | -2623.250 | 3.369 | 2301 | 0.4619 |
| 5 | -2633.109 | 13.228 | 3210 | 0.3877 |
| 6 | -2623.613 | 3.732 | 0321 | 0.4129 |
| 7 | -2628.478 | 8.597 | 3201 | 0.4329 |
| 8 | -2619.881 | 0.000 | 3012 | 0.5181 |
| 9 | -2619.978 | 0.097 | 1230 | 0.5123 |
| 10 | -2622.620 | 2.739 | 3201 | 0.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 |
Show the full program
# 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)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 4: Baum–Welch: Estimating Parameters from Unlabelled Clickstreams. 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 1 · The Last-Click Illusion: Whole Sequences against a Single Page
- 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 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