5D Library › Hundred-Volume Knowledge Hypergraphs, Multimodal Personal Prior Compilation and Autonomous Empire Scaling · 中文

Chapter 2. Three More Primitives: Weibull Hazards, Legendre-Bregman Duality and Bayesian Updating

One hazard law

Many volumes in this library ask how long something lasts: a viewer on a video, a subscriber, a stored file. The Weibull law describes all three with two numbers, a scale and a shape; the shape says whether the hazard falls (below 1), stays flat (1) or rises (above 1). p03 fits the same maximum-likelihood routine to synthetic data from three domains.

Calculation

# Chapter 2. p03: One hazard law, three domains. Weibull survival
# S(t) = exp(-(t / lam)^k) has hazard (k / lam) (t / lam)^(k - 1):
# falling if k < 1, flat if k = 1, rising if k > 1. Synthetic data from
# three illustrative domains, 2,000 cases each: seconds a viewer stays
# with a short video (k 0.6, lam 20); months a subscriber stays (k 0.9,
# lam 14); years until a storage medium fails (k 1.8, lam 12). The same
# maximum-likelihood routine (grid over k, lam in closed form) fits
# each. Seed 100.
import random
from math import log

def fit(ts):
    n, slog = len(ts), sum(log(t) for t in ts)
    best = None
    for i in range(1, 400):
        k = i / 100
        lam = (sum(t ** k for t in ts) / n) ** (1 / k)
        ll = n * log(k) - n * k * log(lam) + (k - 1) * slog - n
        if best is None or ll > best[0]:
            best = (ll, k, lam)
    return best[1], best[2]

DOMAINS = (("video watch", "s", 0.6, 20.0), ("subscription", "mo", 0.9,
           14.0), ("storage medium", "yr", 1.8, 12.0))

if __name__ == "__main__":
    rng = random.Random(100)
    print("   domain          true k  fitted k  fitted lam  median  "
          "hazard t=1 / t=10")
    for name, unit, k, lam in DOMAINS:
        ts = [rng.weibullvariate(lam, k) for _ in range(2000)]
        fk, fl = fit(ts)
        med = fl * log(2) ** (1 / fk)
        h = lambda t: (fk / fl) * (t / fl) ** (fk - 1)
        fmt = ("   {:<14}  {:6.2f}  {:8.2f}  {:7.1f} {:<2}  {:6.1f}"
               "  {:.3f} / {:.3f}")
        print(fmt.format(name, k, fk, fl, unit, med, h(1), h(10)))
#    domain          true k  fitted k  fitted lam  median  hazard t=1 / t=10
#    video watch       0.60      0.60     21.1 s     11.5  0.096 / 0.038
#    subscription      0.90      0.91     13.8 mo     9.2  0.084 / 0.068
#    storage medium    1.80      1.81     12.0 yr     9.8  0.020 / 0.131

The fits recover the shapes (0.60, 0.91 and 1.81 against true 0.60, 0.90 and 1.80) from 2,000 cases each. The hazard of the video viewer falls from 0.096 at one second to 0.038 at ten; that of the subscriber falls a little; that of the storage medium rises from 0.020 to 0.131 a year. “Retention” and “bit rot” are the same calculation with opposite shapes.

One convex function

The logarithmic market scoring rule of a prediction market (Hanson 2003) and the softmax of a neural attention layer are the gradient of the same convex function, log-sum-exp; its convex conjugate is negative entropy, and the Bregman divergence of negative entropy is the Kullback-Leibler divergence (Banerjee et al. 2005). p04 checks this numerically.

Calculation

# Chapter 2. p04: One convex function, two machines. The log-sum-exp
# C(q) = b log(sum exp(q_i / b)) is the cost function of Hanson's
# logarithmic market scoring rule; its gradient, softmax(q / b), is both
# the market's price vector and the weight vector of an attention layer
# or a logit model. Its convex conjugate is b times negative entropy,
# C*(p) = b sum p_i log p_i, and the Bregman divergence of C* is b times
# the Kullback-Leibler divergence. Checked numerically for q = (2, 1,
# 0, -1), b = 1.5, and a trader who believes pi = (0.5, 0.3, 0.15,
# 0.05) and trades until the prices equal pi.
from math import exp, log

B = 1.5
Q = (2.0, 1.0, 0.0, -1.0)
PI = (0.5, 0.3, 0.15, 0.05)

def cost(q):
    return B * log(sum(exp(x / B) for x in q))

def softmax(q):
    z = [exp(x / B) for x in q]
    return [v / sum(z) for v in z]

def neg_entropy(p):
    return B * sum(x * log(x) for x in p if x > 0)

def kl(a, b):
    return sum(x * log(x / y) for x, y in zip(a, b) if x > 0)

