{
  "approach": "Freeze YQ online estimator ablations: sigmaHat=0.000945, lambdaHat=0.8, sizeHat=0.002 (mean size 20 divided by initial Y reserve 10000). Test each separately and all three together; selected sigma. Fixed sizeHat is a constant reserve ratio, not a live 20/reserveY calculation. Preserve the pHat, toxicity, activity, direction, and fee model. Remove update computations and afterSwap storage writes only for frozen estimators.",
  "tried": [
    {
      "params": {
        "variant": "sigma",
        "frozen": {
          "sigmaHat_wad": 945000000000000
        },
        "online_estimators_remaining": 2,
        "code_sha256": "dfaf1fe5aa37191981a17421efceabd6f36d8de2d8d819691d2f3c9d0512580b"
      },
      "seeds": {
        "start": 10000,
        "end": 10499
      },
      "n": 500,
      "mean_diff": -2.4005656543578127,
      "se": 0.07024113840742839,
      "neutral_or_better": false,
      "worst": [
        {
          "seed": 10014,
          "diff": -12.248909976903974
        },
        {
          "seed": 10307,
          "diff": -6.495754978937839
        },
        {
          "seed": 10108,
          "diff": -6.071316003576385
        },
        {
          "seed": 10358,
          "diff": -6.027552461799303
        },
        {
          "seed": 10132,
          "diff": -5.434199309821963
        }
      ]
    },
    {
      "params": {
        "variant": "lambda",
        "frozen": {
          "lambdaHat_wad": 800000000000000000
        },
        "online_estimators_remaining": 2,
        "code_sha256": "0952c70ee2b40acec464d3a95da0f2445198b4ffc35cdf6ca052d7ccfac9a305"
      },
      "seeds": {
        "start": 10000,
        "end": 10499
      },
      "n": 500,
      "mean_diff": -6.226108545095411,
      "se": 0.0632278990804428,
      "neutral_or_better": false,
      "worst": [
        {
          "seed": 10199,
          "diff": -10.753406109412026
        },
        {
          "seed": 10165,
          "diff": -10.274208048828427
        },
        {
          "seed": 10204,
          "diff": -10.207796615401548
        },
        {
          "seed": 10237,
          "diff": -10.164469946959457
        },
        {
          "seed": 10443,
          "diff": -10.070175736570263
        }
      ]
    },
    {
      "params": {
        "variant": "size",
        "frozen": {
          "sizeHat_wad": 2000000000000000
        },
        "online_estimators_remaining": 2,
        "code_sha256": "afe2c710970d00253c5d670f93a3af71441300cf061161f18033db3feb776d3c"
      },
      "seeds": {
        "start": 10000,
        "end": 10499
      },
      "n": 500,
      "mean_diff": -8.260956016422575,
      "se": 0.10618915925952338,
      "neutral_or_better": false,
      "worst": [
        {
          "seed": 10366,
          "diff": -14.643924610339582
        },
        {
          "seed": 10084,
          "diff": -14.601983571085952
        },
        {
          "seed": 10198,
          "diff": -14.442721497348202
        },
        {
          "seed": 10428,
          "diff": -14.242143860266538
        },
        {
          "seed": 10174,
          "diff": -14.162473600365388
        }
      ]
    },
    {
      "params": {
        "variant": "all",
        "frozen": {
          "sigmaHat_wad": 945000000000000,
          "lambdaHat_wad": 800000000000000000,
          "sizeHat_wad": 2000000000000000
        },
        "online_estimators_remaining": 0,
        "code_sha256": "2584d0a56835d9bbb8297410cb8fe8774dd540b103ee453a95a7eb97e0197143"
      },
      "seeds": {
        "start": 10000,
        "end": 10499
      },
      "n": 500,
      "mean_diff": -3.367944651708471,
      "se": 0.07153696377008849,
      "neutral_or_better": false,
      "worst": [
        {
          "seed": 10307,
          "diff": -9.278500118327884
        },
        {
          "seed": 10271,
          "diff": -8.985468753910368
        },
        {
          "seed": 10199,
          "diff": -8.262010819064244
        },
        {
          "seed": 10204,
          "diff": -7.676275687633506
        },
        {
          "seed": 10237,
          "diff": -7.522054698577676
        }
      ]
    }
  ],
  "next": "Retain YQ as incumbent. The selected fixed variant did not clear twice its standard error on the one fresh-seed evaluation; do not promote or retune on those seeds.",
  "code": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.24;\n\nimport {AMMStrategyBase} from \"./AMMStrategyBase.sol\";\nimport {TradeInfo} from \"./IAMMStrategy.sol\";\n\ncontract Strategy is AMMStrategyBase {\n    // --- decay / update constants ---\n    uint256 constant ELAPSED_CAP = 8;\n    uint256 constant SIGNAL_THRESHOLD = WAD / 500; // ~20 bps of reserve\n    uint256 constant DIR_DECAY = 800000000000000000; // 0.80\n    uint256 constant ACT_DECAY = 700000000000000000; // 0.70\n    uint256 constant SIZE_DECAY = 700000000000000000; // 0.70\n    uint256 constant TOX_DECAY = 910000000000000000; // 0.91\n    uint256 constant SIGMA_DECAY = 824000000000000000; // 0.824\n    uint256 constant LAMBDA_DECAY = 990000000000000000; // 0.99\n    uint256 constant SIZE_BLEND_DECAY = 818000000000000000; // 0.818\n    uint256 constant TOX_BLEND_DECAY = 51000000000000000; // 0.051\n    uint256 constant ACT_BLEND_DECAY = 985000000000000000; // 0.985\n    uint256 constant PHAT_ALPHA = 260000000000000000; // 0.26\n    uint256 constant PHAT_ALPHA_RETAIL = 50000000000000000; // 0.05\n    uint256 constant DIR_IMPACT_MULT = 2;\n\n    // --- Adaptive Shock Gate ---\n    uint256 constant GATE_SIGMA_MULT = 10 * WAD;\n    uint256 constant MIN_GATE = 30000000000000000; // 0.03 WAD\n\n    // --- Cubic Toxicity ---\n    uint256 constant TOX_CUBIC_COEF = 15000 * BPS;\n\n    // --- Trade-Tox Boost ---\n    uint256 constant TRADE_TOX_BOOST = 2500 * BPS;\n\n    // --- Asymmetric Stale Dir ---\n    uint256 constant STALE_ATTRACT_FRAC = 1124000000000000000; // 1.124\n\n    // --- state caps ---\n    uint256 constant RET_CAP = WAD / 10; // 10%\n    uint256 constant TOX_CAP = WAD / 5; // 20%\n    uint256 constant TRADE_RATIO_CAP = WAD / 5; // 20%\n    uint256 constant LAMBDA_CAP = 5 * WAD; // max 5 trades/step estimate\n    uint256 constant STEP_COUNT_CAP = 64; // guardrail\n\n    // --- fee model constants ---\n    uint256 constant BASE_FEE = 3 * BPS;\n    uint256 constant SIGMA_COEF = 200000000000000000; // 0.20\n    uint256 constant LAMBDA_COEF = 12 * BPS;\n    uint256 constant FLOW_SIZE_COEF = 4842 * BPS;\n    uint256 constant TOX_COEF = 250 * BPS;\n    uint256 constant TOX_QUAD_COEF = 11700 * BPS;\n    uint256 constant ACT_COEF = 91843 * BPS;\n    uint256 constant DIR_COEF = 20 * BPS;\n    uint256 constant DIR_TOX_COEF = 100 * BPS;\n    uint256 constant STALE_DIR_COEF = 6850 * BPS;\n    uint256 constant SIGMA_TOX_COEF = 500 * BPS;\n    uint256 constant TAIL_KNEE = 500 * BPS;\n    uint256 constant TAIL_SLOPE_PROTECT = 930000000000000000; // 0.93\n    uint256 constant TAIL_SLOPE_ATTRACT = 955000000000000000; // 0.955\n\n    // slots[0] = bid fee\n    // slots[1] = ask fee\n    // slots[2] = last timestamp\n    // slots[3] = dirState (centered at WAD, [0, 2*WAD])\n    // slots[4] = actEma\n    // slots[5] = pHat\n    // slots[6] = sigmaHat\n    // slots[7] = lambdaHat\n    // slots[8] = sizeHat\n    // slots[9] = toxEma\n    // slots[10] = stepTradeCount (raw integer)\n\n    function afterInitialize(uint256 initialX, uint256 initialY) external override returns (uint256, uint256) {\n        slots[0] = BASE_FEE;\n        slots[1] = BASE_FEE;\n        slots[2] = 0;\n        slots[3] = WAD; // neutral direction\n        slots[4] = 0;\n        slots[5] = initialX > 0 ? wdiv(initialY, initialX) : 100 * WAD;\n        slots[6] = 945000000000000; // frozen midpoint sigma\n        slots[7] = 800000000000000000; // 0.8 initial arrival-rate guess\n        slots[8] = 2000000000000000; // 0.2% reserve-size ratio guess\n        slots[9] = 0;\n        slots[10] = 0;\n        return (BASE_FEE, BASE_FEE);\n    }\n\n    function afterSwap(TradeInfo calldata trade) external override returns (uint256, uint256) {\n        uint256 prevBidFee = slots[0];\n        uint256 prevAskFee = slots[1];\n        uint256 lastTs = slots[2];\n        uint256 dirState = slots[3];\n        uint256 actEma = slots[4];\n        uint256 pHat = slots[5];\n        uint256 sigmaHat = 945000000000000; // frozen sigma 0.000945\n        uint256 lambdaHat = slots[7];\n        uint256 sizeHat = slots[8];\n        uint256 toxEma = slots[9];\n        uint256 stepTradeCount = slots[10];\n\n        bool isNewStep = trade.timestamp > lastTs;\n        if (isNewStep) {\n            uint256 elapsedRaw = trade.timestamp - lastTs;\n            uint256 elapsed = elapsedRaw > ELAPSED_CAP ? ELAPSED_CAP : elapsedRaw;\n\n            dirState = _decayCentered(dirState, DIR_DECAY, elapsed);\n            actEma = wmul(actEma, _powWad(ACT_DECAY, elapsed));\n            sizeHat = wmul(sizeHat, _powWad(SIZE_DECAY, elapsed));\n            toxEma = wmul(toxEma, _powWad(TOX_DECAY, elapsed));\n\n            if (stepTradeCount > 0 && elapsedRaw > 0) {\n                uint256 lambdaInst = (stepTradeCount * WAD) / elapsedRaw;\n                if (lambdaInst > LAMBDA_CAP) lambdaInst = LAMBDA_CAP;\n                lambdaHat = wmul(lambdaHat, LAMBDA_DECAY) + wmul(lambdaInst, WAD - LAMBDA_DECAY);\n            }\n\n            stepTradeCount = 0;\n        }\n\n        bool firstInStep = stepTradeCount == 0;\n\n        uint256 spot = trade.reserveX > 0 ? wdiv(trade.reserveY, trade.reserveX) : pHat;\n        if (pHat == 0) pHat = spot;\n\n        uint256 feeUsed = trade.isBuy ? prevBidFee : prevAskFee;\n        uint256 gamma = feeUsed < WAD ? WAD - feeUsed : 0;\n        uint256 pImplied;\n        if (gamma == 0) {\n            pImplied = spot;\n        } else {\n            pImplied = trade.isBuy ? wmul(spot, gamma) : wdiv(spot, gamma);\n        }\n\n        {\n            uint256 ret = pHat > 0 ? wdiv(absDiff(pImplied, pHat), pHat) : 0;\n            uint256 alpha = firstInStep ? PHAT_ALPHA : PHAT_ALPHA_RETAIL;\n            uint256 adaptiveGate = wmul(sigmaHat, GATE_SIGMA_MULT);\n            if (adaptiveGate < MIN_GATE) adaptiveGate = MIN_GATE;\n            if (ret <= adaptiveGate) {\n                pHat = wmul(pHat, WAD - alpha) + wmul(pImplied, alpha);\n            }\n        }\n\n        uint256 tradeRatio = trade.reserveY > 0 ? wdiv(trade.amountY, trade.reserveY) : 0;\n        if (tradeRatio > TRADE_RATIO_CAP) tradeRatio = TRADE_RATIO_CAP;\n\n        if (tradeRatio > SIGNAL_THRESHOLD) {\n            uint256 push = tradeRatio * DIR_IMPACT_MULT;\n            if (push > WAD / 4) push = WAD / 4;\n\n            if (trade.isBuy) {\n                dirState = dirState + push;\n                if (dirState > 2 * WAD) dirState = 2 * WAD;\n            } else {\n                dirState = dirState > push ? dirState - push : 0;\n            }\n\n            actEma = wmul(actEma, ACT_BLEND_DECAY) + wmul(tradeRatio, WAD - ACT_BLEND_DECAY);\n\n            sizeHat = wmul(sizeHat, SIZE_BLEND_DECAY) + wmul(tradeRatio, WAD - SIZE_BLEND_DECAY);\n            if (sizeHat > WAD) sizeHat = WAD;\n        }\n\n        uint256 tox = pHat > 0 ? wdiv(absDiff(spot, pHat), pHat) : 0;\n        if (tox > TOX_CAP) tox = TOX_CAP;\n        toxEma = wmul(toxEma, TOX_BLEND_DECAY) + wmul(tox, WAD - TOX_BLEND_DECAY);\n        uint256 toxSignal = toxEma;\n\n        stepTradeCount = stepTradeCount + 1;\n        if (stepTradeCount > STEP_COUNT_CAP) stepTradeCount = STEP_COUNT_CAP;\n\n        uint256 flowSize = wmul(lambdaHat, sizeHat);\n        uint256 fBase = BASE_FEE + wmul(SIGMA_COEF, sigmaHat) + wmul(LAMBDA_COEF, lambdaHat) + wmul(FLOW_SIZE_COEF, flowSize);\n        uint256 fMid = fBase + wmul(TOX_COEF, toxSignal) + wmul(TOX_QUAD_COEF, wmul(toxSignal, toxSignal)) + wmul(ACT_COEF, actEma);\n\n        fMid = fMid + wmul(SIGMA_TOX_COEF, wmul(sigmaHat, toxSignal));\n\n        // Cubic toxicity\n        {\n            uint256 toxCubed = wmul(toxSignal, wmul(toxSignal, toxSignal));\n            fMid = fMid + wmul(TOX_CUBIC_COEF, toxCubed);\n        }\n\n        uint256 dirDev;\n        bool sellPressure;\n        if (dirState >= WAD) {\n            dirDev = dirState - WAD;\n            sellPressure = true;\n        } else {\n            dirDev = WAD - dirState;\n            sellPressure = false;\n        }\n\n        uint256 skew = wmul(DIR_COEF, dirDev) + wmul(DIR_TOX_COEF, wmul(dirDev, toxSignal));\n\n        uint256 bidFee;\n        uint256 askFee;\n        if (sellPressure) {\n            bidFee = fMid + skew;\n            askFee = fMid > skew ? fMid - skew : 0;\n        } else {\n            askFee = fMid + skew;\n            bidFee = fMid > skew ? fMid - skew : 0;\n        }\n\n        // Directional protection using stale-price sign\n        {\n            uint256 staleShift = wmul(STALE_DIR_COEF, toxSignal);\n            uint256 attractShift = wmul(staleShift, STALE_ATTRACT_FRAC);\n            if (spot >= pHat) {\n                bidFee = bidFee + staleShift;\n                askFee = askFee > attractShift ? askFee - attractShift : 0;\n            } else {\n                askFee = askFee + staleShift;\n                bidFee = bidFee > attractShift ? bidFee - attractShift : 0;\n            }\n        }\n\n        // Trade-aligned toxicity boost\n        {\n            bool tradeAligned = (trade.isBuy && spot >= pHat) || (!trade.isBuy && spot < pHat);\n            if (tradeAligned) {\n                uint256 tradeBoost = wmul(TRADE_TOX_BOOST, tradeRatio);\n                if (trade.isBuy) {\n                    bidFee = bidFee + tradeBoost;\n                } else {\n                    askFee = askFee + tradeBoost;\n                }\n            }\n        }\n\n        // Asymmetric tail compression\n        if (sellPressure) {\n            bidFee = clampFee(_compressTailWithSlope(bidFee, TAIL_SLOPE_PROTECT));\n            askFee = clampFee(_compressTailWithSlope(askFee, TAIL_SLOPE_ATTRACT));\n        } else {\n            askFee = clampFee(_compressTailWithSlope(askFee, TAIL_SLOPE_PROTECT));\n            bidFee = clampFee(_compressTailWithSlope(bidFee, TAIL_SLOPE_ATTRACT));\n        }\n\n        slots[0] = bidFee;\n        slots[1] = askFee;\n        slots[2] = trade.timestamp;\n        slots[3] = dirState;\n        slots[4] = actEma;\n        slots[5] = pHat;\n        slots[7] = lambdaHat;\n        slots[8] = sizeHat;\n        slots[9] = toxEma;\n        slots[10] = stepTradeCount;\n\n        return (bidFee, askFee);\n    }\n\n    function _compressTailWithSlope(uint256 fee, uint256 slope) internal pure returns (uint256) {\n        if (fee <= TAIL_KNEE) return fee;\n        return TAIL_KNEE + wmul(fee - TAIL_KNEE, slope);\n    }\n\n    function _powWad(uint256 factor, uint256 exp) internal pure returns (uint256 result) {\n        result = WAD;\n        while (exp > 0) {\n            if (exp & 1 == 1) result = wmul(result, factor);\n            factor = wmul(factor, factor);\n            exp >>= 1;\n        }\n    }\n\n    function _decayCentered(uint256 centered, uint256 decayFactor, uint256 elapsed) internal pure returns (uint256) {\n        uint256 mul = _powWad(decayFactor, elapsed);\n        if (centered >= WAD) {\n            return WAD + wmul(centered - WAD, mul);\n        }\n        uint256 below = wmul(WAD - centered, mul);\n        return below < WAD ? WAD - below : 0;\n    }\n\n    function getName() external pure override returns (string memory) {\n        return \"yq\";\n    }\n}\n",
  "code_sha256": "dfaf1fe5aa37191981a17421efceabd6f36d8de2d8d819691d2f3c9d0512580b",
  "delta": "--- YQ.sol\n+++ Strategy.sol\n@@ -79 +79 @@\n-        slots[6] = 950000000000000; // 0.095% initial sigma guess\n+        slots[6] = 945000000000000; // frozen midpoint sigma\n@@ -94 +94 @@\n-        uint256 sigmaHat = slots[6];\n+        uint256 sigmaHat = 945000000000000; // frozen sigma 0.000945\n@@ -140,4 +139,0 @@\n-            }\n-            if (firstInStep) {\n-                if (ret > RET_CAP) ret = RET_CAP;\n-                sigmaHat = wmul(sigmaHat, SIGMA_DECAY) + wmul(ret, WAD - SIGMA_DECAY);\n@@ -250 +245,0 @@\n-        slots[6] = sigmaHat;\n",
  "score": {
    "seed_start": 0,
    "n": 1000,
    "mean_edge": 522.1343535539108,
    "se": 2.80317311189271
  },
  "paired": {
    "reference": "YQ 00fae418",
    "seed_start": 500000,
    "n": 2000,
    "mean_diff": -2.51211668182504,
    "se": 0.03551496758242408,
    "worst": [
      {
        "seed": 500772,
        "diff": -13.827445217702063
      },
      {
        "seed": 500887,
        "diff": -13.366904789666023
      },
      {
        "seed": 500767,
        "diff": -7.243211342358109
      },
      {
        "seed": 500612,
        "diff": -6.839491937699449
      },
      {
        "seed": 501424,
        "diff": -6.6936732223372815
      }
    ]
  },
  "seconds_per_simulation": 0.23609232089777166,
  "validate_ok": true,
  "blocked": null,
  "citations": [
    {
      "url": "https://raw.githubusercontent.com/jiayaoqijia/amm-challenge-yq/3e9dc7d5a6192c88b562a0f8d9c172ca566ec94d/contracts/src/Strategy.sol",
      "supports": "Pinned YQ reference code and original estimator update equations; fetched source SHA-256 verified."
    },
    {
      "url": "https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_competition/competition/config.py",
      "supports": "Canonical simulation parameters, initial reserves, and parameter ranges."
    },
    {
      "url": "https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_competition/competition/match.py",
      "supports": "Canonical NumPy parameter draw order and per-simulation seeds; offset generator checked against seeds 0..4."
    },
    {
      "url": "https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_sim_rs/src/evm/strategy.rs",
      "supports": "Rust execution enforces 250000 gas for initialization and trade callbacks."
    },
    {
      "url": "https://raw.githubusercontent.com/dipankar/amm-challenge/main/ANALYSIS.md",
      "supports": "Public state-estimation write-up; background only, no reported score imported into our measurements."
    },
    {
      "url": "https://www.linkedin.com/pulse/amm-fee-challenge-notes-from-our-run-optimization-arena-qe3ff",
      "supports": "Algebra public write-up describes environment estimation and asymmetric fees; background only, no reported score imported."
    }
  ],
  "selection": {
    "rule": "Neutral if mean_diff >= -2*SE on tuning seeds; prefer fewest online estimators, tie break highest mean_diff. If no variant qualifies, deliver least harmful negative result without promotion.",
    "selected": "sigma",
    "fixed_before_fresh_seeds": true,
    "promotion": false,
    "count_definition": "online_estimators_remaining counts only sigmaHat, lambdaHat, and sizeHat; pHat and other original state remain adaptive.",
    "tuning_outcome": "No variant qualified as neutral; deliver sigma-only as the least harmful negative result."
  },
  "scoreboard": {
    "incumbent": {
      "name": "YQ",
      "code_sha256": "00fae418c49cd681ad0879c6f96ebed0163fab5a660b38030f11e6947a39afd4",
      "score": {
        "seed_start": 500000,
        "n": 2000,
        "mean_edge": 522.541516491237,
        "se": 2.0876362472072256
      }
    },
    "selected_fresh_score": {
      "code_sha256": "dfaf1fe5aa37191981a17421efceabd6f36d8de2d8d819691d2f3c9d0512580b",
      "seed_start": 500000,
      "n": 2000,
      "mean_edge": 520.0293998094119,
      "se": 2.101499087732311
    },
    "ranked_promotions": []
  },
  "measurement": {
    "engine_commit": "6b6921f8d463314aa7c96234e1be25a25245f704",
    "comparison": "Each candidate and YQ run separately as submission against VanillaStrategy (30 bps); pair submission edges by seed. Not candidate-versus-YQ in the same market.",
    "se": "Sample standard deviation (ddof=1) divided by sqrt(n); paired SE uses per-seed edge differences.",
    "config": "numpy.random.default_rng(seed) draws Uniform(19,21) mean size, Uniform(0.6,1.0) rate, Uniform(0.000882,0.001008) sigma in that order, then canonical build_config(seed=seed); remaining defaults unchanged. Full 10000-step runs.",
    "heldout": "Selected source and SHA locked before the fresh block. Every held-out seed evaluated once per strategy; no tuning on held-out outcomes.",
    "compiler": {
      "solc": "0.8.24",
      "optimizer_runs": 200,
      "viaIR": true,
      "evmVersion": "paris"
    },
    "workers": 3,
    "batch_size": 25,
    "timing": "seconds_per_simulation is combined wall time of final candidate fresh and score run_batch calls / 3000 with 3 workers; includes config construction, excludes compilation/toolchain/reference runs.",
    "validation": "amm-match validate passed StarterStrategy, YQ, and all four ablations. Unmodified Rust engine applies 250000 gas limits during every measured callback. Only inherited slots[0..10] are used; no history loops added.",
    "scope": "One assigned simplification idea. No leaderboard claim, no official submission, no starter comparison, no additional rounds."
  },
  "interpretation": "All four prescribed midpoint freezes failed tuning neutrality. In YQ, sigmaHat measures implied-price deviation from lagged pHat, lambdaHat counts observed trades per elapsed step, and sizeHat is a thresholded, decaying reserve-ratio signal. These are calibrated proxy signals rather than direct estimates of the three latent process parameters. The measurements reject these exact midpoint replacements; they do not establish that every possible constant replacement would lose."
}
