{
 "approach": "Ablation of CH (6dd68f77) with no retuning: zero each named term alone, measure paired vs CH on seeds 10000..10999 (n=1000, diff=candidate-CH, se=sd/sqrt(n)); keep removals with mean_diff > -1 se. Nothing passed that rule at n=1000. Dir skew (z=-1.36) and sigma*tox (z=-1.22) came close, so both were re-measured on tuning seeds 11000..11999. Pooled over n=2000, sigma*tox comes out at +0.0005 +/- ~0.0014 (free) and is deleted with its constant. Dir skew comes out at -0.0096 +/- ~0.0075 (z=-1.28) and is kept. Every other term costs clearly. Ranked cost of removal on 10000..10999: lambda -38.37 (se 0.11) > ACT -1.70 (0.037) > TOX_QUAD -1.29 (0.044) > trade boost -0.71 (0.026) ~ flow-size -0.70 (0.027) > tail compression -0.33 (0.107) > dir skew -0.018 (0.013) > sigma*tox -0.002 (0.002). Tail slope 0.89/0.89 on the stripped file lost on both tuning sets and was rejected. Frozen candidate = CH minus the sigma*tox term. It is a pure simplification and is neutral on the holdout: +0.0014 +/- 0.0014, which does not clear 2 se, so it is not a promotion on score. It is offered as an equal-score simpler file.",
 "tried": [
  {
   "variant": "remove dir skew (DIR_COEF=0, DIR_TOX_COEF=0)",
   "seeds": "10000..10999",
   "n": 1000,
   "mean_diff": -0.0177,
   "se": 0.013,
   "z": -1.36
  },
  {
   "variant": "remove dir skew (DIR_COEF=0, DIR_TOX_COEF=0)",
   "seeds": "11000..11999",
   "n": 1000,
   "mean_diff": -0.0015,
   "se": 0.0076,
   "z": -0.2
  },
  {
   "variant": "remove sigma*tox term (SIGMA_TOX_COEF)",
   "seeds": "10000..10999",
   "n": 1000,
   "mean_diff": -0.0022,
   "se": 0.0018,
   "z": -1.22
  },
  {
   "variant": "remove sigma*tox term (SIGMA_TOX_COEF)",
   "seeds": "11000..11999",
   "n": 1000,
   "mean_diff": 0.0032,
   "se": 0.0021,
   "z": 1.52
  },
  {
   "variant": "remove TOX_QUAD term",
   "seeds": "10000..10999",
   "n": 1000,
   "mean_diff": -1.2877,
   "se": 0.0439,
   "z": -29.33
  },
  {
   "variant": "remove trade boost (TRADE_TOX_BOOST)",
   "seeds": "10000..10999",
   "n": 1000,
   "mean_diff": -0.7144,
   "se": 0.0264,
   "z": -27.06
  },
  {
   "variant": "remove tail compression (both slopes=1)",
   "seeds": "10000..10999",
   "n": 1000,
   "mean_diff": -0.3257,
   "se": 0.1073,
   "z": -3.04
  },
  {
   "variant": "remove ACT term",
   "seeds": "10000..10999",
   "n": 1000,
   "mean_diff": -1.6961,
   "se": 0.0365,
   "z": -46.47
  },
  {
   "variant": "remove lambda term (LAMBDA_COEF)",
   "seeds": "10000..10999",
   "n": 1000,
   "mean_diff": -38.3714,
   "se": 0.1063,
   "z": -360.97
  },
  {
   "variant": "remove flow-size term (FLOW_SIZE_COEF)",
   "seeds": "10000..10999",
   "n": 1000,
   "mean_diff": -0.6999,
   "se": 0.0265,
   "z": -26.41
  },
  {
   "variant": "remove dir skew AND sigma*tox",
   "seeds": "10000..10999",
   "n": 1000,
   "mean_diff": -0.0155,
   "se": 0.0127,
   "z": -1.22
  },
  {
   "variant": "remove dir skew AND sigma*tox",
   "seeds": "11000..11999",
   "n": 1000,
   "mean_diff": -0.0019,
   "se": 0.0069,
   "z": -0.28
  },
  {
   "variant": "stripped file (sigma*tox code+constant deleted)",
   "seeds": "10000..10999",
   "n": 1000,
   "mean_diff": -0.0022,
   "se": 0.0018,
   "z": -1.22
  },
  {
   "variant": "stripped file (sigma*tox code+constant deleted)",
   "seeds": "11000..11999",
   "n": 1000,
   "mean_diff": 0.0032,
   "se": 0.0021,
   "z": 1.52
  },
  {
   "variant": "stripped + tail slopes 0.89/0.89 (was 0.93/0.85)",
   "seeds": "10000..10999",
   "n": 1000,
   "mean_diff": -0.0269,
   "se": 0.029,
   "z": -0.93
  },
  {
   "variant": "stripped + tail slopes 0.89/0.89 (was 0.93/0.85)",
   "seeds": "11000..11999",
   "n": 1000,
   "mean_diff": -0.0116,
   "se": 0.0099,
   "z": -1.17
  }
 ],
 "next": "Nothing left to strip here: every remaining term costs at least ~1 se when removed, and lambda/ACT/TOX_QUAD/boost/flow are large. Dir skew is nearly free (-0.01, ~1.3 se). A future simplify item could replace it with the stale-direction shift alone and retune STALE_DIR_COEF. Tail compression costs only 0.33 (se 0.11) to remove and has high per-seed variance, so it could be a single slope worth retuning (0.89 shared already lost). The large lambda term (-38 when removed) suggests the arrival-rate coefficient is the most valuable deepen target.",
 "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    uint256 constant ELAPSED_CAP = 8;\n    uint256 constant SIGNAL_THRESHOLD = WAD / 650;\n    uint256 constant DIR_DECAY = 750000000000000000;\n    uint256 constant ACT_DECAY = 530000000000000000;\n    uint256 constant SIZE_DECAY = 530000000000000000;\n    uint256 constant TOX_DECAY = 910000000000000000;\n    uint256 constant SIGMA_DECAY = 690000000000000000;\n    uint256 constant LAMBDA_DECAY = 990000000000000000;\n    uint256 constant SIZE_BLEND_DECAY = 828000000000000000;\n    uint256 constant TOX_BLEND_DECAY = 51000000000000000;\n    uint256 constant ACT_BLEND_DECAY = 985000000000000000;\n    uint256 constant PHAT_ALPHA = 270000000000000000;\n    uint256 constant PHAT_ALPHA_RETAIL = 60000000000000000;\n    uint256 constant DIR_IMPACT_MULT = 4;\n    uint256 constant GATE_SIGMA_MULT = 7200000000000000000;\n    uint256 constant MIN_GATE = 30000000000000000;\n    uint256 constant TRADE_TOX_BOOST = 3500 * BPS;\n    uint256 constant STALE_ATTRACT_FRAC = 1050000000000000000;\n    uint256 constant RET_CAP = WAD / 35;\n    uint256 constant TOX_CAP = WAD / 5;\n    uint256 constant TRADE_RATIO_CAP = WAD / 5;\n    uint256 constant LAMBDA_CAP = 1820000000000000000;\n    uint256 constant STEP_COUNT_CAP = 64;\n    uint256 constant BASE_FEE = 4 * BPS;\n    uint256 constant SIGMA_COEF = 200000000000000000;\n    uint256 constant LAMBDA_COEF = 12 * BPS;\n    uint256 constant FLOW_SIZE_COEF = 4842 * BPS;\n    uint256 constant TOX_COEF = 300 * BPS;\n    uint256 constant TOX_QUAD_COEF = 15000 * 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 TAIL_KNEE = 600 * BPS;\n    uint256 constant TAIL_SLOPE_PROTECT = 930000000000000000;\n    uint256 constant TAIL_SLOPE_ATTRACT = 850000000000000000;\n\n    function afterInitialize(uint256 initialX, uint256 initialY) external override returns (uint256, uint256) {\n        slots[0] = BASE_FEE; slots[1] = BASE_FEE; slots[2] = 0; slots[3] = WAD; slots[4] = 0;\n        slots[5] = initialX > 0 ? wdiv(initialY, initialX) : 100 * WAD;\n        slots[6] = 950000000000000; slots[7] = 800000000000000000; slots[8] = 2000000000000000;\n        slots[9] = 0; 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]; uint256 prevAskFee = slots[1]; uint256 lastTs = slots[2];\n        uint256 dirState = slots[3]; uint256 actEma = slots[4]; uint256 pHat = slots[5];\n        uint256 sigmaHat = slots[6]; uint256 lambdaHat = slots[7]; uint256 sizeHat = slots[8];\n        uint256 toxEma = slots[9]; 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            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            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            stepTradeCount = 0;\n        }\n\n        bool firstInStep = stepTradeCount == 0;\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) { pImplied = spot; }\n        else { pImplied = trade.isBuy ? wmul(spot, gamma) : wdiv(spot, gamma); }\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) { pHat = wmul(pHat, WAD - alpha) + wmul(pImplied, alpha); }\n            if (firstInStep) {\n                if (ret > RET_CAP) ret = RET_CAP;\n                sigmaHat = wmul(sigmaHat, SIGMA_DECAY) + wmul(ret, WAD - SIGMA_DECAY);\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            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            actEma = wmul(actEma, ACT_BLEND_DECAY) + wmul(tradeRatio, WAD - ACT_BLEND_DECAY);\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        uint256 dirDev; bool sellPressure;\n        if (dirState >= WAD) { dirDev = dirState - WAD; sellPressure = true; }\n        else { dirDev = WAD - dirState; sellPressure = false; }\n\n        uint256 skew = wmul(DIR_COEF, dirDev) + wmul(DIR_TOX_COEF, wmul(dirDev, toxSignal));\n        uint256 bidFee; 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        {\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        {\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) { bidFee = bidFee + tradeBoost; }\n                else { askFee = askFee + tradeBoost; }\n            }\n        }\n\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; slots[1] = askFee; slots[2] = trade.timestamp;\n        slots[3] = dirState; slots[4] = actEma; slots[5] = pHat;\n        slots[6] = sigmaHat; slots[7] = lambdaHat; slots[8] = sizeHat;\n        slots[9] = toxEma; 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) { return WAD + wmul(centered - WAD, mul); }\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) { return \"no_cubic\"; }\n}\n",
 "code_sha256": "362d6ac9e00ad3e8a7c18076dc17db9f4580599cbe499b6f8a6ea57b7623c1aa",
 "delta": "From CH 6dd68f77: (1) deleted line `uint256 constant SIGMA_TOX_COEF = 500 * BPS;` (2) deleted line `fMid = fMid + wmul(SIGMA_TOX_COEF, wmul(sigmaHat, toxSignal));`. Nothing else changed (getName still returns \"no_cubic\"; slots 0..10 as in CH).",
 "score": {
  "seed_start": 0,
  "n": 1000,
  "mean_edge": 525.525306,
  "se": 2.7894,
  "CH_same_seeds": 525.525882,
  "paired_vs_CH_mean_diff": -0.0006,
  "paired_vs_CH_se": 0.0029
 },
 "paired": {
  "reference": "CH 6dd68f77",
  "seed_start": 600000,
  "n": 2000,
  "mean_diff": 0.0014,
  "se": 0.0014,
  "worst": [
   {
    "seed": 601623,
    "diff": -0.505
   },
   {
    "seed": 601631,
    "diff": -0.309
   },
   {
    "seed": 601165,
    "diff": -0.285
   },
   {
    "seed": 600937,
    "diff": -0.252
   },
   {
    "seed": 601027,
    "diff": -0.221
   }
  ],
  "chunked": "4 x 500, measured once after freezing"
 },
 "seconds_per_simulation": {
  "wall_32_workers": 0.0175,
  "note": "amm_sim_rs.run_batch, 32 workers, 10000 steps per simulation"
 },
 "validate_ok": true,
 "checks": {
  "slots_used": "0..10",
  "gas_afterSwap_max_measured": 114000,
  "gas_note": "found by bisecting the gas limit in pyrevm: first call ~113.8k, same-step ~52k, later new step ~36.8k; CH ~114.1k; limit 250000",
  "loops": "no loops over history; the only loop is CH's bounded _powWad exponentiation-by-squaring with exp<=ELAPSED_CAP=8",
  "harness_check": "CH on seeds 0..999 reproduced 525.525882178 exactly"
 },
 "blocked": null,
 "citations": [
  "https://github.com/benedictbrady/amm-challenge (commit 6b6921f8d463314aa7c96234e1be25a25245f704)",
  "https://raw.githubusercontent.com/ChengHua926/amm-challenge/d34a5199c59a4c44d5336a25f1436b4ae1f21104/contracts/src/Strategy.sol",
  "No public write-ups were searched for in this item."
 ]
}