{
  "credit": "Taylor (gtmcknight), github.com/gtmcknight/amm-sim@4812a6382311624e1506aca131ca35c378f930d9 research/solved: original replica, Gaussian filter, fee rule, uncertainty tables and exact-arithmetic arb/retail tell. This experiment adds a deterministic second-order fee-center correction to the credited fast LEAN port.",
  "mech": "second-order: residual log-price-to-price center shift, implemented as opposite ask/bid fee adjustments proportional to posterior log-price variance",
  "already_present": {
    "present": false,
    "where": {
      "url": "https://api.imd.fun/artifacts/efb8e653f8fd061cecf3e92b186470c9c96c5188523364b60ed101e9672869aa",
      "encoding": "reference_zb64 is zlib-compressed base64 JSON mapping filenames to source",
      "reviewed": [
        "leanbase.py:Policy",
        "stalerival.py:Policy.on_trade and round_fees",
        "real.py:RealPolicy.lookup and on_trade",
        "tellpol.py:TellPolicy.on_trade"
      ]
    },
    "finding": "No explicit +/- c*variance fee add-on occurs in these four policies. real.py already uses exp to convert log-distance to fees and interpolates Q tables using posterior sd; those may already account for uncertainty effects implicitly. This tests a residual output-fee adjustment; it does not replace those conversions or tables."
  },
  "definition": {
    "formula": "v = posterior Var[ln(price)] = sd_log_price^2 after the existing filter update. delta = c*v; bid_raw_new = bid_raw - delta; ask_raw_new = ask_raw + delta. Both pass through the existing fee_round (truncate to wad, clamp to [0,0.1], avoid Vanilla gamma collision).",
    "units": "c dimensionless; v in squared natural-log price units; delta in fractional fee units. If sd_bps=1e4*sd_log_price, delta_bps = c*sd_bps^2/1e4. Positive c moves both quote prices upward: bid fee decreases, ask fee increases. Before clamping the ask-minus-bid fee difference increases by 2*c*v.",
    "application": "Every afterSwap callback after final lookup including the staleness pull, before rounding. Corrected fees are retained in the existing state and affect future observations and routing. The initial fees are unchanged because v=0 at initialization. When v=0 the mechanism has no effect.",
    "parameters": {
      "swept": [
        "c"
      ],
      "chosen_c": 1.0,
      "additional_gating_or_caps": null
    },
    "state_kept": "No new state. Reuse the existing posterior variance sf[1] (self.v in Python reference). Coefficient is a compile-time constant in a potential contract; only its replica sweep representation is an array.",
    "observability": "Only the existing filter state derived from side, amounts, timestamp and own reserves; no oracle price, rival state, run sigma/rate, or hidden arb label is used.",
    "baseline_options": {
      "LEAN": {
        "a0": 0.35,
        "ainf": 0.35,
        "tau": 2.0
      },
      "AGE": {
        "a0": 0.0,
        "ainf": 0.7,
        "tau": 1.0
      },
      "both": {
        "soft": 1,
        "qm": "qm_k1e-5.json",
        "policy": "leanbase:Policy variant A"
      }
    },
    "selection": {
      "seeds": [
        64000,
        65023
      ],
      "round_size": 128,
      "max_settings": 12,
      "drop": "At each cumulative round, stop that setting iff mean_diff+3*se<0.",
      "choose": "Highest cumulative paired mean among surviving settings after all 1024 selection seeds; ties use the listed coefficient order.",
      "se": "Sample standard deviation of paired per-seed differences (ddof=1) divided by sqrt(n).",
      "report": "Freeze c once; evaluate candidate and LEAN once each on 10000..12047, then the same c stacked on AGE and AGE once each on 10000..12047. No report tuning."
    },
    "reproduce": {
      "dependencies": "CPython 3.12.3; numpy==2.2.6, llvmlite==0.44.0, numba==0.61.2.",
      "base": "https://api.imd.fun/artifacts/a88ff982eba7e078bc6ccb1555b90a211c00c9fd76ec90a62ab13a4074f2c3d0",
      "tables_and_reference": "https://api.imd.fun/artifacts/efb8e653f8fd061cecf3e92b186470c9c96c5188523364b60ed101e9672869aa",
      "patch": "Decode patch_zb64 as base64 then zlib, apply unified diff to original fastlean.py. fastlean.load(amm_dir); fastlean.run_batch(tabs, range(seed0,seed0+n), [c]). None is baseline. AGE tabs use load(amm_dir,a0=0,ainf=0.7,tau=1). Default n_steps=10000.",
      "market_loop": "Unmodified fast LEAN replica, including its existing constant-k approximation; no engine-faithfulness change."
    },
    "provenance": {
      "replica_artifact_sha256": "a88ff982eba7e078bc6ccb1555b90a211c00c9fd76ec90a62ab13a4074f2c3d0",
      "original_fastlean_sha256": "d9c758bb280fe24e09c039496f84ab0804173ff70cec24c332f8683f28efa582",
      "patched_fastlean_sha256": "87718f23022d275b20c5f0639e01206349e15ec237f1b6643fd53971e1dcb159",
      "reference_artifact_sha256": "efb8e653f8fd061cecf3e92b186470c9c96c5188523364b60ed101e9672869aa",
      "policy_sha256": {
        "leanbase.py": "ba015769f7981d7798acbbc9aff8c0e0c59825bb1541d55bcdef49311d7a04bc",
        "stalerival.py": "d124a171d35b024daed840e5449d352b2b5e50a68ce98c8a9aaafaf3419d23da",
        "real.py": "6a4a0f0e7f8b43d38e5eaa9d8b2e847a0771aee7f89551b122fe89bfe7969b5f",
        "tellpol.py": "7d5961c45bec56292533e65607245ad119b8733c760a5bf28f76a2e07bc3960b"
      },
      "table_sha256": {
        "pol_mid.json": "efbb5ee102db4d312f64565d9bf7232b6becbe02b624164f744a402bbbf2983b",
        "qm_k1e-5.json": "5ff53e42ca87390e5f65d1caa3f7a480e67a8212fb0670274dfd562b05ba1afd"
      }
    },
    "checks": {
      "baseline_reference_parity": {
        "n": 128,
        "seeds": [
          63000,
          63127
        ],
        "bit_identical": 128,
        "max_edge_abs_diff": 0.0,
        "zero_linear_and_hidden_hook_arrays_match_baseline": true
      },
      "chosen_reference_checks": {
        "credit": "Taylor (gtmcknight), github.com/gtmcknight/amm-sim@4812a6382311624e1506aca131ca35c378f930d9 research/solved: replica, Gaussian filter, fee rule, qm tables, arb/retail tell",
        "c": 1.0,
        "counts": {
          "callbacks": 171355,
          "labels": 171355,
          "label_mismatches": 0,
          "fee_mismatches": 0,
          "max_fee_abs_diff_bps": 0.0,
          "max_edge_abs_diff": 0.0
        },
        "rows": [
          {
            "profile": "LEAN",
            "seeds": [
              63000,
              63001,
              63002,
              63003,
              63004,
              63005,
              63006,
              63007
            ],
            "max_edge_abs_diff": 0.0,
            "edges_bit_identical": 8
          },
          {
            "profile": "AGE",
            "seeds": [
              63000,
              63001,
              63002,
              63003,
              63004,
              63005,
              63006,
              63007
            ],
            "max_edge_abs_diff": 0.0,
            "edges_bit_identical": 8
          }
        ],
        "passed": true,
        "note": "Shadow compiled callback state receives the same raw trades as independent CPython; full compiled market-loop edges checked separately."
      },
      "method": "Independent CPython subclass changes round_fees by (-c*self.v,+c*self.v). Its compiled shadow receives every identical raw callback, compares both returned fees, and checks tell labels against the big-integer reference. Full independently simulated edges are also compared on eight seeds for each of LEAN and AGE. These auxiliary checks use 63000..63007, outside selection/report ranges.",
      "statistics": "All settings, seed continuity, stopping decisions, report positive counts and paired means/SEs independently recomputed with Python statistics from recorded per-seed rows."
    }
  },
  "settings_tried": [
    {
      "parameters": {
        "c": -64.0
      },
      "n": 128,
      "mean_diff": -0.08366300469285104,
      "se": 0.027128049966811556,
      "dropped_early": true
    },
    {
      "parameters": {
        "c": -16.0
      },
      "n": 1024,
      "mean_diff": -0.012092448294904967,
      "se": 0.004959753284412104,
      "dropped_early": false
    },
    {
      "parameters": {
        "c": -4.0
      },
      "n": 1024,
      "mean_diff": -0.0019862228653640956,
      "se": 0.0034900251295530912,
      "dropped_early": false
    },
    {
      "parameters": {
        "c": -1.0
      },
      "n": 1024,
      "mean_diff": -0.0013569862278070555,
      "se": 0.002735398763651009,
      "dropped_early": false
    },
    {
      "parameters": {
        "c": -0.5
      },
      "n": 1024,
      "mean_diff": -0.005214505364690036,
      "se": 0.0027352228156271377,
      "dropped_early": false
    },
    {
      "parameters": {
        "c": 0.5
      },
      "n": 1024,
      "mean_diff": 0.0014973900263651196,
      "se": 0.0024431027431759257,
      "dropped_early": false
    },
    {
      "parameters": {
        "c": 1.0
      },
      "n": 1024,
      "mean_diff": 0.0017603266840114928,
      "se": 0.0028272787145427664,
      "dropped_early": false
    },
    {
      "parameters": {
        "c": 4.0
      },
      "n": 1024,
      "mean_diff": -0.0016273724442373294,
      "se": 0.0032657132822349034,
      "dropped_early": false
    },
    {
      "parameters": {
        "c": 16.0
      },
      "n": 1024,
      "mean_diff": -0.010855015362286613,
      "se": 0.005178456809687169,
      "dropped_early": false
    },
    {
      "parameters": {
        "c": 64.0
      },
      "n": 128,
      "mean_diff": -0.10037938112898326,
      "se": 0.02490677097839402,
      "dropped_early": true
    },
    {
      "parameters": {
        "c": 256.0
      },
      "n": 128,
      "mean_diff": -0.961030033161185,
      "se": 0.06156594720394303,
      "dropped_early": true
    },
    {
      "parameters": {
        "c": 1024.0
      },
      "n": 128,
      "mean_diff": -286.54915988549567,
      "se": 5.3403176893689706,
      "dropped_early": true
    }
  ],
  "chosen": {
    "parameters": {
      "c": 1.0
    },
    "n": 1024,
    "mean_diff": 0.0017603266840114928,
    "se": 0.0028272787145427664,
    "dropped_early": false,
    "selection_rule": "Highest paired training mean among non-dropped settings; chosen before held-out reports."
  },
  "report": {
    "vs_lean": {
      "n": 2048,
      "mean_diff": 0.004102156314634814,
      "se": 0.001829279029610601,
      "seeds_positive": 1021,
      "seed_range": [
        10000,
        12047
      ],
      "baseline_mean_edge": 536.603390956178,
      "candidate_mean_edge": 536.6074931124926
    },
    "on_age": {
      "n": 2048,
      "mean_diff": -0.0005802840008916621,
      "se": 0.0018498706799454404,
      "seeds_positive": 986,
      "seed_range": [
        10000,
        12047
      ],
      "baseline_mean_edge": 536.6231002985178,
      "candidate_mean_edge": 536.6225200145168
    }
  },
  "promote": false,
  "contract_cost": {
    "storage_words": 0,
    "gas": {
      "estimated_added_per_callback_range": [
        50,
        1000
      ],
      "measured": false
    },
    "characters": {
      "estimated_added_source_range": [
        100,
        400
      ],
      "measured": false
    },
    "runtime_bytes": {
      "estimated_added_range": [
        40,
        250
      ],
      "measured": false
    },
    "basis": "Engineering estimates, not measured bounds: c=1 reuses v without squaring sd or adding sqrt/exp, then applies saturating bid subtraction and ask addition before existing clamps. Requires v in the appropriate fee scale or one fixed rescale; no extra persistent storage or external call. Generic 20-feature replica instrumentation is not needed in a contract port.",
    "headroom_as_given_in_task": {
      "gas": 37000,
      "runtime_bytes": 2136,
      "source_characters": 2770
    },
    "assessment": "Arithmetic-only implementation is expected to fit the stated headroom, but only a Solidity build and engine gas run can establish it."
  },
  "patch_zb64": "eNqlVG1v2zYQ/u5f8SBFEMmS3NhOmsKZi6RFgg4oUmAttg+GYVDi0SKqUAZJR82/31FyEtnLMGCVAfPIOz739hyzLIN4q4TzFQkz2jwOkiRBvndydYXsfHqRvkcSlvEZrq4GOPy0gsIHOC/8QqeYLmc9GXOo/SutzqQ4XSKZYzw6HfSRytrMBlnfXuYppGCcsq5/RIqET9HE+zYqD1gyv4QSrSQGyUGIzciVYkMLdjtnt7N9g/C9wSdLUnt8F49VbRGt/X3xw+h16eMUlhwJW5RvXV09kJyBfmrntVlD6cqTxYOwWpiCRq8hfzWEoialdKHJ+Bm2m0ZYiY3VBWUFHzGCK7Xi5LSBsqLwujaigiLC1mjvXsEtamupNeTyNCG1IZxajJfBZRE2clXV61XrZTic/BOBC5fNezhPBXw52b9DlaNXSvcvPXrN3WGf2vO8SVlnA1mIVrbeGhmpPO6UIijFgVLslE4vzgPJ8uYyyO+CLIKsFh35cttuztqNsC2nLybv0/E5kovpWVj7pG4o9NuFenbS4mQnnCw7q4Z1ZjMS1orHaKfj7P3jhuZ8rqpa+Hdn8YgJEwgXZeNdqKFJFZnoibxMSlN7RCYQcsLdiu5ub66/I8E4RuCf4amaQBgZxAyTGMcvNtM4XDuN4964WKEd4U9RbenG2tpGT7HPguO1L3EsoR0MaV8y4XgXVdowr2MOxeLz8FgmE0Sf4YUpO9rFJ8Erz+tBhCleItl1ei+hcUjhl1NL/m9qY0SOitrIrLaS9wUH3Es3/cV80cs3kKGqIl60U5oxiFvMseM/Y8f91nnkhO7ayQ7Xkt9ag2aA8JOkYLdmlQtflJEXObPNEUledjDzu9oQZ7RynjZuPj7lL23fWTe/FTyxTxw5Ojr6gzaVLgRIrgm1wpeb67uP199uECXt8KIpyTxPQSgoZxjwYyiuGImibL23z1Rb2hGjdn06hN+EN41tL58DXRRLCCkdoqwYPqRI+D/mAjA0oZ1sflBbwLYQ1yk+pviW4q/JlxS/p7jlEQoV2M2+7I9ixc9x1BYm7o2jNmEYn/sV5s/JuGOek6N7baIYv3VsDXvxk/fc79Puw4eWv0NMJ1zEvwHkW/LO",
  "not_done": [
    "No promotion: both held-out mean gains must be >=0.03; this setting does not meet that rule.",
    "No QLX5 or qt1 engine pairing, no fresh engine seeds, no engine gate E, and no leaderboard submission. Replica LEAN/AGE results are not evidence of a board gain.",
    "No Solidity contract returned, no measured gas/source/runtime cost, and no Solidity gate G or OOG certification. Only replica baseline parity and candidate label/fee parity were checked locally.",
    "No engine-faithfulness port or retuning beyond this one 12-coefficient mechanism; inherited fast-replica limitations remain."
  ]
}
