5D Library › Zero-Touch Multi-Site Fault Tolerance, Payment Anti-Fraud State Machines and Passive Cashflow Queuing · 中文

Chapter 6. Allocating Traffic, Promoting Domains and Holding Cash

Allocating paid traffic among 30 sites

The 30 sub-sites on the shared kernel convert at different rates, unknown in advance. Each day a fixed amount of paid traffic (here 3,000 visitors) must be split among them. An even split is simplest but wastes much traffic on sites whose poor conversion has long been obvious. This is a multi-armed bandit problem. Thompson sampling is one of the oldest and simplest solutions: keep a posterior distribution for each site’s conversion rate and allocate by “draw a value from each posterior, give to the largest”, so that the sites most likely to be best get the most traffic while uncertain sites still get tried (Thompson 1933; Russo et al. 2018).

The program draws the 30 sites’ true conversion rates from Beta(2, 98) (mean 2%) and runs for 60 days, comparing an even split with Thompson sampling in daily batches.

Calculation

# Chapter 6. [p11] Thompson sampling of paid traffic across 30 sites.
# Thirty sites on the shared kernel convert visitors with unknown rates
# p_i drawn once from Beta(2, 98) (mean 2%). A daily budget of 3,000
# paid visitors is split across them for 60 days. UNIFORM gives each
# site 100 a day; THOMPSON (in daily batches) draws 200 samples from
# every site's Beta(1 + conversions, 1 + misses) posterior and gives
# each site the share of draws in which it came out best.
# 50 replications with fixed seeds. Reported: conversions per run,
# regret against always using the best site, and the share of traffic
# that went to the three best sites.
import random

SITES, DAILY, DAYS, REPS, DRAWS = 30, 3000, 60, 50, 200

