"""Run: python3 fair_model.py (2027 scale-up) | python3 fair_model.py today (calibrated to 2025)
FAIR-style annual loss exposure (ALE) for agent classes at a reference UK digital bank.
ALE = threat events/yr x P(success | today's controls) x loss per success + secondary annual cost.
Each input is (min, most likely, max), sampled with a PERT distribution. 100k Monte Carlo runs.
Reference bank: ~5m retail customers, digital-first, ~4% share of UK retail fraud flows,
~6% of UK APP mule inflows (digital banks over-index as receivers), voice ID live in telephony.
All inputs are assumptions to replace with client data; sources in the memo.
"""
import numpy as np
rng = np.random.default_rng(7)
N = 100_000

def pert(lo, mode, hi, n=N):
    if hi == lo:
        return np.full(n, float(lo))
    a = 1 + 4 * (mode - lo) / (hi - lo)
    b = 1 + 4 * (hi - mode) / (hi - lo)
    return lo + rng.beta(a, b, n) * (hi - lo)

# class: (events/yr), (p success), (loss per success GBP), (secondary annual GBP)
classes = {
 "E1 Agent-tooled mule / synthetic account opening": ((2000, 6000, 20000), (0.02, 0.06, 0.15), (2000, 6000, 15000), (50e3, 150e3, 600e3)),
 "G  Synthetic voice vs voice ID / contact centre":  ((500, 3000, 15000),  (0.02, 0.10, 0.30), (2000, 6000, 25000), (50e3, 150e3, 500e3)),
 "E2 Automated web/app ATO (cred stuffing, phish kits)": ((100, 400, 1200), (1, 1, 1),          (1000, 3000, 8000),  (20e3, 80e3, 300e3)),
 "H  Phone agent / RAT driving the app":             ((50, 250, 800),     (1, 1, 1),           (2000, 5000, 15000), (20e3, 60e3, 200e3)),
 "C  Customer's own agent tripping controls (FP cost)": ((20e3, 200e3, 1e6), (1, 1, 1),        (0.5, 2, 6),         (0, 100e3, 2e6)),
 "B  Signed agent, no mandate (credential sharing)": ((10, 100, 500),     (1, 1, 1),           (200, 1000, 5000),   (0, 50e3, 500e3)),
 "D  Hijacked mandated agent":           ((0, 20, 300),       (1, 1, 1),           (500, 3000, 50000),  (0, 20e3, 1e6)),
 "F  Benign AI caller handling cost":                ((5e3, 30e3, 150e3), (1, 1, 1),           (0.5, 1.5, 4),       (0, 0, 50e3)),
 "A  Scheme agent residual (issuer share)":          ((20, 100, 500),     (1, 1, 1),           (500, 1000, 3000),   (0, 10e3, 100e3)),
}
import sys
if len(sys.argv)>1 and sys.argv[1]=="today":
    classes["G  Synthetic voice vs voice ID / contact centre"]=((200,1000,5000),(0.02,0.08,0.25),(2000,5000,20000),(20e3,80e3,300e3))
    classes["H  Phone agent / RAT driving the app"]=((20,120,500),(1,1,1),(2000,5000,15000),(20e3,60e3,200e3))
    classes["D  Hijacked mandated agent"]=((0,2,30),(1,1,1),(500,3000,50000),(0,5e3,200e3))
    classes["C  Customer's own agent tripping controls (FP cost)"]=((5e3,40e3,200e3),(1,1,1),(0.5,2,6),(0,20e3,500e3))
rows = []
for name, (ev, p, lm, sec) in classes.items():
    ale = pert(*ev) * pert(*p) * pert(*lm) + pert(*sec)
    rows.append((name, *np.percentile(ale, [10, 50, 90]), ale.mean()))
rows.sort(key=lambda r: -r[4])
print(f"{'Class':55s} {'P10':>8s} {'P50':>8s} {'P90':>8s} {'Mean':>8s}  (GBP k/yr)")
for r in rows:
    print(f"{r[0]:55s} " + " ".join(f"{v/1e3:8.0f}" for v in r[1:]))
