{
  "sources": [
    {
      "url": "https://raw.githubusercontent.com/gtmcknight/amm-sim/e65314f88de945f6d055bfc2aab01c482ab6570e/research/solved/README.md",
      "quote": "A policy sees what afterSwap sees. `run.py` enforces it; a result from any other harness needs the same blindness.\n- Tune on replica seeds 60000 and up. Score once on 10000..12047 and report the `paired{...}` line `pair.py` gives against the nearest reference in `ref/`.\n- Every Track B result carries its misread rate, its price error and what the idea would cost in gas.\n- What Track B wins by 0.3 or more goes into Track A's next port."
    },
    {
      "url": "https://raw.githubusercontent.com/gtmcknight/amm-sim/e65314f88de945f6d055bfc2aab01c482ab6570e/research/solved/real.py",
      "quote": "    def on_trade(self, is_buy, ax, ay, t, x, y, flag=None):\n        s = y / x\n        dt = t - self.tlast\n        is_arb = False\n        self.first = dt >= 1\n        if dt >= 1:\n            self.cnt = 0\n            lo = math.log(self.sp * (1.0 - self.fb)); hi = math.log(self.sp / (1.0 - self.fa))   # no-arb interval of the quotes in force\n            m, v = self.m, self.v\n            for _ in range(min(dt - 1, 12)):          # fully silent steps\n                v += SIG2"
    },
    {
      "url": "https://raw.githubusercontent.com/gtmcknight/amm-sim/e65314f88de945f6d055bfc2aab01c482ab6570e/research/solved/run.py",
      "quote": "        if getattr(inner, 'needs_flag', False): out = inner.on_trade(is_buy, ax, ay, t, x, y, arb)   # diagnostic policies only\n        else: out = inner.on_trade(is_buy, ax, ay, t, x, y)\n        if getattr(inner, 'first', False):\n            self.nfirst += 1\n            if bool(getattr(inner, 'is_arb', False)) != bool(arb): self.nmis += 1\n        m = getattr(inner, 'm', None)\n        if m is not None:\n            e = (m - math.log(p)) * 1e4"
    },
    {
      "url": "https://raw.githubusercontent.com/gtmcknight/amm-sim/e65314f88de945f6d055bfc2aab01c482ab6570e/research/solved/pair.py",
      "quote": "    m, se = stats([x[0] for x in d])\n    worst = sorted(d)[:5]\n    print(run.line('candidate', [a[s] for s in seeds]))\n    print(run.line('reference', [b[s] for s in seeds]))\n    print(json.dumps({'paired': {'n': len(seeds), 'mean_diff': round(m, 4), 'se': round(se, 4), 'worst5': [[s, round(v, 3)] for v, s in worst]}}))"
    },
    {
      "url": "https://raw.githubusercontent.com/gtmcknight/amm-sim/e65314f88de945f6d055bfc2aab01c482ab6570e/research/solved/replica2.py",
      "quote": "                    nrx = x1 + ax * g; nry = K / nrx; yo = y1 - nry\n                    prof = yo - ax * p\n                    if yo > 0 and prof > 0:\n                        x1, y1 = nrx, nry; e1 -= prof; arb1 -= prof; av1 += yo; fees1 += ax * fb * p\n                        fb, fa = policy.on_trade(True, ax, yo, t, x1, y1, True, p, y2 / x2, 0) if wantj else policy.on_trade(True, ax, yo, t, x1, y1, True, p, y2 / x2)\n                        fb, fa = clampf(fb), clampf(fa)"
    },
    {
      "url": "https://raw.githubusercontent.com/gtmcknight/amm-sim/e65314f88de945f6d055bfc2aab01c482ab6570e/research/solved/evalpol.py",
      "quote": "def stats(d):\n    n = len(d); m = sum(d) / n\n    sd = (sum((v - m) ** 2 for v in d) / (n - 1)) ** 0.5\n    return m, sd / n ** 0.5\n\n\nif __name__ == '__main__':\n    paths = sys.argv[1].split(','); mode = sys.argv[2]\n    pool = None\n    if True:\n        if mode == 'hold':\n            seeds = list(range(10000, 12048))\n            rows = json.load(open(CALIB)); ref = {}\n            for r in rows: ref.setdefault(r['kind'], {})[r['seed']] = r\n            for path in paths:"
    },
    {
      "url": "https://raw.githubusercontent.com/gtmcknight/amm-sim/e65314f88de945f6d055bfc2aab01c482ab6570e/research/solved/par.py",
      "quote": "def pmap(fn, tasks):\n    tasks = list(tasks)\n    n = int(os.environ.get('PROCS', '0') or 0) or (os.cpu_count() or 1)\n    if n <= 1 or len(tasks) <= 1:\n        return [fn(t) for t in tasks]\n    try:\n        from multiprocessing import Pool\n        pool = Pool(min(n, len(tasks)))\n    except Exception:\n        return [fn(t) for t in tasks]\n    try:\n        return pool.map(fn, tasks, chunksize=1)\n    finally:\n        pool.close(); pool.join()"
    },
    {
      "url": "https://raw.githubusercontent.com/gtmcknight/amm-sim/e65314f88de945f6d055bfc2aab01c482ab6570e/research/solved/latt.py",
      "quote": "HC = 2.5e-4            # cell width (log)\nNC = 121               # cells: +-150 bps\nN0 = NC // 2\nSIG = 0.000945\n\n\ndef kernel(sig, hc):\n    s = sig / hc; M = int(math.ceil(4.0 * s))\n    w = [math.exp(-0.5 * (k / s) ** 2) for k in range(-M, M + 1)]\n    t = sum(w)\n    return [v / t for v in w], M\n\nKER, KM = kernel(SIG, HC)\n\n\nclass LatticePolicy(RealPolicy):\n    def __init__(self, T, opt=None):\n        RealPolicy.__init__(self, T, opt)"
    },
    {
      "url": "https://raw.githubusercontent.com/gtmcknight/amm-sim/e65314f88de945f6d055bfc2aab01c482ab6570e/research/solved/gates.py",
      "quote": "for seed in (10000, 10001, 10002):\n    a = R.run(seed, DPPolicy(T))['edge']\n    b = R.run(seed, Blind(real.RealPolicy(T, {'soft': 1.0})))['edge']\n    c = R.run(seed, Blind(real.RealPolicy(T, {'soft': 1.0, 'qm': QM})))['edge']\n    d = R.run(seed, Blind(latt.LatticePolicy(T, {'qm': QM})))['edge']\n    print(f'{seed}  {a:.6f}  {b:.6f}  {c:.6f}  {d:.6f}')"
    },
    {
      "url": "https://raw.githubusercontent.com/gtmcknight/amm-sim/e65314f88de945f6d055bfc2aab01c482ab6570e/research/solved/SHA256SUMS",
      "quote": "efbb5ee102db4d312f64565d9bf7232b6becbe02b624164f744a402bbbf2983b  pol_mid.json\n5ff53e42ca87390e5f65d1caa3f7a480e67a8212fb0670274dfd562b05ba1afd  qm_k1e-5.json\n63b8c35048718c37eb0a5dae9e70849fa3af257c335e4a2e3a625c833f1dc1eb  qmdp.py\nb8159b7a7235e3a2b137c95fec27b27fc804aa33eb5e79ec46aef7a32cb96030  README.md\n7fc9f4bdf9a9af094ec2eb0e9a8e8eec8b343c5226bf0ebaa30c798785cb7c58  real.py\n6e37b4e61e1ffa5ffb1e43912ddb8659105989aa986eb48fcd1d7608f8dca685  replica2.py"
    }
  ]
}