def run(rng, rates, policy):
    conv = [0] * SITES
    miss = [0] * SITES
    sent = [0] * SITES
    for _ in range(DAYS):
        if policy == "uniform":
            alloc = [DAILY // SITES] * SITES
        else:
            wins = [0] * SITES
            for _ in range(DRAWS):
                d = [rng.betavariate(1 + conv[k], 1 + miss[k])
                     for k in range(SITES)]
                wins[d.index(max(d))] += 1
            alloc = [DAILY * w // DRAWS for w in wins]
        for i, n in enumerate(alloc):
            c = sum(rng.random() < rates[i] for _ in range(n))
            sent[i] += n
            conv[i] += c
            miss[i] += n - c
    top = sorted(range(SITES), key=lambda k: -rates[k])[:3]
    return sum(conv), sum(sent[k] for k in top) / sum(sent)

if __name__ == "__main__":
    out = {"uniform": [], "thompson": []}
    best = []
    for r in range(REPS):
        rng = random.Random(75011 + r)
        rates = [rng.betavariate(2, 98) for _ in range(SITES)]
        best.append(max(rates) * DAILY * DAYS)
        for pol in out:
            out[pol].append(run(rng, rates, pol))
    ideal = sum(best) / REPS
    print("   ideal (best site only): {:.1f} conversions".format(ideal))
    print("   policy     conversions   regret   share to top 3")
    for pol, res in out.items():
        c = sum(x for x, _ in res) / REPS
        s = sum(y for _, y in res) / REPS
        print("   {:9s} {:12.1f} {:8.1f} {:16.3f}".format(
            pol, c, ideal - c, s))
    print("\nReading: Thompson sampling moves most paid traffic to the")
    print("few sites that convert best and recovers most of the gap")
    print("to the ideal; uniform splitting wastes it on sites whose low")
    print("rate was obvious within days.")
#    ideal (best site only): 9905.2 conversions
#    policy     conversions   regret   share to top 3
#    uniform         3473.4   6431.9            0.100
#    thompson        9435.2    470.0            0.911
#
# Reading: Thompson sampling moves most paid traffic to the
# few sites that convert best and recovers most of the gap
# to the ideal; uniform splitting wastes it on sites whose low
# rate was obvious within days.

Knowing the best site from the start and sending it all the traffic would yield 9905.2 conversions in 60 days. The even split yields only 3473.4, short of the ideal by 6431.9; Thompson sampling yields 9435.2, short by only 470.0, and sends 0.911 of the traffic to the three best sites.

When to promote a sub-site to its own domain

A sub-site that does well can be promoted from a sub-path of the shared kernel to a domain of its own, for its own brand and search traffic. Let a standalone domain cost an extra 6,000 a year (domain, separate content, links and monitoring), lift 20,000 organic visits a year by 30%, and a conversion be worth 60. Promotion pays when the conversion rate exceeds c*=6000/(0.30×20000×60)=1/60c^* = 6000/(0.30 \times 20000 \times 60) = 1/60. Each site gets 100 visitors a day, and the program compares three promotion rules: a posterior rule (promote when P(p>c*)≥0.95P(p > c^*) \ge 0.95, give up when it is ≤0.05\le 0.05), a count rule (promote after 20 conversions), and an observed-rate rule (from day 7, promote as soon as the observed rate exceeds c*c^*; give up if it has not by day 60).

Calculation

# Chapter 6. [p12] When to promote a site from a sub-path to its own domain.
# A standalone domain costs K = 6,000 a year (domain, separate content,
# links, monitoring) and is assumed to lift a site's 20,000 organic
# visits a year by u = 30%; a conversion is worth 60. Promotion pays
# when p > c* = K / (u 20,000 60) = 1/60. Each site receives 100
# visitors a day; its rate p ~ Beta(2, 98) as in p11. Rules, checked
# daily for up to 365 days:
#   BAYES  - promote when P(p > c* | data) >= 0.95 under a Beta(1, 1)
#            prior, drop when it is <= 0.05;
#   COUNT  - promote after 20 conversions (a common rule of thumb);
#   PEEK   - promote as soon as the observed rate exceeds c* after at
#            least 7 days, drop after 60 days below it.
# 2,000 simulated sites. Reported: wrong promotions (p < c*), missed
# good sites, mean days to a decision, undecided after a year.
import math
import random

K, U, VISITS, VALUE = 6000.0, 0.30, 20000, 60.0
C_STAR = K / (U * VISITS * VALUE)

def betacf(a, b, x):
    qab, qap, qam = a + b, a + 1, a - 1
    c, d = 1.0, 1 - qab * x / qap
    d = 1 / d
    h = d
    for m in range(1, 300):
        m2 = 2 * m
        aa = m * (b - m) * x / ((qam + m2) * (a + m2))
        d = 1 / (1 + aa * d)
        c = 1 + aa / c
        h *= d * c
        aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2))
        d = 1 / (1 + aa * d)
        c = 1 + aa / c
        h *= d * c
        if abs(d * c - 1) < 1e-12:
            break
    return h

def beta_cdf(x, a, b):
    lbt = (math.lgamma(a + b) - math.lgamma(a) - math.lgamma(b)
           + a * math.log(x) + b * math.log(1 - x))
    if x < (a + 1) / (a + b + 2):
        return math.exp(lbt) * betacf(a, b, x) / a
    return 1 - math.exp(lbt) * betacf(b, a, 1 - x) / b

def decide(rng, p, rule):
    conv = n = 0
    for day in range(1, 366):
        c = sum(rng.random() < p for _ in range(100))
        conv, n = conv + c, n + 100
        if rule == "BAYES":
            up = 1 - beta_cdf(C_STAR, 1 + conv, 1 + n - conv)
            if up >= 0.95:
                return True, day
            if up <= 0.05:
                return False, day
        elif rule == "COUNT":
            if conv >= 20:
                return True, day
        else:
            if day >= 7 and conv / n > C_STAR:
                return True, day
            if day >= 60:
                return False, day
    return None, 365

