"""Duplication model v2, 22.11.2026. Conservative, ranged, Jevons-separated. $ in billions USD.
Run: python3 calc2.py  (stdlib only, seeded)"""
import random, statistics as st
random.seed(42)
N_SIM = 20000

def pct(xs, p): xs = sorted(xs); return xs[int(p * (len(xs) - 1))]
def band(xs, f="{:.0f}"): return f"{f.format(pct(xs,.1))} / {f.format(pct(xs,.5))} / {f.format(pct(xs,.9))}  (p10/median/p90)"

# ---------- inputs: (low, high) uniform ranges, sources: /blog/ai-duplication-sources.md ----------
CAPEX       = (720, 800)    # big-4 hyperscaler capex 2026 incl. leases
AI_SHARE    = (0.70, 0.90)  # ASSUMPTION: share of that capex that is AI
TRAIN_SHARE = (0.55, 0.74)  # training+R&D share of lab compute (SemiAnalysis ~2/3, Anthropic 74%; 0.55 = conservative floor)
SAME_WORK   = (0.50, 0.90)  # ASSUMPTION: share of each lab's training that repeats what another lab also does (rest = genuinely different research)
N_LABS      = (4, 6)        # US labs training comparable frontier models (OpenAI, Anthropic, Google, Meta, xAI)
KEEP        = 2             # we KEEP 2 labs for competition + resilience (conservative)
GW_COST     = (50, 60)      # $bn per GW all-in (Huang)
LOAD        = (0.6, 0.8)    # ASSUMPTION: average load factor of AI data centers
LAB_REV     = (131, 155)    # OpenAI 50(-74 gross) + Anthropic 81 run rate, autumn 2026
MARGIN      = (0.40, 0.60)  # gross margin on AI revenue

sav_frac, sav_usd, sav_twh, years_dup, years_nodup = [], [], [], [], []
for _ in range(N_SIM):
    capex = random.uniform(*CAPEX); ai = capex * random.uniform(*AI_SHARE)
    t = random.uniform(*TRAIN_SHARE); d = random.uniform(*SAME_WORK)
    n = random.randint(*N_LABS)
    f = t * d * (1 - KEEP / n)           # share of AI capex that is pure duplication
    sav_frac.append(f); sav_usd.append(ai * f)
    gw = ai * f / random.uniform(*GW_COST)
    sav_twh.append(gw * 8760 * random.uniform(*LOAD) / 1000)
    # self-financing: revenue*margin >= AI capex. Revenue +60%/yr (mid case), capex flat.
    m = random.uniform(*MARGIN)
    for cap, out in ((ai, years_dup), (ai * (1 - f), years_nodup)):
        R, y = random.uniform(*LAB_REV), 2026
        while R * m < cap and y < 2060: R *= 1.6; y += 1
        out.append(y)

print("A) Pure duplication in 2026 AI capex (2 labs kept, only repeated work counted)")
print("   share of AI capex :", band([x*100 for x in sav_frac], "{:.0f}%"))
print("   $ per year        :", band(sav_usd, "${:.0f}bn"))
print("   electricity       :", band(sav_twh, "{:.0f} TWh/yr"), " (compare: Switzerland used 57.5 TWh in 2024, SFOE)")
print("\nB) Year AI revenue pays its own capex (revenue +60%/yr, capex flat)")
print("   with the race     :", band(years_dup))
print("   without duplication:", band(years_nodup))

# ---------- C) Jevons check: inference grows with demand, training per lab grows too ----------
print("\nC) Jevons check: does duplication still matter when inference explodes?")
print("   inference demand x3/yr (Jevons), training spend per lab x2/yr, 5 labs, keep 2, same-work 70%")
inf, train_per_lab = 0.33, 0.67 / 5      # 2026 split of lab compute, normalised to 1
for y in range(2026, 2031):
    total = inf + 5 * train_per_lab
    dup = 0.7 * (5 - KEEP) * train_per_lab
    print(f"   {y}: duplication = {dup/total:.0%} of all compute, absolute {dup:.2f} (2026 total = 1.00)")
    inf *= 3; train_per_lab *= 2

# ---------- D) debt ----------
print("\nD) Who pays: debt share of 2026 capex")
for issued in (250, 300):
    print(f"   hyperscaler bonds ${issued}bn / capex ~$760bn = {issued/760:.0%} debt-financed (plus ~$65bn off-balance-sheet SPVs)")
