5D Library › 结账顺手加购弹性、一键升单动力学与跨领域推荐矩阵 · English
第三章 跨舰队推荐的条件互信息路由
一个合成的需求模型
作者经营的站点分成四个「舰队」:A 测算、B 健康、C 声音与心智、D 商业工具,共 30 个站点(程序中记作 S01 到 S30)。一个读者在某个站点上暴露出的需要,常常与另一个站点提供的东西有关。程序用一个合成模型说明怎样把这种关系变成推荐路由:六种潜在需要(感情、金钱、睡眠、压力、健康、事业),一个共同的「压力负荷」因子使金钱、睡眠与压力问题倾向于同时出现;每个站点服务一到两种需要,例如 S01 偏感情、S03 偏金钱、S13 偏睡眠、S15 偏压力、S23 与 S26 偏商业。这些参数都是假设,不是用户数据。
两个站点访问指示变量之间的互信息 度量「知道读者访问了 ,能减少多少对其是否访问 的不确定性」(Cover and Thomas 2006)。每个站点的推荐路由取其他舰队中互信息最大的两个站点。
推导
# Chapter 3. p05: Cross-sell routing by mutual information - on a
# SYNTHETIC model. 30 sites in four fleets: A divination (S01-S08),
# B health (S09-S16), C sound and mind (S17-S22), D business tools
# (S23-S30). Six latent needs; a shared "strain" factor makes needs
# co-occur (money trouble with poor sleep, and so on). A user visits a
# site with a probability set by the needs the site serves. From
# simulated users we estimate I(X_i; X_j) for every pair of visit
# indicators and route each site to its top two sites in OTHER fleets.
import random
from math import log2
NEEDS = ("love", "money", "sleep", "stress", "health", "career")
FLEET = ["A"] * 8 + ["B"] * 8 + ["C"] * 6 + ["D"] * 8
SERVES = {}
for i in range(30):
SERVES[i] = {NEEDS[i % 6]: 0.35, NEEDS[(i + 2) % 6]: 0.10}
SERVES[0] = {"love": 0.40, "stress": 0.10} # S01 relationships
SERVES[2] = {"money": 0.40, "stress": 0.10} # S03 money trouble
SERVES[12] = {"sleep": 0.45} # S13 sleep sound
SERVES[14] = {"stress": 0.40, "sleep": 0.10} # S15 vagal reset
SERVES[22] = {"money": 0.35, "career": 0.15} # S23 business game
SERVES[25] = {"career": 0.35, "money": 0.15} # S26 deal tools
def user(rng):
strain = rng.random() < 0.3
base = {"love": 0.2, "money": 0.2, "sleep": 0.15, "stress": 0.2,
"health": 0.2, "career": 0.2}
boost = {"money": 0.3, "sleep": 0.35, "stress": 0.4, "love": 0.15}
return {n: rng.random() < base[n] + (boost.get(n, 0) if strain else 0)
for n in NEEDS}
def visits(needs, rng):
out = []
for i in range(30):
miss = 1.0
for n, w in SERVES[i].items():
if needs[n]:
miss *= 1 - w
out.append(rng.random() < 1 - miss * 0.98)
return out
def mi(a, b, both, n):
tot = 0.0
for x in (0, 1):
for y in (0, 1):
nxy = both if x and y else (a - both if x else (
b - both if y else n - a - b + both))
px, py = (a if x else n - a) / n, (b if y else n - b) / n
if nxy:
tot += nxy / n * log2(nxy / n / (px * py))
return tot
def estimate(n, seed):
rng = random.Random(seed)
cnt, pair = [0] * 30, [[0] * 30 for _ in range(30)]
for _ in range(n):
v = visits(user(rng), rng)
on = [i for i in range(30) if v[i]]
for i in on:
cnt[i] += 1
for j in on:
pair[i][j] += 1
return cnt, pair
def routes(cnt, pair, n):
out = {}
for i in range(30):
cand = [(mi(cnt[i], cnt[j], pair[i][j], n), j) for j in range(30)
if FLEET[j] != FLEET[i]]
out[i] = [j for _, j in sorted(cand, reverse=True)[:2]]
return out
if __name__ == "__main__":
N = 60000
cnt, pair = estimate(N, 78005)
r = routes(cnt, pair, N)
for i in (0, 2, 12, 14, 22, 25):
a, b = r[i]
ma = mi(cnt[i], cnt[a], pair[i][a], N)
mb = mi(cnt[i], cnt[b], pair[i][b], N)
fmt = (" S{:02d} ({}) -> S{:02d} ({}) {:.4f} bits,"
" S{:02d} ({}) {:.4f}")
print(fmt.format(i + 1, FLEET[i], a + 1, FLEET[a], ma, b + 1,
FLEET[b], mb))
# S01 (A) -> S19 (C) 0.0254 bits, S25 (D) 0.0250
# S03 (A) -> S14 (B) 0.0304 bits, S20 (C) 0.0285
# S13 (B) -> S27 (D) 0.0353 bits, S21 (C) 0.0337
# S15 (B) -> S04 (A) 0.0295 bits, S22 (C) 0.0283
# S23 (D) -> S03 (A) 0.0252 bits, S02 (A) 0.0216
# S26 (D) -> S06 (A) 0.0214 bits, S18 (C) 0.0207在 60,000 名合成用户上,S01 推荐 S19(C 舰队,同样偏感情)与 S25;S03 推荐 S14 与 S20;S13 推荐 S27 与 S21;S15 推荐 S04 与 S22;S23 推荐 S03 与 S02。互信息都只有百分之几比特(例如 S13 到 S27 为 0.0353 比特):两个站点的访问相关,但远不是彼此的强预测。
需要多少用户
推导
# Chapter 3. p06: How many users before the top-two routes are stable?
# Same synthetic model as p05. "Truth" = routes estimated from 400,000
# simulated users. For smaller samples (independent seeds) we count the
# share of the 30 sites whose estimated top-two set equals the true one,
# and the mean upward bias of the plug-in mutual information of an
# independent pair (the Miller-Madow bias, about 1/(2 n ln 2) bits).
import importlib.util
from math import log
def load(name):
spec = importlib.util.spec_from_file_location(name, name + ".py")
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
m = load("p05_cross_sell_mi")
if __name__ == "__main__":
cnt, pair = m.estimate(400000, 1)
truth = m.routes(cnt, pair, 400000)
print(" users sites with the true top-two set (of 30)")
for n in (2000, 10000, 60000):
hits = []
for seed in (11, 12, 13):
c, p = m.estimate(n, seed)
r = m.routes(c, p, n)
hits.append(sum(set(r[i]) == set(truth[i]) for i in range(30)))
print(" {:6,} {}".format(n, ", ".join(str(h) for h in hits)))
for n in (2000, 10000, 60000):
print(" bias, independent pair, n = {:6,}: {:.6f} bits".format(
n, 1 / (2 * n * log(2))))
# users sites with the true top-two set (of 30)
# 2,000 10, 14, 10
# 10,000 14, 12, 10
# 60,000 16, 17, 14
# bias, independent pair, n = 2,000: 0.000361 bits
# bias, independent pair, n = 10,000: 0.000072 bits
# bias, independent pair, n = 60,000: 0.000012 bits以 400,000 名合成用户估出的路由为「真值」,用较小的样本重估:2,000 名用户时,30 个站点中只有 10 到 14 个站点的前两条与真值相同;60,000 名用户时也只有 14 到 17 个。原因不是估计有偏(独立变量对的偏差在 2,000 人时只有 0.000361 比特),而是候选站点之间的互信息相差太小,排序在抽样误差中来回翻转。
注
实务含义。 当候选之间差距很小时,「前两名」并不比第三、第四名更值得推荐。更稳健的做法是把互信息接近的站点作为一组轮流展示,并用第八章的留出实验度量真正的增量,而不是固定一条路由。
小结
条件互信息可以把站点间的需求相关变成推荐路由;但在合成模型中互信息都很小,前两名在 60,000 名用户的样本上仍有一半站点不稳定。路由应当按组轮换,并以留出实验检验。
参考文献
Fan, Y. (2026). Order-Bump Friction Elasticity, One-Click Upsell Kinetics and Cross-Domain Recommendation Matrices: Zero-Reentry Impulse Attachment, Conditional Mutual Information and Cart-Value Multipliers [结账顺手加购弹性、一键升单动力学与跨领域推荐矩阵] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.24090207 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).