if __name__ == "__main__":
    print("   break-even rate c* = {:.4f}".format(C_STAR))
    print("   rule    wrong promotions  missed good  mean days  undecided")
    for rule in ("BAYES", "COUNT", "PEEK"):
        rng = random.Random(75012)
        wrong = missed = und = days = good = bad = 0
        for _ in range(2000):
            p = rng.betavariate(2, 98)
            d, t = decide(rng, p, rule)
            days += t
            if p > C_STAR:
                good += 1
                missed += d is not True
            else:
                bad += 1
                wrong += d is True
            und += d is None
        print("   {:6s} {:17.3f} {:12.3f} {:10.1f} {:10.3f}".format(
            rule, wrong / bad, missed / good, days / 2000, und / 2000))
    print("\nReading: the count rule promotes nearly every site, since")
    print("twenty conversions arrive within weeks at almost any rate;")
    print("peeking at the observed rate promotes almost a fifth of the")
    print("sites that do not pay. The posterior rule decides most sites")
    print("within weeks, with about one wrong promotion in seventeen;")
    print("sites near c* stay undecided longest, and for them the")
    print("choice hardly matters.")
#    break-even rate c* = 0.0167
#    rule    wrong promotions  missed good  mean days  undecided
#    BAYES              0.060        0.054       23.2      0.024
#    COUNT              0.995        0.000       20.3      0.003
#    PEEK               0.182        0.011       29.0      0.000
#
# Reading: the count rule promotes nearly every site, since
# twenty conversions arrive within weeks at almost any rate;
# peeking at the observed rate promotes almost a fifth of the
# sites that do not pay. The posterior rule decides most sites
# within weeks, with about one wrong promotion in seventeen;
# sites near c* stay undecided longest, and for them the
# choice hardly matters.

The count rule promotes almost every site (0.995 of the sites that should not be promoted are), because at any conversion rate 20 conversions pile up within weeks. The observed-rate rule promotes 0.182 of the sites that should not be: early observed rates are very noisy, and promoting the moment one exceeds the line amounts to repeated peeking. The posterior rule decides in 23.2 days on average, promotes wrongly 0.060 of the time, misses 0.054 of the sites that should be promoted, and leaves only 0.024 undecided after a year – all of them sites whose rate is close to c*c^*, where promoting or not makes little difference.

How much reserve does “passive” cash flow need?

Cash flow is a queue too: the money for an order arrives some time later, while costs are paid every day. The program describes it as a compound Poisson process: 40 orders a day on average, order values lognormal (mean 60, coefficient of variation 4; a few high-priced licences dominate), money arriving 7 days after the order, 3% of orders refunded or charged back after 30 days, and fixed costs of 1,800 a day. The expected net income per day is 528.0.

Calculation

# Chapter 6. [p13] "Passive" cash flow as a compound Poisson process.
# Orders arrive at 40 a day (Poisson); an order's value is lognormal
# with mean 60 and CV 4 (a few licences dominate). Money reaches the
# account 7 days after the order (settlement delay); 3% of orders are
# refunded or charged back 30 days after the order. Fixed costs are
# 1,800 a day (servers, AI inference, part-time staff). Starting from a
# reserve R0, the program simulates 400 years of daily balances and
# reports the share of years in which the balance ever goes negative,
# with and without a revenue shock (orders halve for 90 days from day
# 30, as after a search-ranking loss).
import math
import random

LAM, MEAN, CV, COST = 40.0, 60.0, 4.0, 1800.0
SIG = math.sqrt(math.log(1 + CV * CV))
MU = math.log(MEAN) - SIG * SIG / 2

def poisson(rng, lam):
    k, p, L = 0, 1.0, math.exp(-lam)
    while True:
        p *= rng.random()
        if p < L:
            return k
        k += 1

def year(rng, r0, shock):
    inflow = [0.0] * 400
    out = [0.0] * 400
    bal, low = r0, r0
    for d in range(365):
        lam = LAM / 2 if shock and 30 <= d < 120 else LAM
        for _ in range(poisson(rng, lam)):
            v = rng.lognormvariate(MU, SIG)
            inflow[d + 7] += v
            if rng.random() < 0.03:
                out[min(399, d + 30)] += v
        bal += inflow[d] - out[d] - COST
        low = min(low, bal)
    return low < 0, bal