if __name__ == "__main__":
    p = softmax(Q)
    print("   prices = attention weights: " + "  ".join(
        "{:.4f}".format(x) for x in p))
    gap = cost(Q) + neg_entropy(p) - sum(a * b for a, b in zip(Q, p))
    print("   Fenchel-Young gap at the gradient below 1e-12: {}".format(
        "yes" if abs(gap) < 1e-12 else "no"))
    q2 = [B * log(x) + cost(Q) for x in PI]
    paid = cost(q2) - cost(Q)
    gain = sum(pi * (a - b) for pi, a, b in zip(PI, q2, Q))
    print("   trader moves prices to pi: net payment {:.4f}, expected"
          " payout {:.4f}".format(abs(paid), gain))
    print("   expected profit {:.4f} = b x KL(pi || p) = {:.4f}".format(
        gain - paid, B * kl(PI, p)))
#    prices = attention weights: 0.5229  0.2685  0.1378  0.0708
#    Fenchel-Young gap at the gradient below 1e-12: yes
#    trader moves prices to pi: net payment 0.0000, expected payout 0.0093
#    expected profit 0.0093 = b x KL(pi || p) = 0.0093

The four prices (0.5229, 0.2685, 0.1378, 0.0708) are the attention weights of the same scores; the Fenchel-Young gap at the gradient is zero to machine precision; and a trader who moves the prices to her own beliefs expects to earn 0.0093, exactly the liquidity parameter times the Kullback-Leibler divergence. What a prediction market pays for information and what a classifier’s loss charges for error are one quantity.

One update rule

The last of the three is Bayes’ rule in its simplest conjugate form. p05 applies one Beta-binomial update to a checkout’s conversion, to the share of a diagnostic station’s users who later open the matching book, and to the failure rate of a yearly checksum.

Calculation

# Chapter 2. p05: One update rule, three uses. A Beta(a, b) prior on a
# rate, after s successes in n trials, becomes Beta(a + s, b + n - s).
# (i) A checkout's conversion rate: prior Beta(2, 48), 30 sales in 500
# visits. (ii) The share of a diagnostic station's users who later open
# the matching book: prior Beta(1, 9), 12 of 80. (iii) The chance that
# a stored file fails its yearly checksum: prior Beta(1, 999), no
# failures in 2,000 checks. Posterior mean, and 95% interval from
# 200,000 draws. Seed 100.
import random

CASES = (("conversion", 2, 48, 30, 500), ("station to book", 1, 9, 12, 80),
         ("checksum failure", 1, 999, 0, 2000))

if __name__ == "__main__":
    rng = random.Random(100)
    print("   use               prior  data       posterior  95% interval")
    for name, a, b, s, n in CASES:
        a2, b2 = a + s, b + n - s
        xs = sorted(rng.betavariate(a2, b2) for _ in range(200000))
        fmt = "   {:<16}  {:.4f}  {:>4}/{:<5}  {:9.5f}  {:.5f} to {:.5f}"
        print(fmt.format(name, a / (a + b), s, n, a2 / (a2 + b2),
                      xs[5000], xs[195000]))
#    use               prior  data       posterior  95% interval
#    conversion        0.0400    30/500      0.05818  0.04024 to 0.07917
#    station to book   0.1000    12/80       0.14444  0.07974 to 0.22355
#    checksum failure  0.0010     0/2000     0.00033  0.00001 to 0.00123

The conversion estimate moves from a prior mean of 0.0400 to 0.05818; the station-to-book share from 0.1000 to 0.14444, with a wide 95% interval (0.07974 to 0.22355) because only 80 users were seen; and 2,000 clean checksum checks leave an upper 95% limit of 0.00123 a year on the failure rate. The rule is the same; the width of what it teaches depends only on how much data each domain supplies.

Summary

The hazard law, the log-sum-exp duality and the conjugate update are three more pieces of mathematics that recur across this library. Each runs unchanged across domains, and in each the domain contributes the data and the parameters, not the form.

References

Banerjee, Arindam, Srujana Merugu, Inderjit S. Dhillon, and Joydeep Ghosh. 2005. “Clustering with Bregman Divergences.” Journal of Machine Learning Research 6: 1705 to 1749.
Hanson, Robin. 2003. “Combinatorial Information Market Design.” Information Systems Frontiers 5 (1): 107 to 119.

Fan, Y. (2026). Hundred-Volume Knowledge Hypergraphs, Multimodal Personal Prior Compilation and Autonomous Empire Scaling: Cross-Disciplinary Isomorphisms, Zero-Marginal-Cost Replication and Century-Scale Knowledge Preservation [百卷知识超图拓扑、多模态个人先验编译与自主知识帝国标度律] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.24090260 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).