if __name__ == "__main__":
    rng = random.Random(75013)
    print("   expected net per day: {:.1f}".format(
        LAM * MEAN * 0.97 - COST))
    print("   costs before the first settlement: {:,.0f}".format(7 * COST))
    print("   reserve R0   P(negative) plain   P(negative) with shock")
    for r0 in (15000, 20000, 30000, 60000, 90000):
        a = sum(year(rng, r0, False)[0] for _ in range(400)) / 400
        b = sum(year(rng, r0, True)[0] for _ in range(400)) / 400
        print("   {:10,d} {:19.3f} {:24.3f}".format(r0, a, b))
    print("\nReading: a business that earns on average still needs a")
    print("reserve, because cash arrives a week late and the costs do")
    print("not wait. A few weeks of costs covers ordinary noise; a")
    print("three-month traffic shock needs several times as much. The")
    print("queue of money is as real as the queue of requests.")
#    expected net per day: 528.0
#    costs before the first settlement: 12,600
#    reserve R0   P(negative) plain   P(negative) with shock
#        15,000               0.028                    0.995
#        20,000               0.000                    0.980
#        30,000               0.000                    0.970
#        60,000               0.000                    0.318
#        90,000               0.000                    0.000
#
# Reading: a business that earns on average still needs a
# reserve, because cash arrives a week late and the costs do
# not wait. A few weeks of costs covers ordinary noise; a
# three-month traffic shock needs several times as much. The
# queue of money is as real as the queue of requests.
Eight yearly balance paths with a reserve of 60,000: no shock (blue) and orders halved on days 30-120 (purple).

A business earning 528.0 a day on average must pay 12,600 of costs before the first payout arrives. With a reserve of 15,000, an ordinary year sees a negative balance with probability 0.028; from 20,000 up, ordinary years no longer go negative. But under a shock that halves orders for three months (a fall in search ranking, say), a reserve of 30,000 goes negative in 0.970 of years, 60,000 still in 0.318, and only 90,000 brings it to 0. The “passive” in passive cash flow presupposes a reserve. Ruin probabilities of compound Poisson processes are a classical problem of actuarial mathematics; here simulation gives only the numbers for the stated parameters.

Hypothesis · not established

The author’s interpretation (not yet tested): the author’s site revenue is highly concentrated on one search engine, and a three-month traffic shock is not rare; the reserve should therefore be set for the shock scenario, not for ordinary years – at least 50 days of fixed costs. What would show this wrong: if real traffic records for the past three years showed no 30-day stretch with orders down by more than three tenths, the shock scenario may be too pessimistic and the reserve could be set for ordinary years.

Summary

Thompson sampling concentrates paid traffic on the best few sites and falls only 470.0 conversions short of the ideal, against 6431.9 for an even split. Promotion to a standalone domain should follow the posterior rule: the count rule promotes almost everything and the observed-rate rule promotes 0.182 wrongly. Cash flow needs a reserve: 20,000 suffices in ordinary years, while three months of halved orders need 90,000.

References

Russo, Daniel J., Benjamin Van Roy, Abbas Kazerouni, Ian Osband, and Zheng Wen. 2018. “A Tutorial on Thompson Sampling.” Foundations and Trends in Machine Learning 11 (1): 1–96.
Thompson, William R. 1933. “On the Likelihood That One Unknown Probability Exceeds Another in View of the Evidence of Two Samples.” Biometrika 25 (3/4): 285–94.

Fan, Y. (2026). Zero-Touch Multi-Site Fault Tolerance, Payment Anti-Fraud State Machines and Passive Cashflow Queuing: Receipt-Hash Deduplication, Self-Referral Blocking, Host-Header Routing and Availability Bounds [零人工多站点容错架构、支付防欺诈状态机与被动现金流排队论] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.24090201 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).